I am new using chart.js.
I have this image:
I am trying to delete the number over the bar, how can I do it? I am using Angular 9 and typescript but I need to show it only when you click on the bar.
thx
The solution for mi is that:
plugins: {
datalabels: {
...
formatter: function(value, context) {
return "";
}
}
}
Related
I have a component code like below
changeData() {
const changedData = this.getData();
this.chartData = Object.assign({}, changedData);
this.lineTooltips = this.getLineTooltips();
this.chartOptions = {
tooltips: {
enabled: true,
callbacks: {
label: function(tooltipItem, data) {
return (data.tooltips[tooltipItem.index]).split('#');
},
}
}
}
}
and also HTML file
<div class="chartStyle">
<p-chart type="line" [data]="chartData" responsive="true" [options]="chartOptions" (onDataSelect)="selectData($event)"></p-chart>
The chart can load the line and data, but I hover to each item on the chart, the callback function did not work to load tooltip.
If you log data.tooltips you will see its undefined, and since you try to read things from it it will throw an error and not draw anything.
Looking at the documentation I think you have to replace data.tooltips with data.datasets. (https://www.chartjs.org/docs/latest/configuration/tooltip.html#label-callback)
The best thing you can do is just log both tooltipItem and data and go through the objects to see how you can combine them to get to the point you need and then insert that in your callback.
I have embedded ember-cli-chart in my hbs file as
<div class="chart">
{{ember-chart type='line' data=data options=options}}
</div>
In my component file I have created an options property as
options: computed('metric', function() {
let opts = defaultOptions;
if (this.metric === 'height') {
opts.scales.yAxes = [{
ticks: {
callback: function(value, index, values) {
// code to return labels
}
}
}]
} else {
opts.scales.yAxes = [{
ticks: {
callback: function(item, index, items) {
// code to return labels
}
}
}]
}
return opts;
});
I want to display Y-Axis labels based on the current selected metric.
When first time chart loads it renders correct labels on y-Axis and if I change the metric then the same callback is getting used instead of the other one (in else part) and renders same labels but with updated data values.
Can anyone help on this?
Hmmm I don't know the addon or chart.js for the matter, but when looking at the source code for the ember-chart component, I see
didUpdateAttrs() {
this._super(...arguments);
this.updateChart();
},
updateChart() {
let chart = this.get('chart');
let data = this.get('data');
let options = this.get('options');
let animate = this.get('animate');
if (chart) {
chart.config.data = data;
chart.config.options = options;
if (animate) {
chart.update();
} else {
chart.update(0);
}
}
}
So, in order for chart.js to update, you need didUpdateAttrs to fire, which means in your case here that options itself needs to change. I don't know how you're creating defaultOptions, but assuming this reference never changes, there's no reason that didUpdateAttrs would fire since you aren't changing the reference to options (you're only changing child props of defaultOptions in the computed). I would suppose that:
import { assign } from '#ember/polyfills';
...
options: computed('metric', function() {
let opts = assign({}, defaultOptions);
if (this.metric === 'height') {
opts.scales.yAxes = [{
ticks: {
callback: function(value, index, values) {
// code to return labels
}
}
}]
} else {
opts.scales.yAxes = [{
ticks: {
callback: function(item, index, items) {
// code to return labels
}
}
}]
}
return opts;
})
would be enough to trigger the behavior you want since we always return a new object when a recomputation of options occurs.
In this wonderful sample, we know blue and red belong to stack: 'Stack 0'. There are some old questions related in SO, after a year what's the simplest way to display 'Stack 0' and 'Stack 1' inside the chart?
I found the datalabels plugin the easiest to use and quite simple to install: https://github.com/chartjs/chartjs-plugin-datalabels
A quick demo is here:
https://chartjs-plugin-datalabels.netlify.com/samples/charts/bar.html
Edit: Oh I see you want to actually just label the data with what it represents, and not the value. You may still be able to do this by providing a formatter function as an option. https://chartjs-plugin-datalabels.netlify.com/formatting . In this case, you'd override the data to just display the label. The label data might be in the context.
Like David said you can use chartjs-plugin-datalabels with a implementation like this:
plugins: {
datalabels: {
align: 'start',
anchor: 'start',
color: 'black',
formatter: function(value, context) {
let ds = context.chart.data.datasets
// check if it's the first ds
if(ds[context.datasetIndex - 1]) {
// check if the ds is in the same stack as the ds before
if(ds[context.datasetIndex - 1].stack == ds[context.datasetIndex].stack) {
return ''
} else {
return ds[context.datasetIndex].stack;
}
} else {
return ds[context.datasetIndex].stack;
}
}
}
}
I'm using chartjs with Bootstrap 4, on version 2.6.0.
Everything is working fine, except that I'm trying to replace an information when value is 0 on tooltip to not voted yet
I've tried a lot, and can't figure it out.
Graph picture
Can you help me?
There's an image attached that illustrates.
The value should still be zero inside the Graph, but I need to translate this information on the tooltip when the value is 0.
My code already has this information:
tooltips: {
custom: function(tooltip) {
if (!tooltip) return;
// disable displaying the color box;
tooltip.displayColors = false;
},
callbacks: {
title: function(tooltipItem, data) {
return this._data.labels[tooltipItem[0].index];
}
}
},
Thanks a lot.
Edit: Here is the second image after the suggested fix.
I'm not good with it but this will get it done. You might have to work it differently. But if you put how you are creating your chart then I can make it an exact match. Essentially you will want this:
callbacks: {
title: function(tooltipItem, data) {
return this._data.labels[tooltipItem[0].index];
},
label: function(tooltipItem, data){
var lab = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
return lab === 0 ? "Não Avaliado" : "Avaliação Geral: " + lab;
}
}
I am using chart.js to show a line chart. How can I hide a tooltip label for a chart.js line chart? The label in the tooltip is showing undefined so I want to hide the label (please see the screenshot)?
Perhaps there is a way to modify tooltip where I can show only the legends value in tooltip? My code is as follows:
myLine = new Chart(ctx).Line(lineChartData, {
type: 'line',
responsive: true,
scaleShowGridLines : false,
bezierCurve : false,
animationEasing: "linear",
tooltipEvents: ["mousemove", "touchstart", "touchmove"],
showTooltips: true,
scaleLineColor: "rgba(0,0,0,.8)",
});
Just set the tooltipTitleFontSize to 0 in your options.
Preview
Script
myLine = new Chart(ctx).Line(lineChartData, {
...
tooltipTitleFontSize: 0
});
I know I am late, but I guess it still has merit to add this one.
check this : https://stackoverflow.com/a/44632748/12061669
It uses a function to hide the title:
options:{
tooltips:{
callbacks:{
title: ()=>{}
}
}
}
Setting the title's font size to zero is not ideal as you will still see an (ugly) extra space on top of the tooltip, as if the title line is still there - which honestly is what you'd expect.
Instead, I used Yumin Gui's answer but had to return null for it to work:
`
tooltips: {
callbacks: {
title: () => null,
},
},
The result is exactly as in the pie charts (which does not have a title in its default tooltips).
To hide the tooltip title/label, it should be added in the options object for that chart as follows:
options: {
plugins: {
tooltip: {
callbacks: {
title : () => null // or function () { return null; }
}
}
}
}
Do refer to the documentation to understand better that it should be handled with a custom callback function, and it is not a config that can be set in options directly.
The accepted answer won't work in newer versions of chart.js, as Aman said in comments, what you can use now is this:
tooltips: { titleFontSize: 0 }
Example:
var bar_chart = document.getElementById('bar_canvas').getContext('2d');
window.myBar = new Chart(bar_chart, {
type: 'bar',
data: bar_chart_data,
options: {
tooltips: {
titleFontSize: 0,
bodyFontSize: 14,
}
}
});