echarts动态添加数据

 

<template>
    <!--为echarts准备一个具备大小的容器dom-->
  	<div id="main" style="width: 600px;height: 400px;"></div>
</template>
<script>
    import echarts from 'echarts'

    export default {
        name: 'Chart',
        data () {
            return {
                charts: '',
            }
        },
        methods:{
            initLine(id){
                this.charts = echarts.init(document.getElementById(id))
                this.charts.setOption({
                    title: {
                        text: '动态数据 + 时间坐标轴'
                    },
                    tooltip: {
                        trigger: 'axis',
                        formatter: function (params) {
                            params = params[0]
                            return params.value[0] + ' : ' + params.value[1]
                        },
                        axisPointer: {
                            animation: false
                        }
                    },
                    xAxis: {
                        type: 'time',
                        splitLine: {
                            show: false
                        }
                    },
                    yAxis: {
                        type: 'value',
                        boundaryGap: [0, '100%'],
                        splitLine: {
                            show: false
                        }
                    },
                    animation: false
                })
             }
        },
        mounted(){
            this.$nextTick(function() {
                this.initLine('main')
                this.charts.setOption({
                    series : [
                        {
                            name : '模拟数据0',
                            type : 'line',
                            showSymbol : false,
                            hoverAnimation : false,
                            data : [['2018-01-02', '3'],['2018-01-05', '4']]
                        }
                    ]
                })
                
                setTimeout(() => {
                    this.charts.appendData({
                        seriesIndex:0,
                        data : [['2018-01-03', '1'],['2018-01-07', '2']]
                    })
                },2000)
                
                setTimeout(() => {
                    this.charts.resize();    
                },4000)

                setTimeout(() => {
                    this.charts.setOption({
                        series : [
                            {},
                            {
                                name : '模拟数据1',
                                type : 'line',
                                showSymbol : false,
                                hoverAnimation : false,
                                data : [['2018-01-02', '5'],['2018-01-05', '10']]
                            }
                        ]
                    })
                    this.charts.appendData({
                        seriesIndex:1,
                        data : [['2018-01-03', '11'],['2018-01-10', '2']]
                    })
                },6000) 
                setTimeout(() => {
                    this.charts.resize();    
                },8000)
            })
        }
    }
</script>
<style scoped>
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }
</style>

 

补充

主动使用echarts的resize方法改变图表大小:

(opts?: {
    width?: number|string,
    height?: number|string,
    silent?: boolean
})

当在参数中填入宽高,this.echarts.resize({width:300}),dom层必须有一个初始化像素的宽高,百分比的宽高该方法不会生效。

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现 Vue 3 中 Echarts 动态加载数据并进行滚动刷新,可以按照以下步骤进行操作: 1. 在 Vue 3 组件中引入 Echarts,并在 `data` 中定义一个数据数组用于存储动态数据: ```javascript import echarts from 'echarts' export default { data() { return { chartData: [] // 存储动态数据的数组 } }, mounted() { this.initChart() // 初始化图表 this.loadData() // 加载初始数据 }, methods: { initChart() { this.chart = echarts.init(this.$refs.chartContainer) // 在这里进行图表的初始化配置 this.chart.setOption({ // 图表的配置项 }) }, loadData() { // 模拟异步加载数据 setTimeout(() => { // 假设获取到了新的数据 newData const newData = [/* 新的数据数组 */] this.chartData.push(...newData) this.refreshChart() this.loadData() // 继续加载数据 }, 2000) // 每隔 2 秒加载一次数据,可以根据需求调整时间间隔 }, refreshChart() { this.chart.setOption({ series: [{ data: this.chartData // 更新图表的数据 }] }) } } } ``` 2. 在模板中添加一个容器元素,并使用 `ref` 引用它: ```html <template> <div> <div ref="chartContainer" style="width: 100%; height: 400px;"></div> </div> </template> ``` 在上述代码中,`mounted` 钩子函数中调用了 `initChart` 方法来初始化图表,并在 `loadData` 方法中模拟异步加载数据的过程。每隔 2 秒加载一次数据,将新的数据追加到 `chartData` 数组中,并调用 `refreshChart` 方法来更新图表的数据。这样就能实现动态加载数据并进行滚动刷新了。 请注意,上述代码只是一个示例,你需要根据你的实际需求进行适当的修改和调整。希望对你有所帮助!如果还有其他问题,请继续提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值