echarts一個界面多個餅圖簡單封裝

<body>
        <div id="lamp" style="height: 174px;"></div>
        <div id="landscape" style="height: 174px;"></div>
        <div id="saving" style="height: 208px;"></div>
        
        <script src="echarts.min.js"></script>
        <script type="text/javascript">
             var lampOpenCount =3;
             var convertCtrlOpen=1;
             var saveEnergy=1;
             var lampCount=2;
             var convert=2;
             var noIntelLight=4;
             
            drawPieChart('lamp', 1,lampOpenCount,lampCount);
            drawPieChart('landscape', 1,convertCtrlOpen,convert);
            drawPieChart('saving', 2,saveEnergy,noIntelLight);
            
            function drawPieChart(id, type,openCount,count) {
                var myChart = echarts.init(document.getElementById(id));
                var option = {
                    tooltip: {
                        show: false
                    },
                    legend: {
                        show: false
                    },
                    color: ['#efae25', '#f0f2f5'],
                    series: [{
                        name: type === 1 ? '亮燈率' : '節電率',
                        type: 'pie',
                        radius: type === 1 ? ['60%', '80%'] : ['48%', '68%'],
                        center: ['50%', '50%'],
                        hoverAnimation: false,
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                position: 'center',
                                textStyle: {
                                    fontSize: '16',
                                    color: '#999'
                                },
                                formatter: "{a} \n {d}%"
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: [{
                            value: openCount,
                            label: {
                                normal: {
                                    show: true
                                }
                            },
                            itemStyle: {
                                emphasis: {
                                    color: '#efae25'
                                }
                            },
                        },
                            {
                                value: count-openCount,
                                label: {
                                    normal: {
                                        show: false
                                    }
                                },
                                itemStyle: {
                                    emphasis: {
                                        color: '#f0f2f5'
                                    }
                                }
                            }
                        ]
                    }]
                };;

                if (option && typeof option === "object") {
                    myChart.setOption(option, true);
                }
            }
        </script>
    </body>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章