欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue2利用echarts繪制折線圖,餅圖和大圖

 更新時間:2023年04月26日 14:10:12   作者:不求人0  
這篇文章主要為大家詳細(xì)介紹了Vue2如何利用echarts繪制折線圖,餅圖和大圖,文中的示例代碼簡潔易懂,具有一定的借鑒價值,需要的可以參考一下

折線圖,餅圖

chartPan.vue

<template>
    <div>
        <div
            class="chart-header"
            :style="{'margin-bottom': chartType == 'line' ? '20px' : 0}"
        >
            <span class="chart-title">{{ title }}</span>
            <slot name="right" />
        </div>
        <div
            v-if="!isNoData"
        >
            <div
                ref="pie"
                class="echarts"
                :style="{'height': chartHeight}"
            />
        </div>
        <div
            v-else
            class="no-data"
        >
            <empty :imagew="160" />
        </div>
    </div>
</template>
<script>
import echarts from 'echarts';
export default {
    props: {
        chartType: {
            type: String,
            default: 'pie'
        },
        chartHeight: {
            type: String,
            default: '220px'
        },
        title: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            isNoData: false
        };
    },
    methods: {
        pieInit(data, text, subtext, tab = 'node') {
            data = data.filter(i=>+i.value != 0);
            if (data.length < 1) {
                this.isNoData = true;
            } else {
                this.isNoData = false;
            }
            if (this.isNoData) return;
            this.$nextTick(()=>{
                let myChart = echarts.init(this.$refs['pie']);
                myChart.clear();
                myChart.setOption({
                    tooltip: {
                        trigger: 'item'
                    },
                    legend: {
                        orient: 'vertical',
                        right: '10%',
                        top: tab === 'node' || data.length < 5 ? '22%' : null,
                        itemGap: tab === 'node' ? 17 : 10,
                        itemWidth: 8,
                        icon: 'circle',
                        textStyle: {
                            color: '#666',
                            lineHeight: 25
                        },
                        formatter: (name) => {
                            let tarValue;
                            for (let i = 0; i < data.length; i++) {
                                if (data[i].name == name) {
                                    tarValue = data[i].value;
                                }
                            }
                            return `${name}:  ${tarValue}`;
                        }
                    },
                    title: {
                        show: !!subtext,
                        text: text,
                        textAlign: 'middle',
                        subtext: `${subtext}`,
                        left: '26%',
                        top: '25%',
                        itemGap: 15,
                        textStyle: {
                            fontSize: 14,
                            color: '#666',
                            fontWeight: 400
                        },
                        subtextStyle: {
                            fontSize: 24,
                            color: '#333',
                            fontWeight: 600
                        }
                    },
                    textStyle: {
                        rich: {
                            a: {
                                verticalAlign: 'middle'
                            }
                        },
                        lineHeight: 8,
                        padding: [0, 5, -2, 0]
                    },
                    color: ['#6E99F0', '#47C8A3', '#FAC858', '#FFA660', '#FF8579', '#A3B3D6'],
                    series: [
                        {
                            type: 'pie',
                            minAngle: 25,
                            center: [subtext ? '5%' : '18%', subtext ? '20%' : '45%'],
                            radius: ['75%', '116%'],
                            left: 'center',
                            top: 'center',
                            avoidLabelOverlap: false,
                            labelLine: {
                                show: false
                            },
                            label: {
                                show: false
                            },
                            data
                        }
                    ]
                });
                myChart.resize();
                window.addEventListener('resize', () => {
                    myChart.resize();
                });
            });
        },
        lineInit(date, data, subtext = '(%)', tab = 'node') {
            data = data.filter(i=>+i.value != 0);
            if (data.length < 1) {
                this.isNoData = true;
            } else {
                this.isNoData = false;
            }
            if (this.isNoData) return;
            this.$nextTick(()=>{
                let myChart = echarts.init(this.$refs['pie']);
                myChart.clear();
                myChart.setOption({
                    tooltip: {
                        trigger: 'axis',
                        position: function(point, params, dom, rect, size) {
                            var x = 0; // x坐標(biāo)位置
                            var y = 0; // y坐標(biāo)位置
                            var pointX = point[0];
                            var pointY = point[1];
                            // 提示框大小
                            var boxWidth = size.contentSize[0];
                            var boxHeight = size.contentSize[1];
                            if (boxWidth > pointX) {
                                x = 5;
                            } else { // 左邊放的下
                                x = pointX - boxWidth;
                            }
                            if (boxHeight > pointY) {
                                y = 5;
                            } else { // 上邊放得下
                                y = pointY - boxHeight;
                            }
                            return [x, y];
                        },
                        formatter: function(params) {
                            let color = params[0].color;
                            let marker = '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:' + color + ';"></span>';
                            let res = params[0].name + '<br/>' + marker + params[0].value + subtext;
                            return res;
                        }
                    },
                    grid: {
                        top: '13%',
                        left: '15%',
                        right: 0,
                        bottom: '20%'
                    },
                    xAxis: {
                        type: 'category',
                        offset: 8,
                        data: date,
                        axisLabel: {
                            color: '#666',
                            formatter: function(value) {
                                return value.substring(value.length - 8);
                            }
                        }
                    },
                    title: {
                        subtext: subtext,
                        subtextStyle: {
                            textAlign: 'right',
                            color: '#666'
                        },
                        // padding: [0, 10],
                        top: 0,
                        left: tab === 'node' ? '5%' : '8%',
                        itemGap: 0
                    },
                    yAxis: {
                        type: 'value',
                        boundaryGap: false,
                        axisLine: {
                            show: false
                        },
                        axisLabel: {
                            color: '#666'
                        },
                        axisTick: {
                            show: false
                        }
                    },
                    series: [
                        {
                            type: 'line',
                            symbol: 'none',
                            sampling: 'lttb',
                            itemStyle: {
                                color: 'rgb(110, 153, 240)'
                            },
                            areaStyle: {
                                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                    {
                                        offset: 0,
                                        color: '#6E99F0'
                                    },
                                    {
                                        offset: 1,
                                        color: '#ffffff'
                                    }
                                ])
                            },
                            data: data
                        }
                    ]
                });
                myChart.resize();
                window.addEventListener('resize', () => {
                    myChart.resize();
                });
            });
        },
        markLineInit(date, data, subtext = '(%)', tab = 'node', limit, request) {
            data = data.filter(i=>+i.value != 0);
            if (data.length < 1) {
                this.isNoData = true;
            }
            if (this.isNoData) return;
            this.$nextTick(()=>{
                let myChart = echarts.init(this.$refs['pie']);
                myChart.clear();
                myChart.setOption({
                    tooltip: {
                        trigger: 'axis',
                        position: function(point, params, dom, rect, size) {
                            var x = 0; // x坐標(biāo)位置
                            var y = 0; // y坐標(biāo)位置
                            var pointX = point[0];
                            var pointY = point[1];
                            // 提示框大小
                            var boxWidth = size.contentSize[0];
                            var boxHeight = size.contentSize[1];
                            if (boxWidth > pointX) {
                                x = 5;
                            } else { // 左邊放的下
                                x = pointX - boxWidth;
                            }
                            if (boxHeight > pointY) {
                                y = 5;
                            } else { // 上邊放得下
                                y = pointY - boxHeight;
                            }
                            return [x, y];
                        },
                        formatter: function(params) {
                            let color = params[0].color;
                            let marker = '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:' + color + ';"></span>';
                            let res = params[0].name + '<br/>' + marker + params[0].value + subtext;
                            return res;
                        }
                    },
                    color: ['#6E99F0'],
                    title: {
                        subtext: subtext,
                        subtextStyle: {
                            textAlign: 'right',
                            color: '#666'
                        },
                        top: 0,
                        left: tab === 'node' ? '5%' : '8%',
                        itemGap: 0
                    },
                    grid: {
                        top: '15%',
                        left: '15%',
                        right: 0,
                        bottom: '15%'
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: date,
                        axisLabel: {
                            color: '#666',
                            formatter: function(value) {
                                return value.substring(value.length - 8);
                            }
                        }
                    },
                    yAxis: {
                        type: 'value',
                        max: limit >= request ? limit + 1 : request + 1,
                        boundaryGap: false,
                        axisLine: {
                            show: false
                        },
                        axisLabel: {
                            color: '#666'
                        },
                        axisTick: {
                            show: false
                        }
                    },
                    series: {
                        type: 'line',
                        data: data,
                        markLine: {
                            silent: true,
                            data: [
                                {
                                    yAxis: limit,
                                    lineStyle: {
                                        color: '#47C8A3',
                                        type: 'solid',
                                        cap: 'round'
                                    }
                                },
                                {
                                    yAxis: request,
                                    lineStyle: {
                                        type: 'solid',
                                        cap: 'round',
                                        color: '#FAC858'
                                    }
                                }
                            ]
                        }
                    }
                });
                myChart.resize();
                window.addEventListener('resize', () => {
                    myChart.resize();
                });
            });
        }
    }
};
</script>
<style lang="scss" scoped>
.echarts {
	width: 100%;
}
.no-data {
	width: 100%;
	height: 220px;
	display: flex;
	justify-content: center;
	align-items: center;
}
.chart-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
}
.chart-title {
	font-weight: 600;
	color: #333333;
	font-size: 16px
}
</style>

使用 chartPan.vue 之餅圖

<chartPan
 ref="pieNode"
:title="tab == 'node' ? '節(jié)點(diǎn)' : '負(fù)載'"
 />
console.log('arr', arr); // [ {"name": "master節(jié)點(diǎn)","value": "1" },{ "name": "worker節(jié)點(diǎn)", "value": "1"}]
console.log('sum', sum); // sum 2
this.$refs.pieNode ? this.$refs.pieNode.pieInit(arr, `${this.tab === 'node' ? '節(jié)點(diǎn)' : ''}總數(shù)`, sum, 'node) : '';

效果

使用 chartPan.vue 之折線圖

    <chartPan
        ref="cpu"
        :title="tab == 'node' ? 'CPU使用率' : 'CPU用量'"
        chart-type="line"
        chart-height="200px"
    >
        <div slot="right">
            <i
                class="iconfont icon-color icon-shuaxin"
                @click="getUseRateCpu"
            />
            <i
                class="iconfont icon-color icon-fangda"
                style="margin-left: 12px"
                @click="handlePreViewChart('cpu', tab == 'node' ? 'CPU使用率' : 'CPU用量')"
            />
        </div>
    </chartPan>
                console.log('chartDate', chartDate); // ['2023-04-25 17:57:01', '2023-04-25 17:58:01', '2023-04-25 17:59:01',]
                console.log('chartData', chartData); // [4.92, 4.84, 5.07, 4.96, 5.06,]
                this.$refs.cpu ? this.$refs.cpu.lineInit(chartDate, chartData, this.tab === 'node' ? '(%)' : '(核)', this.tab) : '';

效果

展開大圖

handlePreViewChart 事件

            <maxChart
                ref="maxChart"
                :chart-data="chartPublicData"
            />
        // 展示大dialog 圖形
        handlePreViewChart(type, title) {
            console.log('maxCpuChartData', this.maxCpuChartData); //
			{
				subtext: "(%)", 
				maxChartDate : [ "2023-04-25 17:57:10", "2023-04-25 17:58:10","2023-04-25 17:59:10","2023-04-25 18:00:10",],
				maxChartData : [4.92,4.84,5.07]
			}
            this.chartPublicData = type === 'cpu' ? { ...this.maxCpuChartData } : { ...this.maxMemChartData };
            this.$refs.maxChart.title = title;
            this.$refs.maxChart.dialogVisible = true;
        },

大圖組件 maxChart.vue

<template>
    <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        append-to-body
        width="1000px"
        @open="openDialogVisible"
    >
        <div class="dialog-wrap">
            <div
                ref="chart"
                class="echarts"
            />
        </div>
    </el-dialog>
</template>
<script>
import echarts from 'echarts';
export default {
    props: {
        lineType: {
            type: String,
            default: 'line'
        },
        chartData: {
            type: Object,
            default: () => {}
        }
    },
    data() {
        return {
            title: '',
            dialogVisible: false
        };
    },
    methods: {
        openDialogVisible() {
            this.$nextTick(() => {
                this.lineType === 'line' ? this.lineInit(this.chartData.maxChartDate, this.chartData.maxChartData, this.chartData.subtext)
                    : this.markLineInit(this.chartData.maxChartDate, this.chartData.maxChartData, this.chartData.subtext, this.chartData.limit, this.chartData.request);
            });
        },
        lineInit(date = [], data, subtext = '(%)') {
            this.$nextTick(()=>{
                let myChart = echarts.init(this.$refs['chart']);
                myChart.clear();
                myChart.setOption({
                    tooltip: {
                        trigger: 'axis',
                        position: function(point, params, dom, rect, size) {
                            var x = 0; // x坐標(biāo)位置
                            var y = 0; // y坐標(biāo)位置
                            var pointX = point[0];
                            var pointY = point[1];
                            // 提示框大小
                            var boxWidth = size.contentSize[0];
                            var boxHeight = size.contentSize[1];
                            if (boxWidth > pointX) {
                                x = 5;
                            } else { // 左邊放的下
                                x = pointX - boxWidth;
                            }
                            if (boxHeight > pointY) {
                                y = 5;
                            } else { // 上邊放得下
                                y = pointY - boxHeight;
                            }
                            return [x, y];
                        },
                        formatter: function(params) {
                            let color = params[0].color;
                            let marker = '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:' + color + ';"></span>';
                            let res = params[0].name + '<br/>' + marker + params[0].value + subtext;
                            return res;
                        }
                    },
                    dataZoom: [
                        {
                            show: date.length > 10,
                            type: 'slider'
                        }
                    ],
                    grid: {
                        top: '13%',
                        left: '10%',
                        right: 0,
                        bottom: '10%'
                    },
                    xAxis: {
                        type: 'category',
                        offset: 15,
                        data: date,
                        axisLabel: {
                            color: '#666',
                            formatter: function(value) {
                                return value.substring(value.length - 8);
                            }
                        }
                    },
                    title: {
                        subtext: subtext,
                        subtextStyle: {
                            textAlign: 'right',
                            color: '#666'
                        },
                        padding: [0, 60],
                        top: 0,
                        itemGap: 0
                    },
                    yAxis: {
                        type: 'value',
                        boundaryGap: false,
                        axisLine: {
                            show: false
                        },
                        axisLabel: {
                            color: '#666'
                        },
                        axisTick: {
                            show: false
                        }
                    },
                    series: [
                        {
                            type: 'line',
                            symbol: 'none',
                            sampling: 'lttb',
                            itemStyle: {
                                color: 'rgb(110, 153, 240)'
                            },
                            areaStyle: {
                                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                    {
                                        offset: 0,
                                        color: '#6E99F0'
                                    },
                                    {
                                        offset: 1,
                                        color: '#ffffff'
                                    }
                                ])
                            },
                            data: data
                        }
                    ]
                });
                myChart.resize();
                window.addEventListener('resize', () => {
                    myChart.resize();
                });
            });
        },
        markLineInit(date, data, subtext = '(%)', limit, request) {
            data = data.filter(i=>+i.value != 0);
            if (data.length < 1) {
                this.isNoData = true;
            }
            if (this.isNoData) return;
            this.$nextTick(() => {
                let myChart = echarts.init(this.$refs['chart']);
                myChart.clear();
                myChart.setOption({
                    tooltip: {
                        trigger: 'axis',
                        position: function(point, params, dom, rect, size) {
                            var x = 0; // x坐標(biāo)位置
                            var y = 0; // y坐標(biāo)位置
                            var pointX = point[0];
                            var pointY = point[1];
                            // 提示框大小
                            var boxWidth = size.contentSize[0];
                            var boxHeight = size.contentSize[1];
                            if (boxWidth > pointX) {
                                x = 5;
                            } else { // 左邊放的下
                                x = pointX - boxWidth;
                            }
                            if (boxHeight > pointY) {
                                y = 5;
                            } else { // 上邊放得下
                                y = pointY - boxHeight;
                            }
                            return [x, y];
                        },
                        formatter: function(params) {
                            let color = params[0].color;
                            let marker = '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:' + color + ';"></span>';
                            let res = params[0].name + '<br/>' + marker + params[0].value + subtext;
                            return res;
                        }
                    },
                    color: ['#6E99F0'],
                    title: {
                        subtext: subtext,
                        subtextStyle: {
                            textAlign: 'right',
                            color: '#666'
                        },
                        top: 0,
                        left: '8%',
                        itemGap: 0
                    },
                    grid: {
                        top: '15%',
                        left: '15%',
                        right: 0,
                        bottom: '15%'
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: date,
                        axisLabel: {
                            color: '#666',
                            formatter: function(value) {
                                return value.substring(value.length - 8);
                            }
                        }
                    },
                    yAxis: {
                        type: 'value',
                        max: limit >= request ? limit + 1 : request + 1,
                        boundaryGap: false,
                        axisLine: {
                            show: false
                        },
                        axisLabel: {
                            color: '#666'
                        },
                        axisTick: {
                            show: false
                        }
                    },
                    series: {
                        type: 'line',
                        data: data,
                        markLine: {
                            silent: true,
                            data: [
                                {
                                    yAxis: limit,
                                    lineStyle: {
                                        color: '#47C8A3',
                                        type: 'solid',
                                        cap: 'round'
                                    }
                                },
                                {
                                    yAxis: request,
                                    lineStyle: {
                                        type: 'solid',
                                        cap: 'round',
                                        color: '#FAC858'
                                    }
                                }
                            ]
                        }
                    }
                });
                myChart.resize();
                window.addEventListener('resize', () => {
                    myChart.resize();
                });
            });
        }
    }
};
</script>
<style lang="scss" scoped>
.echarts {
	width: 100%;
	height: 500px;
}
</style>

大圖效果

以上就是Vue2利用echarts繪制折線圖,餅圖和大圖的詳細(xì)內(nèi)容,更多關(guān)于Vue2 echarts繪制圖表的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • vite.config.js配置入門詳解

    vite.config.js配置入門詳解

    本文主要介紹了vite.config.js配置入門詳解,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • vue 指定組件緩存實例詳解

    vue 指定組件緩存實例詳解

    keep-alive 是 Vue 內(nèi)置的一個組件,可以使被包含的組件保留狀態(tài),或避免重新渲染。這篇文章主要介紹了vue 指定組件緩存,需要的朋友可以參考下
    2018-04-04
  • Vue方法與事件處理器詳解

    Vue方法與事件處理器詳解

    這篇文章主要為大家詳細(xì)介紹了Vue方法與事件處理器,,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • vue3下eslint配置方式

    vue3下eslint配置方式

    這篇文章主要介紹了vue3下eslint配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • vue-cli初始化項目中使用less的方法

    vue-cli初始化項目中使用less的方法

    vue-cli 是 vue.js 的腳手架工具,可以幫助我們編寫基礎(chǔ)代碼、快速搭建開發(fā)環(huán)境。下面這篇文章主要給大家介紹了關(guān)于vue-cli初始化項目中使用less的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起看看吧
    2018-08-08
  • 如何利用vue實現(xiàn)登陸界面及其跳轉(zhuǎn)詳解

    如何利用vue實現(xiàn)登陸界面及其跳轉(zhuǎn)詳解

    在開發(fā)中我們經(jīng)常遇到這樣的需求,需要用戶直接點(diǎn)擊一個鏈接進(jìn)入到一個頁面,下面這篇文章主要給大家介紹了關(guān)于如何利用vue實現(xiàn)登陸界面及其跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • Vue中的ref作用詳解(實現(xiàn)DOM的聯(lián)動操作)

    Vue中的ref作用詳解(實現(xiàn)DOM的聯(lián)動操作)

    這篇文章主要介紹了Vue中的ref作用詳解(實現(xiàn)DOM的聯(lián)動操作),需要的朋友可以參考下
    2017-08-08
  • vuex?mutation?action同級調(diào)用方式

    vuex?mutation?action同級調(diào)用方式

    這篇文章主要介紹了vuex?mutation?action同級調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Vue中點(diǎn)擊active并第一個默認(rèn)選中功能的實現(xiàn)

    Vue中點(diǎn)擊active并第一個默認(rèn)選中功能的實現(xiàn)

    這篇文章主要介紹了Vue中點(diǎn)擊active并第一個默認(rèn)選中功能的實現(xiàn)代碼,代碼簡單易懂,非常不錯具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • 解決ant design vue中樹形控件defaultExpandAll設(shè)置無效的問題

    解決ant design vue中樹形控件defaultExpandAll設(shè)置無效的問題

    這篇文章主要介紹了解決ant design vue中樹形控件defaultExpandAll設(shè)置無效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10

最新評論