40、3D柱状图

104 阅读2分钟

5.png 1、封装

<template>
  <div class="patientWarning_container">
    <div id="patientWarning_id" style="width: 100%;height: 100%;"></div>
  </div>
</template>
<script>
import * as echarts from "echarts";
export default {
  name: "patientWarningPage", //患者预警
  mounted() {
    this.initChartPatientWarning();
  },
  methods: {
    initChartPatientWarning() {
      var patientWarningChartDom = document.getElementById("patientWarning_id");
      var patientMyChart = this.$echarts.init(patientWarningChartDom);
      var dataList = [        { name: "系列一", value: 73, max: 100 },        { name: "系列二", value: 18, max: 100 },        { name: "系列三", value: 43, max: 100 }        // { name: '系列四', value: 28, max: 100 },        // { name: '系列五', value: 56, max: 100 }      ];
      // 头部数据
      let topData = dataList.map(item => {
        return { name: "", value: item.max, symbolPosition: "end" };
      });
      // 底部立体柱子
      let bottomBar = dataList.map(item => {
        return {
          value: item.value
        };
      });
      // 底下圆片
      let bottomCircle = dataList.map(item => {
        return { name: "", value: item.max };
      });
      // 中间圆片
      let middleCircle = dataList.map(item => {
        return { name: "", value: item.value, symbolPosition: "end" };
      });
      // 上边的柱子
      let upBar = dataList.map(item => {
        return { name: item.name, value: item.max - item.value };
      });

      var option = {
        // backgroundColor: "#000",
        tooltip: {
          trigger: "item",
          backgroundColor: "rgba(0,0,0,0.5)",
          borderColor: "rgba(89,211,255,1)",
          borderWidth: 2,
          padding: 5,
          textStyle: {
            color: "rgba(89,211,255,1)",
            fontSize: 18,
            width: 100,
            height: 40
          },
          formatter: "{c}" + "%",
          extraCssText: "box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);"
          // 自定义的 CSS 样式
        },
        grid: {
          bottom: "20%",
          top: "10%",
          right: "0%",
          left: "0%"
        },
        xAxis: {
          data: ["系列一", "系列二", "系列三"], //'系列四', '系列五'
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: "#000"
            },
            margin: 30 //刻度标签与轴线之间的距离。
          }
        },
        yAxis: {
          splitLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            show: false
          }
        },
        series: [
          // 头
          {
            name: "",
            type: "pictorialBar",
            symbolSize: [40, 10],
            symbolOffset: [0, -6],
            z: 12,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: "#73b6ff"
                    },
                    {
                      offset: 1,
                      color: "rgba(115, 182, 255,.8)"
                    }
                  ],
                  false
                )
              }
            },
            data: topData
          },
          //底部立体柱
          {
            stack: "1",
            type: "bar",
            silent: true,
            barWidth: 40,
            data: bottomBar,
            itemStyle: {
              normal: {
                color: {
                  x: 0,
                  y: 0,
                  x2: 0,
                  y2: 1,
                  type: "linear",
                  global: false,
                  colorStops: [
                    {
                      //第一节下面
                      offset: 0,
                      color: "#6cdefa"
                    },
                    {
                      offset: 1,
                      color: "#2f92ea"
                    }
                  ]
                }
              }
            }
          },
          //最底下的圆片
          {
            name: "",
            type: "pictorialBar",
            symbolSize: [40, 10],
            symbolOffset: [0, 4],
            z: 12,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: "#3572e5"
                  },
                  {
                    offset: 1,
                    color: "#77bfff"
                  }
                ])
              }
            },
            data: bottomCircle
          },
          // 中间圆片
          {
            name: "",
            type: "pictorialBar",
            symbolSize: [40, 10],
            symbolOffset: [0, -6],
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: "#32bdea"
                    },
                    {
                      offset: 1,
                      color: "#77bfff"
                    }
                  ],
                  false
                )
              }
            },
            z: 12,
            data: middleCircle
          },
          //上部立体柱
          {
            //上部立体柱
            stack: "1",
            type: "bar",
            itemStyle: {
              normal: {
                color: "#73b6ff",
                opacity: 0.6
              }
            },
            label: {
              show: true,
              position: "insideBottom", //top / left / right / bottom / inside / insideLeft / insideRight / insideTop / insideBottom / insideTopLeft / insideBottomLeft / insideTopRight / insideBottomRight
              distance: 20,
              color: "#000",
              fontSize: 14,
              fontWeight:700,
              formatter: function(item) {
                var a = 100;
                return a - item.value ;
              }
            },
            silent: true,
            barWidth: 40,
            //barGap: '-100%', // Make series be overlap
            data: upBar
          }
        ]
      };
      option && patientMyChart.setOption(option);
      window.addEventListener("resize", function() {
        patientMyChart.resize();
      });
    }
  }
};
</script>
<style scoped lang="less">
.patientWarning_container {
  .px2vw(width, 230);
  .px2vh(height, 210);
  // border: 1px solid orangered;
}
</style>

2、使用

<patientWarning></patientWarning>