ECharts多個柱狀圖動態獲取json數據教程

ECharts多個柱狀圖動態獲取json數據教程

效果圖如下:

一.html部分

<p id="twoColumn" style="width:100%; height:400px;"></p>

二.js部分

<script type="text/javascript">
function loadOneColumn() {
    var myChart = echarts.init(document.getElementById('twoColumn'));
    // 顯示標題,圖例和空的坐標軸
    var series = [];
    var series2 = [];
    myChart.setOption({
        color : ["#26aa1b", "#f9873a"],
        title: {
            text: '異步數據加載示例'
        },
        tooltip: {},
        legend: {
            data: []
        },
        xAxis: {
            type: 'category',
            data: []
        },
        yAxis: {
            splitLine: { show: false },//去除網格線
            name: ''
        },
        series: [{
            name: '數據集',
            type: 'bar',
            data: []
        },
          {
              name: 'API',
              type: 'bar',
              data: []
        }]
    });
    myChart.showLoading();    //數據加載完之前先顯示一段簡單的loading動畫
    var names = [];    //類別數組(實際用來盛放X軸坐標值)
    $.ajax({
        type: 'get',
        url: 'json/echarts/column/columnTwo.txt',//請求數據的地址
        dataType: "json",        //返回數據形式為json
        success: function (result) {
            //請求成功時執行該函數內容,result即為服務器返回的json對象
            $.each(result.jinJian, function (index, item) {
                names.push(item.AREA);    //挨個取出類別並填入類別數組
                series.push(item.LANDNUM);
            });
            $.each(result.banJie, function (index, item) {  
                series2.push(item.LANDNUM);
            });
            myChart.hideLoading();    //隱藏加載動畫
            myChart.setOption({        //加載數據圖表
                xAxis: {
                    data: names
                },
                series: [{                    
                    data: series
                },
                 {                    
                     data: series2
                 }]
            });      
        },
        error: function (errorMsg) {
            //請求失敗時執行該函數
            alert("圖表請求數據失敗!");
            myChart.hideLoading();
        }
    });
};
loadOneColumn();
</script>

三.json格式如下:

{
    "jinJian":[
        {
            "AREA":"選址階段",
            "LANDNUM":475
        },
        {
            "AREA":"用地階段",
            "LANDNUM":475
        },
        {
            "AREA":"設計方案",
            "LANDNUM":475
        },
{
            "AREA":"工程規劃",
            "LANDNUM":475
        },
{
            "AREA":"施工許可",
            "LANDNUM":475
        },
{
            "AREA":"銷售許可",
            "LANDNUM":475
        },
{
            "AREA":"規劃驗收",
            "LANDNUM":475
        },
{
            "AREA":"綜合驗收",
            "LANDNUM":475
        },
{
            "AREA":"檔案驗收",
            "LANDNUM":475
        }
    ],
"banJie":[
        {
            "AREA":"選址階段",
            "LANDNUM":352
        },
        {
            "AREA":"用地階段",
            "LANDNUM":352
        },
        {
            "AREA":"設計方案",
            "LANDNUM":352
        },
{
            "AREA":"工程規劃",
            "LANDNUM":352
        },
{
            "AREA":"施工許可",
            "LANDNUM":352
        },
{
            "AREA":"銷售許可",
            "LANDNUM":352
        },
{
            "AREA":"規劃驗收",
            "LANDNUM":352
        },
{
            "AREA":"綜合驗收",
            "LANDNUM":352
        },
{
            "AREA":"檔案驗收",
            "LANDNUM":352
        }
    ]
}

發佈留言