图表前端整合显示

76 阅读1分钟

1、在api/sta.js中创建接口方法

//查询图表显示数据
showData(searchObj){
  return request({
    url: `/staservice/stadaily/showData/${searchObj.type}/${searchObj.begin}/${searchObj.end}`,
    method: 'get'
  })
}

2 、改造daily/show.vue页面js方法

<script>
import echarts from "echarts";
import sta from "@/api/sta";
export default {
  data() {
    return {
      searchObj: {},
      btnDisabled: false,
      xData: [],
      yData: []
    };
  },
  created() {},
  methods: {
    showChart() {
      sta.showData(this.searchObj).then(response => {
        this.xData = response.data.dateCalculatedList;
        this.yData = response.data.dataList;
        this.setCharts();
      });
    },
    setCharts() {
      var myChart = echarts.init(document.getElementById("chart"));
      var option = {
        //x轴是类目轴(离散数据),必须通过data设置类目数据
        xAxis: {
          type: "category",
          data: this.xData
        },
        //y轴是数据轴(连续数据)
        yAxis: {
          type: "value"
        },
        //系列列表。每个系列通过 type 决定自己的图表类型
        series: [
          {
            //系列中的数据内容数组
            data: this.yData,
            //折线图
            type: "line"
          }
        ],
        dataZoom: [
          {
            show: true,
            height: 30,
            xAxisIndex: [0],
            bottom: 30,
            start: 10,
            end: 80,
            handleIcon:
              "path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z",
            handleSize: "110%",
            handleStyle: {
              color: "#d3dee5"
            },
            textStyle: {
              color: "#fff"
            },
            borderColor: "#90979c"
          },
          {
            type: "inside",
            show: true,
            height: 15,
            start: 1,
            end: 35
          }
        ]
      };
      myChart.setOption(option);
    }
  }
};
</script>