Highcharts将类别数据csv分组

时间:2016-05-19 10:00:36

标签: javascript arrays xml csv highcharts

大家好,我有这个示例条形图:

如您所见,它已将类别分组

现在我希望能够通过csv或xml导入数据。 我遇到了highcharts的数据模块,但这不适用于我认为的分组类别。

到目前为止,我所取得的成就是:

HTML Head:

<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="highcharts.js"></script>
<script type="text/javascript" src="exporting.js"></script>
<script type="text/javascript" src="grouped-categories.js"></script>
<script type="text/javascript" src="data.js"></script>

<script type="text/javascript">
$.get('data.csv', function(csv) {
    $('#container').highcharts({
        chart: {
            type: 'bar'
        },
        data: {
            csv: csv,
            itemDelimiter: ';'
        },

        title: {
            text: 'Roadmap'
        },
        yAxis: {
            title: {
                    text: 'Date'
                },
            labels: {
                    style: {
                        fontSize: '12px'
                    }
            },
            type: 'datetime',
            min : Date.UTC(2015, 10, 1),
            max : Date.UTC(2021, 01, 1),
            tickInterval: 30.41667 * 24 * 3600 * 1000
        }
    });
});
</script>

这会生成此条形图: enter image description here

出于这个csv:

Category,Date
Task 1,1483138800000
Task 2,1457046000000
Task 1,1467237600000
Task 1,1483138800000

是否可以从csv或xml生成分组的类别条形图?

我在考虑这样的事情:

Project1; Phase 1; Task 1, Value
Project1; Phase 2; Task 1, Value
Project1; Phase 2; Task 2, Value
Project1; Phase 3; Task 1, Value

然后尝试获取此输出,以便highcharts可以使用它:

categories: [{
      name: "Project 1",
      categories: [{
        name: "Phase 1",
        categories: ["Task 1"]
      }, {
        name: "Phase 2",
        categories: ["Task 1"]
      }, {
        name: "Phase 3",
        categories: ["Task 1", "Tas k2"]
      }]

1 个答案:

答案 0 :(得分:2)

Grouped Categories插件需要Highcharts不直接支持的类别格式。数据模块是Highcharts官方插件,因此默认情况下或使用某些可用设置无法正常工作。解析CSV数据 - 创建系列数据数组和类别对象可能是解决问题的方法。

以下是该解析器的示例:http://jsfiddle.net/vboak5hr/

  $.get('data.csv', function(data) {
    // Split the lines
    var lines = data.split('\n');
    var series = [];

    // Iterate over the lines and add categories or series
    $.each(lines, function(lineNo, line) {
      var items = line.split(';'),
        hasThisCategory = false,
        categoryIndex = -1,
        categoryPath = options.xAxis.categories;

            //add data point to series data
      options.series[0].data.push(parseInt(items[3]));

            // I. level of categories
      Highcharts.each(categoryPath, function(category, i) {
        if (category.name === items[0]) {
          hasThisCategory = true;
          categoryIndex = i;
        }
      });
      if (!hasThisCategory) {
        categoryPath.push({
          name: items[0],
          categories: []
        });
      }
      if (categoryIndex === -1) categoryIndex = categoryPath.length - 1;

            // II. level of categories
      categoryPath = categoryPath[categoryIndex].categories;
      hasThisCategory = false;
      categoryIndex = -1;

      Highcharts.each(categoryPath, function(category, i) {
        if (category.name === items[1]) {
          hasThisCategory = true;
          categoryIndex = i;
        }
      });
      if (!hasThisCategory) {
        categoryPath.push({
          name: items[1],
          categories: []
        });
      }
      if (categoryIndex === -1) categoryIndex = categoryPath.length - 1;

            // III. level of categories
      categoryPath = categoryPath[categoryIndex].categories;
      hasThisCategory = false;
      categoryIndex = -1;

      Highcharts.each(categoryPath, function(category, i) {
        if (category === items[2]) {
          hasThisCategory = true;
        }
      });
      if (!hasThisCategory) {
        categoryPath.push(items[2]);
      }

    });

    // Create the chart
    var chart = new Highcharts.Chart(options);
  });