为复杂数据amcharts创建串行条形图

时间:2016-07-13 11:48:28

标签: javascript jquery amcharts

以下是我从服务器获取的json响应:

[{
  "data1": {
    "name": "Test1",
    "count": 0,
    "amount": 0,
    "amtData": [
      0,0,0,0
    ],
    "cntData": [
      0,0,0,0
    ],
    "color": "#FF0F00"
  },
  "data2": {
    "name": "Test2",
    "count": 1,
    "amount": 4164,
    "amtData": [
      4164,0,0,0
    ],
    "cntData": [
      1,0,0,0
    ],
    "color": "#FF0F00"
  },
  "data3": {
    "name": "Test3",
    "count": 1,
    "amount": 2509,
    "amtData": [
      2509,0,0,0
    ],
    "cntData": [
      1,0,0,0
    ],
    "color": "#FF0F00"
  },
  "data4": {
    "name": "Test4",
    "count": 1,
    "amount": 9909,
    "amtData": [
      9909,0,0,0
    ],
    "cntData": [
      1,0,0,0        
    ],
    "color": "#FF0F00"
  },
  "data5": {
    "name": "Test5",
    "count": 0,
    "dollars": 0,
    "amount": [
      0,0,0,0
    ],
    "cntData": [
      0,0,0,0
    ],
    "color": "#FF0F00"
  }
}]

我正在尝试创建我的chart

var chart = AmCharts.makeChart("chartdiv", {
  "theme": "light",
  "type": "serial",
  "dataProvider": data, //assigning it as data
  "startDuration": 1,
  "graphs": [{
    "balloonText": "<b>[[category]]</b><br>starts at [[count]]<br>ends at [[amount]]",
    "colorField": "color",
    "fillAlphas": 0.8,
    "lineAlpha": 0,
    "openField": "count", //base start field
    "type": "column",
    "valueField": "amount" //value field
  }],
  "rotate": true,
  "columnWidth": 0.8,
  "categoryField": "name", //name field
  "categoryAxis": {
    "gridPosition": "start",
    "axisAlpha": 0,
    "gridAlpha": 0,
    "position": "left"
  },
  "export": {
    "enabled": true
  }
});

我尝试了多少,我得到了未定义而不是chart ..我可以使用console errors。如何重组这些复杂数据并从中生成图表?

以下是 fiddle ,这只是我所指的 sample fiddle

1 个答案:

答案 0 :(得分:1)

您应该从data1删除data5dataProvider个媒体资源:

var data = [
  {
    "name": "Test1",
    "count": 0,
    "amount": 0,
    "amtData": [
      0,
      0,
      0,
      0
    ],
    "cntData": [
      0,
      0,
      0,
      0
    ],
    "color": "#FF0F00"
  },
  ...
];

here