score:6
It is somehow doable. A simple approach presented below assumes one dataset only (it should be easy to extend the approach for handling more datasets, though). The idea is as follows. We will create a plugin that will override the beforeUpdate
method (which is called at the start of every update). At the start of every update, the exact Y pixels of the min and max values of the dataset are calculated. A vertical linear gradient is then created from the context of the canvas using createLinearGradient
, with a kind of red for the Y pixel that corresponds to the min value of the dataset and a jazzy kind of blue for the Y pixel that corresponds to the max value of the dataset. Look at the commented code for more information. There may be some glitches regarding hovering over points and legend coloring, which I am not very keen on looking into. A working fiddle is here and the code is also available below.
var gradientLinePlugin = {
// Called at start of update.
beforeUpdate: function(chartInstance) {
if (chartInstance.options.linearGradientLine) {
// The context, needed for the creation of the linear gradient.
var ctx = chartInstance.chart.ctx;
// The first (and, assuming, only) dataset.
var dataset = chartInstance.data.datasets[0];
// Calculate min and max values of the dataset.
var minValue = Number.MAX_VALUE;
var maxValue = Number.MIN_VALUE;
for (var i = 0; i < dataset.data.length; ++i) {
if (minValue > dataset.data[i])
minValue = dataset.data[i];
if (maxValue < dataset.data[i])
maxValue = dataset.data[i];
}
// Calculate Y pixels for min and max values.
var yAxis = chartInstance.scales['y-axis-0'];
var minValueYPixel = yAxis.getPixelForValue(minValue);
var maxValueYPixel = yAxis.getPixelForValue(maxValue);
// Create the gradient.
var gradient = ctx.createLinearGradient(0, minValueYPixel, 0, maxValueYPixel);
// A kind of red for min.
gradient.addColorStop(0, 'rgba(231, 18, 143, 1.0)');
// A kind of blue for max.
gradient.addColorStop(1, 'rgba(0, 173, 238, 1.0)');
// Assign the gradient to the dataset's border color.
dataset.borderColor = gradient;
// Uncomment this for some effects, especially together with commenting the `fill: false` option below.
// dataset.backgroundColor = gradient;
}
}
};
Chart.pluginService.register(gradientLinePlugin);
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["First", "Second", "Third", "Fourth", "Fifth"],
datasets: [{
label: 'My Sample Dataset',
data: [20, 30, 50, 10, 40],
// No curves.
tension: 0,
// No fill under the line.
fill: false
}],
},
options: {
// Option for coloring the line with a gradient.
linearGradientLine: true,
scales: {
yAxes: [{
ticks: {
min: 0,
max: 100,
stepSize: 20
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
<canvas id="myChart" width="400" height="200"></canvas>
There is also a pluginless method, mentioned here, but that method is lacking. According to that method, one would have to set the borderColor
to a gradient that should have been created before the creation of the chart. The gradient is calculated statically and will never fit an arbitrary range or respond to resizing as is.
Source: stackoverflow.com
Related Query
- Gradient line chart with ChartJS
- Display line chart with connected dots using chartJS
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- ChartJS - Line Chart with different size datasets
- ChartJS - Line chart issue with only 1 point
- ChartJs line chart - display permanent icon above some data points with text on hover
- Chartjs doughnut chart with gradient color
- 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?
- Adding line over stacked line chart with ChartJS
- How to reduce the number of points shown on line chartjs chart with a large dataset?
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
- Vertical Line chart with ChartJS
- Real-time line chart with ChartJS using Ajax data
- Plot Multiple Line Chart in Ionic 3 with ChartJS
- drawing line chart with chartjs
- Trouble with setting background color for Line chart at Chartjs version 3.5.1
- Draw stacked horizontal bar chart with Average line using ChartJS
- Chartjs line chart gets all scrambled up when an x value coincides with a label
- Chartjs - line chart display with addData() is bugged?
- ChartJS Line Chart shoud not start at 0 with all Lines
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- Chartjs random colors for each part of pie chart with data dynamically from database
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- ChartJS - Draw chart with label by month, data by day
- line chart with {x, y} point data displays only 2 values
- Chart.js - line chart with two yAxis: "TypeError: yScale is undefined"
- How to fix chart Legends width-height with overflow scroll in ChartJS
- How to display Line Chart dataset point labels with Chart.js?
- show label in tooltip but not in x axis for chartjs line chart
More Query from same tag
- UnCaught IndexSizeError: ChartJS
- ng2-charts access chartjs object to apply chartjs functions
- Async showing data in pieChart from chart.js with typescript
- ChartJS Data Overflowing Chart area
- How do I use this new extension for chart.js?
- How to draw a chart with Chart.JS?
- How to add padding to the vertical scale (X-axis) in Chart.js?
- is there a way to change the date format in php?
- Using ng2-charts, how to "zoom out"?
- Convert a dynamic piechart from CanvasJS to Google Charts?
- Chart.js how to create chart wirhout y-axis for two data sets
- ngx-charts-advanced-pie-chart is reading my rest api response as null
- Rendering Rails json data in chartjs
- Chartjs add and remove data
- How to send data to chart js using angular
- Draw y-axis inside the graph area with chart js
- Ng2-charts / chart.js - how to refresh/update chart - angular 4
- ChartJS - How to change color of some data points in graph
- How do I customize the chart.js tooltip? Two labels have the same data, which I want to show you with each data
- Strange lines between horizontal bars in Chart.js 2.0
- Issue with int numbers using chart.js
- How can I add a unit to the end of my Y Axis values in ChartJS?
- Chart.Mvc scale begin at 0 - Nullable bool property remains null
- Can't change the time of the xAxes in the Chart.js
- Chartjs / ng2-charts line on hover does not work
- Chart JS not displaying graph
- Problems with ChartJS "Uncaught TypeError: Cannot read property 'length' of null"
- Sorting arrays and comparing array values php
- chart.js - link to other page when click on specific section in chart
- Create a rounded bar graph with Angular and chartJS