【问题标题】:How to create grouped bars in amCharts?如何在 amCharts 中创建分组柱?
【发布时间】:2016-01-04 11:34:38
【问题描述】:

我正在使用 amCharts 在我的表单中创建图表。我想创建一个分组条形图。

我的 JSON 数据是:

  [{date:'10-dec-2015',visits:4025,country:'USA'},{date:'10-dec-2015',visits:1182,country:'India'},{date:'10-dec-2015',visits:o,country:'Spain'},{date:'11-dec-2015',visits:1322,country:'USA'},{date:'11-dec-2015',visits:1122,country:'India'},{date:'11-dec-2015',visits:1114,country:'Spain'},{date:'12-dec-2015',visits:984,country:'India'},{date:'13-dec-2015',visits:711,country:'Poland'},]

我现有的代码如下:

chartData = generateChartData();   //function call  
//creating column chart

var chart = AmCharts.makeChart("chartdiv", {
    "type": "serial",
    "theme": "light",
    "dataProvider": chartData,
    "categoryField": "date",
    "rotate": false,
    "startDuration": 1,
    "categoryAxis": {
        "labelRotation": 90,
        "gridPosition": "start",
        "position": "left",
        "autoGridCount": false,
    },
    "trendLines": [],
    "graphs": [
        {
            "balloonText": "[[country ]]:[[value]]",
            "fillAlphas": 0.8,
            "id": "AmGraph-1"+i,
            "lineAlpha": 0.2,
            "title": "title",
            "type": "column",
            "valueField": "visits",
             "color":colors[0]
        }
    ],
    "chartScrollbar": {
        "autoGridCount": false,
        "graph": "AmGraph-1"+i,
        "scrollbarHeight": 10
    },
    "legend": {
        "align": "center",
        "position": "right",
        "markerType": "square",
        "right": 6,
        "labelText": "[[title]]",
        "valueText": "",
        "valueWidth": 80,
        "textClickEnabled": true,
        "rollOverColor": "blue",
        "fontSize": 13,
        "useGraphSettings": true

    },
    "guides": [],
    "valueAxes": [
        {
            "id": "ValueAxis-1",
            "position": "left",
            "axisAlpha": 0
        }
    ],
    "allLabels": [],
    "balloon": {},
    "titles": [],
    "export": {
        "enabled": true
    },
});
//function definition

 function generateChartData() {
    chartData = [];       
        for (var i = 0; i < $scope.datalength; i++) {
            var newdate = $scope.data[i].Date;
            var visits = $scope.data[i].visits;
            var country = $scope.data[i].country;

            chartData.push({
                date: newdate,
                visits: visits,
                country : country 
            });
        }          
    }       
    return chartData;
}

现有输出如下:

预期输出为:

谁能帮我达到我的预期输出。

提前致谢。

【问题讨论】:

    标签: angularjs amcharts


    【解决方案1】:

    你需要做几件事才能达到目标:

    1) 将同一类别的数据点归为同一数据点,这样你的最终数据就变成了这样:

    [ {
      date: '10-dec-2015',
      USA: 4025,
      India: 1182,
      Spain: 1000
    }, {
      date: '11-dec-2015',
      USA: 1322,
      India: 1122,
      Spain: 1114
    }, {
      date: '12-dec-2015',
      India: 984
    }, {
      date: '13-dec-2015',
      Poland: 711
    } ]
    

    2) 为每个国家/地区创建图表:

    "graphs": [ {
      "balloonText": "[[title]]:[[value]]",
      "fillAlphas": 0.8,
      "lineAlpha": 0.2,
      "title": "USA",
      "type": "column",
      "valueField": "USA"
    }, {
      "balloonText": "[[title]]:[[value]]",
      "fillAlphas": 0.8,
      "lineAlpha": 0.2,
      "title": "India",
      "type": "column",
      "valueField": "India"
    }, {
      "balloonText": "[[title]]:[[value]]",
      "fillAlphas": 0.8,
      "lineAlpha": 0.2,
      "title": "Spain",
      "type": "column",
      "valueField": "Spain"
    }, {
      "balloonText": "[[title]]:[[value]]",
      "fillAlphas": 0.8,
      "lineAlpha": 0.2,
      "title": "Poland",
      "type": "column",
      "valueField": "Poland"
    } ]
    

    3) 在值轴上启用堆叠,使用stackType

    "valueAxes": [ {
      "id": "ValueAxis-1",
      "position": "left",
      "axisAlpha": 0,
      "stackType": "regular"
    } ]
    

    对于数据,由于您已经有一个处理源数据的函数,您可以修改它创建分组数据点,如上所述。

    function generateChartData() {
      var chartData = [],
        categories = {};
      for ( var i = 0; i < $scope.datalength; i++ ) {
        var newdate = $scope.data[ i ].Date;
        var visits = $scope.data[ i ].visits;
        var country = $scope.data[ i ].country;
    
        // add new data point
        if ( categories[ newdate ] === undefined ) {
          categories[ newdate ] = {
            date: newdate
          };
          chartData.push( categories[ newdate ] );
        }
    
        // add value to existing data point
        categories[ newdate ][ country ] = visits;
      }
      return chartData;
    }
    

    【讨论】:

    • 非常感谢你。我的分组条形图是通过您提供的代码实现的预期输出。
    【解决方案2】:

    我找到了一个带有最新 amcharts 的示例,我想与您分享:

    https://www.amcharts.com/demos/clustered-column-chart/

    它正在使用 amcharts4 和 XYChart。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多