在vue+ts中使用echarts

1、首先在package.json包中加入:

    "echarts": "4.9.0",
    "@types/echarts": "0.0.12"

如下图所示:

2、然后执行npm install进行安装。

3、然后在.vue文件中加入div以及echart,如下代码所示:

<template>
        <div>
            登录页面
        </div>
        <div>
            <p class="col-md-6">
              <button class="btn btn-primary">测试按ssss钮</button>
            </p>
        </div>
        <div id="chart" style="width:800px;height:400px;"></div>
</template>
<script lang="ts">
import { Options, Vue } from 'vue-class-component';
import echarts from 'echarts';
$(function () {
  console.log('2222222222222222');
  const option = {
    title: {
      text: '堆叠区域图'
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#6a7985'
        }
      
      }
    },
    legend: {
      data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎']
    },
    toolbox: {
      feature: {
        saveAsImage: {}
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [
      {
        type: 'category',
        boundaryGap: false,
        data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
      }
    ],
    yAxis: [
      {
        type: 'value'
      }
    ],
    series: [
      {
        name: '邮件营销',
        type: 'line',
        stack: '总量',
        areaStyle: {},
        data: [120, 132, 101, 134, 90, 230, 210]
      },
      {
        name: '联盟广告',
        type: 'line',
        stack: '总量',
        areaStyle: {},
        data: [220, 182, 191, 234, 290, 330, 310]
      },
      {
        name: '视频广告',
        type: 'line',
        stack: '总量',
        areaStyle: {},
        data: [150, 232, 201, 154, 190, 330, 410]
      },
      {
        name: '直接访问',
        type: 'line',
        stack: '总量',
        areaStyle: {},
        data: [320, 332, 301, 334, 390, 330, 320]
      },
      {
        name: '搜索引擎',
        type: 'line',
        stack: '总量',
        label: {
          normal: {
            show: true,
            position: 'top'
          }
        },
        areaStyle: {},
        data: [820, 932, 901, 934, 1290, 1330, 1320]
      }
    ]
  };
  const chart = echarts.init(document.getElementById('chart') as HTMLDivElement);
  chart.setOption(option);
});
export default class Login extends Vue {}
</script>

4、然后打开浏览器查看结果,如下图所示:

5、注意由于vue是异步加载dom的,因此需要将初始化echarts.init放在jquery内才行,或者放置在组件挂载div后进行初始化,否则会出现错误,这里是放置在jquery内,具体配置请看:

https://blog.csdn.net/sunxiaoju/article/details/109372698

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页