Chart.js - add gradient instead of solid color - implementing solution

前端 未结 4 1080
南方客
南方客 2020-12-14 02:09

I am using Chart.js and everything is ok, but I want to replace current color background (fillColor : \"rgba(250,174,50,0.5)\") with a gradient. I have solution for replaci

相关标签:
4条回答
  • 2020-12-14 02:30

    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

    0 讨论(0)
  • 2020-12-14 02:37

    NOTE: Copied From CODEPEN

    This solution creates a nice vertical gradient graph with a red theme

        // ============================================
    // As of Chart.js v2.5.0
    // http://www.chartjs.org/docs
    // ============================================
    
    var chart    = document.getElementById('chart').getContext('2d'),
        gradient = chart.createLinearGradient(0, 0, 0, 400);
    
    gradient.addColorStop(0, 'rgba(255, 0,0, 0.5)');
    gradient.addColorStop(0.5, 'rgba(255, 0, 0, 0.25)');
    gradient.addColorStop(1, 'rgba(255, 0, 0, 0)');
    
    
    var data  = {
        labels: [ 'January', 'February', 'March', 'April', 'May', 'June' ],
        datasets: [{
                label: 'Custom Label Name',
                backgroundColor: gradient,
                pointBackgroundColor: 'white',
                borderWidth: 1,
                borderColor: '#911215',
                data: [50, 55, 80, 81, 54, 50]
        }]
    };
    
    
    var options = {
        responsive: true,
        maintainAspectRatio: true,
        animation: {
            easing: 'easeInOutQuad',
            duration: 520
        },
        scales: {
            xAxes: [{
                gridLines: {
                    color: 'rgba(200, 200, 200, 0.05)',
                    lineWidth: 1
                }
            }],
            yAxes: [{
                gridLines: {
                    color: 'rgba(200, 200, 200, 0.08)',
                    lineWidth: 1
                }
            }]
        },
        elements: {
            line: {
                tension: 0.4
            }
        },
        legend: {
            display: false
        },
        point: {
            backgroundColor: 'white'
        },
        tooltips: {
            titleFontFamily: 'Open Sans',
            backgroundColor: 'rgba(0,0,0,0.3)',
            titleFontColor: 'red',
            caretSize: 5,
            cornerRadius: 2,
            xPadding: 10,
            yPadding: 10
        }
    };
    
    
    var chartInstance = new Chart(chart, {
        type: 'line',
        data: data,
            options: options
    });
    

    check out this pen https://codepen.io/grayghostvisuals/pen/gpROOz

    0 讨论(0)
  • 2020-12-14 02:38

    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

    0 讨论(0)
  • 2020-12-14 02:39

    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

    0 讨论(0)
提交回复
热议问题