问题描述
您好,我正在尝试通过调用 API 来使用 chartjs 显示不同的图表.下面的代码显示了我如何格式化chart.vue
Chart.vue:
<模板><div 类=图表容器"风格=位置:相对;高度:40vh;宽度:100%;"><插槽名称=test1"></slot><插槽名称=test2"></slot></div></模板><脚本>导出默认{名称:'图表',数据 () {返回 {日期: [],挑战: [],数据: []}},挂载(){this.check(8, 'chart_8')this.check(7, 'chart_7')},方法: {检查(身份证,姓名){this.$http.get(`/api_chart/${ id }/full`).then((响应) => {this.date = response.data.datethis.challenge = response.data.challengethis.data = this.date.map((日期, 索引) => ({x:新日期(日期 * 1000),y: this.challenge[索引]}))const ctx = document.getElementById([name]).getContext('2d')让 myChart = new Chart(ctx, {类型:'线',数据: {数据集:[{标签:挑战",数据:this.data,边框颜色:'#EA5455',}]},选项: {线张力:0,保持纵横比:假,秤:{y轴:[{刻度标签:{显示:假},滴答声:{beginAtZero:是的,回调(值){返回`${value}%`}}}],x轴:[{类型:'时间',时间: {单位:'月'},刻度标签:{显示:真,}}]}}})})}}}</脚本>App.vue:
<模板><div class="为了显示chart1"><图表显示><画布槽=test1";id="chart_7";></画布></图表显示></div><div class="为了显示chart1"><图表显示><画布槽=test2";id="chart_8";></画布></图表显示></div></模板><脚本>从 './Chart.vue' 导入图表显示导出默认{组件:{图表显示}}</脚本>如您所见,我已经共享了我的 Chart.vue 和 App.vue,我可以在浏览器中看到我的图表,但是每当我运行代码或刷新页面时,图表都会闪烁并停止.然后在我的控制台中出现以下错误:
请有人帮我解决这个问题,并请告诉我是否应该对我的代码进行任何更改来解决它.请把修改码发给我.
正如我在评论中所写,图表被渲染了两次.这会导致闪烁.
//每次使用<chart-display>,都会渲染2个图表,也就是说图表1会渲染//自身和图表 2,char 2 渲染自身和图表 1,这在 Vue 中通常是一个不好的模式挂载(){this.check(8, "chart_8");this.check(7, "chart_7");}进行以下更改:
ChartDisplay.vue
<template>App.vue
<template><div class="为了显示chart1"><图表显示/></div></div></模板><脚本>从./ChartDisplay.vue"导入 ChartDisplay;导出默认{组件:{ ChartDisplay },};</脚本>在 沙盒上查看它p>
Hello i am trying to display different charts using the chartjs by calling the API. Below code shows how i have formatted the chart.vue
Chart.vue:
<template>
<div class="chart-container" style="position: relative; height: 40vh; width:100%;">
<slot name="test1"></slot>
<slot name="test2"></slot>
</div>
</template>
<script>
export default {
name: 'charts',
data () {
return {
date: [],
challenge: [],
data: []
}
},
mounted () {
this.check(8, 'chart_8')
this.check(7, 'chart_7')
},
methods: {
check (id, name) {
this.$http.get(`/api_chart/${ id }/full`)
.then((response) => {
this.date = response.data.date
this.challenge = response.data.challenge
this.data = this.date.map((date, index) => ({
x: new Date(date * 1000),
y: this.challenge[index]
}))
const ctx = document.getElementById([name]).getContext('2d')
let myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [
{
label: 'Challenge',
data: this.data,
borderColor: ' #EA5455',
}
]
},
options: {
lineTension: 0,
maintainAspectRatio: false,
scales: {
yAxes: [
{
scaleLabel: {
display: false
},
ticks: {
beginAtZero: true,
callback (value) {
return `${value}%`
}
}
}
],
xAxes: [
{
type: 'time',
time: {
unit: 'month'
},
scaleLabel: {
display: true,
}
}
]
}
}
})
})
}
}
}
</script>
App.vue:
<template>
<div class="In order to display chart1">
<chart-display> <canvas slot="test1" id="chart_7" ></canvas> </chart-display>
</div>
<div class="In order to display chart1">
<chart-display> <canvas slot="test2" id="chart_8" ></canvas> </chart-display>
</div>
</template>
<script>
import chart-display from './Chart.vue'
export default {
component: {chart-display}
}
</script>
As you can see i have shared my Chart.vue and App.vue, i am able to see my chart in the browser, but whenever i run the code or refresh the page, the charts flickers and stops. And then in my console i get below error:
Please someone help me to get rid of this issue, and please tell me if any changes i should do in my code to solve it. Please send me the modification code.
解决方案 As I wrote in my comment, the charts are rendered twice. This causes flickering.
// every time you use <chart-display>, 2 charts are rendered, this means chart 1 renders
// itself and chart 2, char 2 renders itself and chart 1, this is a bad pattern in Vue in general
mounted() {
this.check(8, "chart_8");
this.check(7, "chart_7");
}
Make the following changes:
ChartDisplay.vue
<template>
<div
class="chart-container"
style="position: relative; height: 40vh; width: 100%"
>
<canvas ref="chart_7"></canvas>
<canvas ref="chart_8"></canvas>
</div>
</template>
<script>
import Chart from "chart.js";
export default {
name: "ChartDisplay",
data() {
return {
date: [],
challenge: [],
data: [],
// save charts in an array
charts: [],
// charts options
options: {
lineTension: 0,
maintainAspectRatio: false,
scales: {
yAxes: [
{
scaleLabel: {
display: false,
},
ticks: {
beginAtZero: true,
callback(value) {
return `${value}%`;
},
},
},
],
xAxes: [
{
type: "time",
time: {
unit: "month",
},
scaleLabel: {
display: true,
},
},
],
},
},
};
},
mounted() {
this.render(7, this.$refs.chart_7);
this.render(8, this.$refs.chart_8);
},
methods: {
render(id, ctx) {
this.fetchData(id).then((response) => {
let data = response.date.map((date, index) => ({
x: new Date(date * 1000),
y: response.challenge[index],
}));
this.charts.push(
new Chart(ctx, {
type: "line",
data: {
datasets: [
{
label: "Challenge",
data: data,
borderColor: " #EA5455",
},
],
},
options: this.options,
})
);
});
},
fetchData(id) {
return this.$http.get(`/api_chart/${ id }/full`);
},
},
beforeDestroy() {
this.charts.forEach((chart) => chart.destroy());
},
};
</script>
<style >
[v-cloak] {
display: none;
}
</style>
App.vue
<template>
<div>
<div class="In order to display chart1">
<chart-display/>
</div>
</div>
</template>
<script>
import ChartDisplay from "./ChartDisplay.vue";
export default {
components: { ChartDisplay },
};
</script>
See it on sandbox
这篇关于Vuejs上下文中chartjs的闪烁图表和getcontext错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20
前端开发问题
13
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)...
2024-04-20
前端开发问题
6
How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社...
2024-04-20
前端开发问题
6
How to use coffeescript in developing web-sites?(如何在开发网站时使用coffeescript?)...
2024-04-20
前端开发问题
10
Expose a javascript api with coffeescript(使用 coffeescript 公开一个 javascript api)...
2024-04-20
前端开发问题
15
Coffeescript wrapping files in a function(Coffeescript 在函数中包装文件)...
2024-04-20
前端开发问题
8
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序

大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)