【问题标题】:How to pass multiple Highcharts parameters in JSON array如何在 JSON 数组中传递多个 Highcharts 参数
【发布时间】:2014-12-19 09:09:03
【问题描述】:

我想通过 JSON 数组将一些图形参数传递给图形。其中,它应该是图表的标题、单位、……以及数据。

一旦我开始尝试将 JSON 数组从一个简单的“数据”数组转换为可以容纳更多信息的数组,它就不再起作用了。我想这与我使用的那种括号有关。对哪些是正确的感到困惑。

我把它放在into a fiddle

$(function () {
var options = {
    chart: {
        renderTo: 'container',
        type: 'spline',
        marginBottom: 50
    },
    xAxis: {
    },
    title: 
    {
        text: "Title",
        align: "center",
    },
    plotOptions:
    {
        series:
        {
            marker:
            {
                enabled: false
            }
        }
    },
    series: [{}]
};



/* This works 
data = [
          {
              "name": "France",
               "data": [[2006,2189260],[2007,2239300],[2008,2237490],[2009,2167070],[2010,2204450]]
          }
       ];
*/



/* This doesn't */
data = [
            {
                 "series":
                  [{
                      "name": "France",
                       "data": [[2006,2189260],[2007,2239300],[2008,2237490],[2009,2167070],[2010,2204450]]
                  }]
            }
       ];


/* load the stuff in the JSON like this= */
options.series  = data["series"];

var chart = new Highcharts.Chart(options);

});

非常感谢任何提示我做错了什么。

【问题讨论】:

    标签: arrays json highcharts


    【解决方案1】:

    数据对象是数组,所以你需要先引用第一个元素,然后再引用对象。

    options.series  = data[0].series;
    

    示例:http://jsfiddle.net/jd41gz1q/6/

    【讨论】:

    • 啊,好的。谢谢。但是,如果我想在稍后阶段向它添加“title”和“xAxis-title”,那将如何工作。我仍在与这个(我猜,相当微不足道的)事情作斗争。在我的 [example fiddle] (jsfiddle.net/luftikus143/jd41gz1q/9) 中,它部分有效;但它看起来相当错误。这样做的正确方法是什么?非常感谢您的帮助!
    • 但是,这种方法一般是否值得推荐?如果 JSON 中元素的顺序发生变化,代码将不再起作用 - 我肯定会添加新元素,例如版权、xaxis、...:options.title = data[0].title; options.xAxis = data[1].xAxis; options.series = data[2].series; 难道没有更通用的方法来通过 JSON 传递这些参数并读出它们?您对我有什么提示/建议吗?
    • 好的。 Does work:data = {"title": {"text": "Here goes the title"},"xAxis": {"title": {"text": "Here go the units"}},"series": [{"name": "France","data": [[2006,2189260],[2007,2239300],[2008,2237490],[2009,2167070],[2010,2204450]]}]};options.series = data["series"];options.title = data["title"];options.xAxis = data["xAxis"];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 2013-11-21
    • 2012-06-12
    • 1970-01-01
    • 2019-03-21
    相关资源
    最近更新 更多