canvasJS 通过直接传递一个对象来插入新的数据点

canvasJS Insert new datapoint by directly passing an object

所以,

我正在获取一个对象,其中包含我想要以这种形式(通过 Json)以如下形式绘制的数据:

chartData(GraphData.year, GraphData.month, GraphData.day, GraphData.hour, GraphData.minute, GraphData.y, GraphData.indexLabel, GraphData.markerColor, GraphData.markerType))

基本上我有:

chartData{year:2017, month:11, day:2, hour:10, y: 21 , indexLabel: "Pump 1 On", markerColor: "green", markerType: "triangle" }

但是 canvasJS 需要它们像:

chartData{ x: new Date(1987,03,25, 08, 39), y: 658 , indexLabel: "Pump 1 On", markerColor: "green", markerType: "triangle" }

如何将我的对象转换为所需的形式? 简而言之,chartData class 的构造函数应该是什么样子? 我还想在数组中添加多个 theese 对象,如

dailyGraphData.push(new chartData);

(当然我会在将 chartData 对象推入 dailyGraphData 数组之前更新它)

提前TY!

你实际上是在自己回答这个问题。记住你只需要一个文本文件和一个浏览器来测试其中的一些东西。

例如,取两块你所拥有的 posted.. 这是做什么的:

dailyGraphData.push({year:2017, month:11, day:2, hour:10, y: 21 , indexLabel: "Pump 1 On", markerColor: "green", markerType: "triangle" });

如果这可行,您所需要的只是来自其他对象的数据。您如何组合这些数据?

好吧,你可以在 class 上有一个方法,或者我猜是一个独立的函数:

function mapData(graphData){
     return { year: graphData.year, month:graphData.month ...... };
}

然后合并:

dailyGraphData.push(mapData(firstFormat));

对于测试,请确保您熟悉 console.log 和 JSON.stringify 以及您的浏览器开发人员工具,以便您可以调查正在发生的事情 - 如果您知道如何使用这些工具,请尝试一些更多内容和 post 提问时代码失败。

好的,供以后参考

class chartTempData {
  constructor(year, month, day, hour, minute, levelCM, indexLabel, markerColor, markerType) {
    this.year = year;
    this.month = month;
    this.day = day;
    this.hour = hour;
    this.minute = minute;
    this.levelCM = levelCM;
    this.indexLabel = indexLabel;
    this.markerColor = markerColor;
    this.markerType = markerType;
  }
}



class chartData {
          constructor(date, y, indexLabel, markerColor, markerType) {
            this.x = date;
            this.y = y;
            this.indexLabel = indexLabel;
            this.markerColor = markerColor;
            this.markerType = markerType;
          }
        }


        var dailyGraphData = new Array();
//The following line is just for test purpuse, normaly these values are asigned by incomming JSON data
        var dailyTempGraphData = new chartTempData(2017, 11, 2, 08, 13, 25, "Pump 1 On", "green", "triangle");  //incomming data is firstly stored here element by element then stored in the regular array en mase




        function transferDataToArray()
        {
            var dailyGraphDataArrayLength=0;
            dailyGraphDataArrayLength = dailyGraphData.push(new chartData(new Date(dailyTempGraphData.year, dailyTempGraphData.month, dailyTempGraphData.day, dailyTempGraphData.hour, dailyTempGraphData.minute), dailyTempGraphData.levelCM, dailyTempGraphData.indexLabel, dailyTempGraphData.markerColor, dailyTempGraphData.markerType));
            dailyGraphDataArrayLength = dailyGraphData.push(new chartData(new Date(dailyTempGraphData.year, dailyTempGraphData.month, dailyTempGraphData.day, dailyTempGraphData.hour+2, dailyTempGraphData.minute+15), dailyTempGraphData.levelCM+14, "Hollo worls", "purple", "cross"));
            console.log(dailyGraphDataArrayLength);
            console.log(dailyGraphData[0]);
            console.log(dailyTempGraphData);
            chart.render();

        }