Closed scryptoking closed 5 years ago
update:
I replaced the chart for a simple table and the table does show different data for each entry. This means that the issue is within highcharts
Hi @bddexter ,
Thank you for reporting the issue. Could you try to produce the minimal working example where the issue occurs? It would be much convenient to debug it using minified live copy of your project.
Kind regards!
@Denyllon Updated from version 1.2.0 to 1.3.5. Solved the issue for some reason. Maybe closed now.
I have the same issue with highcharts-vue and v-for
// PieChart.vue
<template>
<highcharts :options="chartOptions" :ref="title" />
</template>
<script>
import { Chart } from 'highcharts-vue';
export default {
components: {
highcharts: Chart,
},
props: {
title: {
type: String,
default: '',
},
data: {
type: Array,
default: () => [],
},
},
computed: {
chartOptions() {
console.log(this.title);
let options = Object.assign({}, piechartDefaultOptions);
if (this.title) {
options.title.text = this.title;
}
if (this.data.length) options.series[0].data = this.data;
return options;
},
},
};
const piechartDefaultOptions = {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie',
},
title: {
text: 'Pie Chart',
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>',
},
accessibility: {
point: {
valueSuffix: '%',
},
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
},
},
},
series: [
{
name: 'Brands',
colorByPoint: true,
data: [],
},
],
};
</script>
<style lang="scss" scoped></style>
// RiskCharts.vue
<template>
<v-row>
<v-col v-for="(item, i) in risks" :key="i" sm="6" md="4" lg="3">
<pie-chart :key="`chart-${i}`" :title="item.title" :data="item.data" />
</v-col>
</v-row>
</template>
<script>
export default {
components: {
PieChart: () => import('./PieChart'),
},
created() {
this.risks = risks;
},
};
const risks = [
{
title: 'Conservative',
data: [
{
name: 'Bonds',
y: 80,
},
{
name: 'Equities',
y: 20,
},
],
},
{
title: 'Cautious',
data: [
{
name: 'Bonds',
y: 70,
},
{
name: 'Equities',
y: 30,
},
],
},
{
title: 'Balanced',
data: [
{
name: 'Bonds',
y: 50,
},
{
name: 'Equities',
y: 50,
},
],
},
{
title: 'Assertive',
data: [
{
name: 'Bonds',
y: 30,
},
{
name: 'Equities',
y: 70,
},
],
},
{
title: 'Aggressive',
data: [
{
name: 'Bonds',
y: 20,
},
{
name: 'Equities',
y: 80,
},
],
},
];
</script>
You can see the live site here: https://highcharts-vue-v-for-issue.netlify.app/
All of the highcharts have the last data in the array. The issue is the same whether or not having "ref" in PieChart.vue.
Hi @yarielg ,
Sorry for a bit of late in reply. Could you reproduce the app you attached above using the Codesandbox/Stackblitz platform, so that I would be able to take a look on the code and its structure? Unfortunately, it's a bit hard to debug the app deployed on Netlify.
Kind regards!
Hello, @Denyllon
Yes, sure, thank you https://codesandbox.io/s/small-hill-6fe7h?file=/src/App.vue
Could you check this?
Hello @yarielg ,
Thank you for your patience.
I've just checked it, and found the direct source of the problem within your implementation of the PieChart.vue
file, where the shallow copy of the piechartDefaultOptions
is done through computed property definition. Please note that the Object.assign
copies only the first level of the object, but keeps all internal object references as they're, so all charts refer to the same series
and text
objects.
I also applied quick solution into your code, where used the object spread operator, and now it works correctly. Please take a look on it.
chartOptions() {
let options = {
...piechartDefaultOptions,
title: {
...piechartDefaultOptions.title,
text: this.title || ""
},
series: [{
...piechartDefaultOptions.series[0],
data: this.data || []
}]
}
return options;
}
Live example: https://codesandbox.io/s/dawn-sky-ktj1s
Kind regards!
Hello, @Denyllon You're right. Thank you.
Hi,
What I'm trying to do is to re-use a vue component with a prop that gets it's data from a
json
object. My problem is, that all the components contain the same data instead of the data based on the index of dejson
object.My
json
data looks like this (snippet):My main component (which basically holds a v-for loop to fill the component that I want to re-use) looks like this:
And my c-srchartindicatorfee component looks like this:
In this last component I want to fill the
setChartData
prop with theindicatorChartData array
from myjson
object.I would expect that (since vue components can be re-used) every generated
c-srchartindicatorfee
component would hold it's own data (indicatorChartData
) from thejson
via thesetChartData
prop, but in this case every component holds the data of the last entry within myjson
object.