javascript - How to add gradient background to Line Chart [vue-chart.js] - Stack Overflow

how can I add a gradient backgroundColor to a line chart, I pass the data as props.this is my LineChart

how can I add a gradient backgroundColor to a line chart, I pass the data as props.

this is my LineChart.vue ponent:

import { Line, mixins } from 'vue-chartjs'
  const { reactiveProp } = mixins

  export default {
    extends: Line,
    mixins: [reactiveProp],
    data: () => ({
      gradient: null,
      options: {
        layout: {
          padding: {
            bottom: -20,
            left: -20,
          }
        },
        maintainAspectRatio: false,
        legend: {
          display: false
        },
        scales: {
          yAxes: [{
            ticks: {
              beginAtZero: true,
              min: 0,
              display: false,
            },
            gridLines: {
              drawBorder: false,
              showBorder: false,
              display: false,
            },
          }],
          xAxes: [{
            gridLines: {
              drawBorder: false,
              showBorder: false,
              display: false,
            },
            ticks: {
              display: false
            }
          }]
        }
      }
    }),
    mounted () {
      // this.chartData is created in the mixin.
      // If you want to pass options please create a local options object.

      this.renderChart(this.chartData, this.options)
    }
  }

and this is the chartdata initialization in Home.vue:

    this.chartdata3 = {
      labels: ['bla', 'bla', 'bla', 'bla'],
      datasets: [
        {
          backgroundColor: "rgba(42, 115, 237, 0.9)",
          data: ['200', '400', '100', '700'],
        }
      ]
    };

How can I have a gradient instead of solid color in the background of the line chart?

how can I add a gradient backgroundColor to a line chart, I pass the data as props.

this is my LineChart.vue ponent:

import { Line, mixins } from 'vue-chartjs'
  const { reactiveProp } = mixins

  export default {
    extends: Line,
    mixins: [reactiveProp],
    data: () => ({
      gradient: null,
      options: {
        layout: {
          padding: {
            bottom: -20,
            left: -20,
          }
        },
        maintainAspectRatio: false,
        legend: {
          display: false
        },
        scales: {
          yAxes: [{
            ticks: {
              beginAtZero: true,
              min: 0,
              display: false,
            },
            gridLines: {
              drawBorder: false,
              showBorder: false,
              display: false,
            },
          }],
          xAxes: [{
            gridLines: {
              drawBorder: false,
              showBorder: false,
              display: false,
            },
            ticks: {
              display: false
            }
          }]
        }
      }
    }),
    mounted () {
      // this.chartData is created in the mixin.
      // If you want to pass options please create a local options object.

      this.renderChart(this.chartData, this.options)
    }
  }

and this is the chartdata initialization in Home.vue:

    this.chartdata3 = {
      labels: ['bla', 'bla', 'bla', 'bla'],
      datasets: [
        {
          backgroundColor: "rgba(42, 115, 237, 0.9)",
          data: ['200', '400', '100', '700'],
        }
      ]
    };

How can I have a gradient instead of solid color in the background of the line chart?

Share Improve this question asked Dec 11, 2019 at 22:05 Anas LatiqueAnas Latique 4081 gold badge7 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You should be able to add ref="chart" to your canvas. After that you can declare the new variable that will store the background color.

Like this:

var gradientFill = this.$refs.chart.createLinearGradient(500, 0, 100, 0);
gradientFill.addColorStop(0, "rgba(128, 182, 244, 0.6)");
gradientFill.addColorStop(1, "rgba(244, 144, 128, 0.6)");

Then you should be able to use backgroundColor: gradientFill in your chart options.

Please put this code in created or mounted methods in Vue lifecycle as needed to make sure it renders properly when ponent displays.

That should be it.

Yo can try and play with it here: https://codepen.io/plavookac/pen/RKjNEV

I hope this helps.

Let me know if you have any questions.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745066830a4609304.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信