echarts 折线图组件 动态数据

组件代码

<template>
    <div :class="className" :id="id" :style="{height:height,width:width}"></div>
</template>

<script>
    import echarts from 'echarts'
   // import resize from './mixins/resize'

    export default {
      //  mixins: [resize],
        props: {
            className: {
                type: String,
                default: 'chart'
            },
            id: {
                type: String,
                default: 'chart'
            },
            width: {
                type: String,
                default: '100%'
            },
            height: {
                type: String,
                default: '200px'
            },
            chartData: {
                type: Object
            }
        },
        data() {
            return {
                chart: null
            }
        },
        mounted() {
            this.initChart()
        },
        beforeDestroy() {
            if (!this.chart) {
                return
            }
            this.chart.dispose()
            this.chart = null
        },
        watch: {
            chartData: {
                immediate: true,
                deep: true,
                handler(newVal, oldVal) {
                    if (this.chart) {
                        if (newVal) {
                            this.setOption(newVal)
                        } else {
                            this.setOption(oldVal)
                        }
                    }
                }
            }
        },
        methods: {
            initChart() {
                this.chart = echarts.init(document.getElementById(this.id), 'macarons');
                this.chart.clear();
                this.setOption(this.chartData);
                this.chart.resize();
            },
            setOption(option) {
                this.chart.setOption({
                    xAxis: {
                        data: this.chartData.xAxis.data,
                        axisTick: {
                            show: false
                        }
                    },
                    grid: {
                        left: 10,
                        right: 10,
                        bottom: 20,
                        top: 30,
                        containLabel: true
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross'
                        },
                        padding: [5, 10]
                    },
                    yAxis: {
                        axisTick: {
                            show: false
                        }
                    },
                    legend: {
                        data: this.chartData.legend.data
                    },
                    dataZoom: [{
                        type: 'inside',
                        filterMode:'empty',
                        start: 0,
                        end: 100
                    }, {
                        start: 0,
                        end: 10,
                        filterMode:'empty',
                        handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
                        handleSize: '90%',
                        handleStyle: {
                            color: '#448BF6',
                            shadowBlur: 3,
                            shadowColor: 'rgba(96, 192, 221, 0.8)',
                            shadowOffsetX: 2,
                            shadowOffsetY: 2
                        }
                    }],
                    series: [
                        {
                            name: this.chartData.series[0].name,
                            type: this.chartData.series[0].type,
                            data: this.chartData.series[0].data,
                            lineStyle: {
                                color: "#0174DF",
                                width: 1
                            },
                            // 仅仅修改标注点颜色
                            itemStyle: {
                                color: '#0174DF'
                            },
                            animationDuration: 2000,
                        }
                    ]
                })
            }
        }
    }
</script> 

父组件代码

<line-chart2 v-if="chartFlag" ref="lineChartOne" :id="myChart1" :height="chartHeight" width="chartWidth" :chart-data="chartData1"></line-chart2>

 

引入

import leftPart from './leftPart'
import lineChart from './lineChart'
import global from '../../Global'
import bus from '../../assets/common/js/bus'
import lineChart2 from './lineChart2'
import timeUtils from '../../assets/common/js/timeUtils'
export default {
    name: "secondTab",
    components: {
        leftPart,
        lineChart,
        lineChart2,
    },
    data(){
        return{
            myChart1:'myChart1',
            chartHeight:'200px',
            chartWidth:'1400px',
            chartFlag:false,
            chartData1: {
                xAxis: {
                    data: []
                },
                legend: {
                    data: []
                },
                series: [
                    {
                        name: '123',
                        type: 'line',
                        data: []
                    }
                ]
            },
    },
methods:{
    //获取趋势图数据
    getFirstChartData(){
        console.log(1);
        this.$axios({
            url:url,
            methods:'get',
            params:{},
        }).then((res)=>{
            if(res.status==200){
                this.chartData1.xAxis.data = 'x轴值';
                this.chartData1.series[0].name = '趋势图';
                this.chartData1.series[0].data = ‘y轴值’;
             
            }else {
                this.tips('网络故障','error')
            }
        })
    },

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Echarts支持通过dataZoom组件来实现上下拖动数据的功能。在使用折线图时,你可以按照以下步骤来实现: 1.在option中添加dataZoom组件: ```javascript dataZoom: [ { type: 'slider', show: true, xAxisIndex: [0], start: 0, end: 100, bottom: 10, height: 20, handleIcon: 'M0,0 v9.7h5 v-9.7h-5 Z M5,0 v9.7h5 v-9.7h-5 Z', handleSize: '80%', handleStyle: { color: '#fff', borderColor: '#5476c2', borderWidth: 1, shadowBlur: 2, background: '#5476c2', shadowColor: '#5476c2', }, }, ], ``` 这里使用了type为slider的dataZoom组件,xAxisIndex指定了要控制的x轴,start和end控制了滑块的初始位置和结束位置,bottom和height控制了滑块的位置和高度,handleIcon和handleStyle控制了滑块的样式。 2.在x轴中开启boundaryGap选项: ```javascript xAxis: [ { type: 'category', boundaryGap: true, // 开启边界间隙 data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'], }, ], ``` 这里使用了boundaryGap选项,用于控制折线图的边界间隙,这样才能在图表上下拖动数据。 3.在series中添加dataZoom属性: ```javascript series: [ { name: '邮件营销', type: 'line', data: [120, 132, 101, 134, 90, 230, 210], dataZoom: true, // 添加dataZoom属性 }, // ... ], ``` 这里在series中添加了dataZoom属性,用于与dataZoom组件进行关联。 通过以上步骤,你就可以实现上下拖动数据的功能了。当然,你可以根据自己的需求进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值