Amcharts:如何使用带有因子的csv文件?

时间:2015-12-02 19:03:17

标签: csv amcharts

是否可以根据因子列使用绘制几行?我会解释一下。我有下一个csv文件:

year,lessThanWorkingAge,withinWorkingAge,moreThanWorkingAge,sex
2004,203,4928,32143,female
2005,195,4728,32005,female
2006,155,4211,31578,female
2007,150,3997,30526,female
2008,151,3800,30930,female
2009,180,3544,30614,female
2010,194,3411,31236,female
2011,171,3294,29193,female
2012,181,3243,29913,female
2013,187,2992,29505,female
2004,298,17634,19317,male
2005,252,17070,19120,male
2006,223,15264,18594,male
2007,179,14023,18591,male
2008,206,13153,18469,male
2009,230,11998,18341,male
2010,238,11449,18867,male
2011,221,11043,17810,male
2012,242,10485,17961,male
2013,232,9879,17509,male

如何为femalemale usnig sex列绘制两行作为因子,year作为x轴类别,lessThanWorkingAge作为y轴值?

1 个答案:

答案 0 :(得分:0)

通常情况下,amCharts不支持这样的数据。

但是,如果您使用Data Loader将CSV数据加载到图表,则可以使用其postProcess处理程序自动重新格式化数据,以便将相同类别的值合并到同一数据点,所以它确实有效。

这样的事情:

"dataLoader": {
  "url": "data.csv",
  "format": "csv",
  "useColumnNames": true,
  "postProcess": function(data) {
    var tmp = {};
    var newData = [];
    for (var i = 0; i < data.length; i++) {
      var row = data[i];
      if (tmp[row.year] === undefined)
        tmp[row.year] = {
          "year": row.year
        };
      tmp[row.year][row.sex] = row.lessThanWorkingAge;
    }
    for (var x in tmp) {
      if (tmp.hasOwnProperty(x))
        newData.push(tmp[x]);
    }
    return newData;
  }
}

这是使用数据加载器加载数据的图表working example