0

I want to have multiple highcharts with different data in my page , without having to repeat the highchart code.

here is how i define my highchart

chartOptions: {
    chart: {
      type: "pie"
    },
    title: {
      text: ""
    },

    credits: {
      enabled: false
    },

    plotOptions: {
      pie: {
        allowPointSelect: true,
        cursor: "pointer",
        dataLabels: {
          enabled: true,
          format: '<b>{point.name}</b>: {point.percentage:.1f} %'
        }
      }
    },
    series: [
      { 
        name: 'Comparison',
        data: [],
      },
    ]
  },

I call it to the html like this-

<highcharts :options="chartOptions"  id="chart1"></highcharts>

I use the event bus listener to send the data to the highchart series

 EventBus.$on("btn-clicked", data => {
  this.chartOptions.series[0].data = data.newData;
});

Since i am using the highchart-vuejs wrapper i am able to repeat the highcharts, but all the charts will get the same data.Is there a way that i could send the data to a particular chart so it is different from the others?

2
  • Is there any particular reason you're not passing the data as props? Commented May 27, 2020 at 7:38
  • im new to vuejs so i am not very familiar with a lot of things, so whatever solution i find online, i integrate it . Will passing it as props help me in my issue? Commented May 27, 2020 at 7:41

1 Answer 1

2

You're passing down chartOptions into the highcharts component. If you've defined this data on the parent component, Vue will have made it reactive, so when you change the data the chart will update automatically.

Below is a basic example of how this would work:

<template>
  <div>
    <highcharts :options="chartOptions[0]"></highcharts>
    <highcharts :options="chartOptions[1]"></highcharts>
    <button @click="changeData">Change data for first chart</button>
  </div>
</template>

<script>
import { Chart } from "highcharts-vue";

export default {
  components: {
    highcharts: Chart
  },
  data() {
    return {
      chartOptions: [
        {
          series: [
            {
              data: [1, 2, 3]
            }
          ]
        },
        {
          series: [
            {
              data: [4, 5, 6]
            }
          ]
        },
      ]
    }
  },
  methods: {
    changeData() {
      this.chartOptions[0].series[0].data = [4, 8, 1];
    }
  }
};
</script>

EDIT:

To create multiple charts with the same options, you could create a custom chart component, and pass in just the data series as a prop:

<template>
  <highcharts :options="chartOptions"></highcharts>
</template>

<script>
import { Chart } from "highcharts-vue";

export default {
  name: 'CustomPie',
  components: {
    highcharts: Chart
  },
  props: ['data'],
  data() {
    return {
      chartOptions: {
        chart: {
          type: "pie"
        },
        title: {
          text: ""
        },
        credits: {
          enabled: false
        },
        plotOptions: {
          pie: {
            allowPointSelect: true,
            cursor: "pointer",
            dataLabels: {
              enabled: true,
              format: '<b>{point.name}</b>: {point.percentage:.1f} %'
            }
          }
        },
        series: [
          { 
            name: 'Comparison',
            data: this.data,
          },
        ]
      },
    }
  },
  watch: {
    data(newVal) {
      this.chartOptions.series[0].data = newVal;
    }
  }
};
</script>

Note that you have to set up a watcher on the data prop, in order to update the components chartOptions when the data changes.

And your parent component (where you're displaying the charts) would look something like this:

<template>
  <div>
    <CustomPie :data="chartData1" />
    <CustomPie :data="chartData2" />
    <button @click="changeData">Change data for first chart</button>
  </div>
</template>

<script>
import CustomPie from "./CustomPie";

export default {
  components: {
    CustomPie
  },
  data() {
    return {
      chartData1: [1,2,3],
      chartData2: [4,5,6]
    }
  },
  methods: {
    changeData() {
      this.chartData1 = [4, 8, 1];
    }
  }
};
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

hey this helped a lot thanks, but now when i try putting the remaining chart code such as plotOptions, credits etc, i get an error and the only was to resolve it was to put it for each of the series, which would be basically repeating the chart code, is there any way around this?
I've edited my answer to show how you could get around this with a custom component. Hope that helps!
Hey, am sorry for continuously asking, new to vuejs, i didnt understand how this helps with repeating chart code with different data, somehow the first answer u gave helped me better with my issue, only plotOptions was an issue. Is there a work around to the first answer as it helps easier with my issue
thnakyou this is perfect

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.