score:0
Looking at the bar and global options, this doesn't seem possible using only chartjs.
http://www.chartjs.org/docs/#bar-chart-chart-options
http://www.chartjs.org/docs/#getting-started-global-chart-configuration
As chartsjs uses a canvas element to display its elements, you won't be able to implement a CSS solution either. If you really want to do this, you could try editing the chartjs library. Or just pick a solid color.
score:5
There is a section on patterns and gradients in the ChartJS documentation that allows to pass a CanvasPattern or CanvasGradient object instead of a string colour.
Read about it here:
score:6
Old question, but now there's the patternomaly add-on :-)
https://github.com/ashiguruma/patternomaly
It contains 21 pre-defined patterns you can use in chart.js.
score:9
tl;dr
Just pass a CanvasPattern
or CanvasGradient
to the dataset's backgroundColor
property as the official docs say.
Excuse me, what?
This can be done through a 3rd party library like patternomaly, but if you just want a few simple pattern it is unnecessary since you can easily create a custom function that takes a color and give you back a canvas pattern:
function createDiagonalPattern(color = 'black') {
// create a 10x10 px canvas for the pattern's base shape
let shape = document.createElement('canvas')
shape.width = 10
shape.height = 10
// get the context for drawing
let c = shape.getContext('2d')
// draw 1st line of the shape
c.strokeStyle = color
c.beginPath()
c.moveTo(2, 0)
c.lineTo(10, 8)
c.stroke()
// draw 2nd line of the shape
c.beginPath()
c.moveTo(0, 8)
c.lineTo(2, 10)
c.stroke()
// create the pattern from the shape
return c.createPattern(shape, 'repeat')
}
Then just call it in your datasets (don't forget to add a border if you need that):
datasets: [{
label: 'Good questions',
data: [3, 4, 1, 6, 10],
backgroundColor: createDiagonalPattern('green'),
// create a border with the same color
borderColor: 'green',
borderWidth: 1,
}],
Edge cases
Keep in mind that canvas has anti-aliasing so when you draw stuff around the corners it can mess up your pattern. To mitigate this just draw your lines from an edge.
If you create the diagonal line between the corners like this:
c.beginPath()
c.moveTo(0, 0)
c.lineTo(10, 10)
c.stroke()
Then the pattern wouldn't look seamless because the corner crops off parts so you lose that infinite effect:
Demo
var element = document.getElementById('chart');
var ctx = element.getContext("2d");
function createDiagonalPattern(color = 'black') {
let shape = document.createElement('canvas')
shape.width = 10
shape.height = 10
let c = shape.getContext('2d')
c.strokeStyle = color
c.beginPath()
c.moveTo(2, 0)
c.lineTo(10, 8)
c.stroke()
c.beginPath()
c.moveTo(0, 8)
c.lineTo(2, 10)
c.stroke()
return c.createPattern(shape, 'repeat')
}
var graph = new Chart(element, {
type: 'bar',
data: {
labels: ['jan', 'feb', 'mar', 'apr', 'may'],
datasets: [
{
label: 'Good questions',
data: [3, 4, 1, 6, 10],
backgroundColor: createDiagonalPattern('green'),
borderColor: 'green',
borderWidth: 1,
},
{
label: 'Bad questions',
data: [2, 7, 3, 5, 1],
backgroundColor: createDiagonalPattern('#FF0000'),
borderColor: '#FF0000',
borderWidth: 1,
},
],
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
},
}],
},
},
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<canvas id="chart" ></canvas>
Source: stackoverflow.com
Related Query
- Fill Chart.js bar chart with diagonal stripes or other patterns
- ChartJS Bar chart with time scale - Bars overlap with each other
- Horizontal stacked bar chart with chart.js
- Vertical stacked bar chart with chart.js
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- chart js how to fill legend box with colour
- Chart.js Mixed Bar and Line chart with different scales
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- How to display inline values in a stacked bar chart with Chart.js?
- Chart.js - Draw bar chart with multiple labels
- ChartJS bar chart with legend which corresponds to each bar
- angular-chart.js bar chart with 100% opacity color
- Chartjs - data format for bar chart with multi-level x-axes
- Chart.js - Bar chart with linear cartesian (numerical) X axis?
- Getting chart js bar chart to fill window
- Stacked horizontal bar chart along total count with chart.js
- Stacked bar chart with rounded corner of bar using Chart.js
- How to add area with break on line chart with fill color
- Issue while plotting bar chart with custom x-axis with month and year in chart.js
- Combo Bar Line Chart with Chart.js
- Horizontal bar with two yaxes chart js
- Chart JS : Getting issue with draw bar chart
- ChartJS: how to make a bar chart with a horizontal guideline:
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- How to create Bar chart that gets updated from new ajax requests with ChartJS?
- Having problems getting a line chart in Chartsjs to go *up* to 1, with bkg fill *under* the chart line
- Chart.Js how to hide datapoints with "0" value in Stacked Bar Chart
- Chart.js - Horizontal line on Bar chart interferes with tooltip
- AmCharts: Clustered bar chart with sub-categories
- Chart.js Bar Chart with a switch/radio button to group data / Troubleshooting bar color
More Query from same tag
- Set size chartjs in div
- charts.js stacked y-Axis
- Chartjs - my old data show sometimes, bugs?
- How to fix the distance between horizontal points (x-axis) in chartJS
- Chart.js - Getting data from HTML
- cartesian coordinate system with chart.js
- Django Multiple chartJs chats in a page
- generate basic pie chart using chartjs
- converting int to List<double?>, ChartJS Core
- Chart.js Plugin Not Recognized as Options Property in Angular
- Unable to load Chart.js data from getJSON / PHP?
- I need to increase data in a bar chart by one with a button click
- ChartJS yAxis showing not coherent data
- chart.js v2.0.0 redrawing the chart
- how to get multiple x-axis labels for a react-chartjs-2 chart
- Why is chart.js not showing charts?
- chartjs creating equilvant background like in chartjs.org
- How to put dynamic value in Doughnutchart Using chartjs?
- The real time chart sometimes does not display when I switch the target
- what is the correct way to destructure a nested json object with fetch?
- Add multiple lines as Y axis title in chartJs
- Trying to get total sum based on months on ApexCharts with Php
- Automatic colors assignment in chart.js 2.x doesn't work anymore, used to work in v. 1.x
- Chart JS tooltip appears differently when set from script instead of html
- ChartJS re-rendering bug
- Remove Legends ChartsJS 2.8.0
- Execute chartjs after an ajax call with PHP
- Error when implementing charts.js in angular
- ChartJS Datalabels plugin and negative numbers
- Use multi data to draw chart with chartjs