溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Echarts橫向堆疊柱狀圖和markLine怎么使用

發(fā)布時間:2022-06-06 10:38:57 來源:億速云 閱讀:228 作者:zzz 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Echarts橫向堆疊柱狀圖和markLine怎么使用”,在日常操作中,相信很多人在Echarts橫向堆疊柱狀圖和markLine怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Echarts橫向堆疊柱狀圖和markLine怎么使用”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

1.Echarts 橫向堆疊柱狀圖 + markLine

效果圖

根據(jù)月份計算百分比展示markLine

Echarts橫向堆疊柱狀圖和markLine怎么使用

思路: 根據(jù)月份計算百分比展示markLine,例如3月就是25%,這里的圖表是數(shù)值,所以markLine要展示百分比需要進行一下計算,思路是在series里添加一個專門為了markLine處理的(這里是雙柱子所以要采用這種方法,如果是單個柱子就不需要,可以直接在series里邊項寫markLine),具體計算方式在option代碼上面,大家自行看一下這里不復(fù)制重復(fù)寫了

驗證:我這里的x軸隱藏掉了,大家為了驗證計算的對不對可以把axisLabel show: 改為true,對比下數(shù)值和markLine值是否正確

代碼如下:

mychart() {
    var myChart = echarts.init(document.getElementById('profitTotal6'));
    let echartData = [{
        name: "其他",
        value1: 64,
        value2: 84,
    },
    {
        name: "運輸",
        value1: 104,
        value2: 164,
    },
    {
        name: "化工",
        value1: 619.59,
        value2: 354.00,
    },
    {
        name: "煤炭",
        value1: 338.01,
        value2: 154.00,
    },
    {
        name: "光伏",
        value1: 248.69,
        value2: 934.00,
    },
    {
        name: "風(fēng)電",
        value1: 556.43,
        value2: 654.00,
    },
    {
        name: "水電",
        value1: 816.31,
        value2: 354.00,
    },
    {
        name: "火電",
        value1: 221.87,
        value2: 154.00,
    }
    ];
    let xAxisData = echartData.map(v => v.name);
    let yAxisData1 = echartData.map(v => v.value1);
    let yAxisData2 = echartData.map(v => v.value2);

    let bgdata = [];
    echartData.map(item => {
        bgdata.push(parseInt(item.value1 + item.value2) + 100);
    })
    let maxxAxis = Math.max.apply(null,bgdata);//設(shè)置x軸最大值
    let date_ = new Date();
    let month = date_.getMonth() + 1;
    let markyAxis = maxxAxis / 12 * month;  //markLine值
    let markyvalueText = parseInt(markyAxis / maxxAxis * 100); //為了控制百分樣式
    let paddingStyle;//根據(jù)數(shù)值動態(tài)設(shè)置padding樣式
    if (0 <= markyvalueText && markyvalueText < 10) {
        paddingStyle = [10, 7];
    } else if (10 <= markyvalueText && markyvalueText < 100) {
        paddingStyle = [10, 5];
    } else {
        paddingStyle = [14, 5];
    }

    option = {
        // tooltip: {
        //     trigger: 'axis',
        //     axisPointer: {
        //         type: 'shadow'
        //     }
        // },
        legend: {
            data: ['年度投資完成額(滯后)', '年度投資計劃'],
            orient: "horizontal",//vertical
            x: 'center',
            // y: 'bottom',
            // right: '-50%',
            bottom: '2%',
            icon: "roundRect1",
            selectedMode: false,//取消圖例上的點擊事件
            itemWidth: 16,  // 設(shè)置寬度
            itemHeight: 10, // 設(shè)置高度
            itemGap: 10,// 設(shè)置間距
            textStyle: {//文字根據(jù)legend顯示 
                color: "#FFFFFF",
                fontSize: 12,
            },
        },
        grid: {
            left: '8%',
            top: '18%',
            right: '8%',
            bottom: '12%',
            containLabel: true
        },
        yAxis: {
            type: 'category',
            triggerEvent: true,
            data: xAxisData,
            axisLine: {
                show: false
            },
            axisLabel: {
                color: "#FFFFFF",
                fontSize: '14',
                // interval: 0,
                // rotate: rotate//文字旋轉(zhuǎn)角度
            },
            axisTick: {
                show: false,
                alignWithLabel: true,
                lineStyle: {
                    color: '#0C4F81',
                    type: 'solid'
                }
            },
        },
        xAxis: {
            type: 'value',
            max: maxxAxis,
            nameTextStyle: {
                color: '#4F88BD',
                padding: [0, 25, -5, 0],
                fontSize: 12,
                fontFamily: 'Microsoft YaHei',
            },
            axisLine: {
                show: false,
                lineStyle: {
                    color: "#0C4F81"
                }
            },
            axisLabel: {
                show: false,//
                color: "#4F88BD",
                fontSize: '12',
                formatter: '{value}'
            },
            splitLine: {
                show: false,
                lineStyle: {
                    type: "dotted",
                    color: "#0C4F81"
                }
            },
            axisTick: {
                show: false
            },
        },
        series: [
            {
                name: '年度投資完成額(滯后)',
                type: 'bar',
                barMaxWidth: 15,
                stack: 'Ad',
                emphasis: {
                    focus: 'series'
                },
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            // position: 'top',
                            color: '#ffffff'
                        },
                        color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                            offset: 0,
                            color: "rgba(128, 123, 254, 1)"
                        },
                        {
                            offset: 1,
                            color: "rgba(230, 143, 252, 1)"
                        }
                        ]),
                    }
                },
                data: yAxisData1,
            },
            {
                name: '年度投資計劃',
                type: 'bar',
                barMaxWidth: 15,
                stack: 'Ad',
                emphasis: {
                    focus: 'series'
                },
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            // position: 'top',
                            color: '#ffffff'
                        },
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: "rgba(13, 78, 137, 1)"
                        },
                        {
                            offset: 1,
                            color: "rgba(13, 78, 137, 1)"
                        }
                        ]),
                    }
                },
                data: yAxisData2,
            },
            {
                // 為了處理markline
                name: '最長背景',
                type: 'bar',
                barMaxWidth: 5,
                color: 'transparent',
                data: bgdata,
                markLine: {
                    data: [
                        { name: '考核臨界線',xAxis:markyAxis},
                    ],
                    silent: true,
                    symbol:'none',//去掉箭頭
                    itemStyle: {
                        normal: {
                            lineStyle: {
                                color: '#FA7F3C',
                                type: 'solid'
                            },
                            label:{
                                // color: '#FA7F3C',
                                formatter:'{c}%',
                                show:true,
                                backgroundColor: '#FFF7F2',
                                color: '#DB6525',
                                fontSize: '100%',
                                borderColor: '#FFF7F2',
                                formatter: function(v){
                                    var s = parseInt(v.value / maxxAxis * 100);
                                    return s + '%';
                                },
                                padding:paddingStyle,
                                borderRadius: 50,
                            }
                        }
                    },
                },
            },
        ]
    };
    myChart.clear();
    myChart.setOption(option);
},

2.Echarts 橫向堆疊柱狀圖 + markLine

效果圖

根據(jù)數(shù)據(jù)計算百分比展示markLine

Echarts橫向堆疊柱狀圖和markLine怎么使用

代碼如下

根據(jù)數(shù)據(jù)計算百分比展示markLine,和上面基本同理,這個只是數(shù)值上的轉(zhuǎn)換,和月份沒有關(guān)系了

mychart() {
    var myChart = echarts.init(document.getElementById('profitTotal2'));
    let echartData = [{
        name: "其他",
        value1: 64,
        value2: 84,
    },
    {
        name: "運輸",
        value1: 104,
        value2: 164,
    },
    {
        name: "化工",
        value1: 619.59,
        value2: 354.00,
    },
    {
        name: "煤炭",
        value1: 338.01,
        value2: 154.00,
    },
    {
        name: "光伏",
        value1: 248.69,
        value2: 934.00,
    },
    {
        name: "風(fēng)電",
        value1: 556.43,
        value2: 654.00,
    },
    {
        name: "水電",
        value1: 816.31,
        value2: 354.00,
    },
    {
        name: "火電",
        value1: 221.87,
        value2: 154.00,
    }
    ];
    let xAxisData = echartData.map(v => v.name);
    let yAxisData1 = echartData.map(v => v.value1);
    let yAxisData2 = echartData.map(v => v.value2);

    let bgdata = [];
    echartData.map(item => {
        bgdata.push(parseInt(item.value1 + item.value2) + 100);
    })
    let maxxAxis = Math.max.apply(null,bgdata);//設(shè)置x軸最大值
    let markyAxis = maxxAxis  * 0.9;  //markLine值90%
    let markyvalueText = parseInt(markyAxis / maxxAxis * 100); //為了控制百分樣式
    let paddingStyle;//根據(jù)數(shù)值動態(tài)設(shè)置padding樣式
    if (0 <= markyvalueText && markyvalueText < 10) {
        paddingStyle = [10, 7];
    } else if (10 <= markyvalueText && markyvalueText < 100) {
        paddingStyle = [10, 5];
    } else {
        paddingStyle = [14, 5];
    }
    option = {
        // tooltip: {
        //     trigger: 'axis',
        //     axisPointer: {
        //         type: 'shadow'
        //     }
        // },
        legend: {
            data: ['合同總額(預(yù)警)', '項目概算'],
            orient: "horizontal",//vertical
            x: 'center',
            // y: 'bottom',
            // right: '-50%',
            bottom: '2%',
            icon: "roundRect1",
            selectedMode: false,//取消圖例上的點擊事件
            itemWidth: 16,  // 設(shè)置寬度
            itemHeight: 10, // 設(shè)置高度
            itemGap: 10,// 設(shè)置間距
            textStyle: {//文字根據(jù)legend顯示 
                color: "#FFFFFF",
                fontSize: 12,
            },
        },
        grid: {
            left: '8%',
            top: '18%',
            right: '8%',
            bottom: '12%',
            containLabel: true
        },
        yAxis: {
            type: 'category',
            triggerEvent: true,
            data: xAxisData,
            axisLine: {
                show: false
            },
            axisLabel: {
                color: "#FFFFFF",
                fontSize: '14',
                // interval: 0,
                // rotate: rotate//文字旋轉(zhuǎn)角度
            },
            axisTick: {
                show: false,
                alignWithLabel: true,
                lineStyle: {
                    color: '#0C4F81',
                    type: 'solid'
                }
            },
        },
        xAxis: {
            type: 'value',
            max: maxxAxis,
            nameTextStyle: {
                color: '#4F88BD',
                padding: [0, 25, -5, 0],
                fontSize: 12,
                fontFamily: 'Microsoft YaHei',
            },
            axisLine: {
                show: false,
                lineStyle: {
                    color: "#0C4F81"
                }
            },
            axisLabel: {
                show: false,
                color: "#4F88BD",
                fontSize: '12',
                formatter: '{value}'
            },
            splitLine: {
                show: false,
                lineStyle: {
                    type: "dotted",
                    color: "#0C4F81"
                }
            },
            axisTick: {
                show: false
            },
        },
        series: [
            {
                name: '合同總額(預(yù)警)',
                type: 'bar',
                barMaxWidth: 15,
                // zlevel: 1,
                stack: 'Ad',
                emphasis: {
                    focus: 'series'
                },
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            // position: 'top',
                            color: '#ffffff'
                        },
                        color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                            offset: 0,
                            color: "rgba(252, 175, 159, 1)"
                        },
                        {
                            offset: 1,
                            color: "rgba(241, 88, 135, 1)"
                        }
                        ]),
                    }
                },
                data: yAxisData1,
            },
            {
                name: '項目概算',
                type: 'bar',
                barMaxWidth: 15,
                // zlevel: 1,
                stack: 'Ad',
                emphasis: {
                    focus: 'series'
                },
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            // position: 'top',
                            color: '#ffffff'
                        },
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: "rgba(13, 78, 137, 1)"
                        },
                        {
                            offset: 1,
                            color: "rgba(13, 78, 137, 1)"
                        }
                        ]),
                    }
                },
                data: yAxisData2,
            },
            {
                // 為了處理markline
                name: '最長背景',
                type: 'bar',
                barMaxWidth: 5,
                // barGap: '-100%',
                color: 'transparent',
                // itemStyle: {
                //     normal: {
                //         color: '#1B375E',
                //         barBorderRadius: 0,
                //     },
                // },
                data: bgdata,
                markLine: {
                    data: [
                        { name: '考核臨界線',xAxis:markyAxis},
                    ],
                    silent: true,
                    symbol:'none',//去掉箭頭
                    itemStyle: {
                        normal: {
                            lineStyle: {
                                color: '#FA7F3C',
                                type: 'solid'
                            },
                            label:{
                                formatter:'{c}%',
                                show:true,
                                backgroundColor: '#FFF7F2',
                                color: '#DB6525',
                                fontSize: '100%',
                                borderColor: '#FFF7F2',
                                formatter: function(v){
                                    var s = parseInt(v.value / maxxAxis * 100);
                                    return s + '%';
                                },
                                padding:paddingStyle,
                                borderRadius: 50,
                            }
                        }
                    },
                },
            },
        ]
    };
    myChart.clear();
    myChart.setOption(option);
},

到此,關(guān)于“Echarts橫向堆疊柱狀圖和markLine怎么使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI