Skip to content

Commit 31d0441

Browse files
fix!: replace htmllabels and htmllabelitem with annotation based labels (#4481)
* feat: remove unsupported htmllabels and implement annotation support * refactor: add convenience methods and remove unused fields and classes * docs: add javadocs for the new api * refactor: remove unnecessary styling * test: add serialization test * test: add annotation based label display test * docs: remove param annotations from javadocs * refactor: remove clearLabels method * docs: add missing javadoc params and rename point options --------- Co-authored-by: Diego Cardoso <[email protected]>
1 parent 8b6c3f1 commit 31d0441

File tree

10 files changed

+299
-175
lines changed

10 files changed

+299
-175
lines changed

vaadin-charts-flow-parent/vaadin-charts-flow-integration-tests/src/main/java/com/vaadin/flow/component/charts/examples/area/AreaChart.java

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.vaadin.flow.component.charts.Chart;
44
import com.vaadin.flow.component.charts.examples.AbstractChartExample;
55
import com.vaadin.flow.component.charts.examples.SkipFromDemo;
6+
import com.vaadin.flow.component.charts.model.AnnotationItemLabel;
7+
import com.vaadin.flow.component.charts.model.AnnotationItemLabelPoint;
68
import com.vaadin.flow.component.charts.model.ChartType;
79
import com.vaadin.flow.component.charts.model.Configuration;
810
import com.vaadin.flow.component.charts.model.ListSeries;
@@ -37,6 +39,10 @@ public void initDemo() {
3739
y.setTitle("Rainfall (mm)");
3840
configuration.addyAxis(y);
3941

42+
AnnotationItemLabel label = new AnnotationItemLabel("Sample label");
43+
label.setPoint(new AnnotationItemLabelPoint(100, 100));
44+
configuration.addLabel(label);
45+
4046
NativeButton changeTitleButton = new NativeButton();
4147
changeTitleButton.setId("change_title");
4248
changeTitleButton.setText("Change title");

vaadin-charts-flow-parent/vaadin-charts-flow-integration-tests/src/main/java/com/vaadin/flow/component/charts/examples/combinations/ColumnLineAndPie.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
import com.vaadin.flow.component.charts.Chart;
44
import com.vaadin.flow.component.charts.examples.AbstractChartExample;
5+
import com.vaadin.flow.component.charts.model.AnnotationItemLabel;
6+
import com.vaadin.flow.component.charts.model.AnnotationItemLabelPoint;
57
import com.vaadin.flow.component.charts.model.Configuration;
68
import com.vaadin.flow.component.charts.model.DataSeries;
79
import com.vaadin.flow.component.charts.model.DataSeriesItem;
8-
import com.vaadin.flow.component.charts.model.HTMLLabelItem;
9-
import com.vaadin.flow.component.charts.model.HTMLLabels;
1010
import com.vaadin.flow.component.charts.model.PlotOptionsColumn;
1111
import com.vaadin.flow.component.charts.model.PlotOptionsPie;
1212
import com.vaadin.flow.component.charts.model.PlotOptionsSpline;
1313
import com.vaadin.flow.component.charts.model.XAxis;
14-
import com.vaadin.flow.component.charts.model.style.Style;
1514
import com.vaadin.flow.component.dependency.CssImport;
1615

1716
@CssImport(value = "./styles/ColumnLineAndPie.css", themeFor = "vaadin-chart")
@@ -32,11 +31,11 @@ public void initDemo() {
3231
"Plums" });
3332
conf.addxAxis(x);
3433

35-
Style labelStyle = new Style();
36-
labelStyle.setTop("8px");
37-
labelStyle.setLeft("40px");
38-
conf.setLabels(new HTMLLabels(labelStyle,
39-
new HTMLLabelItem("Total fruit consumption")));
34+
AnnotationItemLabel label = new AnnotationItemLabel(
35+
"Total fruit consumption");
36+
label.setPoint(new AnnotationItemLabelPoint(100, 100));
37+
label.setUseHTML(true);
38+
conf.addLabel(label);
4039

4140
DataSeries series = new DataSeries();
4241
PlotOptionsColumn plotOptions = new PlotOptionsColumn();

vaadin-charts-flow-parent/vaadin-charts-flow-integration-tests/src/test/java/com/vaadin/flow/component/charts/tests/BasicChartIT.java

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import com.vaadin.flow.component.charts.examples.AbstractChartExample;
1717
import com.vaadin.flow.component.charts.examples.area.AreaChart;
1818

19+
import java.util.List;
20+
1921
import static org.junit.Assert.assertTrue;
2022

2123
public class BasicChartIT extends AbstractTBTest {
@@ -57,4 +59,15 @@ public void Chart_SeriesNameIsSet() {
5759
assertTrue(series.getText().contains("Tokyo"));
5860
}
5961

62+
@Test
63+
public void Chart_LabelDisplayed() {
64+
final TestBenchElement chart = getChartElement();
65+
waitUntil(driver -> {
66+
List<TestBenchElement> labels = chart.$("*")
67+
.attributeContains("class", "highcharts-label").all();
68+
return !labels.isEmpty()
69+
&& labels.stream().map(TestBenchElement::getText)
70+
.anyMatch("Sample label"::equals);
71+
});
72+
}
6073
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright 2000-2022 Vaadin Ltd.
3+
*
4+
* This program is available under Vaadin Commercial License and Service Terms.
5+
*
6+
* See <https://vaadin.com/commercial-license-and-service-terms> for the full
7+
* license.
8+
*/
9+
package com.vaadin.flow.component.charts.model;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
/**
15+
* Container for labels on the chart
16+
*/
17+
public class AnnotationItem extends AbstractConfigurationObject {
18+
19+
private List<AnnotationItemLabel> labels;
20+
21+
/**
22+
* @see #setLabels(AnnotationItemLabel...)
23+
* @return Labels
24+
*/
25+
public List<AnnotationItemLabel> getLabels() {
26+
if (labels == null) {
27+
labels = new ArrayList<>();
28+
}
29+
return labels;
30+
}
31+
32+
/**
33+
* Sets labels that can be positioned anywhere in the chart area.
34+
*
35+
* @param labels
36+
*/
37+
public void setLabels(AnnotationItemLabel... labels) {
38+
clearLabels();
39+
addLabels(labels);
40+
}
41+
42+
/**
43+
* Adds multiple labels
44+
*
45+
* @see #setLabels(AnnotationItemLabel...)
46+
* @param labels
47+
*/
48+
public void addLabels(AnnotationItemLabel... labels) {
49+
for (AnnotationItemLabel label : labels) {
50+
addLabel(label);
51+
}
52+
}
53+
54+
/**
55+
* Adds a single label
56+
*
57+
* @see #setLabels(AnnotationItemLabel...)
58+
* @param label
59+
*/
60+
public void addLabel(AnnotationItemLabel label) {
61+
getLabels().add(label);
62+
}
63+
64+
/**
65+
* Clears all labels
66+
*
67+
* @see #setLabels(AnnotationItemLabel...)
68+
*/
69+
public void clearLabels() {
70+
getLabels().clear();
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.vaadin.flow.component.charts.model;
2+
3+
import com.vaadin.flow.component.charts.model.style.Style;
4+
5+
/**
6+
* Label that can be positioned anywhere in the chart area
7+
*/
8+
public class AnnotationItemLabel extends AbstractConfigurationObject {
9+
10+
private AnnotationItemLabelPoint point;
11+
private Style style;
12+
private String text;
13+
private Boolean useHTML;
14+
15+
/**
16+
* Constructs an AnnotationItemLabel with the given text
17+
*
18+
* @param text
19+
* Text to be displayed
20+
*/
21+
public AnnotationItemLabel(String text) {
22+
this.text = text;
23+
}
24+
25+
/**
26+
* @see #setPoint(AnnotationItemLabelPoint)
27+
*/
28+
public AnnotationItemLabelPoint getPoint() {
29+
return point;
30+
}
31+
32+
/**
33+
* Sets the {@link AnnotationItemLabelPoint} that contains the coordinate
34+
* data for the label
35+
*
36+
* @param point
37+
* Label point options
38+
*/
39+
public void setPoint(AnnotationItemLabelPoint point) {
40+
this.point = point;
41+
}
42+
43+
/**
44+
* @see #setStyle(Style)
45+
*/
46+
public Style getStyle() {
47+
return style;
48+
}
49+
50+
/**
51+
* Sets the label style options
52+
*
53+
* @param style
54+
* Label style options
55+
*/
56+
public void setStyle(Style style) {
57+
this.style = style;
58+
}
59+
60+
/**
61+
* @see #setText(String)
62+
*/
63+
public String getText() {
64+
return text;
65+
}
66+
67+
/**
68+
* Sets the text to be displayed
69+
*
70+
* @param text
71+
* Text to be displayed
72+
*/
73+
public void setText(String text) {
74+
this.text = text;
75+
}
76+
77+
/**
78+
* @see #setUseHTML(Boolean)
79+
*/
80+
public Boolean getUseHTML() {
81+
return useHTML;
82+
}
83+
84+
/**
85+
* Whether to enable HTML parsing for the label contents
86+
*
87+
* @param useHTML
88+
* Whether to enable HTML
89+
*/
90+
public void setUseHTML(Boolean useHTML) {
91+
this.useHTML = useHTML;
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.vaadin.flow.component.charts.model;
2+
3+
/**
4+
* Contains coordinates for {@link AnnotationItemLabel}
5+
*/
6+
public class AnnotationItemLabelPoint extends AbstractConfigurationObject {
7+
8+
private Number x;
9+
private Number y;
10+
11+
/**
12+
* Constructs an AnnotationItemLabelPoint with the given coordinates
13+
*
14+
* @param x
15+
* Horizontal offset
16+
* @param y
17+
* Vertical offset
18+
*/
19+
public AnnotationItemLabelPoint(Number x, Number y) {
20+
this.x = x;
21+
this.y = y;
22+
}
23+
24+
/**
25+
* @see #setX(Number)
26+
*/
27+
public Number getX() {
28+
return x;
29+
}
30+
31+
/**
32+
* Sets the horizontal offset of the label within chart
33+
*
34+
* @param x
35+
* Horizontal offset
36+
*/
37+
public void setX(Number x) {
38+
this.x = x;
39+
}
40+
41+
/**
42+
* @see #setY(Number)
43+
*/
44+
public Number getY() {
45+
return y;
46+
}
47+
48+
/**
49+
* Sets the vertical offset of the label within chart
50+
*
51+
* @param y
52+
* Vertical offset
53+
*/
54+
public void setY(Number y) {
55+
this.y = y;
56+
}
57+
}

vaadin-charts-flow-parent/vaadin-charts-flow/src/main/java/com/vaadin/flow/component/charts/model/Configuration.java

+36-19
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ public class Configuration extends AbstractConfigurationObject
4848
private Legend legend;
4949
private Credits credits;
5050
private Map<String, AbstractPlotOptions> plotOptions = new HashMap<>();
51-
private HTMLLabels labels;
52-
5351
private List<Series> series = new ArrayList<>();
5452
private Drilldown drilldown;
5553

@@ -62,6 +60,7 @@ public class Configuration extends AbstractConfigurationObject
6260
private NoData noData;
6361
private Navigator navigator;
6462
private Time time;
63+
private List<AnnotationItem> annotations;
6564

6665
@JsonIgnore
6766
private final List<ConfigurationChangeListener> changeListeners = new ArrayList<>();
@@ -622,23 +621,6 @@ public void addPlotOptions(AbstractPlotOptions plotOptions) {
622621
}
623622
}
624623

625-
/**
626-
* @see #setLabels(HTMLLabels)
627-
* @return Labels or null if not defined
628-
*/
629-
public HTMLLabels getLabels() {
630-
return labels;
631-
}
632-
633-
/**
634-
* Sets HTML labels that can be positioned anywhere in the chart area.
635-
*
636-
* @param labels
637-
*/
638-
public void setLabels(HTMLLabels labels) {
639-
this.labels = labels;
640-
}
641-
642624
/**
643625
* Sets whether to enable exporting
644626
*
@@ -1163,4 +1145,39 @@ public void addColorAxis(ColorAxis axis) {
11631145
colorAxis.addAxis(axis);
11641146
}
11651147

1148+
/**
1149+
* @see #setLabels(AnnotationItemLabel...)
1150+
* @return Labels
1151+
*/
1152+
public List<AnnotationItemLabel> getLabels() {
1153+
return getLabelsAnnotation().getLabels();
1154+
}
1155+
1156+
/**
1157+
* Sets labels that can be positioned anywhere in the chart area
1158+
*
1159+
* @param labels
1160+
* The labels to set
1161+
*/
1162+
public void setLabels(AnnotationItemLabel... labels) {
1163+
getLabelsAnnotation().setLabels(labels);
1164+
}
1165+
1166+
/**
1167+
* Adds a single label
1168+
*
1169+
* @param label
1170+
* The label to add
1171+
* @see #setLabels(AnnotationItemLabel...)
1172+
*/
1173+
public void addLabel(AnnotationItemLabel label) {
1174+
getLabels().add(label);
1175+
}
1176+
1177+
private AnnotationItem getLabelsAnnotation() {
1178+
if (annotations == null) {
1179+
annotations = List.of(new AnnotationItem());
1180+
}
1181+
return annotations.get(0);
1182+
}
11661183
}

0 commit comments

Comments
 (0)