Clear chart series

I am creating charts based on selection in list box on button click. Chart series are created on over another. If i click the button two times additional data points are created on older one. (see image)

How can I remove old series and its data or re-initialize? Echarts documentation talk about NoMerge option but how can i use it in jmix. (https://echarts.apache.org/en/api.html#echartsInstance.setOption

Screenshot 2024-08-13 214605

I am using two chart series separately, barseries and pieseries. On click of button only one series should show with new data.

I tries to use this code (as I don’t know which series is present in chart) to remove series but it is also giving error.

if (chart1.getSeries(“a”) != null) {
chart1.removeSeries(chart1.getSeries(“a”));
}

NullPointerException: Cannot invoke “java.util.List.stream()” because the return value of “io.jmix.chartsflowui.kit.component.model.ChartOptions.getSeries()” is null

Hi!

The error that occurs is a bug, I have created a task to fix it: ChartOptions.getSeries produces NPE · Issue #3574 · jmix-framework/jmix · GitHub

I can suggest storing the series in a local object when you create them and then use it to removing.
Also, you can create another chart object and display it.

To use the option noMerge you can use a JavaScript function call:

        chart.getElement().executeJs("""
                $0._root.setOption({ /*your options here*/ }, true);
                """);

But this code will work after the client side is initialized, otherwise _root property will be undefined.

Best regards,
Dmitriy

This is working for me -

  1. user vbox to host chart component.
  2. User vbox1.removeAll(); to remove other chart on click of button.
  3. Create new chart and add it to vbox.

Will creation of 100s of chart component will cause any memory issue? or they will be disposed by garbage collector. (I have limited knowledge of programming)

Thanks