简体   繁体   English

Vue3多个Highcharts

[英]Vue3 Multiple Highcharts

I'm new to Vue, and struggling to display multiple Highcarts on a Vue3 page using vue3-highcharts.我是 Vue 的新手,并且正在努力使用 vue3-highcharts 在 Vue3 页面上显示多个 Highcarts。 I followed the Reactive Chart Example here , which works great, but I'm confused how to implement the chartOptions object for multiple charts using v-for.我在这里遵循了反应图表示例,效果很好,但我很困惑如何使用 v-for 为多个图表实现 chartOptions object。

A single chart using the following code works:使用以下代码的单个图表有效:

<vue-highcharts
    type="chart"
    :options="chartOptions"
    :redraw-on-update="true"
    :one-to-one-update="false"
    :animate-on-update="true"
    @updated="onUpdate"/>

and the associated setup / computed code以及相关的设置/计算代码

setup() {
          const seriesData = ref([25, 39, 30, 15]);
          const categories = ref(['Jun', 'Jul', 'Aug', 'Sept']);
    
          const chartOptions = computed(() => ({
            chart: {
              type: 'line',
            },
            title: {
              text: 'Number of project stars',
            },
            xAxis: {
              categories: categories.value,
            },
            yAxis: {
              title: {
                text: 'Number of stars',
              },
            },
            series: [{
              name: 'New project stars',
              data: seriesData.value,
            }],
          }));

I have tried multiple methods to use v-for to load multiple charts, here is the latest / most successful:我尝试了多种方法来使用 v-for 加载多个图表,这是最新/最成功的:

<vue-highcharts
  v-for="item in hmCharts"
  :key="item.id"
  :options="item.options"
  :redraw-on-update="true"
  :one-to-one-update="false"
  :animate-on-update="true"
  @rendered="onRender"
  @update="onUpdate"
  @destroy="onDestroy" />
  

const hmCharts = ref([
      {
        id: 'one',
        options: getChartoptions('one')
      },{
        id: 'two',
        options: getChartoptions('two')
      }])

 const seriesData  = [[0, 0, 10], [0, 1, 19], [0, 2, 8], [0, 3, 24], [0, 4, 67], [1, 0, 92], [1, 1, 58], [1, 2, 78], [1, 3, 117], [1, 4, 48], [2, 0, 35], [2, 1, 15], [2, 2, 123], [2, 3, 64], [2, 4, 52], [3, 0, 72], [3, 1, 132], [3, 2, 114], [3, 3, 19], [3, 4, 16], [4, 0, 38], [4, 1, 5], [4, 2, 8], [4, 3, 117]] 

    hmCharts.value[0].options.series[0].data = seriesData
    hmCharts.value[1].options.series[0].data = seriesData

This method will eventually load both charts, but it takes a full minute or more to load.此方法最终将加载两个图表,但加载需要整整一分钟或更长时间。 I found the following error in the console:我在控制台中发现以下错误:

runtime-core.esm-bundler.js?9e79:6620 [Vue warn]: Maximum recursive updates exceeded. runtime-core.esm-bundler.js?9e79:6620 [Vue 警告]:超过最大递归更新。 This means you have a reactive effect that is mutating its own dependencies and thus recursively triggering itself.这意味着您有一个反应性效果,它正在改变它自己的依赖关系,从而递归地触发它自己。 Possible sources include component template, render function, updated hook or watcher source function.可能的来源包括组件模板、渲染 function、更新的钩子或观察者源 function。

I have spent more time and research on this than I care to admit, and really hoping someone can help me understand what it happening here.我在这方面花费的时间和研究比我愿意承认的要多,并且真的希望有人可以帮助我了解这里发生的事情。 Thanks!谢谢!

Update: I created a codesandboxio project to reproduce the issue: codesandboxio project .更新:我创建了一个 codesandboxio 项目来重现该问题: codesandboxio project It takes awhile to load the charts, and then throws errors in the console.加载图表需要一段时间,然后在控制台中抛出错误。

Not wrapping hmCharts inside a ref seems to fix the problem:不将hmCharts包装在ref中似乎可以解决问题:

https://codesandbox.io/s/vue-highcharts-recursive-error-forked-gwqd4?file=/src/App.vue https://codesandbox.io/s/vue-highcharts-recursive-error-forked-gwqd4?file=/src/App.vue

The gist of it is a Highcharts instance can't be watched by Vue for reactivity, as it contains itself at various levels, and every time something changes (mutates) inside of it, Vue will attempt to update its dependencies, creating a never ending loop.它的要点是一个 Highcharts 实例不能被 Vue 观察到反应性,因为它包含在各个级别的自身,并且每次在它内部发生变化(变异)时,Vue 都会尝试更新它的依赖关系,创建一个永无止境的环形。

You're free to mark the data as reactive, as they do in their examples, but don't include a Highcharts instance inside a ref (or any other reactive object).您可以自由地将数据标记为反应性,就像他们在示例中所做的那样,但不要在ref (或任何其他reactive对象)中包含 Highcharts 实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM