Newer
Older
smartKitchenFront / src / views / dashboard / components / commonSelectChart.vue
<template>
  <div class="chart-container">
    <div class="chart-header">
      <!--标题  -->
      <span class="title">{{ name }}</span>
      <el-select v-model="value" placeholder="请选择" size="mini">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </div>
    <div class="chart-content" :style="{ height: height, width: width }">
      <!-- echarts内容 -->
      <MixChart
        :xAxisData="xAxisData"
        :seriesData="seriesData"
        :yAxisData="yAxisData"
        :legend="legend"
      />
    </div>
  </div>
</template>
    
    <script>
import MixChart from "@/components/echart/barChart/MixChart.vue";
export default {
  name: "commonSelectChart",
  components: {
    MixChart,
  },
  props: {
    name: {
      type: String,
      default: "",
    },
    width: {
      type: String,
      default: "500px",
    },
    height: {
      type: String,
      default: "300px",
    },
    fetchdata: {
      type: Function,
      required: true,
    },
    legendDefault: {
      type: Array,
      default: () => ["", "", ""],
    },
  },
  data() {
    return {
      value: "year",
      options: [
        {
          value: "year",
          label: "近一年",
        },
        {
          value: "month",
          label: "近一月",
        },
      ],
      xAxisData: [],
      yAxisData: [],
      seriesData: [],
      legend: [],
    };
  },
  mounted() {
    this.fetchData();
    // setTimeout(() => {
    //   // 模拟数据
    //   this.seriesData = [
    //     {
    //       name: "经销商订单数",
    //       type: "bar",
    //       tooltip: {
    //         valueFormatter: function (value) {
    //           return value;
    //         },
    //       },
    //       data: [10, 15, 13, 19, 22, 9, 33],
    //       barWidth:20
    //     },
    //     {
    //       name: "同值比",
    //       type: "line",
    //       data: [1.1, 1.6, 1.3, 0.9, 0.1, 2.2, 0.4],
    //       smooth: false, // 折线平滑
    //       symbol: "circle", // 控制折线处小点
    //     },
    //     {
    //       name: "环值比",
    //       type: "line",
    //       data: [1.6, 1.6, 1.3, 1.9, 0.1, 3.2, 1.2],
    //       smooth: false, // 折线平滑
    //       symbol: "circle", // 控制折线处小点
    //     },
    //   ];
    //   this.xAxisData = ["10-1", "10-2", "10-3", "10-4", "10-5", "10-6", "10-7"];
    //   this.yAxisData = [
    //     {
    //       type: "value",
    //       name:'111',
    //       // show: true,
    //       axisLabel: {
    //         show:true,
    //         formatter: "{value}",
    //         fontSize: 12,
    //         color: "#000",
    //       },
    //       axisLine: {
    //         show: false,
    //       },
    //       splitLine: {
    //         //多条横线
    //         show: true,
    //       },
    //       interval: 20,
    //     },
    //     {
    //       type: "value",
    //       name:'111',
    //       // show: true,
    //       axisLabel: {
    //         show:true,
    //         formatter: "{value}%",
    //         fontSize: 12,
    //         color: "#000",
    //       },
    //       splitLine: {
    //         //多条横线
    //         show: true,
    //       },
    //       axisLine: {
    //         show: false,
    //       },
    //       interval: 20,
    //     },
    //   ];
    //   this.legend = ["经销商订单数", "同值比", "环值比"];
    // }, 1000);
  },
  methods: {
    fetchData() {
      this.fetchdata(this.value).then((res) => {
        // 图表数据
        let seriesData = [];
        let legend = [];
        if (res.data.length > 0) {
          for (var user in res.data[0]) {
            legend.push(user);
            seriesData.push({
              name: user,
              data: [],
            });
          }
        }
        for (var i = 0; i < res.data.length; i++) {
          seriesData.forEach((item) => {
            if (item.name in res.data[i]) {
              item.data.push(res.data[i][item.name]);
            }
          });
        }
        this.seriesData = [
        {
          name:this.legendDefault[0],
          type: "bar",
          data: seriesData[1].data,
          barWidth:20
        },
        {
          name: this.legendDefault[1],
          type: "line",
          data: seriesData[2].data.map(item => parseFloat(item)),
          tooltip: {
            valueFormatter: function (value) {
              return value + '%';
            },
          },
          smooth: false, // 折线平滑
          symbol: "circle", // 控制折线处小点
        },
        {
          name: this.legendDefault[2],
          type: "line",
          data: seriesData[3].data.map(item => parseFloat(item)),
          tooltip: {
            valueFormatter: function (value) {
              return value + '%';
            },
          },
          smooth: false, // 折线平滑
          symbol: "circle", // 控制折线处小点
        },
      ];
      this.xAxisData = seriesData[0].data;
      this.legend = this.legendDefault
      this.yAxisData = [
        {
          type: "value",
          show: true,
          position: 'left',
          axisLabel: {
            formatter: "{value}",
            fontSize: 12,
            color: "#000",
          },
          axisLine: {
            show: false,
          },
          splitLine: {
            //多条横线
            show: true,
          },
          // interval: 20,
        },
        {
          type: "value",
          show: true,
          position: 'right',
          axisLabel: {
            formatter: "{value}%",
            fontSize: 12,
            color: "#000",
          },
          splitLine: {
            //多条横线
            show: true,
          },
          axisLine: {
            show: false,
          },
          // interval: 20,
        },
      ];
      });
    },
  },
  watch: {
    value(newVal) {
      this.fetchData();
    },
  },
};
</script>
    
    <style lang="scss" scoped>
.chart-container {
  .chart-header {
    width: 500px;
    align-items: center;
    display: flex;
    justify-content: space-between;
  }
}
</style>