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

Problems when updating chart through echartsInstance.setOption() #6202

Open
jeffrey-roosendaal opened this issue Jul 13, 2017 · 63 comments
Open
Labels
bug pending We are not sure about whether this is a bug/new feature. priority: high

Comments

@jeffrey-roosendaal
Copy link

jeffrey-roosendaal commented Jul 13, 2017

When updating the charts through echartsInstance.setOption(), the series are not always correctly updated/drawn.

For example, when I initialize the chart (custom made funnel, but this happens with every chart type) with 3 series, it may look like this:

chart_1

It shows 3 series, which all display correctly. Now, when I create a new chart object with 6 series, and overwrite my current chart, it changes (and animates) to:

chart_2

This, also, looks good. The problem is when updating again, to a chart with less series then the current chart. See what happens when I update the chart to 3 series:

chart_3

It draws the 3 new series over the first old 3 series, and keeps series 4, 5 and 6.


When I output the chart object to console, it shows:

"option": {
    "title": {
        "show": false
    },
    "legend": {
        "show": false,
        "data": [
            {
                "name": "Smartphone",
                "icon": "circle"
            },
            {
                "name": "Desktop",
                "icon": "circle"
            },
            {
                "name": "Tablet",
                "icon": "circle"
            }
        ],
        "selected": {
            "Smartphone": true,
            "Desktop": true,
            "Tablet": true
        }
    },
    "grid": {
        "containLabel": false,
        "top": 0,
        "right": 2,
        "bottom": 20,
        "left": 0,
        "height": 190
    },
    "toolbox": {
        "show": false
    },
    "xAxis": {
        "type": "category",
        "boundaryGap": true,
        "axisLabel": {
            "textStyle": {
                "color": "#444"
            }
        },
        "axisTick": {
            "interval": "auto",
            "lineStyle": {
                "color": "#e5e5e5"
            }
        },
        "axisLine": {
            "lineStyle": {
                "color": "#e5e5e5"
            }
        },
        "splitLine": {
            "show": false,
            "interval": 0,
            "lineStyle": {
                "color": "#e5e5e5"
            }
        },
        "data": [
            {
                "value": "Bezoeken",
                "textStyle": {
                    "align": "center",
                    "fontSize": 10
                }
            },
            {
                "value": "Actief",
                "textStyle": {
                    "align": "center",
                    "fontSize": 10
                }
            },
            {
                "value": "Winkelwagens",
                "textStyle": {
                    "align": "center",
                    "fontSize": 10
                }
            },
            {
                "value": "Bestellingen",
                "textStyle": {
                    "align": "center",
                    "fontSize": 10
                }
            }
        ]
    },
    "yAxis": [
        {
            "boundaryGap": [
                0,
                "10%"
            ],
            "type": "value",
            "show": false,
            "min": 0,
            "axisLabel": {
                "textStyle": {
                    "color": "#444",
                    "fontSize": 10
                }
            },
            "axisTick": {
                "lineStyle": {
                    "color": "#e5e5e5"
                }
            },
            "axisLine": {
                "lineStyle": {
                    "show": false,
                    "color": "#fff"
                }
            },
            "splitLine": {
                "interval": 0,
                "show": false,
                "lineStyle": {
                    "color": "#e5e5e5"
                }
            },
            "data": [
                "Smartphone",
                "Desktop",
                "Tablet"
            ]
        }
    ],
    "series": [
        {
            "name": "Smartphone",
            "type": "bar",
            "stack": "metric",
            "label": {
                "normal": {
                    "show": true,
                    "position": "top",
                    "offset": [
                        0,
                        -10
                    ],
                    "textStyle": {
                        "color": "#444"
                    }
                }
            },
            "itemStyle": {
                "normal": {
                    "color": "#1AB394"
                }
            },
            "data": [
                185,
                89,
                8,
                0
            ]
        },
        {
            "name": "Desktop",
            "type": "bar",
            "stack": "metric",
            "label": {
                "normal": {
                    "show": true,
                    "position": "top",
                    "offset": [
                        0,
                        -10
                    ],
                    "textStyle": {
                        "color": "#444"
                    }
                }
            },
            "itemStyle": {
                "normal": {
                    "color": "#23C6C8"
                }
            },
            "data": [
                104,
                51,
                9,
                1
            ]
        },
        {
            "name": "Tablet",
            "type": "bar",
            "stack": "metric",
            "label": {
                "normal": {
                    "show": true,
                    "position": "top",
                    "offset": [
                        0,
                        -10
                    ],
                    "textStyle": {
                        "color": "#444"
                    }
                }
            },
            "itemStyle": {
                "normal": {
                    "color": "#F8AC59"
                }
            },
            "data": [
                38,
                25,
                4,
                0
            ]
        }
    ],
    "tooltip": {
        "trigger": "axis",
        "position": "inside",
        "axisPointer": {
            "shadowStyle": {
                "color": "#000",
                "shadowBlur": 0,
                "opacity": 0.07
            },
            "type": "shadow"
        },
        "textStyle": {
            "fontFamily": "Roboto"
        },
        "extraCssText": "padding: 8px 15px; font-size: 13px;"
    }
}

There is not a single mention of "Motorola", "Huawei" or "Overig" in my code, but, when I add console.log(params) to charts.tooltip.formatter, it shows:

[
	{
		"componentType": "series",
		"componentSubType": "bar",
		"seriesType": "bar",
		"seriesIndex": 0,
		"seriesId": "\u0000Smartphone\u00000",
		"seriesName": "Smartphone",
		"name": "Bezoeken",
		"dataIndex": 0,
		"data": 185,
		"value": 185,
		"color": "#1AB394",
		"marker": "<span style=\"display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:#1AB394;\"></span>",
		"$vars": [
			"seriesName",
			"name",
			"value"
		],
		"axisDim": "x",
		"axisIndex": 0,
		"axisType": "xAxis.category",
		"axisId": "\u0000\u0000-\u00000",
		"axisValue": "Bezoeken",
		"axisValueLabel": "Bezoeken"
	},
	{
		"componentType": "series",
		"componentSubType": "bar",
		"seriesType": "bar",
		"seriesIndex": 1,
		"seriesId": "\u0000Desktop\u00000",
		"seriesName": "Desktop",
		"name": "Bezoeken",
		"dataIndex": 0,
		"data": 104,
		"value": 104,
		"color": "#23C6C8",
		"marker": "<span style=\"display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:#23C6C8;\"></span>",
		"$vars": [
			"seriesName",
			"name",
			"value"
		],
		"axisDim": "x",
		"axisIndex": 0,
		"axisType": "xAxis.category",
		"axisId": "\u0000\u0000-\u00000",
		"axisValue": "Bezoeken",
		"axisValueLabel": "Bezoeken"
	},
	{
		"componentType": "series",
		"componentSubType": "bar",
		"seriesType": "bar",
		"seriesIndex": 2,
		"seriesId": "\u0000Tablet\u00000",
		"seriesName": "Tablet",
		"name": "Bezoeken",
		"dataIndex": 0,
		"data": 38,
		"value": 38,
		"color": "#F8AC59",
		"marker": "<span style=\"display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:#F8AC59;\"></span>",
		"$vars": [
			"seriesName",
			"name",
			"value"
		],
		"axisDim": "x",
		"axisIndex": 0,
		"axisType": "xAxis.category",
		"axisId": "\u0000\u0000-\u00000",
		"axisValue": "Bezoeken",
		"axisValueLabel": "Bezoeken"
	},
	{
		"componentType": "series",
		"componentSubType": "bar",
		"seriesType": "bar",
		"seriesIndex": 3,
		"seriesId": "\u0000Motorola\u00000",
		"seriesName": "Motorola",
		"name": "Bezoeken",
		"dataIndex": 0,
		"data": 7,
		"value": 7,
		"color": "#ED5565",
		"marker": "<span style=\"display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:#ED5565;\"></span>",
		"$vars": [
			"seriesName",
			"name",
			"value"
		],
		"axisDim": "x",
		"axisIndex": 0,
		"axisType": "xAxis.category",
		"axisId": "\u0000\u0000-\u00000",
		"axisValue": "Bezoeken",
		"axisValueLabel": "Bezoeken"
	},
	{
		"componentType": "series",
		"componentSubType": "bar",
		"seriesType": "bar",
		"seriesIndex": 4,
		"seriesId": "\u0000Huawei\u00000",
		"seriesName": "Huawei",
		"name": "Bezoeken",
		"dataIndex": 0,
		"data": 4,
		"value": 4,
		"color": "#1C84C6",
		"marker": "<span style=\"display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:#1C84C6;\"></span>",
		"$vars": [
			"seriesName",
			"name",
			"value"
		],
		"axisDim": "x",
		"axisIndex": 0,
		"axisType": "xAxis.category",
		"axisId": "\u0000\u0000-\u00000",
		"axisValue": "Bezoeken",
		"axisValueLabel": "Bezoeken"
	},
	{
		"componentType": "series",
		"componentSubType": "bar",
		"seriesType": "bar",
		"seriesIndex": 5,
		"seriesId": "\u0000Other\u00000",
		"seriesName": "Other",
		"name": "Bezoeken",
		"dataIndex": 0,
		"data": 9,
		"value": 9,
		"color": "#dedede",
		"marker": "<span style=\"display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:#dedede;\"></span>",
		"$vars": [
			"seriesName",
			"name",
			"value"
		],
		"axisDim": "x",
		"axisIndex": 0,
		"axisType": "xAxis.category",
		"axisId": "\u0000\u0000-\u00000",
		"axisValue": "Bezoeken",
		"axisValueLabel": "Bezoeken"
	}
]

So, they are not in my chart object, but Echarts still draws them, and as you can see, they still exist in the tooltip params.

The chart does update well when using chartInstance.clear() before chartInstance.setOption(), but then I'll lose all the nice transitions and animations, which make ECharts look so beautiful.

@jeffrey-roosendaal jeffrey-roosendaal changed the title Problems when updating chart through echartsInstance.setOptions() Problems when updating chart through echartsInstance.setOption() Jul 13, 2017
@Bkengs
Copy link

Bkengs commented Jul 13, 2017

I have the same problem recently , i solved it by set the series's data = null which you do not want. Also you can try it setOption(option, true)

@jeffrey-roosendaal
Copy link
Author

jeffrey-roosendaal commented Jul 13, 2017

Thanks for your suggestions!

Setting data = null on all the previous series may lead to a lot of code rewriting on my part, which is not my prefered option.

And, using setOption(option, true), is the same as calling refresh(), which removes the transition animations, which is what I like so much about ECharts. But for now, I'll go with setOption(option, true) when changing data, until the ECharts team comes with a solution or fix. Thanks!

@sorenhoyer
Copy link

sorenhoyer commented Mar 7, 2018

This is still a problem. And a very annoying one. I'd call it a bug rather than an enhancement, since it's a very big problem when dealing with realtime data (calling setOption frequently) and building the chart from data in React. :)

@skraemer-eis
Copy link

I have the same problems and I agree with @sorenhoyer: I would also call it a bug. It should be possible to delete series with setOptions

@anasanzari
Copy link

+1

2 similar comments
@tehZevo
Copy link

tehZevo commented Apr 15, 2018

+1

@pchaganti
Copy link

👍

@pchaganti
Copy link

setOption() is still leaving the old data on the chart BTW.

@ghost
Copy link

ghost commented Jun 25, 2018

+1

@sys4soft
Copy link

Until this is solved, I've come with the radical solution of clearing the chart and "recreate" it again.
Not a problem for me, since my char is only updated after I submit a form in the same page and the graphic only comes visible after a success ajax call.

Basically:
All echart construction inside a function (EchartConstruction);
When ajax is called and have a success return: [instance].clear() and execute EchartConstruction function
Then, set the axis and series.

@eviltomato
Copy link

eviltomato commented Jul 3, 2018

I have the same issue with bar chart

@mishantidev
Copy link

+1!

@VictorChen
Copy link

This sounds like a bug to me, if you can add a series via setOption, then you should be able to remove a series as well. Here's a quick reproducer of this bug:

http://jsfiddle.net/douykvbc/

Clicking on One Line and then Two Lines => works

Clicking on Two Lines and then One Line => doesn't work

@VictorChen
Copy link

@100pah I've noticed you added the new-feature label, but I believe this is a bug. Please see my post above.

@sswaroopk
Copy link

+1

2 similar comments
@emailhy
Copy link

emailhy commented Sep 14, 2018

+1

@HugoMitsumori
Copy link

+1

@satonreb
Copy link

Same issue here. To resolve it I set notMerge to true in setOption() as per ECharts Documentation

@DavidDuranPerez
Copy link

Same issue here. To resolve it I set notMerge to true in setOption() as per ECharts Documentation

It does not solve the problem for me.

@melanke
Copy link

melanke commented Nov 29, 2018

This is a bug

@rredondog
Copy link

I've solved it creating two functions one only with myChart.clear and another for draw all the chart and allways first call de function wich clear the chart and after the ones for draw it.

@melanke
Copy link

melanke commented Dec 6, 2018

I solved it by storing the options in a variable and doing the merge by myself :/

@klausb
Copy link

klausb commented Jan 8, 2019

@melanke

I solved it by storing the options in a variable and doing the merge by myself :/

But you still nee the clear() call before setting your merged result, correct?

@klausb
Copy link

klausb commented Jan 8, 2019

To me this is a serious defect, as it kills most of the animation feature from echarts, when morphing across changes. Doing a clear() inbetween all updates is destroying most of the nice rendering effects.

@ckz8780
Copy link

ckz8780 commented Jan 8, 2019

Nearly two years and still no response to this. This isn't an "enhancement" ... it's an extremely annoying and show stopping bug. Any idea on when this will be resolved? All these hacky solutions are mediocre at best and leave the potential for a future update to Echarts that fixes the problem to break the solution users have come up with on their own.

jact added a commit to migasfree/migasfree-frontend that referenced this issue Mar 18, 2021
@symlons
Copy link

symlons commented Mar 30, 2021

I tried all the suggested hacks listed above but I am getting the same error. I am using socket.io to update the data but I have no idea how to approach this issue after trying the suggested hacks. Any suggestions what the underlying cause of this problem might by?

@mathias22osterhagen22
Copy link

mathias22osterhagen22 commented Oct 4, 2021

This issue has been a high priority since 2019.... please...

I fixed it in vue-echarts like this:

 fixIssue6202Echart() {
   //Fix: https://github.com/apache/echarts/issues/6202
   if (this.$refs.yourChart) this.$refs.yourChart.setOption(this.datas, true);
  }
  • yourChart is the ref of the v-chart component: <v-chart ref="snapechart">
  • this.datas is the data you give to as option containing: legend, xAxis, yAxis etc...
  • you need to call the fixIssue6202Echart() function after every time you update your series

@T0miii
Copy link

T0miii commented Oct 7, 2021

So is there a recommended way by now to remove a Series from the Chart, without losing every other config option?

@LeaVerou
Copy link

Just ran into a similar bug when changing axis names and using setOption(). Here's a reduced testcase: https://codepen.io/leaverou/pen/xxLBMJJ?editors=0010

@pissang
Copy link
Contributor

pissang commented Nov 21, 2021

Sorry to be late here. 5.0 introduces a new parameter replaceMerge that can remove series or any component without loosing interaction states from other components, like legend selection.

https://echarts.apache.org/en/api.html#echartsInstance.setOption

Example usage:

myChart.setOption({
  series: newSeriesHere
}, { replaceMerge: ['series'] })

jact added a commit to migasfree/migasfree-frontend that referenced this issue Jan 20, 2022
@marccompte
Copy link

A pending bug from 5 years ago in high priority, and it is still not fixed ...

@pissang
Copy link
Contributor

pissang commented Feb 9, 2022

@marccompte Please see my comment above

@marccompte
Copy link

I tried that but did not work. I'm trying other chart libraries now.

@stereokai
Copy link

@marccompte I have struggled with the same issue, replaceMerge works as you'd expect. Try this:

  chart.setOption(options, {
    replaceMerge: ["series", "yAxis", "xAxis", "grid"],
  });

@hoangviet-98
Copy link

I solved this for React and only set "notMerge" to true.

<ReactECharts option={options} opts={{ renderer: 'svg' }} notMerge={true} />

This help for me <3

@towertop
Copy link

I felt some cases above might relate to this issue #14192 , where the seettings of components like axis need passed to setOption() to trigger relevant graph redraw correctlly. I was using 5.3.2 and that issue still exists.

@TIGeMi
Copy link

TIGeMi commented Feb 22, 2023

Is there any news for this issue. Recently I got issue that cannot change the symbolSize of the lines series. Not sure that relates to this issue.
Here is an example

@jk4235
Copy link

jk4235 commented Mar 28, 2023

for someone who may meet this problem, you may have wrapped a component like this:

useEffect(() => {
if (!chartInstanceRef.current) return;
chartInstanceRef.current?.setOption(option, { notMerge: true }); // set notMerge true
}, [options])

@LaykDimon
Copy link

the problem is still present
when I try to switch the expandAndCollapse state in the tree viewer from true to false, it continues expanding and collapsing the tree, although it shouldn't
so the setOption still doesn't work properly
can you please look at this problem and try to fix it?
would be really grateful!

@Aleksashka11
Copy link

Yeah, I’m still experiencing this problem.
Moreover, in my case optional parameter notMerge wasn’t helpful at all.
Therefore, It would be really nice to have this bug fixed.
Thank you for time and effort in advance!

@Aleksashka11
Copy link

Are there any updates?

@seanjamu
Copy link

Tengo el mismo problema, lo resolví configurando la serie data = nullque no quieres. También puedes probarlo. setOption(option, true)

Thanks, I have this problem with echart 5.4.4 too

@branogoga
Copy link

Using notMerge with echarts-for-react can help, but it can also has unexpected effects - e.g. selection tools do not work. Despite echarts-for-react does not support the replaceMerge yet, You can still use custom effect and call it Yourself, e.g.:

  useEffect(() => {
    if (!chartRef.current) {
      return
    }
    // @ts-ignore
    const chart = chartRef.current.getEchartsInstance()
    if (!chart) {
      return
    }

    chart.setOption(option, {
      replaceMerge: ['series'],
    })
  }, [chartRef, ...])

@titivermeesch
Copy link

This issue is still relevant... Luckily setting replaceMerge: ['series'] seems to fix it but I doubt that that is the intended behaviour

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug pending We are not sure about whether this is a bug/new feature. priority: high
Projects
None yet
Development

No branches or pull requests