I'm trying to use Chart.js (v3.4.1) to make a simple line chart. My data is an array of objects, where each one has both x and y properties, each of which is an integer. For example: [{x: 12, y: 122}]
My minimal proof of concept is as follows:
<!DOCTYPE html>
<html>
<head>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.4.1/chart.min.js"
integrity="sha512-5vwN8yor2fFT9pgPS9p9R7AszYaNn0LkQElTXIsZFCL7ucT8zDCAqlQXDdaqgA1mZP47hdvztBMsIoFxq/FyyQ=="
crossorigin="anonymous"
referrerpolicy="no-referrer">
</script>
</head>
<body>
<canvas id="myCanvas"></canvas>
</body>
<script type="text/javascript">
new Chart('myCanvas', {
type: 'line',
data: {
datasets: [{
data: [
{x: 1, y: 2},
{x: 5, y: 6}
]
}]
}
});
</script>
</html>
Here is Chart.JS's own documentation on doing this: https://www.chartjs.org/docs/latest/general/data-structures.html#object
Alas, while the y values are respected, the x values appear to be completely ignored. When hovering over a point, "0" is displayed as the point's label.
Temporary Workaround
This leaves a sour taste in my mouth, but does work:
const myData = [
{x: 1, y: 2},
{x: 5, y: 6}
];
new Chart('myCanvas', {
type: 'line',
data: {
labels: myData.map(row=>row.x),
datasets: [{
data: myData
}]
}
});