0
function addChart(key, colors) {
  const chartData = getChartData(currentYear.value, key);

  const chart = {
    chart: null,
    data: chartData,
    colors: colors,
    key: key
  };

  charts.value.push(chart);
}

onMounted(async () => {
  addChart("key1", colors1);
  addChart("key2", colors1);
  addChart("key3", colors1);
});

<template>
  <div id="main">
    <div id="chartContainers" class="chart-container">
      <div v-for="(chartData, index) in charts" :key="index" class="chart-wrapper">
        <div class="chart-title">{{ chartData.key }}</div>
        <div class="canvas-container">
          <Doughnut
              :data="{ labels: ['A', 'B', 'C'], datasets: [{ data: chartData.data, backgroundColor: chartData.colors }] }"
              :options="{ responsive: true, maintainAspectRatio: false, animation: { animateRotate: true, animateScale: true }, plugins: { tooltip: { enabled: true } } }"
          />
        </div>
      </div>
    </div>
  </div>
</template>

I'm working on a component that consists of multiple charts. When all is done, the user is supposed to be able to dynamically add charts to the page. However right now, I'm unable to resize the charts so they all fit their parent container. When the component is rendered on my page, the charts added at the beginning appear really small and get progressively larger, with the last chart added being its regular size. How do I make it so all my charts are the same size and fit their container?

2
  • Insofar chart.js is concerned, you have already done what is required for the chart to resize to the container div, that is set options responsive: true, maintainAspectRatio: false. Of course, the doughnut chart's ability to resize is limited by the fact that the doughnut is circular. Other than that you have to do standard debugging by adding borders and/or transparent backgrounds on the div to verify that they are scaling as you expected. Commented Jun 12, 2024 at 11:52
  • And if you want an answer on SO, repeating the question doesn't help, you have to make the effort to provide a runnable example, possibly in a stackblitz or codesanbox, since this is a complex structure and it's very unlikely that someone would be able to run it in their imagination, especially without data and css. Commented Jun 12, 2024 at 11:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.