Files
chart-app/src/main/java/com/example/components/AxisChart.java
T
Pit Friedrich 640616c60a
CI / build-and-test (pull_request) Successful in 2m21s
fix: update ApexCharts data in place instead of re-rendering (#23)
Every data change went through renderChart, which rebuilt the chart from a
full option set: animations restarted and zoom/selection state was lost.

Add an updateData path that patches the live chart via ApexCharts'
updateSeries (and updateOptions only when categories/labels actually
change), exposed as AxisChart.updateData / PieChart.updateData. Before the
first render there is nothing to patch, so those fall back to setData.
The theme overlay is re-applied to the merged options, so a rebuild after
a detach starts from the patched data.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0124BiJikbhbiEsNfJxdWM69
2026-07-28 19:29:09 +02:00

42 lines
1.4 KiB
Java

package com.example.components;
import java.util.List;
import java.util.Map;
public abstract class AxisChart extends ApexChart {
protected AxisChart(String chartType) {
super(chartType);
}
public void setData(String seriesName, List<Double> values, List<String> categories) {
Map<String, Object> options = DialectTheme.baseOptions(getChartType());
options.put("series", List.of(Map.of("name", seriesName, "data", values)));
options.put("xaxis", Map.of(
"categories", categories,
"axisBorder", Map.of("show", false),
"axisTicks", Map.of("show", false)
));
sendOptions(options);
}
/**
* Replaces the data of an already rendered chart without re-rendering it:
* the client patches series (and categories) into the live chart, so the
* update animates from the previous values instead of flashing. Falls back
* to {@link #setData} as long as nothing has been rendered yet.
*/
public void updateData(String seriesName, List<Double> values, List<String> categories) {
if (!hasSentOptions()) {
setData(seriesName, values, categories);
return;
}
sendDataPatch(Map.of(
"series", List.of(Map.of("name", seriesName, "data", values)),
"categories", categories
));
}
}