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

前端 未结 4 1077
南方客
南方客 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(
              
    
        )
      }
    }
    

    this is only my second answer so please forgive me if I have made any mistakes in writing

提交回复
热议问题