echarts學習之----動態排序柱狀圖


直接上代碼:

<template>
    <div id="chart-test2" :style="{ height: '500px',width:'1000px'}"></div>
</template>

<script>
    export default {
        data() {
            return {
                testOption2: {
                    backgroundColor: '', //設置背景色透明
                    xAxis: {
                        max: 'dataMax',
                    },
                    yAxis: {
                        type: 'category',
                        data: ['蘋果', '香蕉', '火龍果', '西瓜', '獼猴桃', '哈密瓜', '黃瓜', '西紅柿', '水蜜桃', '橘子'],
                        inverse: true,
                        animationDuration: 300,
                        animationDurationUpdate: 300,
                        max: 10
                    },
                    series: [{
                        realtimeSort: true,
                        name: 'X',
                        type: 'bar',
                        data: [],
                        label: {
                            show: true,
                            position: 'right',
                            valueAnimation: true
                        }
                    }],
                    legend: {
                        show: true
                    },
                    animationDuration: 0,
                    animationDurationUpdate: 3000,
                    animationEasing: 'linear',
                    animationEasingUpdate: 'linear'
                },
            };
        },
        components: {},
        mounted() {
            //測試二
            let chartTest2 = this.$echarts.init(document.getElementById("chart-test2"), 'dark');
            //初始化數據
            let test2_Data = [];
            for (let i = 0; i < 10; ++i) {
                test2_Data.push(Math.round(Math.random() * 200));
            }
            this.testOption2.series[0].data = test2_Data //初始化數據
            chartTest2.setOption(this.testOption2) //初始化圖表

            let that = this;
            setTimeout(function() {
                that.getData();
            }, 0);
            setInterval(function() {
                that.getData();
            }, 3000);
        },
        methods: {
            //模擬獲取數據
            getData() {
                let chartTest2 = this.$echarts.init(document.getElementById("chart-test2"), 'dark');
                var data = this.testOption2.series[0].data;
                for (var i = 0; i < data.length; ++i) {
                    if (Math.random() > 0.9) {
                        data[i] += Math.round(Math.random() * 2000);
                    } else {
                        data[i] += Math.round(Math.random() * 200);
                    }
                }
                this.testOption2.series[0].data = data
                chartTest2.setOption(this.testOption2); //得到數據后重新渲染圖表
            }
        }
    }
</script>

<style>

</style>

效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM