Skip to content

Commit

Permalink
fix!: replace htmllabels and htmllabelitem with annotation based labe…
Browse files Browse the repository at this point in the history
…ls (#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]>
  • Loading branch information
ugur-vaadin and DiegoCardoso authored Jan 30, 2023
1 parent 8b6c3f1 commit 31d0441
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.vaadin.flow.component.charts.Chart;
import com.vaadin.flow.component.charts.examples.AbstractChartExample;
import com.vaadin.flow.component.charts.examples.SkipFromDemo;
import com.vaadin.flow.component.charts.model.AnnotationItemLabel;
import com.vaadin.flow.component.charts.model.AnnotationItemLabelPoint;
import com.vaadin.flow.component.charts.model.ChartType;
import com.vaadin.flow.component.charts.model.Configuration;
import com.vaadin.flow.component.charts.model.ListSeries;
Expand Down Expand Up @@ -37,6 +39,10 @@ public void initDemo() {
y.setTitle("Rainfall (mm)");
configuration.addyAxis(y);

AnnotationItemLabel label = new AnnotationItemLabel("Sample label");
label.setPoint(new AnnotationItemLabelPoint(100, 100));
configuration.addLabel(label);

NativeButton changeTitleButton = new NativeButton();
changeTitleButton.setId("change_title");
changeTitleButton.setText("Change title");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import com.vaadin.flow.component.charts.Chart;
import com.vaadin.flow.component.charts.examples.AbstractChartExample;
import com.vaadin.flow.component.charts.model.AnnotationItemLabel;
import com.vaadin.flow.component.charts.model.AnnotationItemLabelPoint;
import com.vaadin.flow.component.charts.model.Configuration;
import com.vaadin.flow.component.charts.model.DataSeries;
import com.vaadin.flow.component.charts.model.DataSeriesItem;
import com.vaadin.flow.component.charts.model.HTMLLabelItem;
import com.vaadin.flow.component.charts.model.HTMLLabels;
import com.vaadin.flow.component.charts.model.PlotOptionsColumn;
import com.vaadin.flow.component.charts.model.PlotOptionsPie;
import com.vaadin.flow.component.charts.model.PlotOptionsSpline;
import com.vaadin.flow.component.charts.model.XAxis;
import com.vaadin.flow.component.charts.model.style.Style;
import com.vaadin.flow.component.dependency.CssImport;

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

Style labelStyle = new Style();
labelStyle.setTop("8px");
labelStyle.setLeft("40px");
conf.setLabels(new HTMLLabels(labelStyle,
new HTMLLabelItem("Total fruit consumption")));
AnnotationItemLabel label = new AnnotationItemLabel(
"Total fruit consumption");
label.setPoint(new AnnotationItemLabelPoint(100, 100));
label.setUseHTML(true);
conf.addLabel(label);

DataSeries series = new DataSeries();
PlotOptionsColumn plotOptions = new PlotOptionsColumn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import com.vaadin.flow.component.charts.examples.AbstractChartExample;
import com.vaadin.flow.component.charts.examples.area.AreaChart;

import java.util.List;

import static org.junit.Assert.assertTrue;

public class BasicChartIT extends AbstractTBTest {
Expand Down Expand Up @@ -57,4 +59,15 @@ public void Chart_SeriesNameIsSet() {
assertTrue(series.getText().contains("Tokyo"));
}

@Test
public void Chart_LabelDisplayed() {
final TestBenchElement chart = getChartElement();
waitUntil(driver -> {
List<TestBenchElement> labels = chart.$("*")
.attributeContains("class", "highcharts-label").all();
return !labels.isEmpty()
&& labels.stream().map(TestBenchElement::getText)
.anyMatch("Sample label"::equals);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright 2000-2022 Vaadin Ltd.
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See <https://vaadin.com/commercial-license-and-service-terms> for the full
* license.
*/
package com.vaadin.flow.component.charts.model;

import java.util.ArrayList;
import java.util.List;

/**
* Container for labels on the chart
*/
public class AnnotationItem extends AbstractConfigurationObject {

private List<AnnotationItemLabel> labels;

/**
* @see #setLabels(AnnotationItemLabel...)
* @return Labels
*/
public List<AnnotationItemLabel> getLabels() {
if (labels == null) {
labels = new ArrayList<>();
}
return labels;
}

/**
* Sets labels that can be positioned anywhere in the chart area.
*
* @param labels
*/
public void setLabels(AnnotationItemLabel... labels) {
clearLabels();
addLabels(labels);
}

/**
* Adds multiple labels
*
* @see #setLabels(AnnotationItemLabel...)
* @param labels
*/
public void addLabels(AnnotationItemLabel... labels) {
for (AnnotationItemLabel label : labels) {
addLabel(label);
}
}

/**
* Adds a single label
*
* @see #setLabels(AnnotationItemLabel...)
* @param label
*/
public void addLabel(AnnotationItemLabel label) {
getLabels().add(label);
}

/**
* Clears all labels
*
* @see #setLabels(AnnotationItemLabel...)
*/
public void clearLabels() {
getLabels().clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.vaadin.flow.component.charts.model;

import com.vaadin.flow.component.charts.model.style.Style;

/**
* Label that can be positioned anywhere in the chart area
*/
public class AnnotationItemLabel extends AbstractConfigurationObject {

private AnnotationItemLabelPoint point;
private Style style;
private String text;
private Boolean useHTML;

/**
* Constructs an AnnotationItemLabel with the given text
*
* @param text
* Text to be displayed
*/
public AnnotationItemLabel(String text) {
this.text = text;
}

/**
* @see #setPoint(AnnotationItemLabelPoint)
*/
public AnnotationItemLabelPoint getPoint() {
return point;
}

/**
* Sets the {@link AnnotationItemLabelPoint} that contains the coordinate
* data for the label
*
* @param point
* Label point options
*/
public void setPoint(AnnotationItemLabelPoint point) {
this.point = point;
}

/**
* @see #setStyle(Style)
*/
public Style getStyle() {
return style;
}

/**
* Sets the label style options
*
* @param style
* Label style options
*/
public void setStyle(Style style) {
this.style = style;
}

/**
* @see #setText(String)
*/
public String getText() {
return text;
}

/**
* Sets the text to be displayed
*
* @param text
* Text to be displayed
*/
public void setText(String text) {
this.text = text;
}

/**
* @see #setUseHTML(Boolean)
*/
public Boolean getUseHTML() {
return useHTML;
}

/**
* Whether to enable HTML parsing for the label contents
*
* @param useHTML
* Whether to enable HTML
*/
public void setUseHTML(Boolean useHTML) {
this.useHTML = useHTML;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.vaadin.flow.component.charts.model;

/**
* Contains coordinates for {@link AnnotationItemLabel}
*/
public class AnnotationItemLabelPoint extends AbstractConfigurationObject {

private Number x;
private Number y;

/**
* Constructs an AnnotationItemLabelPoint with the given coordinates
*
* @param x
* Horizontal offset
* @param y
* Vertical offset
*/
public AnnotationItemLabelPoint(Number x, Number y) {
this.x = x;
this.y = y;
}

/**
* @see #setX(Number)
*/
public Number getX() {
return x;
}

/**
* Sets the horizontal offset of the label within chart
*
* @param x
* Horizontal offset
*/
public void setX(Number x) {
this.x = x;
}

/**
* @see #setY(Number)
*/
public Number getY() {
return y;
}

/**
* Sets the vertical offset of the label within chart
*
* @param y
* Vertical offset
*/
public void setY(Number y) {
this.y = y;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ public class Configuration extends AbstractConfigurationObject
private Legend legend;
private Credits credits;
private Map<String, AbstractPlotOptions> plotOptions = new HashMap<>();
private HTMLLabels labels;

private List<Series> series = new ArrayList<>();
private Drilldown drilldown;

Expand All @@ -62,6 +60,7 @@ public class Configuration extends AbstractConfigurationObject
private NoData noData;
private Navigator navigator;
private Time time;
private List<AnnotationItem> annotations;

@JsonIgnore
private final List<ConfigurationChangeListener> changeListeners = new ArrayList<>();
Expand Down Expand Up @@ -622,23 +621,6 @@ public void addPlotOptions(AbstractPlotOptions plotOptions) {
}
}

/**
* @see #setLabels(HTMLLabels)
* @return Labels or null if not defined
*/
public HTMLLabels getLabels() {
return labels;
}

/**
* Sets HTML labels that can be positioned anywhere in the chart area.
*
* @param labels
*/
public void setLabels(HTMLLabels labels) {
this.labels = labels;
}

/**
* Sets whether to enable exporting
*
Expand Down Expand Up @@ -1163,4 +1145,39 @@ public void addColorAxis(ColorAxis axis) {
colorAxis.addAxis(axis);
}

/**
* @see #setLabels(AnnotationItemLabel...)
* @return Labels
*/
public List<AnnotationItemLabel> getLabels() {
return getLabelsAnnotation().getLabels();
}

/**
* Sets labels that can be positioned anywhere in the chart area
*
* @param labels
* The labels to set
*/
public void setLabels(AnnotationItemLabel... labels) {
getLabelsAnnotation().setLabels(labels);
}

/**
* Adds a single label
*
* @param label
* The label to add
* @see #setLabels(AnnotationItemLabel...)
*/
public void addLabel(AnnotationItemLabel label) {
getLabels().add(label);
}

private AnnotationItem getLabelsAnnotation() {
if (annotations == null) {
annotations = List.of(new AnnotationItem());
}
return annotations.get(0);
}
}
Loading

0 comments on commit 31d0441

Please sign in to comment.