yangfeng
2023-12-11 da4d9e70d2c37f59a365b0fb88ad70e8c1ba79b4
src/views/cockpitPage/components/BarChart.vue
@@ -3,8 +3,8 @@
    <ChartTitle name="车间正品率"></ChartTitle>
    <div class="bar-contents">
      <div class="bar-total">
        <div class="bar-item">当日合计生产:{{ chartData.total }}</div>
        <div class="bar-item">正品率:{{ chartData.rate }}</div>
        <div class="bar-item">7日合计生产:{{ chartData.total }}</div>
        <div class="bar-item">正品率:{{ chartData.rate }}%</div>
      </div>
      <div class="chart" ref="chart"></div>
    </div>
@@ -15,47 +15,76 @@
import ChartTitle from "@/views/cockpitPage/components/ChartTitle.vue";
//引入echart
import * as echarts from "echarts";
let myChart;
export default {
  components: {
    ChartTitle,
  },
  props: {},
  props: {
    chartData: {
      type: Object,
      require: true,
      default: () => {
        return {
          total: 0,
          rate: "0",
          datax: [],
          datay: [],
          datay2: [],
        };
      },
    },
  },
  data() {
    return {
      chartData: {
        total: 1000,
        rate: "99%",
        datax: [
          "第一车间",
          "第二车间",
          "第三车间",
          "第四车间",
          "第五车间",
          "第六车间",
          "第七车间",
          "第八车间",
          "第九车间",
        ],
        datay: [120, 200, 150, 180, 170, 150, 130, 180, 140],
        datay2: [20, 30, 50, 40, 70, 50, 30, 80, 40],
      },
      // chartData: {
      //   total: 1000,
      //   rate: "99%",
      //   datax: [
      //     "第一车间",
      //     "第二车间",
      //     "第三车间",
      //     "第四车间",
      //     "第五车间",
      //     "第六车间",
      //     "第七车间",
      //     "第八车间",
      //     "第九车间",
      //   ],
      //   datay: [120, 200, 150, 180, 170, 150, 130, 180, 140],
      //   datay2: [20, 30, 50, 40, 70, 50, 30, 80, 40],
      // },
      startValue: 0,
      endValue: 5,
      chartTimer: null,
    };
  },
  mounted() {
    this.pieChart("chart", this.chartData);
  watch: {
    "chartData.datay"(val) {
      this.startValue = 0;
      this.endValue = 5;
      this.pieChart("chart", this.chartData);
    },
  },
  watch: {},
  mounted() {
    // this.pieChart("chart", this.chartData);
  },
  methods: {
    //在职
    pieChart(chartName, data) {
      let that = this;
      clearInterval(this.chartTimer);
      let chartDom = this.$refs[chartName];
      let myChart = echarts.init(chartDom);
      if (myChart != null && myChart != "" && myChart != undefined) {
        myChart.dispose(); //销毁
      }
      myChart = echarts.init(chartDom);
      let option;
      let lineColor = "#35ddc74d";
      let spirit = "/cockpitPage/order-bg.png";
      if (data) {
        option = {
          color: ["#dcb018", "#00FFFF"],
          color: ["#fccd1d", "#00FFFF"],
          tooltip: {
            trigger: "axis",
            axisPointer: {
@@ -88,7 +117,8 @@
                '<span style=width:70px;display:inline-block">' +
                "正品率" +
                "</span>&nbsp;" +
                data.rate;
                data.rate +
                "%";
              list.push(lastString);
              listItem = list.join("<br>");
              return '<div class="showBox">' + listItem + "</div>";
@@ -105,8 +135,11 @@
              type: "inside",
              xAxisIndex: 0,
              show: false,
              startValue: 0, // 从头开始
              endValue: 5, // 一次性展示几个
              startValue: that.startValue, // 从头开始
              endValue: that.endValue, // 一次性展示几个
              zoomOnMouseWheel: false,
              moveOnMouseWheel: true,
              moveOnMouseMove: true,
            },
          ],
          legend: {
@@ -120,7 +153,7 @@
                name: "正品数量",
                icon: "circle",
                textStyle: {
                  color: "#dcb018",
                  color: "#fccd1d",
                },
              },
              {
@@ -145,11 +178,16 @@
                },
              },
              axisLabel: {
                // rotate:45,
                margin: 10,
                show: true,
                textStyle: {
                  color: "#00FFFF",
                },
                formatter: function (value) {
                  if (value.length > 4) {
                    return `${value.slice(0, 3)}...`;
                  }
                  return value;
                },
              },
              data: data.datax ? data.datax : [],
@@ -159,7 +197,6 @@
            {
              type: "value",
              name: "",
              // min: data.yAxis[0].min?data.yAxis.min:0,
              minInterval: 1, //坐标轴是整数
              max: Math.ceil(eval(`Math.max(${data.datay})`) / 5) * 5, //数据最大值加3
              interval:
@@ -229,36 +266,34 @@
          ],
          series: [
            {
              type: "bar",
              type: "pictorialBar",
              name: "正品数量",
              barWidth: "15",
              itemStyle: {
                normal: {
                  //柱形图圆角,初始化效果
                  barBorderRadius: [4, 4, 0, 0],
                },
              },
              symbol: spirit,
              symbolRepeat: true,
              symbolMargin: "25%",
              symbolClip: true,
              symbolSize: [17, 4],
              barCategoryGap: "40%",
              data: data.datay,
            },
            {
              type: "bar",
              type: "pictorialBar",
              barGap: "10%",
              name: "次品数量",
              barWidth: "15",
              itemStyle: {
                normal: {
                  //柱形图圆角,初始化效果
                  barBorderRadius: [4, 4, 0, 0],
                },
              },
              symbol: spirit,
              symbolRepeat: true,
              symbolMargin: "25%",
              symbolClip: true,
              symbolSize: [17, 4],
              data: data.datay2,
            },
          ],
        };
        setInterval(function () {
        this.chartTimer = setInterval(function () {
          // 每次向左滑动一个,最后一个从头开始。
          if (option.dataZoom[0].endValue == that.chartData.datay.length) {
            option.dataZoom[0].startValue = 0;
            option.dataZoom[0].endValue = 5;
            option.dataZoom[0].startValue = that.startValue;
            option.dataZoom[0].endValue = that.endValue;
          } else {
            option.dataZoom[0].endValue = option.dataZoom[0].endValue + 1;
            option.dataZoom[0].startValue = option.dataZoom[0].startValue + 1;