鍍金池/ 問答/PHP  HTML/ 關(guān)于echarts一個實例修改,來請教

關(guān)于echarts一個實例修改,來請教

我想要還原設(shè)計圖的效果

總分100分,如果得分是80分,半圓就會有80%紅色,20%白色
clipboard.png

問題

如何不動態(tài)的變數(shù)據(jù),并且達(dá)到我想要的效果

echarts社區(qū)上的效果

clipboard.png

對應(yīng)代碼

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div id="main" style="width: 600px;height:400px;"></div>
<!--<script src="echarts.js"></script>-->
<script src="https://cdn.bootcss.com/echarts/3.8.5/echarts.min.js"></script>
<script>
    var myChart = echarts.init(document.getElementById('main'));
    function detectionData(str) {
        var color = new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
            offset: 0,
            color: 'rgba(243, 238,67, 1)'
        }, {
            offset: 0.5,
            color: 'rgba(225, 128,78, 1)'
        }, {
            offset:1,
            color: 'rgba(237, 92,79, 1)'
        }]);
        if (str >= 30 && str <= 60) {
            color =  new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
                offset: 0,
                color: 'rgba(243, 238,67, 1)'
            }, {
                offset: 0.5,
                color: 'rgba(225, 128,78, 1)'
            }, {
                offset:1,
                color: 'rgba(237, 92,79, 1)'
            }]);
        } else if (str > 60) {
            color = new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
                offset: 0,
                color: 'rgba(243, 238,67, 1)'
            }, {
                offset: 0.5,
                color: 'rgba(225, 128,78, 1)'
            }, {
                offset:1,
                color: 'rgba(237, 92,79, 1)'
            }]);
        }
        return color;
    }
    var option = {
        backgroundColor:'#333333',
        tooltip: {
            formatter: "{a} <br/> : {c}%"
        },
        series: [{
            name: "儀盤表",
            type: "gauge",
            splitNumber:60,
            axisLine: {
                "lineStyle": {
                    "color": [
                        [0.31, "#333"],
                        [1, "#333"]
                    ],
                    "width": 20
                },

            },
            axisTick: {
                lineStyle: {
                    color:'#ccc',
                    width: 2
                },
                length: 20,
                splitNumber:2
            },
            pointer: {           // 分隔線
                shadowColor : '#fff', //默認(rèn)透明
                shadowBlur: 5,
                show:false
            },
            axisLabel: {
                distance: 10,
                textStyle: {
                    color: "#fff"
                },
                show:false,
            },
            splitLine: {
                "show": false
            },
            itemStyle: {
                normal: {
                    color: "#494f50"
                }
            },
            detail: {
                formatter: "{value}%",
                offsetCenter: [0, "30%"],
                textStyle: {
                    fontSize: 60,
                    color: "#F37B1D"
                }
            },
            title: {
                offsetCenter: [0, "60%"]
            },
            data: [{
                name: "CPU",
                value: 31
            }]
        }]
    }
    myChart.timeTicket = setInterval(function() {
        var value = (Math.random() * 100).toFixed(2) - 0;
        option.series[0].data[0].value = value;
        option.series[0].axisLine.lineStyle.color[0][0] = value / 100;
        option.series[0].axisLine.lineStyle.color[0][1] = detectionData(value);
        myChart.setOption(option, true);
    }, 500);
    myChart.setOption(option);
</script>
</body>
</html>

我現(xiàn)在已經(jīng)修改后,做到的效果

對應(yīng)代碼

clipboard.png

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div id="main" style="width: 600px;height:400px;"></div>
<!--<script src="echarts.js"></script>-->
<script src="https://cdn.bootcss.com/echarts/3.8.5/echarts.min.js"></script>
<script>
    var myChart = echarts.init(document.getElementById('main'));
    function detectionData(str) {
        var color = new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
            offset: 0,
            color: 'rgba(243, 238,67, 1)'
        }, {
            offset: 0.5,
            color: 'rgba(225, 128,78, 1)'
        }, {
            offset:1,
            color: 'rgba(237, 92,79, 1)'
        }]);
        if (str >= 30 && str <= 60) {
            color =  new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
                offset: 0,
                color: 'rgba(243, 238,67, 1)'
            }, {
                offset: 0.5,
                color: 'rgba(225, 128,78, 1)'
            }, {
                offset:1,
                color: 'rgba(237, 92,79, 1)'
            }]);
        } else if (str > 60) {
            color = new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
                offset: 0,
                color: 'rgba(243, 238,67, 1)'
            }, {
                offset: 0.5,
                color: 'rgba(225, 128,78, 1)'
            }, {
                offset:1,
                color: 'rgba(237, 92,79, 1)'
            }]);
        }
        return color;
    }
    var option = {
        backgroundColor:'#333333',
        tooltip: {
            formatter: "{a} <br/> : {c}%"
        },
        series: [{
            name: "儀盤表",
            type: "gauge",
            splitNumber:60,
            axisLine: {
                "lineStyle": {
                    "color": [
                        [0.31, "#333"],
                        [1, "#333"]
                    ],
                    "width": 20
                },

            },
            axisTick: {
                lineStyle: {
                    color:'#ccc',
                    width: 2
                },
                length: 20,
                splitNumber:2
            },
            pointer: {           // 分隔線
                shadowColor : '#fff', //默認(rèn)透明
                shadowBlur: 5,
                show:false
            },
            axisLabel: {
                distance: 10,
                textStyle: {
                    color: "#fff"
                },
                show:false,
            },
            splitLine: {
                "show": false
            },
            itemStyle: {
                normal: {
                    color: "#494f50"
                }
            },
            detail: {
                formatter: "{value}%",
                offsetCenter: [0, "30%"],
                textStyle: {
                    fontSize: 60,
                    color: "#F37B1D"
                }
            },
            title: {
                offsetCenter: [0, "60%"]
            },
            data: [{
                name: "CPU",
                value: 31
            }]
        }]
    }
    myChart.timeTicket = setInterval(function() {
        var value = (Math.random() * 100).toFixed(2) - 0;
        option.series[0].data[0].value = value;
        option.series[0].axisLine.lineStyle.color[0][0] = value / 100;
        option.series[0].axisLine.lineStyle.color[0][1] = detectionData(value);
        myChart.setOption(option, true);
    }, 50000);
//    myChart.setOption(option, true);
    myChart.setOption(option);
</script>
</body>
</html>
回答
編輯回答
淺時光

沒太理解,你在獲取到分?jǐn)?shù)以及總分后,通過這兩個數(shù)值初始化echarts圖表就可以了啊

color: [[(currentNum / totalNum), '#4bbd4b'], [1, '#ec3434']],
2017年3月15日 01:11
編輯回答
巷尾

沒理解問題出在哪,【如何不動態(tài)的變數(shù)據(jù),并且達(dá)到我想要的效果】想表達(dá)的到底是什么意思?是指你的效果31%所對應(yīng)的圖表沒有圖表進(jìn)度的狀態(tài)顯示嗎?

2018年8月22日 15:54