echarts-設置折線圖中折線線條顏色和折線點顏色


1、問題背景

     設計一條折線圖,但是圖形中不用插件自帶的顏色,需要自定義線條和折點的顏色

 

2、實現源碼

(1)圖形自分配顏色

<!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="UTF-8">  
        <title>echarts-設置折線圖中折線線條顏色和折線點顏色</title>  
        <link rel="shortcut icon" href="../js/echarts-2.2.7/doc/asset/ico/favicon.png">  
        <script type="text/javascript" src="../js/echarts-2.2.7/doc/asset/js/jquery.min.js" ></script>  
        <script type="text/javascript" src="../js/echarts-2.2.7/doc/example/www2/js/echarts-all.js" ></script>  
        <style>  
            body,html{  
                width: 99%;  
                height: 99%;  
                font-family: "微軟雅黑";  
                font-size: 12px;  
            }  
            #line{  
                width: 100%;  
                height: 100%;  
            }  
        </style>  
        <script>  
            $(function(){  
                var chart = document.getElementById('line');    
                var echart = echarts.init(chart);   
                  
                var option = {  
                    title: {  
                        text: ''  
                    },  
                    tooltip: {  
                        trigger: 'axis'  
                    },  
                    legend: {  
                        data:['銷售量']  
                    },  
                    grid: {  
                        left: '3%',  
                        right: '4%',  
                        bottom: '3%',  
                        containLabel: true  
                    },  
                    toolbox: {  
                        feature: {  
                            saveAsImage: {}  
                        }  
                    },  
                    xAxis: {  
                        type: 'category',  
                        boundaryGap: false,  
                        data: ['周一','周二','周三','周四','周五','周六','周日']  
                    },  
                    yAxis: {  
                        type: 'value'  
                    },  
                    series: [  
                        {  
                            name:'銷售量',  
                            type:'line',  
                            stack: '銷售量',  
                            data:[220, 132, 601, 314, 890, 230, 510]  
                        }  
                    ]  
                };  
                    
                echart.setOption(option);    
            });  
        </script>  
    </head>  
    <body>  
        <div id="line"></div>  
    </body>  
</html>  

(2)線條自定義顏色

 

<!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="UTF-8">  
        <title>echarts-設置折線圖中折線線條顏色和折線點顏色</title>  
        <link rel="shortcut icon" href="../js/echarts-2.2.7/doc/asset/ico/favicon.png">  
        <script type="text/javascript" src="../js/echarts-2.2.7/doc/asset/js/jquery.min.js" ></script>  
        <script type="text/javascript" src="../js/echarts-2.2.7/doc/example/www2/js/echarts-all.js" ></script>  
        <style>  
            body,html{  
                width: 99%;  
                height: 99%;  
                font-family: "微軟雅黑";  
                font-size: 12px;  
            }  
            #line{  
                width: 100%;  
                height: 100%;  
            }  
        </style>  
        <script>  
            $(function(){  
                var chart = document.getElementById('line');    
                var echart = echarts.init(chart);   
                  
                var option = {  
                    title: {  
                        text: ''  
                    },  
                    tooltip: {  
                        trigger: 'axis'  
                    },  
                    legend: {  
                        data:['銷售量']  
                    },  
                    grid: {  
                        left: '3%',  
                        right: '4%',  
                        bottom: '3%',  
                        containLabel: true  
                    },  
                    toolbox: {  
                        feature: {  
                            saveAsImage: {}  
                        }  
                    },  
                    xAxis: {  
                        type: 'category',  
                        boundaryGap: false,  
                        data: ['周一','周二','周三','周四','周五','周六','周日']  
                    },  
                    yAxis: {  
                        type: 'value'  
                    },  
                    series: [  
                        {  
                            name:'銷售量',  
                            type:'line',  
                            stack: '銷售量',  
                            itemStyle : {  
                                normal : {  
                                    lineStyle:{  
                                        color:'#00FF00'  
                                    }  
                                }  
                            },  
                            data:[220, 132, 601, 314, 890, 230, 510]  
                        }  
                    ]  
                };  
                    
                echart.setOption(option);    
            });  
        </script>  
    </head>  
    <body>  
        <div id="line"></div>  
    </body>  
</html> 

 

(3)折點自定義顏色

 

 

<!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="UTF-8">  
        <title>echarts-設置折線圖中折線線條顏色和折線點顏色</title>  
        <link rel="shortcut icon" href="../js/echarts-2.2.7/doc/asset/ico/favicon.png">  
        <script type="text/javascript" src="../js/echarts-2.2.7/doc/asset/js/jquery.min.js" ></script>  
        <script type="text/javascript" src="../js/echarts-2.2.7/doc/example/www2/js/echarts-all.js" ></script>  
        <style>  
            body,html{  
                width: 99%;  
                height: 99%;  
                font-family: "微軟雅黑";  
                font-size: 12px;  
            }  
            #line{  
                width: 100%;  
                height: 100%;  
            }  
        </style>  
        <script>  
            $(function(){  
                var chart = document.getElementById('line');    
                var echart = echarts.init(chart);   
                  
                var option = {  
                    title: {  
                        text: ''  
                    },  
                    tooltip: {  
                        trigger: 'axis'  
                    },  
                    legend: {  
                        data:['銷售量']  
                    },  
                    grid: {  
                        left: '3%',  
                        right: '4%',  
                        bottom: '3%',  
                        containLabel: true  
                    },  
                    toolbox: {  
                        feature: {  
                            saveAsImage: {}  
                        }  
                    },  
                    xAxis: {  
                        type: 'category',  
                        boundaryGap: false,  
                        data: ['周一','周二','周三','周四','周五','周六','周日']  
                    },  
                    yAxis: {  
                        type: 'value'  
                    },  
                    series: [  
                        {  
                            name:'銷售量',  
                            type:'line',  
                            stack: '銷售量',  
                            itemStyle : {  
                                normal : {  
                                    color:'#00FF00',  
                                    lineStyle:{  
                                        color:'#00FF00'  
                                    }  
                                }  
                            },  
                            data:[220, 132, 601, 314, 890, 230, 510]  
                        }  
                    ]  
                };  
                    
                echart.setOption(option);    
            });  
        </script>  
    </head>  
    <body>  
        <div id="line"></div>  
    </body>  
</html>  

 

3、實現結果

(1)圖形自分配顏色

(2)線條自定義顏色

(3)折點自定義顏色

4、問題說明

(1)設置折線線條顏色
        lineStyle:{
                 color:'#00FF00'
         }

 

(2)設置折線折點顏色

       itemStyle : {
                                normal : {
                                    color:'#00FF00'
                                }
                            }

 


免責聲明!

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



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