首页 > 程序开发 > web前端 > JavaScript >

ECharts多个柱状图动态获取json数据

2017-08-04

ECharts多个柱状图动态获取json数据。

ECharts 多个柱状图动态获取json数据

效果图如下:

\

一.html部分

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

二.js部分

<script type="text/Java/" target="_blank">JavaScript">

function loadOneColumn() {

    var myChart = echarts.init(document.getElementById(&#39;twoColumn&#39;));
    // 显示标题,图例和空的坐标轴
    var series = [];
    var series2 = [];
    myChart.setOption({
        color : ["#26aa1b", "#f9873a"],
        title: {
            text: &#39;异步数据加载示例&#39;
        },
        tooltip: {},
        legend: {
            data: []
        },
        xAxis: {
            type: &#39;category&#39;,
            data: []
        },
        yAxis: {
            splitLine: { show: false },//去除网格线
            name: &#39;&#39;
        },
        series: [{
            name: &#39;数据集&#39;,
            type: &#39;bar&#39;,
            data: []
        },
          {
              name: &#39;API&#39;,
              type: &#39;bar&#39;,
              data: []
        }]
    });
    myChart.showLoading();    //数据加载完之前先显示一段简单的loading动画
    var names = [];    //类别数组(实际用来盛放X轴坐标值)
    $.ajax({
        type: &#39;get&#39;,
        url: &#39;json/echarts/column/columnTwo.txt&#39;,//请求数据的地址
        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
        }
    ]
}
相关文章
最新文章
热点推荐