Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

How to add data point labels in Bar charts

Open ghost opened this issue 12 years ago • 26 comments

Hi, I want to show the actual value of the bar above the bar using bar charts. How can I do this?

ghost avatar Mar 08 '14 21:03 ghost

YES! I want to know this also

oreporan avatar Mar 08 '14 21:03 oreporan

Hi oreporan, I modified the chart.js and added the following code in the function

function drawBars(animPc)

just update this function

//updated function function drawBars(animPc) { ctx.lineWidth = config.barStrokeWidth; for (var i = 0; i < data.datasets.length; i++) { for (var j = 0; j < data.datasets[i].data.length; j++) { var barOffset = yAxisPosX + config.barValueSpacing + valueHop * j + barWidth * i + config.barDatasetSpacing * i + config.barStrokeWidth * i; //moved these properties to inside the loop ctx.fillStyle = data.datasets[i].fillColor; ctx.strokeStyle = data.datasets[i].strokeColor; ctx.beginPath(); ctx.moveTo(barOffset, xAxisPosY); ctx.lineTo(barOffset, xAxisPosY - animPc * calculateOffset(data.datasets[i].data[j], calculatedScale, scaleHop) + (config.barStrokeWidth / 2)); ctx.lineTo(barOffset + barWidth, xAxisPosY - animPc * calculateOffset(data.datasets[i].data[j], calculatedScale, scaleHop) + (config.barStrokeWidth / 2)); ctx.lineTo(barOffset + barWidth, xAxisPosY); if (config.barShowStroke) { ctx.stroke(); } ctx.closePath(); //added code to show data labels in the bar chart //you can add more config properties to remove these hardcoded values ctx.fill(); ctx.fillStyle = '#FF3030'; ctx.font = 'bold 12pt '+config.scaleFontFamily; ctx.fillText(data.datasets[i].data[j], barOffset + (barWidth / 2), xAxisPosY - animPc * calculateOffset(data.datasets[i].data[j], calculatedScale, scaleHop) + (config.barStrokeWidth / 2) - 12); ctx.restore(); } } }

ghost avatar Mar 08 '14 21:03 ghost

THanks man

oreporan avatar Mar 08 '14 22:03 oreporan

Hey - Unfortunately its not working for me :( I updated the function and no go

oreporan avatar Mar 08 '14 23:03 oreporan

Give your Email Id, I will mail you my file... you can compare the contents

ghost avatar Mar 09 '14 05:03 ghost

Hi - this is my email. [email protected] Thank you !

On 9 במרץ 2014, at 07:18, jayantvarshney018 [email protected] wrote:

Give your Email Id, I will mail you my file... you can compare the contents

— Reply to this email directly or view it on GitHub.

oreporan avatar Mar 09 '14 07:03 oreporan

Thanks a lot! is this project abandoned? ;(

mihaicris avatar Mar 17 '14 12:03 mihaicris

There is a problem with the label alignment of the first value (bar) from the left, it is a little to the right (when j = 0) This seems to correct but I don't think is the right way. ctx.fillText( data.datasets[i].data[j], barOffset + (barWidth / 2) + (!j ? barOffset / 8 : 0 ), xAxisPosY - animPc * calculateOffset(data.datasets[i].data[j], calculatedScale, scaleHop) + (config.barStrokeWidth / 2) - 12);

mihaicris avatar Mar 17 '14 13:03 mihaicris

I forked the original repo to add this feature. check it out. https://github.com/kshyju/Chart.js

kshyju avatar Mar 17 '14 13:03 kshyju

Thanks a lot, very nice! but one issue in your fork... If I put barLabelFontSize: 8, (in default options of bar) then ctx.font = fontSize+"pt"; in function drawBarText.. doesn't update the font size of the label.

doesn't work for me...

mihaicris avatar Mar 17 '14 22:03 mihaicris

I just change a little bit to centralize the label text.

ctx.fillText(data.datasets[i].data[j], barOffset + (barWidth - ctx.measureText(data.datasets[i].data[j]).width) / 2 + (!j ? barOffset / 8 : 0), xAxisPosY - animPc * calculateOffset(data.datasets[i].data[j], calculatedScale, scaleHop) + (config.barStrokeWidth / 2) - 12);

mauricio82 avatar Jun 11 '14 15:06 mauricio82

bar Hi kshyju i downloaded Chart.js from https://github.com/kshyju/Chart.js. The data labels on bar is working fine. But the Y-axis scale is not showing the first value which i set to 0 by " scaleOverride: true, scaleSteps: 4, scaleStepWidth: Math.ceil(25), scaleStartValue: 0"

the first grid line is empty. I want to show that from 0. And also "responsive:true" is not working when i resize the browser.

ssureshs21 avatar Oct 09 '14 10:10 ssureshs21

@kshyju, I would suggest translating your changes to extend the barchart type instead of a fork of the repo. Please see here for extending http://www.chartjs.org/docs/#advanced-usage-extending-existing-chart-types

tannerlinsley avatar Dec 29 '14 20:12 tannerlinsley

Also, @kshyju, if you are looking to create completely new chart types:

We have updated the project goals today regarding new chart types -- #650

This core project will come with a specific few chart types and is designed to allow modular development of new chart types. We encourage developers to create new chart types and host them in their own repositories.

To find other developers to collaborate with please introduce yourself on https://groups.google.com/forum/#!forum/chartjs-user-discussion to see if there are more people interested in creating this chart type and start hacking!

After v1.0 is released (and it is coming soon), we will create guidelines for third-party projects implementing new chart types. Also we may invite selected chart types to become officially recognized and included beside the core project.

Thank you.

tannerlinsley avatar Dec 29 '14 21:12 tannerlinsley

My chart looks like this : var Options = { scaleFontFamily: "'SegoeUI', 'Segoe UI', 'Arial', sans-serif", scaleFontSize: 10, scaleGridLineColor: "rgba(0,0,0,.125)", responsive: true, showTooltips: true, animation: false, animationSteps: 0, scaleShowValues: true }; var ChartData = { labels: ['one', 'Two', 'Three', 'Four', 'Five', 'Six'], datasets: [ { fillColor: 'rgba(124,190,49,.667)', hightlightFill: 'rgba(124,190,49,1)',data: chartData} ],

}; var drwaChart = new Chart(ctx).Bar(ChartData , Options);

I downloaded the chart.js from https://github.com/kshyju/Chart.js

Still i am not able to display labels on my bar chart. Am i missing something here in options? Please help!!

sgampala05 avatar Feb 27 '15 21:02 sgampala05

me too...I can't display labels on chart...please help! Thanks a lot!

koko20010317 avatar Mar 09 '15 03:03 koko20010317

Thanks repository @kshyju my solicion (static) for the latest version was added:

// line +2310
ctx.font = '22px "Helvetica Neue", Helvetica, Arial, sans-serif';
ctx.fillStyle = "#000";        
ctx.textAlign = "center";   
ctx.fillText(bar.value, this.scale.calculateBarX(this.datasets.length, datasetIndex, index), (this.scale.calculateY(bar.value))-10);

add label bar

Jarvizx avatar Aug 27 '15 17:08 Jarvizx

I would love to see this feature implemented as well. It would be required for presentation purpose - I have many charts to show my clients, and asking them to check each bar's value from the axes seems too much.

Please consider to add this feature! Thanks

dannysindra avatar Apr 25 '16 23:04 dannysindra

@dannysindra take a look at the data_label_combo-bar-line.html sample. It shows a work-around until this is in the core

etimberg avatar Apr 25 '16 23:04 etimberg

Thanks for the quick response, @etimberg ! The sample is really helpful :)

dannysindra avatar Apr 26 '16 22:04 dannysindra