admin管理员组文章数量:1390775
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 ponent I wrote.
What I want to achieve:
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"],
ponents: {},
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 ponent several times per page.
All configuration (options, labels, etc.) gets done in the parent ponent, which uses the wrapper.
Is there a way to get the canvas context from the wrapper to the parent ponent, 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
is undefined in the parent ponent, which prevents me from getting the context, so I can't create a gradient.
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 ponent I wrote.
What I want to achieve:
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"],
ponents: {},
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 ponent several times per page.
All configuration (options, labels, etc.) gets done in the parent ponent, which uses the wrapper.
Is there a way to get the canvas context from the wrapper to the parent ponent, 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
is undefined in the parent ponent, which prevents me from getting the context, so I can't create a gradient.
-
1
hey, I'm trying achieve something similar, I have a doubt, how exactly do you pass
this.gradient
to your chartData object? – Gagan Ganapathy Ajjikuttira Commented Jul 21, 2020 at 14:54
1 Answer
Reset to default 4You can access this.$refs.canvas
in your chart ponent.
If you want to access it from your parent ponent there are several ways.
The fast unclean way is over this.$children https://v2.vuejs/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 ponent.
本文标签:
版权声明:本文标题:javascript - (Vue, ChartJS) Create gradient background for chart from child component canvas context - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744665307a2618493.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论