score:0
For anyone using Angular and ng2-charts, here is my solution. Chart setup was left out for brevity.
import { Component, OnInit, ViewChild, ChangeDetectorRef, AfterViewInit } from '@angular/core';
import { BaseChartDirective } from 'ng2-charts';
import { GenericLineChart } from './generic-line-chart';
@Component({
selector: 'app-stats-chart',
templateUrl: './stats-chart.component.html',
styleUrls: ['./stats-chart.component.scss']
})
export class StatsChartComponent implements OnInit, AfterViewInit {
@ViewChild(BaseChartDirective, { static: true }) chartDirective: BaseChartDirective;
constructor(private changeDetectorRef: ChangeDetectorRef) { }
ngOnInit() {
// ...setup chart
}
ngAfterViewInit() {
// set gradient
const gradient = this.chartDirective.chart.ctx.createLinearGradient(0, 0, 0, 400);
gradient.addColorStop(0, 'rgba(30, 214, 254, .3)');
gradient.addColorStop(1, 'rgba(0,0,0,0)');
this.chart.lineChartData[0].backgroundColor = gradient;
this.changeDetectorRef.detectChanges();
}
}
score:0
You could use this npm package.
https://www.npmjs.com/package/chartjs-plugin-gradient
This makes it very easy to create a gradient backgroundColor or borderColor.
Example:
const chart = new Chart(ctx, {
data: {
datasets: [{
// data
gradient: {
backgroundColor: {
axis: 'y',
colors: {
0: 'red',
50: 'yellow',
100: 'green'
}
},
borderColor: {
axis: 'x',
colors: {
0: 'black',
1: 'white',
2: 'black',
3: 'white'
}
}
}
}]
}
});
score:13
For using in react I did the following way you need to pass an id to your component and then fetch the element using that id
import React, { Component } from 'react'
import { Line } from 'react-chartjs-2'
export default class GraphComponent extends Component{
constructor(props){
super(props)
this.state = {
chartData: {}
}
}
componentDidMount(){
//your code
var ctx = document.getElementById('canvas').getContext("2d")
var gradient = ctx.createLinearGradient(0, 0, 0, 400)
gradient.addColorStop(0, 'rgba(229, 239, 255, 1)')
gradient.addColorStop(1, '#FFFFFF')
const newData = {
labels: [1, 1],
datasets: [
{
label: 'usd',
data: [1,1],
backgroundColor: gradient,
borderColor: this.props.border_color,
pointRadius: 0
}
]
}
this.setState({chartData: newData})
}
//more of your code
render(){
return(
<Line
id='canvas'//you need to give any id you want
data={this.state.chartData}
width={100}
height={30}
options={{
legend: {
display: false
}
}}
/>
)
}
}
this is only my second answer so please forgive me if I have made any mistakes in writing
score:30
Note: For those people who is using newer version (v2.7.0) of Chart.js, find out that is not working while you're copy-paste @bviale's answer back to your code base; Some property names has changed:
fillColor -> backgroundColor
strokeColor -> borderColor
pointColor -> pointBackgroundColor
pointStrokeColor -> pointBorderColor
You will need to update those property names to make it work.
Reference: https://github.com/chartjs/Chart.js/blob/master/docs/charts/line.md#dataset-properties
score:61
The link you provided was pretty clear, you have to put in the field fillColor
in datasets a linearGradient object instead of a plain color. You can do complex gradients, but here is the code of a simple one (changing the opacity of the same orange) :
var gradient = ctx.createLinearGradient(0, 0, 0, 400);
gradient.addColorStop(0, 'rgba(250,174,50,1)');
gradient.addColorStop(1, 'rgba(250,174,50,0)');
And your complete datasets :
datasets: [
{
fillColor : gradient, // Put the gradient here as a fill color
strokeColor : "#ff6c23",
pointColor : "#fff",
pointStrokeColor : "#ff6c23",
pointHighlightFill: "#fff",
pointHighlightStroke: "#ff6c23",
data : [25.0,32.4,22.2,39.4,34.2,22.0,23.2,24.1,20.0,18.4,19.1,17.4]
}
]
See it in action in this JSFiddle
Source: stackoverflow.com
Related Query
- Chart.js - add gradient instead of solid color - implementing solution
- Chart.js - Add gradient to bar chart
- Chartjs doughnut chart with gradient color
- How to add gradient background to Line Chart [vue-chart.js]
- How to add area with break on line chart with fill color
- Tooltips in Chart js are always black instead of the color of the corresponding dataset
- How to make the background color of the chart as gradient using Chart.js
- chart js bar chart add animation to change color
- How can I add background color of length bars in chart (chartJS)?
- Chart.js - Add gradient to line chart background
- Chartjs Bar Chart add background color to value labels
- Is it possible to add image behind doughnut chart with transparency color in ng2chart?
- How to add overlay color to chart.js pie chart segment?
- How to add text inside the doughnut chart using Chart.js?
- Chart area background color chartjs
- How set color family to pie chart in chart.js
- How to add an on click event to my Line chart using Chart.js
- Chart.js line chart set background color
- chartjs datalabels change font and color of text displaying inside pie chart
- Add DataSet Bar Chart - Chart.JS
- How to add an offset to a dataset in Chart js
- ChartJS add tooltip to a grouped bar chart
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How to add text in centre of the doughnut chart using Chart.js?
- Chart.js v2.6: Add arrows to pie chart output values
- Chart.js bar chart : Grid color and hide label
- Unable to parse color in line chart (angular-chart.js)
- Is it possible to add a custom font to chart js?
- set background color to save canvas chart
- How to add datas to chart js from javascript array itself?
More Query from same tag
- Angular Chart.JS Only One Canvas Rendering
- Chart.js wont display normal chart
- Add space between point of the chart and of the border chart in react-chartjs-2
- updating chart.js in ractive
- Is there a way in chartjs to display different Boolean values with an offset in Y over a common timeline?
- Generating different chart results on fly causes flickering
- Is there a way to change part of the title to another font style or set a custom title format in Chart.js?
- Plot bar graph in reactjs
- How to disable event during a state change
- Why the bar are not aligned the y axis
- How to hide/show bars differentiate by a colors and by click on labels, using chartjs bar charts?
- Chart.js Legend not showing
- Chart.js - Display data label leader lines on a pie chart
- ChartJS line chart x = y not rendering astraight line
- Pie Chart shows up only when I zoom in-out
- Chart.js stepSize not working with min
- Using chart.js version 1.0.2 and 2.0 together
- Chart.js Bar Chart change width (not duplicated to bar width questions!)
- implement chartjs zoom with buttons
- What's the most effective way to implement a radar plot with 50 points at arbitrary locations using chart.js
- Chart js cut the title and the legends
- How do I destroy/update Chart Data in this chart.js code example?
- Change <p> and <h> element when selecting something from dropdown
- How do you set up a chart.js scatter line chart through angular-chart.js?
- Dynamically update the options of a chart in chartjs using Javascript
- Set Start Value as '0' With Dynamic Data In Chart.js
- How to rewrite JavaScript currency formatting to abbreviate dollar amount
- How to make intermittent lines to represent periods of presence and absence on a given line during it's length on Multi Axis chart?
- How to hide Fields and Strike-through Legends when the data is empty or Zero in Pie/Polar/Doughnut Chart?
- How to create single value Doughnut or Pie chart using Chart.js?