Echarts图表在Vue项目的按需加载及折线图、柱状图、饼图、雷达图样式修改

一、Echarts 按需加载

有两种方式可以实现按需加载。
第一种

  1. 专门设置一个echarts配置文件
// 文件路径 @/lib/echarts.js 自行配置
// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入柱状图等
require('echarts/lib/chart/bar');
require("echarts/lib/chart/line");
require("echarts/lib/chart/pie");

// 引入提示框和标题组件
require('echarts/lib/component/tooltip');
require('echarts/lib/component/title');
require("echarts/lib/component/dataZoom");
require("echarts/lib/component/markPoint");
require("echarts/lib/component/markLine");
export default echarts

需要引入什么,参考这个地址:https://github.com/apache/incubator-echarts/blob/master/index.js

  1. 在需要的组件内加载echarts,绘制图表
<template>
      <div id="myChart" ref="myChart"></div>     
 </template>
<script>
  import {skillOption} from './option/skillAnalyOption'
// 重点:此位置引入的是你单独配置的echarts
import echarts from '@/lib/echarts'
export default {
    mounted() {
        // 调用绘制图表的方法
      this.draw();
      let that = this;
      window.onresize = function () {
        that.pracChart.resize();
        that.skillChart.resize();
        that.pieChart.resize();
        that.histogramChart.resize();
      }
    },
    methods: {
        draw () {
            // 实例化echarts对象
            this.pieChart= echarts.init(this.$refs.myChart)
                        //下面是具体的配置
                        this.pieChart.setOption(skillOption);
                        //绑定的一些事件
                        this.pieChart.on('mouseover', (params) => {
                        this.changeId = params.name
                        this.getHistogramChartData(this.cur2, this.changeId)
                       });
        },
      getPrcData () {
        this.pracChart.setOption({
          xAxis: {
            data: this.PrcData.date
          },
          series: [{
            data: this.PrcData.hour  //将异步请求获取到的数据进行装载
          }]
        })
      },
  
    }
}   
</script>

第二种方式:
引入插件babel-plugin-equire,配合实现Echarts按需引入

  1. 下载babel-plugin-equire
npm install babel-plugin-equire -D
  1. 在.babelrc文件中的配置
"plugins": [
    "... 其他插件",
    "equire"
]

项目中是这样配置的:

"plugins": [
    ["transform-runtime",
      {
        "polyfill": false
      }
    ],
    "equire"
  ],
  1. 修改@/lib/echarts文件
const echarts = equire([
// 写上你需要的
  'bar',
  'line',
  'pie',
  'radar',
  'legend',
  'title',
  'markLine',
  'dataZoom'
])

export default echarts

二、折线图、柱状图、饼图、雷达图样式修改

  • 折线图


    10d2d18125042b8a679b482c9d0a88f.png
import echarts from 'echarts'
export const praOption = {
  dataZoom: [  //x轴区域缩放
    {
      type: 'slider', //图表下方的伸缩条
      show: true,
      realtime: true,
      start: 0,
      end: 100,
      handleIcon:"M0,0 v9.7h5 v-9.7h-5 Z",
      handleStyle:{ /*手柄的样式*/
        color:"yellow",
        borderColor:"transparent"
      },
      backgroundColor:"transparent", /*背景 */
      fillerColor:"rgba(255,255,255,0.2)", /*被start和end遮住的背景*/
    },
    {
      type: 'inside',  //鼠标滚轮
      realtime : true
    },
  ],
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: [],
    axisLine:{
      lineStyle:{
        color:'#fff',
        width:1,//  改变坐标线的颜色
      }
    }
  },
  yAxis: {
    type: 'value',
    //去掉默认的线
    splitLine: {
      show: false
    },
    axisLine:{
      lineStyle:{
        color:'#fff',
        width:1,//  改变坐标线的颜色
      }
    }
  },
  series: [{
    data: [],
    type: 'line',
    areaStyle: {
      normal: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
          offset: 0,
          color: 'rgba(225,255,255,.3)'
        }, {
          offset: 1,
          color: 'rgba(255, 255, 255,.1)'
        }])
      }
    },
    markLine: {
      silent: true,
      data: [{
        yAxis: 6,
        lineStyle:{
          color: 'yellow'
        },
      }, {
        yAxis: 11,
        lineStyle:{
          color: '#3bf2e7'
        },
      }]
    },
    //修改折线图线的颜色
    itemStyle:{
      normal:{
        color : "#fff", // 图表中各个图区域的边框线拐点颜色
        lineStyle:{
          color:'rgba(255,255,255,.5)' // 图表中各个图区域的边框线颜色
        }
      }
    },
  }]
}


//折线图
    this.pracChart.setOption({
          xAxis: {
            data: this.PrcData.date
          },
          series: [{
            data: this.PrcData.num
          }]
        })
  • 柱状图


    c89f939dde513dcdd2af00dbfcf45f7.png
export const histogramOption = {
  tooltip : {
    trigger: 'axis'
  },
  legend: {
    data:['65分基线','75分基线']
  },
  toolbox: {
    itemSize:0,  //去掉顶部自带的功能按钮
    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',
      data : []
    }
  ],
  yAxis : [
    {
      type : 'value',
      //去掉默认的线
      splitLine: {
        show: false
      }
    }
  ],
  series : [
    {
      name:'65分基线',
      type:'bar',
      data:[],
      itemStyle:{
        normal:{
          color:'#B2DAFE'
        }
      }
    },
    {
      name:'75分基线',
      type:'bar',
      data:[],
      itemStyle:{
        normal:{
          color:'#78BDFE'
        }
      }
    },
    {
      name:'自己',
      type:'bar',
      data:[],
      itemStyle:{
        normal:{
          color:'#F18180'
        }
      }
    }
  ]
}


    //柱状图
     this.histogramChart.setOption({
          xAxis : [
            {
              data : filterData[key].type
            }
          ],
          series : [
            {
              data: filterData[key]['65']
            },
            {
              data: filterData[key]['75']
            },
            {
              data: filterData[key]['self']
            }
          ]
        })
  • 饼状图


    57d121f60bbb8035f7ac90f505d8630.png
export const pieOption = {
  legend: {
    orient: 'horizontal',
    width: 180,
    x: 'center',
    y: '72%',
    itemWidth: 10,
    itemHeight: 10,
    itemGap: 10,
    data: ['Speaking','Listening','Writing','Reading']
  },
  series : [
    {
      name: '访问来源',
      type: 'pie',
      radius : '55%',
      center: ['50%', '40%'],
      data: [],
      itemStyle: {
        emphasis: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        },
        // 饼状图显示数据
        normal:{
          label:{
            show:true,
            position: 'inner',  //让饼状图百分比在区域内显示
            formatter:'{d}%'
          },
          labelLine:{
            show:false
          }
        }
      }
    }
  ],
  color: ['#B691F1','#3FA2FD','#FCAAC0','#3AF2E8']
}

//饼状图
          this.pieChart.setOption({
            series: [{
              data: this.PieData
            }]
          })
  • 雷达图


    1af6ac87dfc64c326f8b852df3db4a7.png
export const skillOption = {
  radar: {
    name: {
      textStyle: {
        color: 'rgba(255,255,255,.8)',
        borderRadius: 3
      }
    },
    indicator: [
      { name: 'Grammar', max: 100},
      { name: 'Oral Fluency', max: 100},
      { name: 'Pronunciation', max: 100},
      { name: 'Spelling', max: 100},
      { name: 'Vocabulary', max: 100},
      { name: 'Written Discourse', max: 100}
    ],
    splitArea : {
      show : true,
      areaStyle : {
        color: ['transparent', 'transparent', 'transparent']  // 图表背景网格的颜色
      }
    },
    splitLine : {
      show : true,
      lineStyle : {
        width : 1,
        color : 'rgba(255,255,255,0.3)' // 图表背景网格线的颜色
      }
    }
  },
  series: [{
    type: 'radar',
    data : [],
    itemStyle: {
      normal: {
        color : "rgba(255,255,255,.8)", // 图表中各个图区域的边框线拐点颜色
        lineStyle: {
          color:"transparent" // 图表中各个图区域的边框线颜色
        },
        areaStyle: {
          color : 'rgba(250,250,250,0.3)'
        }
      }
    }
  }]
}

    //雷达图
     this.skillChart.setOption({
            series: [{
              data: [
                {
                  value : this.skillAnalData.data.analyse,
                  label: {
                    normal: {
                      show: true,
                      formatter:function(params) {
                        return params.value;
                      }
                    }
                  },
                  areaStyle: {
                    normal: {
                      opacity: 0.9,
                      color: new echarts.graphic.RadialGradient(0.5, 0.5, 1, [
                        {
                          color: 'rgba(0,255,255,.4)',
                          offset: 0
                        },
                        {
                          color: 'rgba(0,255,255,.1)',
                          offset: 1
                        }
                      ])
                    }
                  }
                }
              ]
            }]
          })
  • 倒起来的柱状图配置


    1f68d753506d0a219f3cc5511523955.png
const CommunicativeSkills = ['Writing','Speaking','Reading','Listening']
export default {
  grid: {
    left: '3%',
    right: '4%',
    bottom: '10%',
    containLabel: true
  },
  xAxis: {
    type: 'value',
    boundaryGap: [0, 0.01],
    splitLine: {
      show: false
    },
    min: 10,
    max: 90,
    interval: 4
  },
  yAxis: {
    type: 'category',
    data: ['Written Discourse','Vocabulary','Spelling','Pronunciation','Oral Fluency',
'Grammar','Enabing Skills','Writing','Speaking','Reading','Listening','Communicative Skills'],
    axisLabel: {
      margin: 10,
      fontWeight: '600',
      textStyle: {
        color: function (value, index) {
          if (value === 'Enabing Skills' || value === 'Communicative Skills') {
            return '#448AFF'
          } else {
            return '#515151'
          }
        }
      }
    },
    axisTick: {
      show: true,
      alignWithLabel: true,
      interval: function (index, value) {
        if (value === 'Enabing Skills' || value === 'Communicative Skills') {
          return false
        } else {
          return true
        }
      }
    }
  },
  series: [
    {
      name: 'skills',
      type: 'bar',
      data: [],
      label: {
        show: true,
        position: 'right',
        color: '#515151'
      },
      itemStyle:{
        normal:{
          color:function (value) {
            if (CommunicativeSkills.indexOf(value.name) === -1) {
              return '#96CBFA'
            } else {
              return '#448AFF'
            }
          }
        }
      },
      markLine: {
        label: {
          show: true,
          position: 'end',
          formatter: 'Overall Scale:{c}',
          fontWeight: '600',
          color: '#5C6E81'
        },
        symbol: '',  //去除两端的箭头
        silent: true,
        data: []
      },
    }
  ]
}

//视图
this.finalScore = [...dataEnn,'',...dataCom,'']
this.scoreChart.setOption({
          series: [{
            data: this.finalScore,  //将异步请求获取到的数据进行装载
            markLine: {
              data: [{
                xAxis: this.averageScore,
                lineStyle:{
                  type: 'solid',
                  color: '#355888',
                  width: 2
                }
              }]
            }
          }]
        })
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容