简体   繁体   English

将动态数据传递给 Highcharts 系列数据元素

[英]Passing dynamic data to Highcharts series data element

I am currently trying to pass data from my back-end (written in c#) to my front-end to be used within the JavaScript to populate a highcharts x-range graph.我目前正在尝试将数据从我的后端(用 c# 编写)传递到我的前端,以便在 JavaScript 中使用来填充高图表 x 范围图。 The data comes through and is filtered as and when needed correctly but passing the data to the highcharts series data element is proving difficult.数据通过并在需要时正确过滤,但将数据传递到 highcharts 系列数据元素被证明是困难的。 When giving the element the raw data copied from the source, it works fine, however when passing the data dynamically it does not, for example:当给元素从源复制的原始数据时,它可以正常工作,但是当动态传递数据时它不会,例如:

series: [{
  pointWidth: 20,
  turboThreshold: 10000,
  data: JSON.parse(JSON.stringify("[" + dataString + "]"))
}]

the above is what I am tring to do, the dataString is comprised as follows:以上是我想做的,dataString的组成如下:

dataString += "{x: " + startDate + ", x2: " + endDate + ", y: " + vesselCounter + "},"

where each variable is passed through from a row in the datatable in the back-end.其中每个变量都是从后端数据表中的一行传递过来的。 Running the code like this returns an empty chart, however if I run it as follows像这样运行代码会返回一个空图表,但是如果我按如下方式运行它

series: [{
  pointWidth: 20,
  turboThreshold: 10000,
  data: [{x: 1244419200000, x2: 1258329600000, y:0}]
}]

the data displays, I'm not sure if it's not possible to pass the data as I am trying but any help would be great!数据显示,我不确定是否无法在我尝试时传递数据,但任何帮助都会很棒! To clarify the data comes through perfectly and is as it should be but the series -> data element does not like it.澄清数据是完美的,应该是这样,但系列->数据元素不喜欢它。

Docs for reference: https://api.highcharts.com/highcharts/series.xrange.data参考文档: https ://api.highcharts.com/highcharts/series.xrange.data

For solution.为解决。

  1. You remove "," in result string.您删除结果字符串中的“,”。

在此处输入图像描述

  1. add quote in each variable.在每个变量中添加引号。

在此处输入图像描述

This is correct code and you can check in http://jsfiddle.net/1zuwr2vo/16/ In jsfiddle.net, you can see running site.这是正确的代码,您可以查看http://jsfiddle.net/1zuwr2vo/16/在 jsfiddle.net 中,您可以看到正在运行的站点。

const data = [{
  startDate: 12312353,
  endDate: 13312353,
  vesselCounter: 834
}, {
  startDate: 14312353,
  endDate: 15312353,
  vesselCounter: 834
}, {
  startDate: 16312353,
  endDate: 17312353,
  vesselCounter: 834
}];

let dataString = '';

data.forEach(({
  startDate,
  endDate,
  vesselCounter
}) => {
  dataString += `{"x": ${startDate}, "x2": ${endDate}, "y": ${vesselCounter}},`
});

let result_data = `[${dataString.slice(0, -1)}]`;

console.log(result_data);

Highcharts.chart('container', {
  series: [{
    type: 'xrange',
    data: JSON.parse(result_data)
  }]
});

Managed to solve this by passing the series data as an object设法通过将系列数据作为对象传递来解决这个问题

function dateObject(_x, _x2, _y) {
  this.x = _x
  this.x2 = _x2
  this.y = _y
}

thank you everyone for helping and if anyone has questions on this feel free to ask :)谢谢大家的帮助,如果有人对此有任何疑问,请随时提问:)

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

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