score:30
In ChartJS, label is a Category Cartesian Axis. Since you mentioned linear interpolation in your code, I assume the strings like 2017-01-06 18:39:30
are not categories, they represent the numeric values of the x-axis. So we need to inform ChartJS that the strings in the x axis are actually time. We do this in the scale options.
var s1 = {
label: 's1',
borderColor: 'blue',
data: [
{ x: '2017-01-06 18:39:30', y: 100 },
{ x: '2017-01-07 18:39:28', y: 101 },
]
};
var s2 = {
label: 's2',
borderColor: 'red',
data: [
{ x: '2017-01-07 18:00:00', y: 90 },
{ x: '2017-01-08 18:00:00', y: 105 },
]
};
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'line',
data: { datasets: [s1, s2] },
options: {
scales: {
xAxes: [{
type: 'time'
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
<canvas id="myChart"></canvas>
You can find more information in Chart.js documentation.
score:4
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);
<canvas id="graph" height="500" width="700"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.bundle.min.js"></script>
Source: stackoverflow.com
Related Query
- how to plot multiple time series in chartjs where each time series has different times
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- How can I load multiple Chartjs charts with different data on the same page?
- how can i use chart.js to create a chart that has one time series line and one linear line on it at the same time?
- How to create multiple y-axis time series chart
- How to create a scatter plot where x-axis represents a day by hours with datetime object? chartJS
- How to display multiple graphs in real time with chartjs
- How to zoom Y axis on time series or X-Y chartjs graph
- how to add color to each data in chartjs scatter plot
- chartjs - no smooth rendering on time series plot
- Different color for each bar in a bar chart; ChartJS
- plot a bar chart.js time series
- Chartjs time plot xAxis shows year 1970
- How can I create a time series line graph in chart.js?
- How to set a full length background color for each bar in chartjs bar
- How to add ChartJS code in Html2Pdf to view image
- Why is borderColor function in chartjs running multiple times, and how to reduce it?
- Chartjs with multiple y axes and different scales
- chartjs custom y axis values, different text for each one
- how to set color for each item in tooltip with ChartJS
- chartjs - how to set the order in which the different charts are displayed
- ChartJS / Chartjs-plugin-annotation How to draw multiple vertical lines using an array?
- How to draw multiple line on y axis for same x axis in chartjs v2?
- ChartJS 2 lines each with different fill gradients
- How to combine lines with different starting points in chartjs
- How to apply to different bground color for each area in Chart.js
- How to plot chart from external JSON and format X-AXIS to show time using Chart.JS?
- ChartJS tooltip when having multiple lines on a time graph
- How to plot Firebase data with Chartjs in angular
- How to reuse a Chartjs Chart component in with different Data and get past the **Canvas is already in use** error?
More Query from same tag
- ChartJS TypeError: document.getElementById(...).getContext is not a function
- chart.js doughnut legend in the chart
- How do I make the y-axis intersect the x-axis at 0 in chart.js
- how to create a bar chart with php data variables
- Chartjs bar order adjustment after bar chart is drawn
- Chart js background color changes on click
- space before and after data points in chart.js
- chart.js: Place tooltip for stacked bar charts on top of bar
- Angular 5, Chart.js displaying multiple charts, one on each tab
- Blazor Chart.js 2.0.2 raises onClick error
- Chart JS not re rendering in Laravel Livewire
- Chart.js is not rendered until zoom in in angular 8
- chartjs 2.7 how to add labels in center of horizontal bar graph
- ng2-Chart not working with newly created angular 10 project
- How to position yAxes labels in chartJS
- Keep two Pie Charts Side by Side
- How to remove the x-axis showing up for the bottom?
- Django 1.11 - child template not inheriting from parent
- How can I build some chart with Chart.js and ng2-charts using an array of revenus & budgets per month date as Data source?
- How to create two x-axes label using chart.js
- Chart.js not rendering unless i set a timeout
- Trouble setting options for radar chart on Quickchart.io
- Charts.js - onComplete javascript function coming from JSON
- How to add an event when clicking a bubble in bubble chart?
- Conditional in ChartJS axes tick callback function isn't returning the expected labels
- ChartJS 2.6 - Change color of bar in Bar chart programmatically
- Old data flickering after hover on line graph Chart.js
- How do I force Chart.js axes min and max with react-chartjs-2?
- Chart.js vertically symmetrical chart
- how to add second axes to chart JS