<small id='0lvCa'></small><noframes id='0lvCa'>

  1. <tfoot id='0lvCa'></tfoot>
    • <bdo id='0lvCa'></bdo><ul id='0lvCa'></ul>

      <legend id='0lvCa'><style id='0lvCa'><dir id='0lvCa'><q id='0lvCa'></q></dir></style></legend>

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

      Vuejs上下文中chartjs的闪烁图表和getcontext错误

      Flickering of charts and getcontext error with chartjs in the context of Vuejs(Vuejs上下文中chartjs的闪烁图表和getcontext错误)

        <legend id='f9HjI'><style id='f9HjI'><dir id='f9HjI'><q id='f9HjI'></q></dir></style></legend><tfoot id='f9HjI'></tfoot>

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

                <bdo id='f9HjI'></bdo><ul id='f9HjI'></ul>
              • <small id='f9HjI'></small><noframes id='f9HjI'>

                本文介绍了Vuejs上下文中chartjs的闪烁图表和getcontext错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                您好,我正在尝试通过调用 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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                ChartJS Version 3 - common legend for multiple line charts(ChartJS 版本 3 - 多个折线图的常用图例)
                Chart.js yAxes Ticks stepSize not working (fiddle)(Chart.js yAxes Ticks stepSize 不起作用(小提琴))
                How to hide value in Chart JS bar(如何在 Chart JS 栏中隐藏值)
                Show data values in Chart.js bars (version 3)(在 Chart.js 条形图中显示数据值(版本 3))
                Chart.js show x-axis data from right to left(Chart.js 从右到左显示 x 轴数据)
                ChartJS - Finding the minimum and maximum labels that are inside a pan (When zooming)(ChartJS - 查找平底锅内的最小和最大标签(缩放时))
                      <bdo id='6eCCd'></bdo><ul id='6eCCd'></ul>
                      <legend id='6eCCd'><style id='6eCCd'><dir id='6eCCd'><q id='6eCCd'></q></dir></style></legend>
                      <i id='6eCCd'><tr id='6eCCd'><dt id='6eCCd'><q id='6eCCd'><span id='6eCCd'><b id='6eCCd'><form id='6eCCd'><ins id='6eCCd'></ins><ul id='6eCCd'></ul><sub id='6eCCd'></sub></form><legend id='6eCCd'></legend><bdo id='6eCCd'><pre id='6eCCd'><center id='6eCCd'></center></pre></bdo></b><th id='6eCCd'></th></span></q></dt></tr></i><div id='6eCCd'><tfoot id='6eCCd'></tfoot><dl id='6eCCd'><fieldset id='6eCCd'></fieldset></dl></div>

                        <small id='6eCCd'></small><noframes id='6eCCd'>

                        1. <tfoot id='6eCCd'></tfoot>

                            <tbody id='6eCCd'></tbody>