Skip to content

Commit 1a8bc14

Browse files
committed
Refs #258: Fixes chart legend colours.
1 parent caec0d8 commit 1a8bc14

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/control/ControlPanelMediator.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.awt.event.ItemEvent;
1414
import java.awt.event.MouseAdapter;
1515
import java.awt.event.MouseEvent;
16-
import java.util.Arrays;
1716
import java.util.Collection;
1817
import java.util.Collections;
1918
import java.util.HashMap;
@@ -299,11 +298,7 @@ public CyCustomGraphics2<?> createChart(EMStyleOptions options) {
299298
dataSets, columnDescriptor, columnIdFactory);
300299
ChartType type = chartOptions.getType();
301300

302-
List<Color> colors = chartOptions.getColorScheme().getColors();
303-
304-
// Swap UP and ZERO colors if q or p-value (it should not have negative values!)
305-
if ((data == ChartData.FDR_VALUE || data == ChartData.P_VALUE) && colors.size() == 3)
306-
colors = Arrays.asList(new Color[] { colors.get(1), colors.get(0), colors.get(1) });
301+
List<Color> colors = ChartUtil.getChartColors(chartOptions);
307302

308303
Map<String, Object> props = new HashMap<>(type.getProperties());
309304
props.put("cy_dataColumns", columns);
@@ -328,7 +323,7 @@ public CyCustomGraphics2<?> createChart(EMStyleOptions options) {
328323

329324
return chart;
330325
}
331-
326+
332327
@Override
333328
public void handleEvent(SetCurrentNetworkViewEvent e) {
334329
if (getControlPanel().isDisplayable())

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/util/ChartUtil.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import javax.swing.UIManager;
1616

1717
import org.baderlab.csplugins.enrichmentmap.model.EMDataSet;
18+
import org.baderlab.csplugins.enrichmentmap.style.ChartData;
1819
import org.baderlab.csplugins.enrichmentmap.style.ChartOptions;
1920
import org.baderlab.csplugins.enrichmentmap.style.ColorScheme;
2021
import org.baderlab.csplugins.enrichmentmap.style.ColumnDescriptor;
@@ -133,6 +134,21 @@ else if (row.isSet(column.getName()))
133134
return range;
134135
}
135136

137+
public static List<Color> getChartColors(ChartOptions options) {
138+
ColorScheme colorScheme = options != null ? options.getColorScheme() : null;
139+
List<Color> colors = colorScheme != null ? colorScheme.getColors() : null;
140+
ChartData data = options != null ? options.getData() : null;
141+
142+
// Swap UP and ZERO colors if q or p-value (it should not have negative values!)
143+
if ((data == ChartData.FDR_VALUE || data == ChartData.P_VALUE) && colors.size() == 3)
144+
colors = Arrays.asList(new Color[] { colors.get(1), colors.get(0), colors.get(1) });
145+
146+
if (colors == null || colors.size() < 3) // UP, ZERO, DOWN:
147+
colors = Arrays.asList(new Color[] { Color.LIGHT_GRAY, Color.WHITE, Color.DARK_GRAY });
148+
149+
return colors;
150+
}
151+
136152
public static JFreeChart createRadialHeatMapLegend(List<EMDataSet> dataSets, ChartOptions options) {
137153
// All the slices must have the same size
138154
final DefaultPieDataset pieDataset = new DefaultPieDataset();
@@ -169,19 +185,17 @@ public static JFreeChart createRadialHeatMapLegend(List<EMDataSet> dataSets, Cha
169185
plot.setLabelShadowPaint(TRANSPARENT_COLOR);
170186
plot.setToolTipGenerator(new StandardPieToolTipGenerator("{0}"));
171187

172-
ColorScheme colorScheme = options != null ? options.getColorScheme() : null;
173-
List<Color> colors = colorScheme != null ? colorScheme.getColors() : null;
174-
175-
if (colors == null || colors.size() < 3) // UP, ZERO, DOWN:
176-
colors = Arrays.asList(new Color[] { Color.LIGHT_GRAY, Color.WHITE, Color.DARK_GRAY });
188+
List<Color> colors = getChartColors(options);
177189

178190
int total = dataSets.size();
179-
int v = total / -2;
191+
int lowerBound = options.getData() == ChartData.NES_VALUE ? -total : 0;
192+
int upperBound = lowerBound + (2 * total);
193+
int v = lowerBound / 2;
180194

181195
for (EMDataSet ds : dataSets) {
182196
plot.setSectionPaint(
183197
ds.getName(),
184-
ColorUtil.getColor(v, -total, total, colors.get(2), colors.get(1), colors.get(0))
198+
ColorUtil.getColor(v, lowerBound, upperBound, colors.get(2), colors.get(1), colors.get(0))
185199
);
186200
v++;
187201
}
@@ -231,18 +245,16 @@ public static JFreeChart createHeatMapLegend(List<EMDataSet> dataSets, ChartOpti
231245
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
232246
rangeAxis.setVisible(false);
233247

234-
ColorScheme colorScheme = options != null ? options.getColorScheme() : null;
235-
List<Color> colors = colorScheme != null ? colorScheme.getColors() : null;
236-
237-
if (colors == null || colors.size() < 3) // UP, ZERO, DOWN:
238-
colors = Arrays.asList(new Color[] { Color.LIGHT_GRAY, Color.WHITE, Color.DARK_GRAY });
248+
List<Color> colors = getChartColors(options);
239249

240250
List<Color> itemColors = new ArrayList<>();
241251
int total = dataSets.size();
242-
int v = total / 2;
252+
int lowerBound = options.getData() == ChartData.NES_VALUE ? -total : 0;
253+
int upperBound = lowerBound + (2 * total);
254+
int v = lowerBound / 2;
243255

244256
for (int i = 0; i < total; i++)
245-
itemColors.add(ColorUtil.getColor(v--, -total, total, colors.get(2), colors.get(1), colors.get(0)));
257+
itemColors.add(ColorUtil.getColor(v++, lowerBound, upperBound, colors.get(2), colors.get(1), colors.get(0)));
246258

247259
final BarRenderer renderer = new BarRenderer() {
248260
@Override
@@ -264,7 +276,9 @@ public Paint getItemPaint(int row, int column) {
264276
public static JFreeChart createHeatStripsLegend(List<EMDataSet> dataSets, ChartOptions options) {
265277
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
266278
int total = dataSets.size();
267-
int v = total / -2;
279+
int lowerBound = options.getData() == ChartData.NES_VALUE ? -total : 0;
280+
int upperBound = lowerBound + (2 * total);
281+
int v = lowerBound / 2;
268282

269283
for (int i = 0; i < total; i++) {
270284
if (v == 0.0) v = 1; // Just to make sure there is always a bar for each data set name
@@ -313,18 +327,13 @@ public static JFreeChart createHeatStripsLegend(List<EMDataSet> dataSets, ChartO
313327
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
314328
rangeAxis.setVisible(false);
315329

316-
ColorScheme colorScheme = options != null ? options.getColorScheme() : null;
317-
List<Color> colors = colorScheme != null ? colorScheme.getColors() : null;
318-
319-
if (colors == null || colors.size() < 3) // UP, ZERO, DOWN:
320-
colors = Arrays.asList(new Color[] { Color.LIGHT_GRAY, Color.WHITE, Color.DARK_GRAY });
321-
330+
List<Color> colors = getChartColors(options);
322331
List<Color> itemColors = new ArrayList<>();
323332

324333
for (int i = 0; i < total; i++) {
325334
Number n = dataset.getValue(options.getData().toString(), dataSets.get(i).getName());
326335
itemColors.add(
327-
ColorUtil.getColor(n.doubleValue(), -total, total, colors.get(2), colors.get(1), colors.get(0)));
336+
ColorUtil.getColor(n.doubleValue(), lowerBound, upperBound, colors.get(2), colors.get(1), colors.get(0)));
328337
}
329338

330339
final BarRenderer renderer = new BarRenderer() {

0 commit comments

Comments
 (0)