2014-10-31 52 views
0

当我谈到JavaScript时,我仍然非常湿润。我需要一些帮助,将高收益的辅助轴添加到高收益图表中,该高图表也使用$ .getJSON(JSONP)来截断json文件以填充数据。如何将辅助轴添加到highstock图表中?

查看JSFiddle示例here。这里是示例数据集with。最后,在Highcharts的例子secondary axis

$(function() { 
    var seriesOptions = [], 
     seriesCounter = 0, 
     names = ['MSFT', 'AAPL', 'GOOG'], 
     // create the chart when all data is loaded 
     createChart = function() { 

      $('#container').highcharts('StockChart', { 

       rangeSelector: { 
        selected: 4 
       }, 

       yAxis: { 
        labels: { 
         formatter: function() { 
          return (this.value > 0 ? ' + ' : '') + this.value + '%'; 
         } 
        }, 
        plotLines: [{ 
         value: 0, 
         width: 2, 
         color: 'silver' 
        }] 
       }, 

       yAxis: { 
       floor: 0 
      }, 

       plotOptions: { 
        series: { 
         compare: 'value' 
        } 
       }, 

       tooltip: { 
        pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>', 
        valueDecimals: 2 
       }, 

       series: seriesOptions 
      }); 
     }; 

    $.each(names, function (i, name) { 

     $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=' + name.toLowerCase() + '-c.json&callback=?', function (data) { 

      seriesOptions[i] = { 
       name: name, 
       data: data 
      }; 

      // As we're loading the data asynchronously, we don't know what order it will arrive. So 
      // we keep a counter and create the chart when all the data is loaded. 
      seriesCounter += 1; 

      if (seriesCounter === names.length) { 
       createChart(); 
      } 
     }); 
    }); 
}); 

任何帮助和解释(所以我可以学习)非常感谢。我仍然试图学习如何将jsonp和图表绑定在一起,特别是多个数据和json文件系列。

+1

次Y轴?如果是这样,你只需要使用一个数组。例如:'yAxis:[{.. axis1 properties ..},{.. axis 2 properties ..}]'。只要注意,只要没有连接到轴的系列,它就不会显示标签。只需将属性“yAxis:n”添加到您的系列中,其中'n'是轴键(第一个是0,第二个是1 ...) – MorKadosh 2014-10-31 13:54:00

+0

因此,我需要添加一个新的轴标签并添加系列到理想的标签。我之前尝试添加新标签,但没有添加任何系列,因此没有显示任何内容。我想这就是为什么。 – Fastidious 2014-10-31 14:08:41

+0

由于@MorKadosh说你需要有一个轴对象的数组,然后在这个系列对象中,请参阅particualr yAxis。 – 2014-10-31 14:20:42

回答

1

写这样的:

yAxis: [{ 
    labels: { ... }, 
    title: { ... }, 
    ... 
}, 
{ 
    labels: { ... }, 
    title: { ... }, 
    ... 
}] 

http://jsfiddle.net/5eem7a2a/1/

+0

谢谢Raein Hashemi。这正是我需要的。顺便说一句,我编辑你的例子来显示X轴上的时间。 http://jsfiddle.net/pw8v93hj/ – ssw 2016-05-07 21:59:49