More options to create a bubble plot using color/sizeMode/opacity
package tech.tablesaw.plotly.api;
import java.util.List;
+import tech.tablesaw.api.NumericColumn;
import tech.tablesaw.api.Table;
+import tech.tablesaw.columns.Column;
import tech.tablesaw.plotly.components.Figure;
import tech.tablesaw.plotly.components.Layout;
import tech.tablesaw.plotly.components.Marker;
+import tech.tablesaw.plotly.components.Marker.MarkerBuilder;
+import tech.tablesaw.plotly.components.Marker.SizeMode;
import tech.tablesaw.plotly.traces.ScatterTrace;
import tech.tablesaw.table.TableSliceGroup;
@@ -37,23 +41,66 @@ public class BubblePlot {
return new Figure(layout, traces);
}
+ /**
+ * create a bubble plot using more options including color/sizeMode/opacity
+ *
+ * @param title plot title
+ * @param xColumn non-nullable, column data for x-axis
+ * @param yColumn non-nullable, column data for y-axis
+ * @param sizeColumn nullable, indicate the bubble size
+ * @param color color for every data point
+ * @param sizeMode check {@link SizeMode}
+ * @param opacity display opacity
+ * @return bubble plot created from given parameters
+ */
public static Figure create(
- String title, Table table, String xCol, String yCol, String sizeColumn) {
- Layout layout = Layout.builder(title, xCol, yCol).build();
+ String title,
+ Column xColumn,
+ Column yColumn,
+ NumericColumn sizeColumn,
+ double[] color,
+ SizeMode sizeMode,
+ Double opacity) {
- Marker marker =
- Marker.builder()
- .size(table.numberColumn(sizeColumn))
- // .opacity(.75)
- .build();
+ Layout layout = Layout.builder(title, xColumn.name(), yColumn.name()).build();
- ScatterTrace trace =
- ScatterTrace.builder(table.numberColumn(xCol), table.numberColumn(yCol))
- .marker(marker)
- .build();
+ Marker marker = null;
+ MarkerBuilder builder = Marker.builder();
+ if (sizeColumn != null) {
+ builder.size(sizeColumn);
+ }
+ if (opacity != null) {
+ builder.opacity(opacity);
+ }
+ if (color != null) {
+ builder.color(color);
+ }
+ if (sizeMode != null) {
+ builder.sizeMode(sizeMode);
+ }
+ marker = builder.build();
+
+ ScatterTrace trace = ScatterTrace.builder(xColumn, yColumn).marker(marker).build();
return new Figure(layout, trace);
}
+ /**
+ * create a bubble plot using column names
+ *
+ * @param title plot title
+ * @param table source {@link Table} to fetch plot datap points
+ * @param xCol non-nullable, column name for x-axis
+ * @param yCol non-nullable, column name for y-axis
+ * @param sizeCol nullable, column name for bubble size
+ * @return bubble plot created from given parameters
+ */
+ public static Figure create(String title, Table table, String xCol, String yCol, String sizeCol) {
+ NumericColumn xColumn = table.numberColumn(xCol);
+ NumericColumn yColumn = table.numberColumn(yCol);
+ NumericColumn sizeColumn = sizeCol == null ? null : table.numberColumn(sizeCol);
+ return create(title, xColumn, yColumn, sizeColumn, null, null, null);
+ }
+
public static Figure create(
String title, String xTitle, double[] xCol, String yTitle, double[] yCol) {
Layout layout = Layout.builder(title, xTitle, yTitle).build();
