|
| 1 | +package tableplugins; |
| 2 | + |
| 3 | +import net.imagej.plot.AbstractPlot; |
| 4 | +import net.imagej.plot.BoxSeries; |
| 5 | +import net.imagej.plot.CategoryChart; |
| 6 | +import net.imagej.plot.PlotService; |
| 7 | +import net.imagej.table.Column; |
| 8 | +import net.imagej.table.Table; |
| 9 | +import org.scijava.ItemIO; |
| 10 | +import org.scijava.ItemVisibility; |
| 11 | +import org.scijava.command.Command; |
| 12 | +import org.scijava.plugin.Parameter; |
| 13 | +import org.scijava.plugin.Plugin; |
| 14 | +import net.imagej.ui.swing.widget.MultipleChoices; |
| 15 | +import net.imagej.ui.swing.widget.MutableChoices; |
| 16 | + |
| 17 | +import java.util.*; |
| 18 | + |
| 19 | +@Plugin(type = Command.class, menuPath="Table>BoxPlot") |
| 20 | +public class BoxPlotPlugin implements Command { |
| 21 | + |
| 22 | + @Parameter(label = "Select table", callback = "tableChanged") |
| 23 | + private Table<Column<?>,?> table; |
| 24 | + |
| 25 | + @Parameter(label = "Select columns", initializer = "tableChanged") |
| 26 | + public MultipleChoices<Column<Double>> valueColumns; |
| 27 | + |
| 28 | + @Parameter(visibility = ItemVisibility.MESSAGE) |
| 29 | + private final String LABEL_OPTIONAL = "optional"; |
| 30 | + |
| 31 | + @Parameter(label = "Select key column", initializer = "tableChanged", required = false) |
| 32 | + public MutableChoices<Column<?>> keyColumn; |
| 33 | + |
| 34 | + @Parameter(label = "Boxplot", type = ItemIO.OUTPUT) |
| 35 | + public AbstractPlot output; |
| 36 | + |
| 37 | + @Parameter |
| 38 | + private PlotService plotService; |
| 39 | + |
| 40 | + @Override |
| 41 | + public void run() { |
| 42 | + createChart(); |
| 43 | + } |
| 44 | + |
| 45 | + private void tableChanged() { |
| 46 | + if(keyColumn != null) |
| 47 | + keyColumn.setChoices(table, c -> c.getHeader()); |
| 48 | + if(valueColumns != null) { |
| 49 | + Iterable<Column<Double>> choices = TablePluginUtils.filterDoubleColumns(table); |
| 50 | + valueColumns.setChoices(choices, c -> c.getHeader()); |
| 51 | + for(Column<Double> choice : choices) |
| 52 | + valueColumns.set(choice, true); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private void createChart() { |
| 57 | + if (keyColumn != null) |
| 58 | + buildChartWithKeys(); |
| 59 | + else |
| 60 | + buildChartWithoutKeys(); |
| 61 | + } |
| 62 | + |
| 63 | + private void buildChartWithKeys() { |
| 64 | + output = ChartBuilderWithKey.build(plotService, keyColumn.get(), valueColumns.get()); |
| 65 | + buildTitle(); |
| 66 | + } |
| 67 | + |
| 68 | + private void buildChartWithoutKeys() { |
| 69 | + CategoryChart<String> chart = plotService.newCategoryChart(String.class); |
| 70 | + chart.numberAxis().setAutoRange(); |
| 71 | + chart.categoryAxis().setLabel("Column"); |
| 72 | + Map<String, Collection<Double>> data = new TreeMap<>(); |
| 73 | + for (Column<Double> valueColumn : valueColumns.get()) |
| 74 | + data.put(valueColumn.getHeader(), valueColumn); |
| 75 | + BoxSeries<String> series = chart.addBoxSeries(); |
| 76 | + series.setLabel("data"); |
| 77 | + series.setLegendVisible(false); |
| 78 | + series.setValues(data); |
| 79 | + output = chart; |
| 80 | + buildTitle(); |
| 81 | + } |
| 82 | + |
| 83 | + private void buildTitle() { |
| 84 | + String title = "Boxplot - "; |
| 85 | + for(Column<Double> col : valueColumns.get()) |
| 86 | + title += col.getHeader() + ", "; |
| 87 | + output.setTitle(title.substring(0, title.length() - 2)); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + static private class ChartBuilderWithKey<K> { |
| 92 | + |
| 93 | + final PlotService plotService; |
| 94 | + |
| 95 | + private CategoryChart<K> chart; |
| 96 | + |
| 97 | + private ChartBuilderWithKey(PlotService plotService) { |
| 98 | + this.plotService = plotService; |
| 99 | + } |
| 100 | + |
| 101 | + private CategoryChart<K> getChart(Column<K> keyColumn, Iterable<Column<Double>> valueColumns) { |
| 102 | + chart = plotService.newCategoryChart(keyColumn.getType()); |
| 103 | + chart.numberAxis().setAutoRange(); |
| 104 | + chart.categoryAxis().setLabel(keyColumn.getHeader()); |
| 105 | + for (Column<Double> valueColumn : valueColumns) |
| 106 | + addColumn(keyColumn, valueColumn); |
| 107 | + return chart; |
| 108 | + } |
| 109 | + |
| 110 | + private void addColumn(Column<K> keyColumn, Column<Double> valueColumn) { |
| 111 | + BoxSeries<K> series = chart.addBoxSeries(); |
| 112 | + series.setLabel(valueColumn.getHeader()); |
| 113 | + MyMultiMap<K, Double> map = new MyMultiMap<>(keyColumn, valueColumn); |
| 114 | + series.setValues(map.toMap()); |
| 115 | + } |
| 116 | + |
| 117 | + static private <K> CategoryChart<K> build(PlotService plotService, |
| 118 | + Column<K> keyColumn, Iterable<Column<Double>> valueColumn) { |
| 119 | + return new ChartBuilderWithKey<K>(plotService).getChart(keyColumn, valueColumn); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + static private class MyMultiMap<K,V> { |
| 124 | + |
| 125 | + private Map<K, List<V>> map; |
| 126 | + |
| 127 | + public MyMultiMap(Collection<? extends K> keys, Collection<? extends V> values) { |
| 128 | + map = new HashMap<>(); |
| 129 | + Iterator<? extends K> kIterator = keys.iterator(); |
| 130 | + Iterator<? extends V> vIterator = values.iterator(); |
| 131 | + while(kIterator.hasNext() && vIterator.hasNext()) |
| 132 | + add(kIterator.next(), vIterator.next()); |
| 133 | + } |
| 134 | + |
| 135 | + public void add(K k, V v) { get(k).add(v); } |
| 136 | + |
| 137 | + private List<V> get(K k) { |
| 138 | + List<V> list = map.get(k); |
| 139 | + if(list == null) { |
| 140 | + list = new ArrayList<>(); |
| 141 | + map.put(k, list); |
| 142 | + } |
| 143 | + return list; |
| 144 | + } |
| 145 | + |
| 146 | + public Map<K, List<V>> toMap() { return map; } |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments