echarts图表

文章详细描述了如何使用ECharts创建堆叠曲线图,调整饼图label显示,配置地图路线,处理resize失效问题,以及实例的清空和销毁方法,还涉及了主题样式设置和环形轮播效果实现。
摘要由CSDN通过智能技术生成

一、堆叠曲线图

 initChart1() {
      let data1 = [];
      let data2 = [];
      let data3 = [];
      let data4 = [];
      let data5 = [];
      for (let i = 1; i <= 96; i++) {
        data4.push(this.getRandomNumber(90, 100))
        data3.push(this.getRandomNumber(100, 120))
        data2.push(this.getRandomNumber(120, 140))
        data1.push(this.getRandomNumber(140, 160))
        data5.push(this.getRandomNumber(120, 120))
      }
      let myChart = echarts.init(document.getElementById("chart1"));
      window.onresize = myChart.resize;
      let option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        grid: {
          left: '3%',
          right: '5%',
          bottom: "6%",
          top: '15%'
        },
        legend: {
          data: ['曲线1', '曲线2', '曲线3', '曲线4'],
          top: '3%'
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: ["00:15", "00:30", "00:45", "01:00", "01:15", "01:30", "01:45", "02:00", "02:15", "02:30", "02:45",
              "03:00", "03:15", "03:30", "03:45", "04:00", "04:15", "04:30", "04:45", "05:00", "05:15", "05:30", "05:45",
              "06:00", "06:15", "06:30", "06:45", "07:00", "07:15", "07:30", "07:45", "08:00", "08:15", "08:30", "08:45",
              "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45",
              "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45",
              "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45",
              "18:00", "18:15", "18:30", "18:45", "19:00", "19:15", "19:30", "19:45", "20:00", "20:15", "20:30", "20:45",
              "21:00", "21:15", "21:30", "21:45", "22:00", "22:15", "22:30", "22:45", "23:00", "23:15", "23:30", "23:45", "24:00"
            ],
            axisLine: {
              lineStyle: {
                color: '#cecece'
              }
            },
            axisLabel: {
              color: '#666'
            },
            axisTick: {
              show: false
            },
            axisPointer: {
              snap: true
            }
          }
        ],
        yAxis: [

          {
            type: 'value',
            axisTick: {
              show: false
            },
            axisLine: {
              lineStyle: {
                color: '#cecece'
              }
            },
            axisLabel: {
              color: '#666'
            },
            splitLine: {
              show: false
            },
            axisPointer: {
              snap: true
            }
          }
        ],
        series: [

          {
            name: '曲线1',
            type: 'line',
            data: data1,
            smooth: true,
            symbol: 'circle',
            showSymbol: false,
            lineStyle: {
              normal: {
                width: 0
              }
            },
            // markPoint: {
            //   data: [
            //     {
            //       name: '最高',
            //       value: 480,
            //       xAxis: 3,
            //       yAxis: 480,
            //       symbolSize: 40
            //     }
            //   ]
            // },
            itemStyle: {
              // color: '#5171fd',
              color: '#789BFE',
              borderColor: '#fff'
            },
            areaStyle: {
              normal: {
                shadowColor: 'rgba(133,203,247,0.75)',
                shadowBlur: 15
              }
            },
            markLine: {
              symbol: ['none', 'none'],
              data: [
                {
                  name: '阈值',
                  yAxis: 110,
                  lineStyle: { type: 'dashed', color: '#317ECB', width: 1 } // 样式: 线型、颜色、线宽
                },



              ]
            }
          },
          {
            name: '曲线2',
            type: 'line',
            data: data2,
            smooth: true,
            symbol: 'circle',
            showSymbol: false,
            lineStyle: {
              normal: {
                width: 0
              }
            },
            markLine: {
              symbol: ['none', 'none'],
              data: [
                {
                  name: '阈值',
                  yAxis: 110,
                  lineStyle: { type: 'dashed', color: '#317ECB', width: 1 } // 样式: 线型、颜色、线宽
                },



              ]
            },
            // markPoint: {
            //   data: [
            //     {
            //       name: '最高',
            //       value: 360,
            //       xAxis: 3,
            //       yAxis: 360,
            //       symbolSize: 40
            //     }
            //   ]
            // },
            itemStyle: {
              color: '#7CDCF0',
              borderColor: '#fff'
            },
            areaStyle: {
              normal: {
                shadowColor: '#2D7AD6',
                shadowBlur: 10
              }
            }

          },
          {
            name: '曲线3',
            type: 'line',
            data: data3,
            smooth: true,
            symbol: 'circle',
            showSymbol: false,
            lineStyle: {
              normal: {
                width: 0
              }
            },
            markLine: {
              symbol: ['none', 'none'],
              data: [
                {
                  name: '阈值',
                  yAxis: 110,
                  lineStyle: { type: 'dashed', color: '#317ECB', width: 1 } // 样式: 线型、颜色、线宽
                },



              ]
            },
            // markPoint: {
            //   data: [
            //     {
            //       name: '最高',
            //       value: 360,
            //       xAxis: 3,
            //       yAxis: 360,
            //       symbolSize: 40
            //     }
            //   ]
            // },
            itemStyle: {
              color: '#E37E76',
              borderColor: '#fff'
            },
            areaStyle: {
              normal: {
                shadowColor: '#CE916A',
                shadowBlur: 10
              }
            }
          },
          {
            name: '曲线4',
            type: 'line',
            data: data4,
            smooth: true,
            symbol: 'circle',
            showSymbol: false,
            lineStyle: {
              normal: {
                width: 0
              }
            },
            markLine: {
              symbol: ['none', 'none'],
              data: [
                {
                  name: '阈值',
                  yAxis: 110,
                  lineStyle: { type: 'dashed', color: '#317ECB', width: 1 } // 样式: 线型、颜色、线宽
                },



              ]
            },
            // markPoint: {
            //   data: [
            //     {
            //       name: '最高',
            //       value: 360,
            //       xAxis: 3,
            //       yAxis: 360,
            //       symbolSize: 40
            //     }
            //   ]
            // },
            itemStyle: {
              color: '#53fcee',
              borderColor: '#fff'
            },
            areaStyle: {
              normal: {
                shadowColor: '53fcee',
                shadowBlur: 10
              }
            }
          }
        ]
      }
      myChart.setOption(option)
    },

二、echarts饼图series里的label超出边界显示不全

调整label的显示角度

 minAngle: 15,//最小角度
 startAngle:270, //起始角度
 aviodLabelOverlap:true,//避免label堆叠

三、地图配置项

1.type:'lines'

{
                        name: '线路',
                        type: 'lines',
                        smooth:true,//线平滑
                        coordinateSystem: 'geo',
                        polyline: true, // true:线条为多段线条
                        effect: {
                            show: false,
                            period: 10,//路径流动速度
                            trailLength: 0.2,
                            color: '#5fe9b3', //流动点颜色
                            symbol: 'arrow',//流动图案circle pin等
                            symbolSize: 10
                        },
                        lineStyle: {
                            curveness:0.8,//线弯曲程度
                                type: 'solid',//线的样式solid dotted
                                // type: [5, 10],//线的样式solid dotted
                            dashOffset: 5,
                                color: 'rgb(1,255,255)',
                                width: 4.5,
                     
                        },
                        tooltip: {
                            formatter: function (value, ticket, callback) {
                                let zuobiao = myChart.convertToPixel({ geoIndex: 0 }, [
                                    value.data[0], value.data[1]
                                ]);
                                return '';
                            },
                        },
                        data: [
                            {
                                fromName: "",
                                toName: "",
                                value: [],
                                coords: [
                                    [101.963815, 30.050663, 0, ''],//甘孜
                                    [103.04954, 30.01679, 0, ''],//雅安
                                    [104.07274, 30.57899, 0, ''],//成都
                                    [105.59942, 30.53910, 0, ''],//遂宁
                                    [106.64853, 30.47977, 0, ''],//广安
                                    // [114.33832, 40.014408, 0, ''],
                                    // [114.33832, 40.014408, 0, '']
                                ],
                            },
          
                        ]
                    },
                   

2.geo

         geo: {
                    map: `sichuan`,
                    center: [105.194115019531, 35.582111640625],
                    aspectScale: 0.75, //长宽比
                    zoom: 2,
                    roam: false,
                    itemStyle: {
                        normal: {
                            areaColor: `#10359b`,
                            shadowColor: `#337495`,
                            shadowOffsetX: 8,
                            shadowOffsetY: 20,
                            borderColor: `red`,
                            borderWidth: 10,
                            shadowBlur: 1
                        },
                        emphasis: {
                            label: {
                                show: true
                            }
                        }
                    }
                },

四、resize失效问题

ResizeObserver - Web API 接口参考 | MDN

有时候我们可能会通过 JS 或 CSS 调整容器大小,由于页面大小并未发生改变,因此 resize 事件将不会被触发。如果有需要覆盖这种情况,可以借助浏览器的 ResizeObserver API 来实现更细粒度的监听。

resizeObserver.observe(divElem);

 echarts实例的清空和销毁

内存溢出:当程序运行时需要的内存超出了当前应用系统的剩余内存时,就造成了内存溢出的情况,直观表现就是——卡死。

所以只要使用echarts了,最好都clear或dispose一下。

echarts.clear()是清空当前实例,会移除实例中所有的组件和图表。

echarts.dispose()是销毁实例,销毁后实例无法再被使用。

我觉得clear和dispose就类似于v-show和v-if

clear类似于v-show,它只是重绘图表

dispose类似于v-if,它是将echarts对象清理了,然后重新构建echarts对象

如果连图表容器都被销毁了,那就要调用echarts.dispose()销毁实例

使用场景:当echarts图表的数据是动态更新然后渲染图表时,哪怕我们使用watch去监听了数据的变化,已经拿到了更新的数据,但是图表却没有进行相应的渲染,clear或dispose一下。
原文:echarts实例的清空与销毁_echarts 销毁-CSDN博客

 // 销毁实例
    desHander(){
        let myChart = echarts.init(this.$refs["chartDom"]);
        // 销毁实例,销毁后实例无法再被使用。
        // 在什么情况下需要调用该函数进行销毁当前的实例呢?
        // 官方给的说明:在图表容器被销毁之后,调用 echartsInstance.dispose 销毁实例,
        myChart.dispose();
    },
 
    // 清空当前实例
    clearHander(){
        // 清空当前实例,会移除实例中所有的组件和图表。
        let myChart = echarts.init(this.$refs["chartDom"]);
        myChart.clear() 
    },
    init(){
        this.clearHander()
          //或者
        this.desHander()
        let myChart = echarts.init(this.$refs["chartDom"]);
    }

五、echarts设置主题样式

主题编辑器 - Apache ECharts

六、环形轮播效果

<template>
    <div style="width:500px;height:500px;background-color:pink" ref="taskChart">

    </div>
</template>

<script>
export default {
    name: 'Test1DataOverView',

    data() {
        return {
            taskChart: null,
            timer: null,
            currentIndex: -1,
            option: {
                title: {
                    subtext: '   任务管理',
                    left: 'left',
                    subtextStyle: {
                        color: 'white',
                        fontSize: 15
                        // fontWeight: 'bolder'
                    }
                },
                series: [
                    {
                        type: 'pie',
                        radius: ['56%', '66%'], // 通过设置内径与外径将饼图变为圆环饼图
                        center: ['40%', '55%'],
                        color: ['#29C5F6', '#3A9BDC', '#5579C6', '#1260CC'],
                        itemStyle: {
                            borderRadius: 40 // 设置圆环的圆角弧度
                        },
                        emphasis: { // 设置高亮时显示标签
                            label: {
                                show: true
                            },
                            scale: true, // 设置高亮时放大图形
                            scaleSize: 9
                        },
                        label: { // 设置图形标签样式
                            show: false, // 未高亮时不显示标签,否则高亮时显示的标签会与静态的标签重叠
                            textStyle: {
                                color: 'white'
                            },
                            position: 'center',
                            // 设置标签展示内容,其中{d}、{b}为echarts标签内容格式器
                            formatter: '{bStyle|{b}}:{cStyle|{c}}',
                            // 为标签内容指定样式,只能设置series-pie.label支持的样式
                            rich: {
                                bStyle: {
                                    fontSize: 20
                                },
                                cStyle: {
                                    fontSize: 15
                                }
                            }
                        },
                        data: [ // 饼图数据
                            { value: 2, name: '进行中' },
                            { value: 1, name: '未分配' },
                            { value: 1, name: '待执行' },
                            { value: 4, name: '总任务' }
                        ]
                    }
                ]


            }
        };
    },

    mounted() {
        this.init()
    },

    methods: {
        init() {
            this.taskChart = this.$echarts.init(this.$refs.taskChart)
            this.taskChart.setOption(this.option)
            this.taskChart.on('mouseover', (params) => {

                clearInterval(this.timer)
                this.currentIndex = params.dataIndex
                this.highlightPie()
            })
            this.taskChart.on("mouseout", (params) => {
                if (this.timer) {
                    clearInterval(this.timer)

                } this.changePieInterval()
            })
        },
        changePieInterval() {
            this.timer = setInterval(this.selectPie, 1000)
        },
        highlightPie() { // 取消所有高亮并高亮当前图形
            for (const idx in this.option.series[0].data) { // 遍历饼图数据,取消所有图形的高亮效果
                // echarts中支持的图表行为,通过dispatchAction触发。
                this.taskChart.dispatchAction({
                    //             dispatchAction({
                    //     type: 'highlight',
                    //     // 可选,系列 index,可以是一个数组指定多个系列
                    //     seriesIndex?: number|Array,
                    //     // 可选,系列名称,可以是一个数组指定多个系列
                    //     seriesName?: string|Array,
                    //     // 可选,数据的 index
                    //     dataIndex?: number,
                    //     // 可选,数据的 名称
                    //     name?: string
                    // })

                    type: 'downplay',//highlight 高亮指定的数据图形;.downplay 取消高亮指定的数据图形 
                    seriesIndex: 0,
                    dataIndex: idx
                })
            }
            // 高亮当前图形
            this.taskChart.dispatchAction({
                type: 'highlight',
                seriesIndex: 0,
                dataIndex: this.currentIndex
            })
        },
        selectPie() { // 高亮效果切换到下一个图形
            const dataLen = this.option.series[0].data.length
            this.currentIndex = (this.currentIndex + 1) % dataLen
            console.log(0 % 4);
            this.highlightPie()
        }


    },
};
</script>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值