Vue+echarts绘制折线图

效果图:
在这里插入图片描述

<template>
  <div class="mod-demo-echarts">
    <div v-if="date.length>0" id="chartLineBox" class="chart-box" style="width: 90%;height: 70vh;"></div>
  </div>
</template>



<script>
import echarts from "echarts";

export default {
  props:{
    driverNo:{
      type:String
    },
    time:{
      type:String
    }
  },
  data() {
    return {
      //请求后端
      date: [],
      zl: [],
      qx: [],
      tl:[]
    };
  },
  mounted() {
    this.getLine()
    //这是为了解决请求异步问题
    setTimeout(() => {
      this.initChartPie();
    }, 1000);



  },
  activated() {
    // 由于给echart添加了resize事件, 在组件激活时需要重新resize绘画一次, 否则出现空白bug
    // if (this.chartPie) {
    //   this.chartPie.resize();
    // }
  },
  methods: {
    getLine() {
      this.$http({
        url: this.$http.adornUrl(``),
        method: "get",
        params: this.$http.adornParams({
          time:this.time,
          driverNo:this.driverNo
        }),
      }).then(({ data }) => {
        console.log(data.list);
        for(let i=0;i<data.list.length;i++){
          this.date.push(data.list[i].swTimeDate)
          this.zl.push(data.list[i].zlValue)
          this.qx.push(data.list[i].qxValue)
          this.tl.push(data.list[i].tlValue)
        }
      });
    },
    initChartPie() {
      this.chartLine = echarts.init(document.getElementById('chartLineBox'));
      // 指定图表的配置项和数据
      var option = {
        tooltip: {              //设置tip提示
          trigger: 'axis'
        },
        legend: {               //设置区分(哪条线属于什么)
          data:['智力值','情绪值','体力值']
        },
        color: ['#8AE09F', '#FA6F53', '#4c82ff'],       //设置区分(每条线是什么颜色,和 legend 一一对应)
        xAxis: {                //设置x轴
          type: 'category',
          boundaryGap: false,     //坐标轴两边不留白
          data: this.date,
          name: '',           //X轴 name
          nameTextStyle: {        //坐标轴名称的文字样式
            color: '#FA6F53',
            fontSize: 16,
            padding: [0, 0, 0, 20]
          },
          axisLine: {             //坐标轴轴线相关设置。
            lineStyle: {
              color: '#FA6F53',
            }
          }
        },
        yAxis: {
          name: '',
          nameTextStyle: {
            color: '#FA6F53',
            fontSize: 16,
            padding: [0, 0, 10, 0]
          },
          axisLine: {
            lineStyle: {
              color: '#FA6F53',
            }
          },
          type: 'value'
        },
        series: [
          {
            name: '智力值',
            data:  this.zl,
            type: 'line',               // 类型为折线图
            lineStyle: {                // 线条样式 => 必须使用normal属性
              normal: {
                color: '#8AE09F',
              }
            },
          },
          {
            name: '情绪值',
            data: this.qx,
            type: 'line',
            lineStyle: {
              normal: {
                color: '#FA6F53',
              }
            },
          },
          {
            name: '体力值',
            data: this.tl,
            type: 'line',
            lineStyle: {
              normal: {
                color: '#4c82ff',
              }
            },
          }
        ]
      };
      this.chartLine.setOption(option);
    },
  },
};
</script>

<style lang="scss">
.mod-demo-echarts {
  > .el-alert {
    margin-bottom: 10px;
  }
  > .el-row {
    margin-top: -10px;
    margin-bottom: -10px;
    .el-col {
      padding-top: 10px;
      padding-bottom: 10px;
    }
  }
  .chart-box {
    min-height: 200px;
    min-width: 200px;
  }
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,要实现折线图来展示收支情况,您可以使用 Vue.js 和 ECharts 来实现。 首先,您需要使用 Vue.js 来搭建页面,然后使用 ECharts绘制折线图。以下是一个简单的实现步骤: 1. 引入 ECharts 库和 Vue.js 库: ```html <!-- 引入 ECharts 库 --> <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script> <!-- 引入 Vue.js 库 --> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> ``` 2. 在 Vue.js 中定义折线图的数据: ```javascript data() { return { // 折线图数据 chartData: { // X轴数据 xData: ['1月', '2月', '3月', '4月', '5月', '6月'], // Y轴数据 yData: [ { name: '支出', type: 'line', data: [1200, 1500, 800, 1100, 1300, 900] }, { name: '收入', type: 'line', data: [800, 1100, 1500, 1200, 1000, 1400] } ] } } } ``` 3. 在 Vue.js 中使用 ECharts 绘制折线图: ```javascript mounted() { // 初始化 ECharts 实例 const chart = echarts.init(this.$refs.chart) // 设置 ECharts 配置项 const option = { // 图表标题 title: { text: '收支情况' }, // 图表工具箱 toolbox: { show: true, feature: { saveAsImage: { show: true, title: '保存为图片' } } }, // X轴数据 xAxis: { type: 'category', data: this.chartData.xData }, // Y轴数据 yAxis: { type: 'value' }, // 数据系列 series: this.chartData.yData } // 使用 ECharts 绘制折线图 chart.setOption(option) } ``` 4. 在 Vue.js 中渲染折线图: ```html <template> <div> <div ref="chart" style="width: 100%; height: 400px;"></div> </div> </template> ``` 以上是一个简单的实现步骤,您可以根据自己的需求进行修改和完善。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值