• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在Vue使用Echarts来实现数据可视化

武飞扬头像
☆夜幕星河℡
帮助1

一,Echarts

一个基于 JavaScript 的开源可视化图表库
Echarts官网 https://echarts.apache.org/zh/index.html
学新通

1.1 获取ECharts

(1)从 npm 获取(项目获取)

npm install echarts --save

(2)从 CDN 获取
推荐从 jsDelivr 引用 echarts。
(3)从 GitHub 获取
apache/echarts 项目的 release 页面可以找到各个版本的链接。点击下载页面下方 Assets 中的 Source code,解压后 dist 目录下的 echarts.js 即为包含完整 ECharts 功能的文件。

1.2 main.js引入Echarts

// 引入Echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts

在Vue项目中,引入Echarts库后,运行项目,出现以下报错:

报错:"export ‘default’ (imported as ‘echarts’) was not found in ‘echarts’

这是版本的问题,Echarts 5.x 不再支持上面的引入方式,这一点Echarts官网(升级指南)有说明。

把上面引用:import echarts from 'echarts'

改成:import * as echarts from 'echarts'

1.3 使用模板

<template>
  <div id="myChart" :style="{width: '100%', height: '1000px'}" />
</template>
<script>
export default {
  mounted() {
    this.drawLine()
  },
  methods: {
    drawLine() {
      // 基于准备好的dom,初始化echarts实例
      const myChart = this.$echarts.init(document.getElementById('myChart'))
      myChart.setOption({
       //官网实例代码,如下图
      })
    }
  }
}
</script>
学新通

学新通

1.4 实例

1.4.1 柱状图(折线图变换)

<template>
  <div id="myChart" :style="{width: '100%', height: '1000px'}" />
</template>
<script>

export default {
  mounted() {
    this.drawLine()
  },
  methods: {
    drawLine() {
      // 基于准备好的dom,初始化echarts实例
      const myChart = this.$echarts.init(document.getElementById('myChart'))
      myChart.setOption({
        title: {
          text: 'Rainfall vs Evaporation',
          subtext: 'Fake Data'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['Rainfall', 'Evaporation']
        },
        toolbox: {
          show: true,
          feature: {
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar'] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        calculable: true,
        xAxis: [
          {
            type: 'category',
            // prettier-ignore
            data: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: 'Rainfall',
            type: 'bar',
            data: [
              2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3
            ],
            markPoint: {
              data: [
                { type: 'max', name: 'Max' },
                { type: 'min', name: 'Min' }
              ]
            },
            markLine: {
              data: [{ type: 'average', name: 'Avg' }]
            }
          },
          {
            name: 'Evaporation',
            type: 'bar',
            data: [
              2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3
            ],
            markPoint: {
              data: [
                { name: 'Max', value: 182.2, xAxis: 7, yAxis: 183 },
                { name: 'Min', value: 2.3, xAxis: 11, yAxis: 3 }
              ]
            },
            markLine: {
              data: [{ type: 'average', name: 'Avg' }]
            }
          }
        ]
      })
    }
  }
}
</script>
学新通

学新通

1.4.2 极坐标柱状图标签

<template>
  <div id="myChart" :style="{width: '100%', height: '1000px'}" />
</template>
<script>

export default {
  mounted() {
    this.drawLine()
  },
  methods: {
    drawLine() {
      // 基于准备好的dom,初始化echarts实例
      const myChart = this.$echarts.init(document.getElementById('myChart'))
      myChart.setOption({
        title: [
          {
            text: 'Radial Polar Bar Label Position (middle)'
          }
        ],
        polar: {
          radius: [30, '80%']
        },
        radiusAxis: {
          max: 4
        },
        angleAxis: {
          type: 'category',
          data: ['a', 'b', 'c', 'd'],
          startAngle: 75
        },
        tooltip: {},
        series: {
          type: 'bar',
          data: [2, 1.2, 2.4, 3.6],
          coordinateSystem: 'polar',
          label: {
            show: true,
            position: 'middle',
            formatter: '{b}: {c}'
          }
        },
        backgroundColor: '#fff',
        animation: false
      })
    }
  }
}
</script>
学新通

学新通

文章到这里就结束了,这只是最基本的使用,深入的话自己可以去官网看看。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhiahgjc
系列文章
更多 icon
同类精品
更多 icon
继续加载