Open metaperl opened 11 months ago
Wow, this is strange. I boiled it down to this:
def toggle():
chart.options['series'] = [{'data': [1]}, {'data': [2]}] if len(chart.options['series']) == 1 else [{'data': [1]}]
chart.update()
chart = ui.chart({'series': [{'data': [1]}]})
ui.button('Toggle', on_click=toggle)
Even though the options always contain a data point at 1, toggling multiple times leads to a single point at 2.
I wonder if we can reproduce the problem with a pure Highcharts example without NiceGUI...
No, without NiceGUI the example is working:
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container"></div>
<button onclick="toggle()">Toggle</button>
<script>
let chart = Highcharts.chart("container", { series: [{ data: [1] }] });
function toggle() {
if (chart.series.length === 1) {
chart.addSeries({ data: [2] });
} else {
chart.series[1].remove();
}
}
</script>
Now I'm pretty sure the issue is caused by update_chart()
in chart.js which follows a pretty naive heuristic to add and remove series:
https://github.com/zauberzeug/nicegui/blob/eea7d8d4ce91be270edfacb29e26b385ba447d0f/nicegui/elements/chart.js#L40-L52
Minimum reproduction without NiceGUI (but with the same update algorithm from chart.js):
<html>
<head>
<script src="https://code.highcharts.com/highcharts.js"></script>
</head>
<body>
<div id="container"></div>
<button onclick="toggle()">Toggle</button>
<script>
let options = { series: [{ data: [1] }] };
let chart = Highcharts.chart("container", options);
let seriesCount = options.series ? options.series.length : 0;
function toggle() {
if (options.series.length === 1) {
options.series.push({ data: [2] });
} else {
options.series.pop();
}
update();
}
function update() {
while (seriesCount > options.series.length) {
chart.series[0].remove();
seriesCount--;
}
while (seriesCount < options.series.length) {
chart.addSeries({}, false);
seriesCount++;
}
chart.update(options);
}
</script>
</body>
</html>
The question is, how to improve the update()
function.
I created a test page for manipulating Highcharts options:
Currently all series collapse into a point at -1 when clicking "insert low".
And a lengthy discussion with ChatGPT about improving the update()
function wasn't successful.
I still don't have a good solution for fixing this issue. In order to focus on a quick release of upcoming version 1.4, I'll postpone this one to 1.4.1.
I'm currently thinking about how to solve an update like this:
<html>
<head>
<script src="https://code.highcharts.com/highcharts.js"></script>
</head>
<body>
<div id="container"></div>
<button onclick="add()">Add</button>
<script>
let options = { series: [{ data: [1] }, { data: [2] }, { data: [3] }] };
let chart = Highcharts.chart("container", options);
function add() {
options.series.unshift({ data: [0] });
chart.update(options);
}
</script>
</body>
</html>
The chart simply collapses, which doesn't make any sense and looks like a bug in Highcharts. But it's hard to find a related bug report. This one might be relevant, maybe not. With 1.1K open and 13K closed issues it's hard to tell if this behavior is known and/or already worked on.
By the way: A conversation with their AI didn't yield much insight.
Description
In the screenshot attached you see:
this is a bug because they should be the same.
how to reproduce
This video shows the same thing. Regrettably, the capture window did not capture the deselections and selections of the same element - https://www.youtube.com/watch?v=KOKuhgMA9dk