score:2
You can use an inline plugin to achieve it:
var GradientBgPlugin = {
beforeDraw: function(chart, args, options) {
const ctx = chart.ctx;
const canvas = chart.canvas;
const chartArea = chart.chartArea;
// Chart background
var gradientBack = canvas.getContext("2d").createLinearGradient(0, 250, 0, 0);
gradientBack.addColorStop(0, "rgba(213,235,248,1)");
gradientBack.addColorStop(0.16, "rgba(213,235,248,1)");
gradientBack.addColorStop(0.17, "rgba(226,245,234,1)");
gradientBack.addColorStop(0.25, "rgba(226,245,234,1)");
gradientBack.addColorStop(0.26, "rgba(252,244,219,1)");
gradientBack.addColorStop(0.5, "rgba(252,244,219,1)");
gradientBack.addColorStop(0.51, "rgba(251,221,221,1)");
gradientBack.addColorStop(1, "rgba(251,221,221,1)");
ctx.fillStyle = gradientBack;
ctx.fillRect(chartArea.left, chartArea.bottom,
chartArea.right - chartArea.left, chartArea.top - chartArea.bottom);
}
};
Than just include it in your Chart options:
plugins: [GradientBgPlugin]
The result should be similar to this JSFiddle.
EDIT
For Reach Charts JS 2, you need small changes in setup. You define plugin this way:
const plugins = [{
beforeDraw: function(chart) {
const ctx = chart.ctx;
const canvas = chart.canvas;
const chartArea = chart.chartArea;
// Chart background
var gradientBack = canvas.getContext("2d").createLinearGradient(0, 250, 0, 0);
gradientBack.addColorStop(0, "rgba(213,235,248,1)");
gradientBack.addColorStop(0.16, "rgba(213,235,248,1)");
gradientBack.addColorStop(0.17, "rgba(226,245,234,1)");
gradientBack.addColorStop(0.25, "rgba(226,245,234,1)");
gradientBack.addColorStop(0.26, "rgba(252,244,219,1)");
gradientBack.addColorStop(0.5, "rgba(252,244,219,1)");
gradientBack.addColorStop(0.51, "rgba(251,221,221,1)");
gradientBack.addColorStop(1, "rgba(251,221,221,1)");
ctx.fillStyle = gradientBack;
ctx.fillRect(chartArea.left, chartArea.bottom,
chartArea.right - chartArea.left, chartArea.top - chartArea.bottom);
}
}];
Than you plug it this way:
<Line data={data} plugins={plugins} />
You can see it working fine on CodeSandbox here.
score:0
Detailed explanation can be found here: https://medium.com/@omi10859/alternative-background-lines-in-chartjs-a626ce4d3bcb
We can use annotaion plugin with chartjs to create custom elements. we can use annotation plugin to do this
import annotationPlugin from "chartjs-plugin-annotation";
import {Chart} from 'chart.js';
Chart.register(annotationPlugin);
this code will add a box to our chart
{
type: 'box', #type of draw
drawTime: 'beforeDraw', #this will decide background or foreground
yMin: 5, #value min on y axis
yMax: 10, #value max on y axis
borderColor: 'rgb(242, 244, 248, 0.9)', #border color of the box
borderWidth: 1, #boarder width for box
backgroundColor: '#F2F4F8', #colour of the box
}
# add option while rendering
const options = {
plugins: {annotation: {annotations: background_annotation}
}
score:2
You can write a custom inline plugin, that draws the colors on the chart Area. In the options section you can put an object with all the sections you want, from where to where and which color they need to be
Example:
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [100, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
plugins: {
backgrounds: {
hbars: [{
from: 28,
to: 100,
color: "rgb(195, 230, 195)"
},
{
from: 20,
to: 28,
color: "rgb(230, 220, 195)"
},
{
from: 0,
to: 20,
color: "rgb(230, 195, 195)"
}
]
}
}
},
plugins: [{
id: 'backgrounds',
beforeDraw: (chart, args, options) => {
const {
ctx,
chartArea,
scales: {
y
}
} = chart;
options.hbars.forEach((hBar) => {
ctx.save();
ctx.fillStyle = hBar.color;
ctx.fillRect(chartArea.left, y.getPixelForValue(hBar.from), chartArea.right - chartArea.left, y.getPixelForValue(hBar.to) - y.getPixelForValue(hBar.from));
ctx.restore();
})
}
}]
}
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.2.0/chart.js"></script>
</body>
Source: stackoverflow.com
Related Query
- How to apply to different bground color for each area in Chart.js
- chart js - Apply different color for each x-axes label
- Different color for each column in angular-chartjs bar chart
- How to use set the color for each bar in a bar chart using chartjs?
- How do I get a different label for each bar in a bar chart in ChartJS?
- how to create bar chart with group and sam color for each group using chart.js?
- Different color for each bar in a bar chart; ChartJS
- chart.js Line chart with different background colors for each section
- Can I specify a different font size for each row of text in Chart Title?
- How to set a full length background color for each bar in chartjs bar
- how to set chart.js grid color for line chart
- How to set single color on each bar in angular chart js
- How to add label in chart.js for polar chart area
- How to map json array to two different array for chart
- How to add area with break on line chart with fill color
- how to set color for each item in tooltip with ChartJS
- How to show different product name for every bar in chart js
- How to wait for all items to load within ng-repeat before then rendering a chart for each item
- How can I have different values for the chart and the tooltip in chart.js?
- chart.JS i want to put different color for each Y axis value grid line color
- How to remove or color line/grid in polar chart area using Chart.js?
- how to set color for each datapoint in chartjs-chart-treemap
- How to set X coordinate for each bar with react chart js 2?
- Chartjs Different Fill Color For Overlap Area
- ChartJS/High Charts Radar chart - Different radial axis labels for each category that appear on hover
- How make border radius for chart area chart.js?
- How can I set different colours for each bar in angular-chart.js v1.0.0?
- How to apply each color to each dataset
- Set different color for each bars using ChartJS in Angular 4
- How to show area chart with timeseries configuration with different datasets in Chart.js v3?
More Query from same tag
- How to set chartJs Doughnut labels on right side?
- Rails chart.js data coffee script
- Chartjs React Typescript moving x axis labels above chart
- Is there a way in chartjs to display different Boolean values with an offset in Y over a common timeline?
- Vue3 does not show line-chart
- Uncaught type error: mychart.update is not a function
- Draw a single frame in Chart.js
- Chart.js graph with just two cordinates
- Chart.js remove label from legend for if dataset values
- Chart.js draw y-axis only up to point
- Fix ChartJS Values not Well Displayed
- Which scenarios might make Chart.js's canvas element resize?
- In a Stacked Line Chart, is there a way to stack multiple Y-Axes to match each graphed line?
- Chart doesn't render in angular 2 app
- How to increase the ghap between catergories or labels react-chartjs-2
- Chart.js canvas : can't center it
- Change legend position of ng-charts (using angular2)
- How to append text or symbol to tooltip of chart.js
- angular-chart.js bar chart with 100% opacity color
- Chartjs bar min, max and avarage value
- Want to show small part of js Bar Chart when value is zero
- Draw horizontal lines in Chart.js 2.0
- Line chart disable interpolation
- Highlight date in Chart.js line graph
- chart.js d3js chart in time scale, data from CSV file
- Primeng line chart not updating automatically,only update on window refresh
- How to export/import modules between component files in Javascript
- how to draw Line chart using chart.js and datalabels should be shown
- chart.js plugins.register function with outer data
- how to use chart.js in angular 7