如何在amCharts中创建分组栏?

时间:2016-01-04 11:34:38

标签: angularjs amcharts

我正在使用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;
}

现有产出如下: enter image description here

预期产量为: enter image description here

任何人都可以帮助我达到预期的产量。

提前致谢。

2 个答案:

答案 0 :(得分:3)

您需要做几件事来达到目标​​:

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;
}

答案 1 :(得分:0)

我找到了一个最新的amchars实例,想与您分享:

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

它正在使用amcharts4和XYChart。

enter image description here