vue中echarts图表(一)---正负轴柱状图

一、安装

npm i echarts--save

二、引入

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

//局部【这里使用局部引入】
import echarts from 'echarts';

三、代码

 

  1 <template>
  2   <!-- 容器 -->
  3   <div id="cylinderEcharts" ref="cylinderEcharts" style="width:900px;height:500px"></div>
  4 </template>
  5 
  6 <script>
  7 import echarts from 'echarts';
  8 export default {
  9 data() {
 10   return{
 11      // 正负柱状图
 12       zhengfuZhu: {},
 13       data1: [],
 14       data2: [],
 15       data3: [],
 16       data4: [],
 17       xAxisData1: [],//横轴刻度
 18       seriesLabel: { //柱子上的字体
 19         show: false,
 20         position: 'outside',
 21         color: '#BEC3EB',
 22       },
 23       seriesEmphasis: { //
 24         itemStyle: {
 25           shadowBlur: 20,
 26           shadowColor: 'rgba(0,0,0,0.3)'
 27         }
 28       },
 29       seriesBarWidth: 17,//设置柱子粗细
 30       seriesYears: ['2022', '2023'],
 31       seriesColors: ['#5871c0', '#9eca7f', '#f3c96b', '#de6e6a'],
 32   }
 33 },
 34 mounted() {
 35         for (let i = 0; i < 12; i++) {
 36             this.xAxisData1.push(i + 1 + "");
 37             this.data1.push(+(Math.random() * 200).toFixed(2));
 38             this.data3.push(+(Math.random() * 500).toFixed(2));
 39             this.data2.push(-(Math.random() + 300).toFixed(2));
 40             this.data4.push(-(Math.random() + 100).toFixed(2));
 41           }
 42           this.$nextTick(() => { //加这这防止容器还未出来,就去初化下,会报如下错误
 43             this.drawLine();
 44           })
 45 },
 46 methods: {
 47    drawLine(){
 48       this.zhengfuZhu = {
 49         legend: {
 50           data: [`${this.seriesYears[0]}计划完成任务`, `${this.seriesYears[0]}实际完成任务`,
 51           `${this.seriesYears[1]}计划完成任务`, `${this.seriesYears[1]}实际完成任务`],
 52           right: '10%',
 53           icon: 'circle',
 54           textStyle: {
 55             color: ' #BEC3EB',
 56             fontSize: 13
 57           },
 58           itemWidth: 12, // 设置宽度
 59           itemHeight: 12, // 设置高度
 60           itemGap: 15,
 61           formatter: function (value) {
 62             return value
 63           },
 64         },
 65         // tooltip: {
 66         //   formatter: function (params) {
 67         //     var value = params.value;
 68         //     //首先要看看params是怎样的数据结构,里面有什么内容;
 69         //     if (value < 0) {
 70         //         value = -value
 71         //     }
 72         //     return params.seriesName + ':' + value + ''
 73         //   }
 74         // },
 75         tooltip: {
 76           trigger: 'axis',
 77           axisPointer: {
 78             type: 'cross',
 79             crossStyle: {
 80               color: '#999'
 81             }
 82           }
 83         },
 84         xAxis: {
 85           data: this.xAxisData1,
 86           splitArea: { show: false },
 87           axisLabel: {
 88             textStyle: {
 89               color: '#5871c0',
 90               fontSize: 13
 91             },
 92           },
 93         },
 94         yAxis: [
 95           {
 96             type: 'value',
 97             splitNumber: 10,
 98             splitLine: {
 99               show: true, lineStyle: {
100                 color: '#6469A1',
101                 width: 1,
102                 type: 'solid'
103               }
104             },
105             axisLabel: {
106               formatter: function (value) {
107                 // Function formatter
108                 if (value < 0) {
109                     value = -value
110                 } else {
111                     value = value
112                 }
113                 return value + ''
114               },
115               textStyle: {
116                   color: ' #BEC3EB',
117                   fontSize: 13
118               },
119             },
120           }],
121         grid: {
122           bottom: 25,
123           top: 35,
124           right: 0
125         },
126         series: [
127           {
128             name: `${this.seriesYears[0]}计划完成任务`,
129             type: 'bar',
130             stack: 'one',
131             label: this.seriesLabel,
132             emphasis: this.seriesEmphasis,
133             data: this.data1,
134             barWidth: this.seriesBarWidth,
135             itemStyle: {
136               // 柱状图颜色
137               color: this.seriesColors[0]
138             }
139           },
140           {
141             name: `${this.seriesYears[0]}实际完成任务`,
142             type: 'bar',
143             label: this.seriesLabel,
144             stack: 'two',
145             emphasis: this.seriesEmphasis,
146             barWidth: this.seriesBarWidth,
147             data: this.data3,
148             itemStyle: {
149             // 柱状图颜色
150             color: this.seriesColors[1]
151             }
152           },
153           {
154             name: `${this.seriesYears[1]}实际完成任务`,
155             type: 'bar',
156             label: this.seriesLabel,
157             stack: 'one',
158             emphasis: this.seriesEmphasis,
159             data: this.data2,
160             barWidth: this.seriesBarWidth,
161             itemStyle: {
162               // 柱状图颜色
163               color: this.seriesColors[2]
164             }
165           },
166           {
167             name: `${this.seriesYears[1]}计划完成任务`,
168             type: 'bar',
169             label: this.seriesLabel,
170             stack: 'two',
171             emphasis: this.seriesEmphasis,
172             barWidth: this.seriesBarWidth,
173             data: this.data4,
174             itemStyle: {
175               // 柱状图颜色
176               color: this.seriesColors[3]
177             }
178           },
179         ],
180       }
181       console.log('this.$refs.cylinderEcharts--',document.getElementById('cylinderEcharts'));
182       var myChart2 = echarts.init(this.$refs.cylinderEcharts);//echarts.init(document.getElementById('cylinderEcharts'));
183       myChart2.setOption(this.zhengfuZhu);
184     },
185 }
186 
187 }
188 
189 
190 </script>

 

 一定要等容器加载出来,再去初始化图表,要不然会报如下错误

最终效果如下

 参考文章:https://blog.csdn.net/qq_41579104/article/details/132023123

posted @ 2023-12-14 10:08  维维WW  阅读(625)  评论(0)    收藏  举报