score:11
Accepted answer
you cannot just pass the result
array to data
, as it isn't in correct format. data
property expects an array of numbers. so, you need to parse the labels (using moment.js) and data from result
array before using them for the chart.
here is how labels and data should be parsed from the result
array and fed to the chart :
var result = [{ x: "18:00", y: "230" }, { x: "19:00", y: "232" }, { x: "20:00", y: "236" }, { x: "22:00", y: "228" }];
// parse labels and data
var labels = result.map(e => moment(e.x, 'hh:mm'));
var data = result.map(e => +e.y);
var ctx = document.getelementbyid("mychart").getcontext('2d');
var mychart = new chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [{
label: 'voltage fluctuation',
data: data,
borderwidth: 1
}]
},
options: {
scales: {
xaxes: [{
type: 'time',
time: {
unit: 'hour',
displayformats: {
hour: 'hh:mm'
}
}
}]
},
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.6.0/chart.min.js"></script>
<canvas id="mychart"></canvas>
Source: stackoverflow.com
Related Query
- How to feed hour and minute data into chartJS
- How to properly feed data to ChartJS with different number of x(labels) and y(data) points
- How to start the chart from specific time and offest hour and then show the data on chart from target datetime in chartjs
- How do I fetch data from 2 tables and display into 1 chartjs
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How to get the database data into ChartJS using codeigniter
- How to add new data point and remove leftmost data point dynamically in Chartjs
- How to reuse a Chartjs Chart component in with different Data and get past the **Canvas is already in use** error?
- How to set min max for x axis depend on data with Chartjs and Spring Boot?
- How to sum the array value in javascript like chartjs data array and input value sum
- How can I pass the data into the chartjs label?
- how to insert dynamic data from sql into chartjs stacked bar chart javascript
- How to extract ChartJs data and config properties
- How to implement doughnut ChartJS with CodeIgniter 3 and display data from database
- How to inject data into chartjs plugin function?
- How do I add JSON Data into ChartJS
- How to display data from database into line graph using php and mysql?
- Chartjs v2 - format tooltip for multiple data sets (bar and line)
- how to change background in chartjs and remove background lines?
- How to change the color of legend in chartjs and be able to add one more legend?
- React ChartJS prevent new data from being added into state after it's redrawn?
- How to remove gridlines and grid labels in Chartjs Radar?
- How to show data values or index labels in ChartJs (Latest Version)
- How can I display the xAxes and yAxes data in the tooltip, Chart JS?
- How to get onClick Event for a Label in ChartJS and React?
- How to show data values in top of bar chart and line chart in chart.js 3
- Chartjs - Insert additional data into chart tooltip
- ChartJs php array into 'labels' and 'datasets'
- How to add ChartJS code in Html2Pdf to view image
- Chartjs in Reactjs - how can I update data dynamically?
More Query from same tag
- Use chartjs-plugin-annotation 0.5.7 in Chartjs 2.9.x with React
- Merge Jquery Knob Functionality in Chart Js
- Chart.js won't display percentage label when value is above 1000?
- How to get any y value for specified x in chart.js?
- ChartJS and JSON result: Cannot set property 'fillColor' of undefined
- How to show Charts.js lables
- Why I don't see the second plot?
- Zoom Function For Chart.js
- how can conditionally show/hide gridlines on the yAxis using Chart.js?
- Chartjs with Node.js : Unexpected token <
- ChartJS Email HTTP Request API
- How can I apply hover properties only to the current point not the entire dataset
- chartjs-plugin-streaming + chartjs-plugin-zoom
- Chart.js 3.x not able to display data on chart
- Why ChartJS's Bar Chart does not render bar for a specific value?
- Chartjs: dynamic data (array) not working
- Chart in other module
- Can't get bar chart colors in Chart js working in React Js
- Horizontal stacked angular bar charts
- Why can't I use a variable in same function scope in Javascript?
- Chartjs display label & units when mouse is hover stats
- Chartjs - Two Y-axis scale problems
- use chart.js on a page that was loaded by ajax
- Negative bar chart of highcharts in chartjs
- Making sure gridlines do not cross y-axis with ChartJS
- Unable to update state in ReactJS
- Chart.js labels of x-axes makes useless space in chartarea
- Dynamic maximum y-axis value on Chart.js
- How to increase size and family in a radar's Chartjs label
- Chartjs + jsPDF = Blurry image