Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Generating charts in for loop not working as expected #800

Closed
1 task
jrahim54 opened this issue Apr 4, 2022 · 6 comments
Closed
1 task

[Bug]: Generating charts in for loop not working as expected #800

jrahim54 opened this issue Apr 4, 2022 · 6 comments

Comments

@jrahim54
Copy link

jrahim54 commented Apr 4, 2022

Would you like to work on a fix?

  • Check this if you would like to implement a PR, we are more than happy to help you go through the process.

Current and expected behavior

When generating a list of graphs in a for loop, only the very last one is being generated.

          <div v-for="graph in graphs" :key="graph.title">
            <ChartLine
              :chartId="graph.title" class="chart"
              :chart-data="graph"></ChartLine>
          </div>

In the above the ChartLine component implements the LineChart below, but only the very last graph is generated and displayed, this was working in version 3.5.12 but not anymore. Below is my template for the chart.

<template>
  <new-chart
    :chart-options="chartOptions"
    :chart-data="computedChartData"
    :chart-id="chartId"
    :dataset-id-key="chartId"
    :plugins="plugins"
    :css-classes="cssClasses"
    :styles="styles"
    :width="width"
    :height="height"
  />
</template>

Reproduction

https://codesandbox.io/s/distracted-currying-jq6bh1?file=/src/App.vue

chart.js version

v3.7.1

vue-chartjs version

v4.0.4

Possible solution

No response

@zbateson
Copy link

zbateson commented Apr 4, 2022

This is happening to me too having two charts on the same page. I scoured through the documentation trying to figure out if I missed something and couldn't find anything obvious.

@apertureless
Copy link
Owner

Have you tried passing a unique chart-id ?

@zbateson
Copy link

zbateson commented Apr 4, 2022

I did, yes

@zbateson
Copy link

zbateson commented Apr 4, 2022

Here's some more info if it helps:

Edit: I can pare it down to just the chart-data and chart-id and still happens with two charts on the same page.

<bar-chart
          :chart-data="programChartData"
          chart-id="program-chart"
        ></bar-chart>

Second:

<bar-chart
          :chart-data="totalsChartData"
          chart-id="totals-chart"
        ></bar-chart>

In developer tools I can see both 'canvas' elements have different IDs:

<div class=""><canvas id="program-chart" style="box-sizing: border-box;" width="400" height="400"></canvas></div>
<div class=""><canvas id="totals-chart" style="display: block; box-sizing: border-box; height: 920px; width: 920px;" width="920" height="920"></canvas></div>

For some reason the width of the first one doesn't get set also (viewport is 920).

@thabarbados
Copy link
Collaborator

@jrahim54 @zbateson Can you provide a repo of your error? In my sandbox, charts in a loop are drawn without errors

@zbateson
Copy link

zbateson commented Apr 5, 2022

Hi @thabarbados -- thanks, I've copied what you've done and moved it over to recreate the issue. Looks like it might be 'legacy'/vue 2 related.

https://codesandbox.io/s/vue-chartjs-legacy-issue-2ntnh9

thabarbados added a commit that referenced this issue Apr 6, 2022
fix the error, when multiple charts can't render on one page

fix #800 #801
thabarbados added a commit that referenced this issue Apr 7, 2022
fix the error, when multiple charts can't render on one page

fix #800 #801
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants