how to plot multiple time series in chartjs where each time series has different times

后端 未结 2 1432
名媛妹妹
名媛妹妹 2021-02-13 03:43

I have two time series for example:

s1:
  2017-01-06 18:39:30    100
  2017-01-07 18:39:28    101

and

s2:
2017-01-07 18:00:00           


        
2条回答
  •  感情败类
    2021-02-13 04:44

    You can have data of the form [{x:"value", y:"value"}] when your graph is of type scatter.

    So to make your graph work, do this.

    var canvas = document.getElementById("graph");
    var s1 = [{x:"2017-01-06 18:39:30",y:"100"},{x:"2017-01-07 18:39:28",y:"101"}];
    var s2 = [{x:"2017-01-07 18:00:00",y:"90"},{x:"2017-01-08 18:00:00",y:"105"}];
    
    var graphParams = {
    	type:"scatter",
    	data:{
    		datasets: [{
    			label:"Series 1",
    			data:s1,
    			borderColor:"red",
    			backgroundColor:"transparent",
    		},
    		{
    			label:"Series 2",
    			data:s2,
    			borderColor:"blue",
    			backgroundColor:"transparent",
    		}],
    	},
    	options:{
    		maintainAspectRatio:false,
    		responsive:false,	
    		scales:{
    			xAxes:[{
    				type:"time",
    				distribution: "series",
    			}],
    		}
    	}
    
    }
    ctx = new Chart(canvas, graphParams);
    
    

提交回复
热议问题