11👍
In short, I think your example should work. The key is to use y
for the y value in the chart, and to add your arbitrary property to the object, as well.
It looks like this is what you’re doing, but you’ve reported it’s not working… It works for me, though! ?
Longer answer
According to the docs for Line, data
can be a Number[]
:
data: [20, 10]
…but it can also be a Point[]
:
data: [{
x: 10,
y: 20
}, {
x: 15,
y: 10
}]
This was intended to be used for sparsely-populated data sets. Through experimentation, I’ve found you can omit the x
property and simply specify the y
property. The x
value will be inferred, as usual.
You can also add in any other arbitrary properties that can be accessed from your label callback. So, you might end up with something like this:
data: [{
y: 20,
myProperty: "Something"
}, {
y: 10,
myProperty: "Something Else"
}]
2👍
I can confirm that answer @rinogo gave works on latests version. You can add custom properties to dataset and use ’em in tooltip or elsewhere afterwards.
First add data to dataset. On my case I only have one set, so I’m just pushing points.
chartData.datasets[0].data.push({
y: groupedBooking.distinctCount,
distinctUsers: distinctUsers
});
… and finally override callback with chartOptions:
options: {
tooltips: {
callbacks: {
label(tooltipItem, data) {
console.log(data);
return data.datasets[0].data[tooltipItem.index].distinctUsers;
}
}
}
}
If you would happen to have multiple datasets you get datasetIndex from tooltipItem as well… so to confirm, X can be omitted from datapoint and customer properties are preserved within object.
- [Chartjs]-Is it possible in chartjs to hide certain dataset legends?
- [Chartjs]-How to set max and min value for Y axis
0👍
In the data structure doc (version 3.5.1 at the time of writing this), it is documented objects can be used as data
and parsing option is also available to pick the keys.
Example from the doc:
data: {
datasets: [{
data: [{id: 'Sales', nested: {value: 1500}}, {id: 'Purchases', nested: {value: 500}}]
}]
},
options: {
parsing: {
xAxisKey: 'id',
yAxisKey: 'nested.value'
}
}