【问题标题】:Nested json in Highcharts StackedHighcharts Stacked中的嵌套json
【发布时间】:2016-10-07 10:37:42
【问题描述】:

我在 Google 上搜索了有关 Highcharts stacked percentage column 的嵌套 json 的分配信息,但还是要把正确的拿出来。

这是我尝试过的代码,但得到了正确的输出。

谁能帮我解决我在做什么错误?

提前致谢。

$(function () {
  var processed_json = new Array();
  $.getJSON('javascripts/data.json', function(data) {
    // Populate series
    for (i = 0; i < data.length; i++){
      processed_json.push([data[i].key, data[i].value]);
    }
    // draw chart
    $('#container').highcharts({
      chart: {
        type: "bar"
      },
      title: {
        text: "Student data"
      },
      xAxis: {
        type: 'category',
        allowDecimals: false,
        title: {
          text: ""
        }
      },
      yAxis: {
        title: {
          text: "Scores"
        }
      },
      plotOptions: {
        column: {
          stacking: 'normal'
        }
      },
      series: [{
        name: 'Subjects',
        data: processed_json,
      }]
    });
  });
});

// this is how am displaying in data.json

[
  {"key":"john","value":[34,53,45,45,98]},
  {"key":"Rita","value":[98,34,43,54,66,66]},
  {"key":"james","value":[91,33,45,65,65,38]},
  {"key":"jade","value":[98,54,54,45,45,45]},
  {"key":"lara","value":[23,23,98,23,23,23]} 
]
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container" style="height: 400px"></div>

【问题讨论】:

  • 在将输出 (processed_json) 添加到图表之前,它的外观如何?您的 y 值中有数字或字符串吗?您可以尝试将您的值解析为 Int,然后再将它们添加到您的数组中
  • 感谢@GrzegorzBlachliński 的建议,我的背景是 CSS/HTML,不知道如何传递数组

标签: javascript json highcharts


【解决方案1】:

您的变量 processed_json 的格式不是 Highcharts API 可以处理的。

看看这里:http://jsfiddle.net/jo8foxtz/

代码如下:

$(function() {

  var processed_json = new Array();
  
  data = [{
    "key": "john",
    "value": [34, 53, 45, 45, 98]
  }, {
    "key": "Rita",
    "value": [98, 34, 43, 54, 66, 66]
  }, {
    "key": "james",
    "value": [91, 33, 45, 65, 65, 38]
  }, {
    "key": "jade",
    "value": [98, 54, 54, 45, 45, 45]
  }, {
    "key": "lara",
    "value": [23, 23, 98, 23, 23, 23]
  }];

  for (i = 0; i < data.length; i++) {
    var item = {};
    item["name"] = data[i].key;
    item["data"] = data[i].value;
    processed_json.push(item);
  }


  $('#container').highcharts({
    chart: {
      type: 'bar'
    },
    title: {
      text: 'Student data'
    },
    xAxis: {
      type: 'category',
      allowDecimals: false,
      title: {
        text: ""
      }
    },
    yAxis: {
      min: 0,
      title: {
        text: 'Scores'
      }
    },
    tooltip: {
      pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.percentage:.0f}%)<br/>',
      shared: true
    },
    plotOptions: {
      column: {
        stacking: 'percent'
      }
    },
    series: processed_json
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

【讨论】:

  • 谢谢 Dhruvan Ganesh,但我想从外部调用 json 文件,你能把应该在 htmljson 中的唯一代码发给我吗? @CSKAdmin
  • 我已经指出了您偏离 API 文档的地方,我相信这些文档可以回答您的问题。如果您想通过外部 API 访问 JSON,请尝试 JSONP 或 CORS。如果您想要内部托管的文档,请尝试使用 jQuery.getJSON。如果您希望 HTML 中的代码使用
  • 你能提供任何链接吗? @dhruvan Ganesh
  • 当然:This Sitepoint Page 将为您提供 JSONP 或 CORS 为何是一个好主意的基本解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-28
  • 1970-01-01
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
相关资源
最近更新 更多