score:0
with chart.js 2.9.x this is possible with the plugins
option:
plugins: [{
afterdraw: function (chart, options) {
let controller = chart.controller;
let axis = controller.scales['y-axis-0'];
let ctx = chart.ctx;
let labeltostyle = null;
axis._labelitems.foreach(function (value, index) {
var labeltostyle = value
let textwidth = ctx.measuretext(labeltostyle.label).width;
let line_x_start = labeltostyle.x - textwidth;
let line_y = labeltostyle.y + (labeltostyle.font.size / 2) + 3
ctx.linewidth = 3;
ctx.strokestyle = 'orange';
ctx.beginpath();
ctx.moveto(line_x_start, line_y);
ctx.lineto(labeltostyle.x, line_y);
ctx.stroke();
});
}
}]
i think this is a more comfortable approach compared to scale extension.
here goes a fiddle where the y tick labels are underlined.
score:3
you can extend the chart to do this
preview
script
chart.types.line.extend({
name: "linealt",
initialize: function(){
// add some extra width the the y axis labels
this.options.scalelabel = " " + this.options.scalelabel;
chart.types.line.prototype.initialize.apply(this, arguments);
},
draw: function(){
chart.types.line.prototype.draw.apply(this, arguments);
ctx.save();
ctx.fillstyle = '#fcc';
// the fill should be under the text
ctx.globalcompositeoperation = "destination-over"
// draw background under x axis labels
chart.helpers.each(this.scale.xlabels, function(label, index){
var xpos = this.calculatex(index) + chart.helpers.aliaspixel(this.linewidth),
isrotated = (this.xlabelrotation > 0);
ctx.save();
ctx.translate(xpos, (isrotated) ? this.endpoint + 12 : this.endpoint + 8);
ctx.rotate(chart.helpers.radians(this.xlabelrotation) * -1);
var width = ctx.measuretext(label).width;
// add a 4px padding on each side
// the height is set to 1.5 times the font size since there is no method to measure height easily
ctx.fillrect(-width / 2 - 4, 0, width + 8, this.fontsize * 1.5);
ctx.restore();
}, this.scale)
// draw background under y axis labels
var ylabelgap = (this.scale.endpoint - this.scale.startpoint) / this.scale.steps,
xstart = math.round(this.scale.xscalepaddingleft);
chart.helpers.each(this.scale.ylabels,function(labelstring, index){
var ylabelcenter = this.endpoint - (ylabelgap * index);
var width = ctx.measuretext(labelstring).width;
// add some padding on the side - we don't need to increase the width because we use the width added by the extra space
ctx.fillrect(xstart - width - 4, ylabelcenter - this.fontsize * 1.5 / 2, width, this.fontsize * 1.5);
}, this.scale);
ctx.restore();
}
});
and then
...
new chart(ctx).linealt(data);
fiddle - http://jsfiddle.net/e894g5qn/
Source: stackoverflow.com
Related Query
- How to change background color of labels in line chart from chart.js?
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Line chart Change background color of shaded region on hover (Chartjs)
- Chart.js how to show cursor pointer for labels & legends in line chart
- Chart.js line chart set background color
- How to display Line Chart dataset point labels with Chart.js?
- How to align Chart.JS line chart labels to the center
- How can I remove extra whitespace from the bottom of a line chart in chart.js?
- How to change line segment color of a line graph in Chart.js?
- Chart.js: How can a line series (out of many) change line color and thickness upon mouse hover?
- how to set chart.js grid color for line chart
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- Chart.js - Color specific parts of the background in a line chart
- How to add gradient background to Line Chart [vue-chart.js]
- How to add multiple background color in line charts
- How to remove background color and color example from tooltip in Chart.js?
- How to change line segment color based on label value in chart.js?
- How to add area with break on line chart with fill color
- Change Axis Line color in Chart created using chart.js
- How to use segment property to color line / border color based on value in chart js?
- How to change the label and grid line position on a timeseries chart in Chart.js 3?
- How to make aspecific API call from within a Line Chart class in ReactJS using react-chartjs-2?
- change stroke line color in chart according to datasets in react native
- How do I customize y-axis labels and randomly pick the value from the data range for x-axis in Chart js
- I have 10 points inn x-axis, ranging [-25,-20,,-15,-10,0,10,20,30,40,50]. But I want my line chart to start from -15 of x-axis. How we can achieve?
- How to change color of column in chart js
- How to change background color in ng2-charts
- Change the background color of tooltip dynamically using chart color
- Remove background color in chartjs line chart
- How to change the chart line or area colors according to the user need?
More Query from same tag
- Chart.js tooltip yAlign
- How to use the tooltipTemplate on Chart.JS 2.0
- Chartjs bar-chart does not render when values are equal
- Data not fully displaying on bar chart
- how to creat a dynamic datasets and new dynamic multi yAxes
- Chartjs does not render chart when set responsive option to true
- Chart.js, adding footer to chart
- PrimeNg bar chart how to show a label for the y-axis
- Modify the x-axis label in chart js
- Angular data visualisation using chart.js
- chart.js increase value on click
- How to destroy a canvas using Id and add new canvas to the same id
- ChartJS charts not generating within tabs
- Chart.js / HTML Canvas - move line
- How to change the Y-axis values from numbers to strings in Chart.js?
- How to create two x-axes label using chart.js
- Remove chartjs pie chart highlight on hover
- Struggling with dynamic data
- Query with empty results laravel eloquent
- Show X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- Chart.js Console JS Error while destroy the Chart on click event
- Chart.js2 Radar, how to configure the label padding/spacing?
- Plot lap times with Chart.js from time strings
- Line chart: align x axis (timestamps) for multiple data sets
- ChartJS and Radar Chart animation
- Laravel determine the date from database if MONDAY-SUNDAY
- chart.js - link to other page when click on specific section in chart
- Stepped Line with angular-chart.js
- Change color of one value in the x-axis charts.js
- Set Start Value as '0' With Dynamic Data In Chart.js