question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gradient not applied

See original GitHub issue

I’m trying to apply a gradient as background color to labels but it doesn’t work.

Here is code, based on data labels sample:

<script id="script-init">
var ctx = document.getElementById('chart-0').getContext("2d");
var gradientStroke = ctx.createLinearGradient(500, 0, 100, 0);
gradientStroke.addColorStop(0, "#80b6f4");
gradientStroke.addColorStop(1, "#f49080");

	var DATA_COUNT = 8;
	var labels = [];
	Samples.srand(8);

	for (var i = 0; i < DATA_COUNT; ++i) {
		labels.push('' + i);
	}

	Chart.helpers.merge(Chart.defaults.global, {
		aspectRatio: 4/3,
		tooltips: false,
		layout: {
			padding: {
				top: 42,
				right: 16,
				bottom: 32,
				left: 8
			}
		},
		elements: {
			line: {
				fill: false
			}
		},
		plugins: {
			legend: false,
			title: false
		}
	});
</script>

<script id="script-construct">
var chart = new Chart('chart-0', {
	type: 'line',
	data: {
		labels: labels,
		datasets: [{
			backgroundColor: gradientStroke,
			borderColor: gradientStroke,
			data: Samples.numbers({
				count: DATA_COUNT,
				min: 0,
				max: 100
			})
		}]
	},
	options: {
		plugins: {
			datalabels: {
					backgroundColor: function(context) {
						return gradientStroke;
					},
					borderRadius: 4,
					color: 'white',
					font: {
						weight: 'bold'
					},
					formatter: Math.round
				}
			},
			scales: {
				yAxes: [{
					stacked: true
				}]
			}
		}
	});
</script>

Here is the result:

canvas

There is a similar issue in Chart.js, fixed in the master .

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
simonbrunelcommented, Feb 24, 2019

Thanks @stockiNail for the extra details about the context, it looks really great.

1reaction
simonbrunelcommented, Feb 22, 2019

In the meantime, here is a “non ideal” workaround if you use (or want to use) a color lib such as chroma, etc., you could interpolate the background color using scriptable options (jsfiddle).

Read more comments on GitHub >

github_iconTop Results From Across the Web

linear-gradient not working in Chrome - Stack Overflow
After a good amount of search I am stuck with linear-gradient which works in Firefox but not in Chrome.
Read more >
Background Gradient Not Working - HTML & CSS - SitePoint
I have been trying to add some gradient to the body of my template and after a couple of goes it still does...
Read more >
linear-gradient() - CSS: Cascading Style Sheets | MDN
The linear-gradient() CSS function creates an image consisting of a progressive transition between two or more colors along a straight line.
Read more >
Solved: illustrator gradient tool not working? - 8570511
If you select another object and the gradient annotator disappears, then that item is filled with a solid color and not a gradient....
Read more >
Gradient not working in Safari - Support - Themeco Forum
I have a layout page that is using some CSS gradient overlays. ... The gradients show fine on Chrome but in Safari they...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found