score:5
I was able to finally find the solution to this question.
I realized I had access to the chart object within the DOM:
Trends.datasets
Within each of the five datasets there was a points object containing x & y positions:
Trends.datasets[0..4].points
So I parsed through the points with a callback utilizing the global configuration functions in Chart.js:
// Function - Will fire on animation progression.
onAnimationProgress: function(){},
// Function - Will fire on animation completion.
onAnimationComplete: function(){}
I found that I had to use onAnimationComplete
with responsive: true
because the point labels would get wiped out on a resize event.
Once I found the points within the callback, I simply wrote the labels I wanted onto the canvas.
My additions to the code were:
var options = {
onAnimationProgress: function() { drawDatasetPointsLabels() },
onAnimationComplete: function() { drawDatasetPointsLabels() }
}
With the added function:
function drawDatasetPointsLabels() {
ctx.font = '.9rem "Gotham Book",sans-serif';
ctx.fillStyle = '#AAA';
ctx.textAlign="center";
$(Trends.datasets).each(function(idx,dataset){
$(dataset.points).each(function(pdx,pointinfo){
// First dataset is shifted off the scale line.
// Don't write to the canvas for the null placeholder.
if ( pointinfo.value !== null ) {
ctx.fillText(pointinfo.value,pointinfo.x,pointinfo.y - 15);
}
});
});
}
So here is the entire code source along with the new code providing the solution:
var data = {
labels: ["","Jun 2013","Jul 2013","Aug 2013","Sep 2013","Oct 2013","Nov 2013","Dec 2013","Jan 2014","Feb 2014","Mar 2014","Apr 2014","May 2014"],
datasets: [
{
label: "hemoglobin_1",
fillColor: "transparent",
strokeColor: "#65204c",
pointColor: "#65204c",
pointHighlightStroke: "#FFF",
data: [null,5.7,5.7,5.8,5.7,5.8,5.7,5.9,6.7,6.7,6.5,6.4,6.4]
},
{
label: "hemoglobin_2",
fillColor: "transparent",
strokeColor: "#ed7141",
pointColor: "#ed7141",
pointHighlightStroke: "#FFF",
data: [null,15.5,15.5,15.6,15.2,15.6,15.1,15.8,17,17.4,16.8,16.4,16.4]
},
{
label: "hemoglobin_3",
fillColor: "transparent",
strokeColor: "#de4760",
pointColor: "#de4760",
pointHighlightStroke: "#FFF",
data: [null,37.1,37,37.2,37.6,36.9,37.6,36.8,37.6,38,37.5,39.1,37.5]
},
{
label: "hemoglobin_4",
fillColor: "transparent",
strokeColor: "#fdcf7e",
pointColor: "#fdcf7e",
pointHighlightStroke: "#FFF",
data: [null,29.9,30.4,29.5,29.6,30.2,29.4,29.8,26.9,27,28.5,26.8,28.5]
},
{
label: "hemoglobin_5",
fillColor: "transparent",
strokeColor: "#a33a59",
pointColor: "#a33a59",
pointHighlightStroke: "#FFF",
data: [null,11.8,11.4,11.9,11.9,11.5,12.2,11.7,11.8,10.9,10.7,11.3,11.3]
}
]
};
var options = {
responsive: true,
scaleOverride: true,
scaleSteps: 10,
scaleStepWidth: 5,
scaleStartValue: 0,
showTooltips: false,
pointDot: true,
pointDotRadius : 6,
datasetStrokeWidth : 3,
bezierCurve : false,
scaleShowHorizontalLines: false,
scaleGridLineWidth : 2,
scaleGridLineColor : "#EEEEEE",
scaleLineWidth: 3,
scaleLineColor: "#000000",
scaleFontFamily: '"Gotham Book",sans-serif',
scaleFontSize: 18,
onAnimationProgress: function() { drawDatasetPointsLabels() },
onAnimationComplete: function() { drawDatasetPointsLabels() }
}
var ctx = $("#myChart").get(0).getContext("2d");
var Trends = new Chart(ctx).Line(data, options);
function drawDatasetPointsLabels() {
ctx.font = '.9rem "Gotham Book",sans-serif';
ctx.fillStyle = '#AAA';
ctx.textAlign="center";
$(Trends.datasets).each(function(idx,dataset){
// First dataset is shifted off the scale line.
// Don't write to the canvas for the null placeholder.
$(dataset.points).each(function(pdx,pointinfo){
if ( pointinfo.value !== null ) {
ctx.fillText(pointinfo.value,pointinfo.x,pointinfo.y - 15);
}
});
});
}
Here is the original problematic version on jsfiddle: http://jsfiddle.net/s9eannLh
[Updated link] And here is the updated solution on jsfiddle: https://jsfiddle.net/s9eannLh/82/
Thanks all!
score:1
Updated code for @JBMcClure solution:
var data = {
labels: ["","Jun 2013","Jul 2013","Aug 2013","Sep 2013","Oct 2013","Nov 2013","Dec 2013","Jan 2014","Feb 2014","Mar 2014","Apr 2014","May 2014"],
datasets: [
{
label: "hemoglobin_1",
fillColor: "transparent",
strokeColor: "#65204c",
pointColor: "#65204c",
pointHighlightStroke: "#FFF",
data: [null,5.7,5.7,5.8,5.7,5.8,5.7,5.9,6.7,6.7,6.5,6.4,6.4]
},
{
label: "hemoglobin_2",
fillColor: "transparent",
strokeColor: "#ed7141",
pointColor: "#ed7141",
pointHighlightStroke: "#FFF",
data: [null,15.5,15.5,15.6,15.2,15.6,15.1,15.8,17,17.4,16.8,16.4,16.4]
},
{
label: "hemoglobin_3",
fillColor: "transparent",
strokeColor: "#de4760",
pointColor: "#de4760",
pointHighlightStroke: "#FFF",
data: [null,37.1,37,37.2,37.6,36.9,37.6,36.8,37.6,38,37.5,39.1,37.5]
},
{
label: "hemoglobin_4",
fillColor: "transparent",
strokeColor: "#fdcf7e",
pointColor: "#fdcf7e",
pointHighlightStroke: "#FFF",
data: [null,29.9,30.4,29.5,29.6,30.2,29.4,29.8,26.9,27,28.5,26.8,28.5]
},
{
label: "hemoglobin_5",
fillColor: "transparent",
strokeColor: "#a33a59",
pointColor: "#a33a59",
pointHighlightStroke: "#FFF",
data: [null,11.8,11.4,11.9,11.9,11.5,12.2,11.7,11.8,10.9,10.7,11.3,11.3]
}
]
};
var options = {
type: 'line',
data: data,
options: {
responsive: true,
scaleOverride: true,
scaleSteps: 10,
scaleStepWidth: 5,
scaleStartValue: 0,
showTooltips: false,
pointDot: true,
pointDotRadius : 6,
datasetStrokeWidth : 3,
bezierCurve : false,
scaleShowHorizontalLines: false,
scaleGridLineWidth : 2,
scaleGridLineColor : "#EEEEEE",
scaleLineWidth: 3,
scaleLineColor: "#000000",
scaleFontFamily: '"Gotham Book",sans-serif',
scaleFontSize: 18,
onAnimationProgress: function() { drawDatasetPointsLabels() },
onAnimationComplete: function() { drawDatasetPointsLabels() }
}
};
var ctx = document.getElementById('myChart').getContext('2d');
var Trends = new Chart(ctx, options);
function drawDatasetPointsLabels() {
ctx.font = '.9rem "Gotham Book",sans-serif';
ctx.fontWeight = 'bold';
ctx.fillStyle = '#AAA';
ctx.textAlign="center";
$(Trends.datasets).each(function(idx,dataset){
$(dataset.points).each(function(pdx,pointinfo){
if ( pointinfo.value !== null ) {
ctx.fillText(pointinfo.value,pointinfo.x,pointinfo.y - 15);
}
});
});
}
@JBMcClure Here is an updated working link:
score:1
Here's how to do it based on the example provided on github. It looks similar to your solution...
// Define a plugin to provide data labels
Chart.plugins.register({
afterDatasetsDraw: function(chart, easing) {
// To only draw at the end of animation, check for easing === 1
var ctx = chart.ctx;
chart.data.datasets.forEach(function (dataset, i) {
var meta = chart.getDatasetMeta(i);
if (!meta.hidden) {
meta.data.forEach(function(element, index) {
// Draw the text in black, with the specified font
ctx.fillStyle = 'rgb(0, 0, 0)';
var fontSize = 16;
var fontStyle = 'normal';
var fontFamily = 'Helvetica Neue';
ctx.font = Chart.helpers.fontString(fontSize, fontStyle, fontFamily);
// Just naively convert to string for now
var dataString = dataset.data[index].toString();
// Make sure alignment settings are correct
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
var padding = 5;
var position = element.tooltipPosition();
ctx.fillText(dataString, position.x, position.y - (fontSize / 2) - padding);
});
}
});
}
});
From Code: https://github.com/chartjs/Chart.js/blob/master/samples/advanced/data-labelling.html
Example: http://www.chartjs.org/samples/latest/advanced/data-labelling.html
You can also define plugins inline to the chart instead of globally...
var chart = new Chart(ctx, {
plugins: [{
afterDatasetsDraw: ...
}]
});
score:3
In case anyone comes here looking for this as I did recently, there's a new plug in available.
Source: stackoverflow.com
Related Query
- How to display Line Chart dataset point labels with Chart.js?
- How to display data labels outside in pie chart with lines in ionic
- ChartJS - how to display line chart with single element as a line? (not as a dot)
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Can chart.js display text associated with each point on a line chart that is permanently visible?
- Chart.js - How to set a line chart dataset as disabled on load
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- line chart with {x, y} point data displays only 2 values
- Chart.js how to show cursor pointer for labels & legends in line chart
- Display line chart with connected dots using chartJS
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- How to display value of only one datapoint in line chart
- How to display inline values in a stacked bar chart with Chart.js?
- Display a limited number of labels only on X-Axis of Line Chart using Chart.js
- Line chart with large number of labels on X axis
- How to align Chart.JS line chart labels to the center
- How to display the labels in doughnut chart using ng2 charts?
- ChartJS - Line chart issue with only 1 point
- ChartJs line chart - display permanent icon above some data points with text on hover
- how to programmatically make a line chart point active/highlighted
- How to dynamically set ChartJs line chart width based on dataset size?
- How to add images to chart labels with vue-chartjs?
- How to add area with break on line chart with fill color
- How do I use my chart.js line chart with handlebars?
- How to disable Chart.js Radar Chart point labels
- How to dynamically change point location with line in Chart.js?
- How to display chart using Chartjs with JSON data in Laravel
- how to display multiple sum with chart js and laravel?
- Chart.js line chart with correctly spaced x labels
- How to change background color of labels in line chart from chart.js?
More Query from same tag
- Export chart and table both to pdf
- Chart.js coming up as blank
- How to draw High-Precision-Numbers in chartjs
- ChartJS Update callbacks
- Chart.js and PHP
- How to make multiple stacked charts to share the same yAxis?
- Apply color only to lower part of Chart.js line chart
- chartjs doesn't render dates properly
- Stacked Group Bar from Chart.js : Can I give each stack group a unique color?
- Any way to make the y-axis border longer in chart.js?
- colour sets for Chart.js border colour
- Vue-Chartjs reactive chart options passing
- How to put 2 charts next to each other
- Chart.js — drawing an arbitrary vertical line
- Responsive canvas extends beyond div
- Error Message when using ChartJS with React
- Using two JSON objects to make Chart.JS Bar Chart Dynamic in Typescript / Angular
- Retrieve value returned from a function in Angular
- Select data based on value of ID
- How to determine chart type inside 'chart.js' pluginservice
- ChartJS, merge legends for multiple charts
- chartjs + Angular6 is not showing charts or any error
- Change label color Y and X axis chart.js
- Add dynamic data in a demo chart.js file of a django template
- plugins.legend.align are incompatible types typescript chartjs
- Border radius for the bar chart in ChartJS
- ChartJS - would like to create a mixed chart with horizontal Bar and a dot to represent the answer from the current user
- Reference Javascript variable in JSON
- How to add text inside the doughnut chart using Chart.js version 3.2.1
- Angular-Chart-JS - Line chart with different fill colors according to points' range