1. <tfoot id='UIMei'></tfoot>
        <bdo id='UIMei'></bdo><ul id='UIMei'></ul>

        <small id='UIMei'></small><noframes id='UIMei'>

      1. <i id='UIMei'><tr id='UIMei'><dt id='UIMei'><q id='UIMei'><span id='UIMei'><b id='UIMei'><form id='UIMei'><ins id='UIMei'></ins><ul id='UIMei'></ul><sub id='UIMei'></sub></form><legend id='UIMei'></legend><bdo id='UIMei'><pre id='UIMei'><center id='UIMei'></center></pre></bdo></b><th id='UIMei'></th></span></q></dt></tr></i><div id='UIMei'><tfoot id='UIMei'></tfoot><dl id='UIMei'><fieldset id='UIMei'></fieldset></dl></div>

        <legend id='UIMei'><style id='UIMei'><dir id='UIMei'><q id='UIMei'></q></dir></style></legend>

        (Vue, ChartJS) 从子组件画布上下文为图表创建渐变背景

        (Vue, ChartJS) Create gradient background for chart from child component canvas context((Vue, ChartJS) 从子组件画布上下文为图表创建渐变背景)

            <tfoot id='cWG7T'></tfoot>

              <small id='cWG7T'></small><noframes id='cWG7T'>

            • <legend id='cWG7T'><style id='cWG7T'><dir id='cWG7T'><q id='cWG7T'></q></dir></style></legend>

                  <bdo id='cWG7T'></bdo><ul id='cWG7T'></ul>
                    <tbody id='cWG7T'></tbody>
                  <i id='cWG7T'><tr id='cWG7T'><dt id='cWG7T'><q id='cWG7T'><span id='cWG7T'><b id='cWG7T'><form id='cWG7T'><ins id='cWG7T'></ins><ul id='cWG7T'></ul><sub id='cWG7T'></sub></form><legend id='cWG7T'></legend><bdo id='cWG7T'><pre id='cWG7T'><center id='cWG7T'></center></pre></bdo></b><th id='cWG7T'></th></span></q></dt></tr></i><div id='cWG7T'><tfoot id='cWG7T'></tfoot><dl id='cWG7T'><fieldset id='cWG7T'></fieldset></dl></div>
                  本文介绍了(Vue, ChartJS) 从子组件画布上下文为图表创建渐变背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想给我的图表一个渐变背景颜色,但我无法访问画布上下文,因为我的图表在我编写的包装器组件中呈现.

                  I want to give my chart a gradient background color, but I can't access the canvas context as my chart gets rendered in a wrapper component I wrote.

                  我想要实现的目标:

                  我的实际包装器看起来像这样:

                  My actual wrapper rather looks like this:

                  <script>
                  import { Line, mixins } from "vue-chartjs";
                  const { reactiveProp } = mixins;
                  
                  export default {
                    extends: Line,
                    mixins: [reactiveProp],
                    props: ["options"],
                    components: {},
                    mounted() {
                      // this.chartData is created in the mixin
                      this.renderChart(this.chartData, this.options);
                    }
                  };
                  </script>
                  

                  我将包装器用于不同的折线图,并让它的父级传递相关数据 - 有时每页多次使用包装器组件.

                  I'm using my wrapper for different line charts and let it's parent pass down the relevant data - sometimes using the wrapper component several times per page.

                  所有配置(选项、标签等)都在使用包装器的父组件中完成.

                  All configuration (options, labels, etc.) gets done in the parent component, which uses the wrapper.

                  有没有办法将画布上下文从包装器获取到使用包装器的父组件?

                  Is there a way to get the canvas context from the wrapper to the parent component, which uses the wrapper?

                  要创建渐变,你需要这样的东西:

                  To create the gradient, you need something like this:

                  this.gradient = this.$refs.canvas
                    .getContext("2d")
                    .createLinearGradient(0, 0, 0, 450);
                  
                  this.gradient.addColorStop(0, "rgba(255, 0,0, 0.5)");
                  this.gradient.addColorStop(0.5, "rgba(255, 0, 0, 0.25)");
                  this.gradient.addColorStop(1, "rgba(255, 0, 0, 0)");
                  

                  ..but this.$refs.canvas 在父组件中未定义,这使我无法获取上下文,因此无法创建渐变.

                  ..but this.$refs.canvas is undefined in the parent component, which prevents me from getting the context, so I can't create a gradient.

                  推荐答案

                  您可以在图表组件中访问 this.$refs.canvas.如果你想从你的父组件访问它,有几种方法.

                  You can access this.$refs.canvas in your chart component. If you want to access it from your parent component there are several ways.

                  快速不干净的方法就结束了.$childrenhttps://vuejs.org/v2/api/#vm-children

                  The fast unclean way is over this.$children https://vuejs.org/v2/api/#vm-children

                  更好的方法是将渐变内容设置为自己的方法,并将所有数据作为道具传递给图表组件.

                  A better way would be to setup the gradient stuff into a own method and pass in all data as props to your chart component.

                  这篇关于(Vue, ChartJS) 从子组件画布上下文为图表创建渐变背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ChartJS Version 3 - common legend for multiple line charts(ChartJS 版本 3 - 多个折线图的常用图例)
                  Charts.js scales yaxes ticks min max doesnt work(Charts.js 缩放 yaxes 刻度 min max 不起作用)
                  How to expand the slice of donut chart in chartjs?(如何在chartjs中扩展圆环图的切片?)
                  Chart.js yAxes Ticks stepSize not working (fiddle)(Chart.js yAxes Ticks stepSize 不起作用(小提琴))
                  Rounded corners on chartJS v.2 - bar charts (with negative values)(chartJS v.2 上的圆角 - 条形图(带负值))
                  How to hide value in Chart JS bar(如何在 Chart JS 栏中隐藏值)

                          <tbody id='5lGJJ'></tbody>

                          <small id='5lGJJ'></small><noframes id='5lGJJ'>

                          <legend id='5lGJJ'><style id='5lGJJ'><dir id='5lGJJ'><q id='5lGJJ'></q></dir></style></legend>

                          <tfoot id='5lGJJ'></tfoot>
                          • <bdo id='5lGJJ'></bdo><ul id='5lGJJ'></ul>
                          • <i id='5lGJJ'><tr id='5lGJJ'><dt id='5lGJJ'><q id='5lGJJ'><span id='5lGJJ'><b id='5lGJJ'><form id='5lGJJ'><ins id='5lGJJ'></ins><ul id='5lGJJ'></ul><sub id='5lGJJ'></sub></form><legend id='5lGJJ'></legend><bdo id='5lGJJ'><pre id='5lGJJ'><center id='5lGJJ'></center></pre></bdo></b><th id='5lGJJ'></th></span></q></dt></tr></i><div id='5lGJJ'><tfoot id='5lGJJ'></tfoot><dl id='5lGJJ'><fieldset id='5lGJJ'></fieldset></dl></div>