Skip to content

Commit 189ed48

Browse files
vaadin-bottomivirkkiclaude
authored
fix: correct X fallback and _color handling in chart data (#10162) (CP: 25.3) (#10179)
This PR cherry-picks changes from the original PR #10162 to branch 25.3. --- #### Original PR description > ## Description > > Related to #10154 > > Found by the line-removal mutation testing in #10154, which only added tests and left the defects for a separate fix. > > - Fixed `resolveX` so OHLC, Range and Flags data without an `_x` column gets the row index as X instead of `null` > - Every caller passes the `_x` constant, so the `rowIndex` fallback was unreachable > - Excluded the `_color` marker column from the fallback's category and value column classification > - `{revenue: 1000, _color: "#FF0000"}` produced points named `#FF0000`, and two numeric columns plus `_color` were converted as category and value instead of X and Y > - Added tests for OHLC, Range and Flags data without `_x` and for the fallback with a `_color` column > > ## Type of change > > - Bugfix Co-authored-by: Tomi Virkki <tomivirkki@users.noreply.github.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent a2f4d13 commit 189ed48

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎vaadin-ai-components-flow-parent/vaadin-ai-extensions-flow/src/main/java/com/vaadin/flow/component/ai/chart/DefaultDataConverter.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
* Additionally, any pattern that produces a {@code DataSeriesItem} (or
9999
* subclass) supports an optional {@code _color} column. If present, the value
100100
* is used to set the item's color via {@link SolidColor#SolidColor(String)}.
101+
* The column is never treated as data, so the fallback skips it when it picks
102+
* the category and value columns.
101103
* </p>
102104
* <p>
103105
* Column name matching is case-insensitive. Column names are determined from
@@ -609,7 +611,10 @@ private Optional<AbstractSeries> tryHeatmap(List<Map<String, Object>> data,
609611

610612
private DataSeries convertFallback(List<Map<String, Object>> data,
611613
Set<String> columns, Map<String, String> columnMapping) {
614+
// The color column modifies any pattern and is never data, so it must
615+
// not be classified as a category or value column here.
612616
var lowerNames = new ArrayList<>(columns);
617+
lowerNames.remove(COLOR);
613618
if (lowerNames.isEmpty()) {
614619
return new DataSeries();
615620
}
@@ -762,9 +767,15 @@ private static Instant getInstant(Map<String, Object> row,
762767

763768
// --- Utility methods ---
764769

770+
/**
771+
* Resolves the X value of a row: the value of the {@code _x} column, or the
772+
* row index when the data has no such column.
773+
*/
765774
private static Number resolveX(Map<String, Object> row,
766775
Map<String, String> columnMapping, String xCol, int rowIndex) {
767-
return xCol != null ? getNumber(row, columnMapping, xCol) : rowIndex;
776+
return columnMapping.containsKey(xCol)
777+
? getNumber(row, columnMapping, xCol)
778+
: rowIndex;
768779
}
769780

770781
private static void applyColor(DataSeriesItem item, Map<String, Object> row,

‎vaadin-ai-components-flow-parent/vaadin-ai-extensions-flow/src/test/java/com/vaadin/flow/component/ai/chart/DefaultDataConverterTest.java‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ void createsOhlcItems() {
140140
Assertions.assertEquals(5, item.getLow());
141141
Assertions.assertEquals(12, item.getClose());
142142
}
143+
144+
@Test
145+
void withoutXColumn_usesRowIndexAsX() {
146+
var data = List.of(row(OPEN, 10, HIGH, 15, LOW, 5, CLOSE, 12),
147+
row(OPEN, 12, HIGH, 18, LOW, 8, CLOSE, 16));
148+
var result = (DataSeries) convertSingle(data);
149+
Assertions.assertEquals(0, result.getData().getFirst().getX());
150+
Assertions.assertEquals(1, result.getData().get(1).getX());
151+
}
143152
}
144153

145154
// --- BoxPlot ---
@@ -563,6 +572,16 @@ void nullTitle_skipsRow() {
563572
Assertions.assertEquals(200,
564573
((FlagItem) result.getData().getFirst()).getX());
565574
}
575+
576+
@Test
577+
void withoutXColumn_usesRowIndexAsX() {
578+
var data = List.of(row(TITLE, "Flag 1"), row(TITLE, "Flag 2"));
579+
var result = (DataSeries) convertSingle(data);
580+
Assertions.assertEquals(0,
581+
((FlagItem) result.getData().getFirst()).getX());
582+
Assertions.assertEquals(1,
583+
((FlagItem) result.getData().get(1)).getX());
584+
}
566585
}
567586

568587
// --- Range ---
@@ -579,6 +598,14 @@ void createsRangeItems() {
579598
Assertions.assertEquals(5, item.getLow());
580599
Assertions.assertEquals(15, item.getHigh());
581600
}
601+
602+
@Test
603+
void withoutXColumn_usesRowIndexAsX() {
604+
var data = List.of(row(LOW, 5, HIGH, 15), row(LOW, 6, HIGH, 16));
605+
var result = (DataSeries) convertSingle(data);
606+
Assertions.assertEquals(0, result.getData().getFirst().getX());
607+
Assertions.assertEquals(1, result.getData().get(1).getX());
608+
}
582609
}
583610

584611
// --- Bullet ---
@@ -710,6 +737,28 @@ void numericStringValues_detectedAsNumeric() {
710737
Assertions.assertEquals(1.5, result.getData().getFirst().getX());
711738
Assertions.assertEquals(2.5, result.getData().getFirst().getY());
712739
}
740+
741+
@Test
742+
void singleNumericColumnWithColor_colorIsNotUsedAsCategory() {
743+
var data = List.of(row("revenue", 1000, COLOR, "#FF0000"),
744+
row("revenue", 2000, COLOR, "#00FF00"));
745+
var result = (DataSeries) convertSingle(data);
746+
var item = result.getData().getFirst();
747+
Assertions.assertNull(item.getName());
748+
Assertions.assertEquals(0, item.getX());
749+
Assertions.assertEquals(1000, item.getY());
750+
assertColor("#FF0000", item.getColor());
751+
}
752+
753+
@Test
754+
void allNumericColumnsWithColor_setsItemColor() {
755+
var data = List.of(row("a", 1, "b", 2, COLOR, "#FF0000"));
756+
var result = (DataSeries) convertSingle(data);
757+
var item = result.getData().getFirst();
758+
Assertions.assertEquals(1, item.getX());
759+
Assertions.assertEquals(2, item.getY());
760+
assertColor("#FF0000", item.getColor());
761+
}
713762
}
714763

715764
// --- Null handling ---

0 commit comments

Comments
 (0)