Search code examples
javascriptclasscanvasjs

canvasJS Insert new datapoint by directly passing an object


So,

I'm getting an object containing the data i want to graph in this form(by Json) in the following form:

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

So basicly i have:

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

but canvasJS needs them to be like:

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

How can i transform my object to the required form? In short what the constructor of chartData class should look like? Also i want to want to add multiple of theese object in an array like

dailyGraphData.push(new chartData);

(Of course im updating chartData object before pushing it into the dailyGraphData array)

TY in advance!


Solution

  • Ok, for future reference

    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();
    
            }