Echarts中堆疊條形圖的實例&legend參數設置 由水平變爲側邊垂直排列

老規矩,先上效果圖

堆疊條形圖


legend的效果實現主要靠這幾行代碼:

legend: {
                        data:['直接訪問', '郵件營銷','聯盟廣告','視頻廣告','搜索引擎'],
                        orient: 'vertical',  //垂直顯示
                        y: 'center',    //延Y軸居中
                        x: 'right', //居右顯示
                        selectedMode: 'single', 
                        padding:-0.5 //調節legend的位置
                    },

總的HTML代碼如下

<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
</head>

<body>
    <!-- 爲ECharts準備一個具備大小(寬高)的Dom -->
    <div id="main" style="height:400px;width: 700px;margin-left: 30px;"></div>
    <script src="http://echarts.baidu.com/build/dist/echarts.js"></script>
    <script type="text/javascript" src="../js/all.js" ></script>
     <script type="text/javascript">
        // 路徑配置
        require.config({
            paths: {
                echarts: 'http://echarts.baidu.com/build/dist'
            }
        });

        // 使用
        require(
            [
                'echarts',
                'echarts/chart/line',//需要折線圖則加載line模塊
                'echarts/chart/bar' // 使用柱狀圖就加載bar模塊,按需加載
            ],
            function (ec) {
                // 基於準備好的dom,初始化echarts圖表
                var myChart = ec.init(document.getElementById('main')); 

                option = {
                    tooltip : {
                        trigger: 'axis',
                        axisPointer : {            // 座標軸指示器,座標軸觸發有效
                            type : 'shadow'        // 默認爲直線,可選爲:'line' | 'shadow'
                        }
                    },
                    legend: {
                        data:['直接訪問', '郵件營銷','聯盟廣告','視頻廣告','搜索引擎'],
                        orient: 'vertical',  //垂直顯示
                        y: 'center',    //延Y軸居中
                        x: 'right', //居右顯示
                        selectedMode: 'single', 
                        padding:-0.5
                    },
                    toolbox: {
                        show : true,
                        feature : {
                            mark : {show: true},
                            dataView : {show: true, readOnly: false},
                            magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                            restore : {show: true},
                            saveAsImage : {show: true}
                        }
                    },
                    calculable : true,
                    xAxis : [
                        {
                            type : 'value'
                        }
                    ],
                    yAxis : [
                        {
                            type : 'category',
                            data : ['週一','週二','週三','週四','週五','週六','週日']
                        }
                    ],
                    series : [
                        {
                            name:'直接訪問',
                            type:'bar',
                            stack: '總量',
                            itemStyle : { normal: {label : {show: true, position: 'insideRight'}}},
                            data:[320, 302, 301, 334, 390, 330, 320]
                        },
                        {
                            name:'郵件營銷',
                            type:'bar',
                            stack: '總量',
                            itemStyle : { normal: {label : {show: true, position: 'insideRight'}}},
                            data:[120, 132, 101, 134, 90, 230, 210]
                        },
                        {
                            name:'聯盟廣告',
                            type:'bar',
                            stack: '總量',
                            itemStyle : { normal: {label : {show: true, position: 'insideRight'}}},
                            data:[220, 182, 191, 234, 290, 330, 310]
                        },
                        {
                            name:'視頻廣告',
                            type:'bar',
                            stack: '總量',
                            itemStyle : { normal: {label : {show: true, position: 'insideRight'}}},
                            data:[150, 212, 201, 154, 190, 330, 410]
                        },
                        {
                            name:'搜索引擎',
                            type:'bar',
                            stack: '總量',
                            itemStyle : { normal: {label : {show: true, position: 'insideRight'}}},
                            data:[820, 832, 901, 934, 1290, 1330, 1320]
                        }
                    ]
                };



                // 爲echarts對象加載數據 
                myChart.setOption(option); 
            }
        );
    </script>
</body>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章