score:1
You will get the both points on the start and end of the line segment the function is evaluating for as p0
and p1
in here you can access the y value like this: val = ctx.p0.parsed.y;
Live example:
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
segment: {
borderColor: (ctx) => {
val = ctx.p0.parsed.y;
return val >= 15 ? 'green' : val >= 10 ? 'blue' : val >= 5 ? 'pink' : 'purple'
}
}
}]
},
options: {}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.0/chart.js"></script>
</body>
EDIT:
As per my comment you were pretty close with your pixelForValue
, you just had to pass the context also to your gradiant function and dont use the v2 default ID's for the scales:
borderColor: function(context, options) {
const chart = context.chart;
const {
ctx,
chartArea
} = chart;
if (!chartArea) {
return null;
}
return getGradient(ctx, chartArea, chart);
}
function getGradient(ctx, chartArea, chart) {
const chartWidth = chartArea.right - chartArea.left;
const chartHeight = chartArea.bottom - chartArea.top;
if (gradient === null || width !== chartWidth || height !== chartHeight) {
width = chartWidth;
height = chartHeight;
let y2 = 27; // I want this to be the pixel for value I pass, instead of hard value
const { scales: { x, y } } = chart;
console.log(x.getPixelForValue(20), x.getPixelForValue(4))
gradient = ctx.createLinearGradient(0, chartArea.bottom, 0, chartArea.top);
gradient.addColorStop(y2 / chartHeight, 'black');
gradient.addColorStop(0.6, 'yellow');
gradient.addColorStop(0.9, 'red');
gradient = ctx.createLinearGradient(0, chartArea.bottom, 0, chartArea.top);
gradient.addColorStop(y2 / chartHeight, 'black');
gradient.addColorStop(0.6, 'yellow');
gradient.addColorStop(0.9, 'red');
}
return gradient;
}
But you can also choose to make an array of objects with the value and color and use that to make your gradiant:
var ctx = document.getElementById('LineChart').getContext("2d");
let width, height, gradient;
const getcolorStop = (val, max) => (val / max);
const colors = [{
val: 2,
color: 'pink',
},
{
val: 12,
color: 'purple',
},
{
val: 20,
color: 'green',
}
]
function getGradient(ctx, chartArea, chart) {
const chartWidth = chartArea.right - chartArea.left;
const chartHeight = chartArea.bottom - chartArea.top;
if (gradient === null || width !== chartWidth || height !== chartHeight) {
width = chartWidth;
height = chartHeight;
const {
scales: {
y: {
max
}
}
} = chart;
gradient = ctx.createLinearGradient(0, chartArea.bottom, 0, chartArea.top);
colors.forEach((color) => {
if (color.val > max) {
color.val = max;
}
gradient.addColorStop(getcolorStop(color.val, max), color.color);
});
}
return gradient;
}
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ['10', '20', '30', '40', '50'],
datasets: [{
label: 'Hello',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: 'rgba(255, 255, 78, 1)',
borderColor: function(context, options) {
const chart = context.chart;
const {
ctx,
chartArea
} = chart;
if (!chartArea) {
return null;
}
return getGradient(ctx, chartArea, chart);
},
}]
},
});
<h1>Chart One</h1>
<canvas id="LineChart" height="100"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.0/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2"></script>
Source: stackoverflow.com
Related Query
- How to use segment property to color line / border color based on value in chart js?
- How to change line segment color based on label value in chart.js?
- How to sort XY line chart tooltip items based on value and add thousands separators?
- How to display value of only one datapoint in line chart
- How to change line segment color of a line graph in Chart.js?
- how to change Y axis value dynamically based on user input in Chartjs for Line chart?
- how to set chart.js grid color for line chart
- How to dynamically set ChartJs line chart width based on dataset size?
- chart.js bar chart color change based on value
- 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 use set the color for each bar in a bar chart using chartjs?
- How to change background color of labels in line chart from chart.js?
- how can i use chart.js to create a chart that has one time series line and one linear line on it at the same time?
- Chart Js - Globally set bar chart color based on value
- How to get line x value from Label chart js
- How to use chart.js to plot line chart with x axis as time stamp in seconds
- chart.js bar chart change color based on value
- How to change line color based on data - chartist or chart.js
- chart.js Is there way to color line chart ticks depending on value
- Javascript How to give color using conditional statement based on json array state value
- How to draw multiple color bars in a bar chart along a Horizontal Line using chart.js
- How to limit chart JS hover to take only one value from each line chart when zoomed out?
- chartjs - How to use differente color if value is lower than previous value?
- How to make bars change color based on value in chartsjs
- How do I use ChartJS with a background color in the space between two line charts?
- How to plot a single value with line in line chart graph using charts.js?
- How to draw a line in line chart for single value in Charts.JS
- 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?
More Query from same tag
- How to make rectangle in chart.js
- ChartJS adding 3yAxes
- Using chart js options with react-chartjs-2 and typescript
- How to get tooltip on chart.js v1.x?
- How to put indicator between yAxis values on yAxis chartJs?
- Chat js with respose ajax
- Wrong label value is displayed on point hover - Chart.JS
- How to create a charjs bar graph with many bars?
- How to draw baseline for bar chart in chart.js
- Chart.js bar chart mouse hovering highlights all the datasets instead of just the selected one
- How to make label always visible on DoughnutChart.js
- ChartJS Line Chart shoud not start at 0 with all Lines
- Dynamically Setting Height of Chart
- chart.js with JSON Data on Same page not displaying
- Chart.js line chart is not displaying
- chart.js 2.7.1 | Updating charts with variables
- Chart.js How to change y axis title position
- Why does one chart overlays another (chart.js)?
- AJAX request not working unless alert() is used
- return percentage in chart.js (v2+) legend
- How to access data outside the onclick function
- How to render multiple Chart.JS charts on the same page with AJAX data and variables
- Is it possible in chartjs to hide certain dataset legends?
- Using Chart.Js to plot a scatter plot from an Array
- How to select and pass array object to data setting of chart.js config?
- Chart.js misreading arrays for labels and axes
- Ember.Computed is not a function
- how to set title in legend of chart.js?
- Moving point to clicked point in ChartJS (radarchart)
- Y Axes not displaying properly on Chartjs