score:4
You should use the following ...
ctx.canvas.width
var ctx = document.getElementById('myChart').getContext('2d');
var grd = ctx.createLinearGradient(0, 0, ctx.canvas.width, 0);
grd.addColorStop(0.2, 'red');
grd.addColorStop(0.4, 'orange');
grd.addColorStop(0.78, 'limegreen');
grd.addColorStop(0.9, 'black');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'horizontalBar',
// The data for our dataset
data: {
labels: ["Confort"],
datasets: [{
label: ["Confort actuel"],
borderWidth: 0,
borderColor: 'rgba(0,0,0,1.0)',
backgroundColor: 'rgba(0,0,0,0.2)',
hoverBackgroundColor: 'rgba(0,0,0,0)',
data: [64],
}, {
label: ["Confort projeté"],
borderWidth: 0,
borderColor: 'rgba(0,0,0,1.0)',
backgroundColor: grd,
hoverBackgroundColor: grd,
data: [120],
}, {
label: ["Confort optimal"],
borderWidth: 0,
borderColor: 'rgba(0,0,0,1.0)',
backgroundColor: 'rgba(205,205,205,1.0)',
hoverBackgroundColor: 'rgba(205,205,205,1.0)',
data: [100],
}, ]
},
// Configuration options go here
options: {
legend: {
display: false,
},
scales: {
xAxes: [{
stacked: false,
ticks: {
beginAtZero: true,
max: 130,
display: false,
},
gridLines: {
display: false,
drawBorder: false,
},
}],
yAxes: [{
display: false,
stacked: true,
barThickness: 20,
gridLines: {
display: false,
drawBorder: false,
}
}],
},
tooltips: {
enabled: false,
callbacks: {
label: function(tooltipItems, data) {
return " " + tooltipItems.xLabel + "%";
}
}
},
}
});
Chart.pluginService.register({
afterDraw: function(chartInstance) {
var ctx = chartInstance.chart.ctx;
// render the value of the chart above the bar
ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, 'normal', Chart.defaults.global.defaultFontFamily);
ctx.textAlign = 'right';
ctx.textBaseline = 'center';
chartInstance.data.datasets.forEach(function(dataset) {
for (var i = 0; i < dataset.data.length; i++) {
var model = dataset._meta[Object.keys(dataset._meta)[0]].data[i]._model;
if (dataset.label[i] == "Confort projeté") {
ctx.fillText(dataset.label[i] + " (" + dataset.data[i] + "%)", model.x - 8, model.y + 21);
ctx.fillText("▲", model.x + 6, model.y + 19);
} else if (dataset.label[i] == "Confort actuel") {
ctx.fillText(dataset.label[i] + " (" + dataset.data[i] + "%)", model.x - 8, model.y - 14);
ctx.fillText("▼", model.x + 6, model.y - 12);
} else {
ctx.fillText(dataset.label[i] + " (" + dataset.data[i] + "%)", model.x + 130, model.y - 14);
ctx.fillText("▼", model.x + 6, model.y - 12);
}
}
});
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="myChart"></canvas>
Source: stackoverflow.com
Related Query
- Linear gradient width using percentage (%) instead of pixels (px)
- How to run Chart.js samples using source code
- Chart.js - add gradient instead of solid color - implementing solution
- ChartJs bar chart - keep bars left instead of equally spread across the width
- Gradient color for each bar in a bar graph using ChartJS
- using gradient colors in angular-chart.js
- How to show percentage (%) using chartjs-plugin-labels ( Pie chart ) in angular 2/8
- Using chartjs v2 to show categorical values on axis instead of numeric
- How to change Doughnut color to linear gradient on ChartJS?
- how to add percentage value to legend field in pie chart using chart.js
- How to generate color code dynamically by swapping 2 characters within a string using PHP
- Vertical line using Chart.js annotations plugin with linear scale on x axis
- Chart.js 3.5: linear gradient doesn't apply properly when multiple bars in the same chart
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
- Using chart.js, how do I align bars instead of centering them when there are only a few bars to display?
- Using Chart.js onClick function, can you execute code from an undefined or null result?
- How to make the background color of the chart as gradient using Chart.js
- How to decrease bottom width of triangle using line chart in chartJs?
- Vue.js Vue-chart.js linear gradient background fill
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- When using chart.js to draw a line chart, if the peak is between two x values instead of "at a x value"
- null values for instead of date causes the browser to crash while using chart js
- How to change width of the chart using Chart.js
- How do you limit the blur in a linear gradient on a canvas?
- How to control width of chart using angular-chart and chart.js?
- Chart JS is using 1,2,3 as y values for my points for the second dataset instead of their passed in labels
- Chart.js : straight lines instead of curves
- How to add text inside the doughnut chart using Chart.js?
- Setting width and height
- chart js 2 how to set bar width
More Query from same tag
- ChartJs doesn't display the line item legend
- Chart.js point format data for a scatter plot
- How to catch an onClick event on bubble chart?
- Is There any way to show image on top of the stacked bar, Am getting image on every color Instead of that i need only on top of each bar
- How to update your django page by looking for new data in the db?
- How to add Dyamic JSON Dropdown Menu in Angular JS
- How do I prevent my chart from rerendering? (ReactJS)
- Remove custom Chart Js tool tip colour square
- Chart.js is not respecting my container dimensions
- Chart JS dataset disabled by default
- Chart.js - Color specific parts of the background in a line chart
- Chart.js zeros handling
- NuxtJS ChartJS Gradient Color doesnt apply
- Chart.js line chart: gap between December and January
- C# MVC5 JavaScript Chart.js Pie Chart with Realtime Updating from SQL Server Database without Refreshing
- Chart.js Undesired Tooltip Showing Up for Legend
- Passing json_encode to chart.js won't work
- Save data from REST API Get method and use it one more time
- Display multiple chart.js charts using for loop in Django template
- Chart.js multiple datasets on one y axis
- How to make gap in chart.js graph?
- What is the syntax for require.config for chartjs-plugin-streaming and dependencies using RequireJS?
- chart.js: Chart in chart
- Visualization of charts using real time data from MSSQL with node.js webserver
- html-pdf: How to ensure image doesn't span a page break
- Not drawing null values using chart.js
- Create grouped-bar using chart.js, php and mysql
- Control Scaling of Canvas When dragged out of chart limit
- chart.js Failed to create chart: can't acquire context from the given item
- Chart.js: Some sectors not showing if difference is too big