简体   繁体   English

如何将两个数组相乘并动态绘制成HighCharts?

[英]How to Multiply two arrays and dynamically plot into HighCharts?

new to javascript. JavaScript新手。 I am trying to make a new series in Highcharts using one already graphed series multiplied by another already graphed series. 我正在尝试使用一个已经绘制的序列乘以另一个已经绘制的序列在Highcharts中创建一个新序列。 How do I add the new series with its own y-axis? 如何添加具有自己的y轴的新系列? I am using some base code that I have not done myself. 我正在使用一些自己未完成的基本代码。

ChartOptions.Series[index].data holds an array of arrays: so for example :[1563480488000, 12.144] would be timestamp and datavalue for every data point in the series. ChartOptions.Series[index].data保存一个数组数组:因此,例如:[1563480488000,12.144]将是该系列中每个数据点的时间戳和数据值。

So I would want something like this: [1563480488000, 12.144] * [1563480488000, 1.0] = [1563480488000, 12.144] 所以我想要这样的事情: [1563480488000, 12.144] * [1563480488000, 1.0] = [1563480488000, 12.144]

that's 12.144*1.0, and the timestamp is kept. 那是12.144 * 1.0,并保留时间戳。

dynamicChart is a new Highcharts.StockChart(chartOptions) its just away from the snippet I am working with. dynamicChart是一个new Highcharts.StockChart(chartOptions)它与我正在使用的代码段不一样。 I've tried the following code. 我已经尝试了以下代码。

 var voltSeries = chartOptions.series[0].data;
 var ampSeries = chartOptions.series[1].data;
 var wattSeries = [];
 for(var i = 0; i <= voltSeries.length; i++){
    var valu = chartOptions.series[1].data[i,1]*chartOptions.series[0].data[i,1];
    wattSeries[i,1] = valu;
    wattSeries[i,0] = valu;

}
  eval('window.console && console.log(voltSeries)');
  eval('window.console && console.log(wattSeries)');
   dynamicChart.addSeries({
       name: 'Watts',
       data: wattSeries 
    });
dynamicChart.redraw();    

the output of wattSeries is an Array of (2) [NaN, NaN] but It should be the timestamp and the multiplied value. wattSeries的输出是(2)[NaN,NaN]的数组,但是它应该是时间戳和乘数值。 and It seems like I cannot graph it no matter what I do. 而且无论我做什么,似乎我都无法绘制图形。

The problem is incorrectly getting values from the array. 问题是从数组中错误地获取值。 For nested arrays you should use several parentheses: array[x][x] 对于嵌套数组,应使用多个括号: array[x][x]

var dynamicChart = Highcharts.chart('container', chartOptions);

var voltSeries = chartOptions.series[0].data,
    ampSeries = chartOptions.series[1].data,
    wattSeries = [],
    valu;

for (var i = 0; i < voltSeries.length; i++) {
    valu = voltSeries[i][1] * ampSeries[i][1];

    wattSeries.push([ampSeries[i][0], valu]);
}

dynamicChart.addSeries({
    name: 'Watts',
    data: wattSeries,
    yAxis: 1
});

Live demo: http://jsfiddle.net/BlackLabel/vpzLou6r/ 现场演示: http //jsfiddle.net/BlackLabel/vpzLou6r/

API Reference: https://api.highcharts.com/class-reference/Highcharts.Chart#addSeries API参考: https //api.highcharts.com/class-reference/Highcharts.Chart#addSeries

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM