Skip to content

Commit

Permalink
Add chart series support for point shapes and sizes (deephaven#3246)
Browse files Browse the repository at this point in the history
- Add point shape and size to series info, so markers can be plotted with the correct shapes and sizes
- Used the following code in Python to test shapes/sizes, and plot-by series:
```python
from deephaven import empty_table
from deephaven.plot import Figure
import math

math_funcs = ["sin", "cos"]
shapes = ["SQUARE", "CIRCLE", "UP_TRIANGLE", "DOWN_TRIANGLE", "RIGHT_TRIANGLE", "LEFT_TRIANGLE", "DIAMOND"]
size = 50
# Unsupported shapes: ["ELLIPSE", "HORIZONTAL_RECTANGLE", "VERTICAL_RECTANGLE"]

# Create a generic table that has enough columns to display all the shapes
# Re-uses some of the funcs
t = empty_table(size).update([f"name=`` + shapes[(int)Math.floor(i / Math.ceil(size / {len(shapes)}))]", "x=i*0.1"])
for i in range(len(shapes)):
    t = t.update([f"y{i}=Math.{math_funcs[i % len(math_funcs)]}(x+{math.floor(i / len(math_funcs))})"])

# Create a figure showing all the different shapes one for each series
p = Figure()
for i in range(len(shapes)):
    p = p.plot_xy(series_name=shapes[i], t=t, x="x", y=f"y{i}").point(shape=shapes[i], size=len(shapes) - i, visible=True)
p = p.show();

# Create a plot by figure
p2 = Figure().plot_xy(series_name="Multi", t=t, by=["name"], x="x", y="y1").line(visible=True)
for i in range(len(shapes)):
    p2 = p2.point(shape=shapes[i], multi_series_key=[shapes[i]], size=len(shapes) - i, visible=True)
p2 = p2.show()
```
  • Loading branch information
mofojed authored Jan 30, 2023
1 parent f12a7be commit c9beb0b
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public Paint getColor(final Comparable category) {
return colors.get(category);
}

@Override
public Double getPointSize() {
return PlotUtils.numberToDouble(sizes.getDefault());
}

@Override
public Double getPointSize(final Comparable category) {
return PlotUtils.numberToDouble(sizes.get(category));
Expand All @@ -134,11 +139,21 @@ public Paint getSeriesColor() {
return colors.getDefault();
}

@Override
public String getLabel() {
return labels.getDefault();
}

@Override
public String getLabel(final Comparable category) {
return labels.get(category);
}

@Override
public Shape getPointShape() {
return shapes.getDefault();
}

@Override
public Shape getPointShape(final Comparable category) {
return shapes.get(category);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public interface CategoryDataSeriesInternal extends CategoryDataSeries, DataSeri
*/
Paint getColor(final Comparable category);

/**
* Gets the default point size for data points.
*
* @return default point size for the given data point
*/
Double getPointSize();

/**
* Gets the point size for the given data point.
*
Expand All @@ -53,6 +60,12 @@ public interface CategoryDataSeriesInternal extends CategoryDataSeries, DataSeri
*/
Double getPointSize(final Comparable category);

/**
* Gets the default point label for data points.
*
* @return default point label for the given data point
*/
String getLabel();

/**
* Gets the point label for the given data point.
Expand All @@ -62,6 +75,13 @@ public interface CategoryDataSeriesInternal extends CategoryDataSeries, DataSeri
*/
String getLabel(final Comparable category);

/**
* Gets the default point shape for data points.
*
* @return default point shape for data points
*/
Shape getPointShape();

/**
* Gets the point shape for the given data point.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,41 @@ public Paint getSeriesColor() {
return shapeColors.getDefaultValue();
}

@Override
public String getPointLabel() {
return shapeLabels.getDefaultValue();
}

@Override
public String getPointLabel(int i) {
return shapeLabels.get(i);
}

@Override
public Shape getPointShape() {
return pointShapes.getDefaultValue();
}

@Override
public Shape getPointShape(int i) {
return pointShapes.get(i);
}

@Override
public Double getPointSize() {
return shapeSizes.getDefaultValue();
}

@Override
public Double getPointSize(int i) {
return shapeSizes.get(i);
}

@Override
public Paint getPointColor() {
return shapeColors.getDefaultValue();
}

@Override
public Paint getPointColor(int i) {
return shapeColors.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public interface XYDataSeriesInternal extends XYDataSeries, DataSeriesInternal {
*/
double getY(int i);

/**
* Gets the default size for data points.
*
* @return default size for data points
*/
Double getPointSize();

/**
* Gets the size of the data point at index {@code i}.
*
Expand All @@ -43,6 +50,13 @@ public interface XYDataSeriesInternal extends XYDataSeries, DataSeriesInternal {
*/
Double getPointSize(int i);

/**
* Gets the default color for data points.
*
* @return default color for data points.
*/
Paint getPointColor();

/**
* Gets the color of the data point at index {@code i}.
*
Expand All @@ -51,6 +65,13 @@ public interface XYDataSeriesInternal extends XYDataSeries, DataSeriesInternal {
*/
Paint getPointColor(int i);

/**
* Gets the default label for data points.
*
* @return default label for data points.
*/
String getPointLabel();

/**
* Gets the label of the data point at index {@code i}.
*
Expand All @@ -59,6 +80,13 @@ public interface XYDataSeriesInternal extends XYDataSeries, DataSeriesInternal {
*/
String getPointLabel(int i);

/**
* Gets the default shape for data points.
*
* @return default shape for data points.
*/
Shape getPointShape();

/**
* Gets the shape of the data point at index {@code i}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ private FigureDescriptor translateFigure(FigureWidget f, Exporter exporter) {
return clientFigure.build();
}

private static void assignOptionalStringField(Object value, Consumer<String> setter, Runnable clear) {
if (value != null) {
setter.accept(value.toString());
} else {
clear.run();
}
}

private static <T> void assignOptionalField(T value, Consumer<T> setter, Runnable clear) {
if (value != null) {
setter.accept(value);
Expand Down Expand Up @@ -289,6 +297,8 @@ private FigureDescriptor.ChartDescriptor translate(ChartImpl chart) {
assignOptionalField(toCssColorString(s.getLineColor()), clientSeries::setLineColor,
clientSeries::clearLineColor);
// clientSeries.setLineStyle(s.getLineStyle().toString());
assignOptionalField(toCssColorString(s.getSeriesColor()), clientSeries::setShapeColor,
clientSeries::clearShapeColor);
assignOptionalField(s.getPointLabelFormat(), clientSeries::setPointLabelFormat,
clientSeries::clearPointLabelFormat);
assignOptionalField(s.getXToolTipPattern(), clientSeries::setXToolTipPattern,
Expand All @@ -300,6 +310,16 @@ private FigureDescriptor.ChartDescriptor translate(ChartImpl chart) {
// with the x and y we have so far mapped to this

if (s instanceof AbstractXYDataSeries) {
// TODO #3293: Individual point shapes/sizes/labels
// Right now just gets one set for the whole series
AbstractXYDataSeries abstractSeries = (AbstractXYDataSeries) s;
assignOptionalStringField(abstractSeries.getPointShape(), clientSeries::setShape,
clientSeries::clearShape);
assignOptionalField(abstractSeries.getPointSize(), clientSeries::setShapeSize,
clientSeries::clearShapeSize);
assignOptionalField(abstractSeries.getPointLabel(), clientSeries::setShapeLabel,
clientSeries::clearShapeLabel);

if (s instanceof IntervalXYDataSeriesArray) {
// interval (aka histogram)
IntervalXYDataSeriesArray series = (IntervalXYDataSeriesArray) s;
Expand Down Expand Up @@ -334,9 +354,17 @@ private FigureDescriptor.ChartDescriptor translate(ChartImpl chart) {
// warn about other unsupported series types
errorList.add("OpenAPI presently does not support series of type " + s.getClass());
}

// TODO color label size shape
} else if (s instanceof AbstractCategoryDataSeries) {
// TODO #3293: Individual point shapes/sizes/labels
// Right now just gets one set for the whole series
AbstractCategoryDataSeries abstractSeries = (AbstractCategoryDataSeries) s;
assignOptionalStringField(abstractSeries.getPointShape(), clientSeries::setShape,
clientSeries::clearShape);
assignOptionalField(abstractSeries.getPointSize(), clientSeries::setShapeSize,
clientSeries::clearShapeSize);
assignOptionalField(abstractSeries.getLabel(), clientSeries::setShapeLabel,
clientSeries::clearShapeLabel);

if (s instanceof CategoryDataSeriesPartitionedTable) {// bar and pie from a table
CategoryDataSeriesPartitionedTable series = (CategoryDataSeriesPartitionedTable) s;
clientAxes
Expand Down Expand Up @@ -382,7 +410,6 @@ private FigureDescriptor.ChartDescriptor translate(ChartImpl chart) {
} else if (s instanceof CategoryDataSeriesMap) {// bar and plot from constant data
errorList.add("OpenAPI presently does not support series of type " + s.getClass());
}
// TODO color label size shape
}

clientSeries.addAllDataSources(clientAxes.build().collect(Collectors.toList()));
Expand Down

0 comments on commit c9beb0b

Please sign in to comment.