ECharts的使用

1 篇文章 0 订阅

背景

最近公司有做数据面板的需求,后台前端用的是Vue+Element,选用强大的Apache ECharts。

Apache ECharts 官网

官网 https://echarts.apache.org/zh/index.html

导入依赖

TypeScript 版本

npm install echarts --save
npm install @types/echarts

JavaScript版本

npm install echarts --save

页面代码

单折线

在这里插入图片描述

<template>
    <div class="app-container">
        <div id="main" 	style="width:800px;height:400px;"></div>
    </div>
</template>

<script>
//<script lang="ts">
  import {Component,Vue} from "vue-property-decorator";

  @Component
  export default class extends Vue {
    private drawChart() {
      // 基于准备好的dom,初始化echarts实例
      const echarts = require("echarts");
      const myChart = echarts.init(document.getElementById('main') as HTMLDivElement);
      myChart.setOption({
        title: {
          text: "图表标题"
        },
        tooltip: {},
        xAxis: {
          data: []
        },
        yAxis: {},
        series: [
          {
            name: "数量",
            type: "line",
            data: []
          }
        ]
      });
    }

    mounted() {
      this.drawChart();
    }
  }
</script>

双折线、双Y轴

在这里插入图片描述

<template>
    <div class="app-container">
		<div id="chartId" style="width:100%;height:500px;"></div>
	</div>
</template>

<script>
//<script lang="ts">
import {Component,Vue} from "vue-property-decorator";

  @Component({
    components: {}
  })
  export default class extends Vue {

      private drawChart() {
      	   // 基于准备好的dom,初始化echarts实例
          const echarts = require("echarts");
          //清除原本的图表信息(当多个图表时切换时会用到)
          document.getElementById("chartId").removeAttribute("_echarts_instance_"); 
          const chart = echarts.init(document.getElementById('chartId') as HTMLDivElement);
          chart.setOption({
              title: {
                  text: "表格标题"
              },
              tooltip: {
                  trigger: 'axis', //提示框组件
              },
              xAxis: {
                  name: "x轴名称",
                  data: [] //x轴显示的数据
              },
              legend: { //图例组件
                  data: ['第一条线','第二条线'] //多条折现的图例组件名称
              },
             // yAxis: { //一条折现时
             //     position:'left',
             //     name: "金额"
             // },
              yAxis: [ //两条折现
              {
               position:'left',
               name: "金额"
              },
              {
                position:'right',
             	name: "笔数"
              }
              ]
              series: [
                  {
                      name: yAxisSeriesName1,
                      yAxisIndex: 1, //使用第二Y轴
                      type: "line", // bar 柱状图
                      label: {
                          show: true //显示折现上的数字
                      },
                      itemStyle: {
                          color: '#32CD32'
                      },
                      data: []
                  },
                  {
                      name: yAxisSeriesName,
                      type: "line", 
                      label: {
                          show: true
                      },
                      itemStyle: {
                          color: '#5c7bd9'
                      },
                      data: []
                  }
              ]
          });
      	}
      }
</script>

把data数组中的所有count,添加到另外一个数组

private getCount(data) {
  this.transactionCountList = data.map(function (item) { return item.count});
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值