Skip to content

Commit e66d698

Browse files
committed
Show color legend in chart legend. Refs #266
1 parent cb0471e commit e66d698

File tree

4 files changed

+180
-121
lines changed

4 files changed

+180
-121
lines changed

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/style/ChartType.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,33 @@
33
import java.util.AbstractMap.SimpleEntry;
44
import java.util.Collections;
55
import java.util.Map;
6+
import java.util.Map.Entry;
67
import java.util.stream.Collectors;
78
import java.util.stream.Stream;
89

910
import org.baderlab.csplugins.enrichmentmap.style.charts.Rotation;
1011
import org.baderlab.csplugins.enrichmentmap.style.charts.radialheatmap.RadialHeatMapChart;
1112

1213
public enum ChartType {
14+
15+
1316
RADIAL_HEAT_MAP(
1417
RadialHeatMapChart.FACTORY_ID,
1518
"Radial Heat Map",
1619
Collections.unmodifiableMap(Stream.of(
17-
new SimpleEntry<>("cy_borderWidth", 0.0f),
18-
new SimpleEntry<>("cy_rotation", Rotation.CLOCKWISE),
20+
new SimpleEntry<>("cy_borderWidth", 0.0f),
21+
new SimpleEntry<>("cy_rotation", Rotation.CLOCKWISE),
1922
new SimpleEntry<>("cy_startAngle", 90.0f))
20-
.collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue())))
23+
.collect(Collectors.toMap(Entry::getKey, Entry::getValue)))
2124
),
2225
HEAT_MAP(
2326
"org.cytoscape.HeatMapChart",
2427
"Heat Map",
2528
Collections.unmodifiableMap(Stream.of(
2629
new SimpleEntry<>("cy_orientation", "VERTICAL"),
27-
new SimpleEntry<>("cy_showDomainAxis", false),
28-
new SimpleEntry<>("cy_showRangeAxis", false))
29-
.collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue())))
30+
new SimpleEntry<>("cy_showDomainAxis", false),
31+
new SimpleEntry<>("cy_showRangeAxis", false))
32+
.collect(Collectors.toMap(Entry::getKey, Entry::getValue)))
3033
),
3134
HEAT_STRIPS(
3235
"org.cytoscape.BarChart",
@@ -36,7 +39,7 @@ public enum ChartType {
3639
new SimpleEntry<>("cy_orientation", "VERTICAL"),
3740
new SimpleEntry<>("cy_showDomainAxis", false),
3841
new SimpleEntry<>("cy_showRangeAxis", false))
39-
.collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue())))
42+
.collect(Collectors.toMap(Entry::getKey, Entry::getValue)))
4043
);
4144

4245
private final String id;
@@ -78,4 +81,5 @@ public static ChartType toChartType(String chartName) {
7881
public String toString() {
7982
return label;
8083
}
84+
8185
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.baderlab.csplugins.enrichmentmap.view.control;
22

3-
import static org.baderlab.csplugins.enrichmentmap.style.EMStyleBuilder.Columns.EDGE_DATASET_VALUE_COMPOUND;
4-
import static org.baderlab.csplugins.enrichmentmap.style.EMStyleBuilder.Columns.EDGE_INTERACTION_VALUE_SIG;
5-
import static org.baderlab.csplugins.enrichmentmap.style.EMStyleBuilder.Columns.NODE_GS_TYPE;
6-
import static org.baderlab.csplugins.enrichmentmap.style.EMStyleBuilder.Columns.NODE_GS_TYPE_ENRICHMENT;
3+
import static org.baderlab.csplugins.enrichmentmap.style.EMStyleBuilder.Columns.*;
74
import static org.baderlab.csplugins.enrichmentmap.view.util.SwingUtil.invokeOnEDT;
85

96
import java.awt.Color;
@@ -293,18 +290,17 @@ public CyCustomGraphics2<?> createChart(EMStyleOptions options) {
293290
// Ignore Signature Data Sets in charts
294291
Set<EMDataSet> dataSets = filterDataSets(options.getDataSets());
295292

296-
if (dataSets.size() > 0) {
293+
if (!dataSets.isEmpty()) {
297294
ColumnDescriptor<Double> columnDescriptor = data.getColumnDescriptor();
298295
List<CyColumnIdentifier> columns = ChartUtil.getSortedColumnIdentifiers(options.getAttributePrefix(),
299296
dataSets, columnDescriptor, columnIdFactory);
300297
ChartType type = chartOptions.getType();
301298

302299
List<Color> colors = ChartUtil.getChartColors(chartOptions);
300+
List<Double> range = ChartUtil.calculateGlobalRange(options.getNetworkView().getModel(), columns);
303301

304302
Map<String, Object> props = new HashMap<>(type.getProperties());
305303
props.put("cy_dataColumns", columns);
306-
307-
List<Double> range = ChartUtil.calculateGlobalRange(options.getNetworkView().getModel(), columns);
308304
props.put("cy_range", range);
309305
props.put("cy_autoRange", false);
310306
props.put("cy_globalRange", true);

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/legend/ColorLegendPanel.java

Lines changed: 89 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -43,128 +43,122 @@
4343

4444
package org.baderlab.csplugins.enrichmentmap.view.legend;
4545

46+
import java.awt.BorderLayout;
4647
import java.awt.Color;
4748
import java.awt.Dimension;
48-
import java.awt.Font;
4949
import java.awt.GradientPaint;
5050
import java.awt.Graphics;
5151
import java.awt.Graphics2D;
5252
import java.awt.RenderingHints;
5353
import java.awt.geom.Point2D;
5454
import java.awt.geom.Rectangle2D;
5555

56+
import javax.swing.BorderFactory;
57+
import javax.swing.JLabel;
5658
import javax.swing.JPanel;
5759
import javax.swing.UIManager;
5860

59-
import org.cytoscape.util.swing.LookAndFeelUtil;
60-
61-
/**
62-
* Created by
63-
* User: risserlin
64-
* Date: Feb 5, 2009
65-
* Time: 3:55:52 PM
66-
* <p>
67-
* enrichment map legend panel
68-
*/
61+
import org.baderlab.csplugins.enrichmentmap.view.util.SwingUtil;
6962

7063
@SuppressWarnings("serial")
7164
public class ColorLegendPanel extends JPanel {
7265

7366
private static final int WIDTH = 150;
7467
private static final int HEIGHT = 36;
75-
76-
private final Color minColor;
77-
private final Color maxColor;
78-
private final String phenotype1;
79-
private final String phenotype2;
68+
69+
private final Color minColor;
70+
private final Color maxColor;
71+
72+
private final boolean legacy;
8073

8174
public ColorLegendPanel(Color minColor, Color maxColor, String phenotype1, String phenotype2) {
82-
this.minColor = minColor;
83-
this.maxColor = maxColor;
84-
this.phenotype1 = phenotype1;
85-
this.phenotype2 = phenotype2;
86-
87-
setPreferredSize(new Dimension(WIDTH, HEIGHT));
88-
setOpaque(false);
89-
}
90-
91-
@Override
92-
public void paint(Graphics g) {
93-
final int w = getWidth();
94-
final int h = getHeight();
95-
96-
if (w <= 0 || h <= 0)
97-
return;
75+
this(minColor, maxColor, phenotype1, phenotype2, true);
76+
}
77+
78+
public ColorLegendPanel(Color minColor, Color maxColor, String phenotype1, String phenotype2, boolean legacy) {
79+
this.legacy = legacy;
80+
this.minColor = minColor;
81+
this.maxColor = maxColor;
82+
83+
setPreferredSize(new Dimension(WIDTH, HEIGHT));
84+
setOpaque(false);
85+
86+
JPanel gradientRectangle = new GradientRectanglePanel();
87+
JPanel gradientParentPanel = new JPanel(new BorderLayout());
88+
gradientParentPanel.add(gradientRectangle, BorderLayout.CENTER);
89+
gradientParentPanel.setBorder(BorderFactory.createLineBorder(UIManager.getColor("Separator.foreground")));
9890

99-
Graphics2D g2d = (Graphics2D) g;
100-
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
101-
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
91+
JLabel minLabel = new JLabel(phenotype1);
92+
JLabel maxLabel = new JLabel(phenotype2);
93+
SwingUtil.makeSmall(minLabel, maxLabel);
94+
JPanel labelPanel = new JPanel(new BorderLayout());
95+
labelPanel.add(minLabel, BorderLayout.WEST);
96+
labelPanel.add(maxLabel, BorderLayout.EAST);
97+
labelPanel.setOpaque(false);
98+
99+
setLayout(new BorderLayout());
100+
add(gradientParentPanel, BorderLayout.CENTER);
101+
add(labelPanel, BorderLayout.SOUTH);
102+
}
103+
104+
private class GradientRectanglePanel extends JPanel {
105+
@Override
106+
public void paint(Graphics g) {
107+
if (getWidth() <= 0 || getHeight() <= 0)
108+
return;
109+
110+
Graphics2D g2d = (Graphics2D) g;
111+
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
112+
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
113+
114+
if (legacy)
115+
paintLegacyOneDatasetGradient(g2d);
116+
else
117+
paintNormalGradient(g2d);
118+
}
119+
120+
private void paintNormalGradient(Graphics2D g2d) {
121+
final int w = getWidth();
122+
final int h = getHeight();
123+
124+
Point2D.Float p1 = new Point2D.Float(0f, 0f); // Gradient line start
125+
Point2D.Float p2 = new Point2D.Float(w, 0f); // Gradient line end
126+
127+
GradientPaint g1 = new GradientPaint(p1, minColor, p2, maxColor, false); // Acyclic gradient
128+
Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x, p1.y, w, h);
129+
130+
g2d.setPaint(g1);
131+
g2d.fill(rect1);
132+
}
102133

103-
float ww = w / 5.f;
104-
float hPad = ww / 2.f; // To center the legend horizontally
105-
106-
Point2D.Float p1 = new Point2D.Float(hPad, 0.f); //Gradient line start
107-
Point2D.Float p2 = new Point2D.Float(hPad + ww, 0.f); //Gradient line end
108-
109-
//empty white box
110-
Point2D.Float p3 = new Point2D.Float(hPad + ww, 0.f); //Gradient line start
111-
112-
Point2D.Float p5 = new Point2D.Float(hPad + 2 * ww, 0.f); //Gradient line start
113-
114-
Point2D.Float p7 = new Point2D.Float(hPad + 3 * ww, 0.f); //Gradient line start
115-
Point2D.Float p8 = new Point2D.Float(hPad + 4 * ww, 0.f); //Gradient line end
116-
117-
float w1 = 30;
118-
float w2 = 30;
119-
float hh = h / 2;
120-
121-
// Need to create two gradients, one one for the max and one for the min
122-
GradientPaint g1 = new GradientPaint(p1, minColor, p2, Color.WHITE, false); //Acyclic gradient
123-
GradientPaint g2 = new GradientPaint(p7, Color.WHITE, p8, maxColor, false); //Acyclic gradient
124-
Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x , p1.y, w1, hh);
125-
Rectangle2D.Float rect2 = new Rectangle2D.Float(p3.x , p3.y, w2, hh);
126-
Rectangle2D.Float rect3 = new Rectangle2D.Float(p5.x , p5.y, w2, hh);
127-
Rectangle2D.Float rect4 = new Rectangle2D.Float(p7.x , p7.y, w1, hh);
128-
129-
g2d.setFont(getLabelFont());
130-
float tyOffset = hh + h / 3.f; // Text y offset
131-
132-
if (minColor != Color.WHITE) {
134+
private void paintLegacyOneDatasetGradient(Graphics2D g2d) {
135+
final int w = getWidth();
136+
final int h = getHeight();
137+
float ww = w / 5f;
138+
139+
// first box
140+
Point2D.Float p1 = new Point2D.Float(0f, 0f); // Gradient line start
141+
Point2D.Float p2 = new Point2D.Float(ww, 0f); // Gradient line end
142+
143+
// empty white box
144+
Point2D.Float p3 = new Point2D.Float(ww * 4, 0f); // Gradient line start
145+
Point2D.Float p4 = new Point2D.Float(ww * 5, 0f); // Gradient line start
146+
147+
GradientPaint g1 = new GradientPaint(p1, minColor, p2, Color.WHITE, false); // Acyclic gradient
148+
GradientPaint g2 = new GradientPaint(p3, Color.WHITE, p4, maxColor, false); // Acyclic gradient
149+
150+
Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x, p1.y, ww, h);
151+
Rectangle2D.Float rect2 = new Rectangle2D.Float(p2.x, p2.y, ww*3, h);
152+
Rectangle2D.Float rect3 = new Rectangle2D.Float(p3.x, p3.y, ww, h);
153+
133154
g2d.setPaint(g1);
134155
g2d.fill(rect1);
135156
g2d.setPaint(Color.WHITE);
136-
137-
// make a white block
138-
g2d.setPaint(Color.WHITE);
139157
g2d.fill(rect2);
140-
141-
g2d.setPaint(getLabelForeground());
142-
g2d.drawString(phenotype1, p1.x, p1.y + tyOffset);
143-
} else {
144-
g2d.setPaint(getLabelForeground());
145-
g2d.drawString(phenotype1, p5.x, p5.y + tyOffset);
158+
g2d.setPaint(g2);
159+
g2d.fill(rect3);
160+
146161
}
147-
148-
// Make a white block
149-
g2d.setPaint(Color.WHITE);
150-
g2d.fill(rect3);
151-
152-
g2d.setPaint(g2);
153-
g2d.fill(rect4);
154-
155-
// Border
156-
g2d.setPaint(UIManager.getColor("Separator.foreground"));
157-
g2d.draw(new Rectangle2D.Float(p1.x , p1.y, (2 * w1 + 2 * w2), hh));
158-
159-
g2d.setPaint(getLabelForeground());
160-
g2d.drawString(phenotype2, p7.x, p7.y + tyOffset);
161162
}
162163

163-
private static Font getLabelFont() {
164-
return UIManager.getFont("Label.font").deriveFont(LookAndFeelUtil.getSmallFontSize());
165-
}
166-
167-
private static Color getLabelForeground() {
168-
return UIManager.getColor("Label.foreground");
169-
}
170164
}

0 commit comments

Comments
 (0)