Skip to content

Commit 39cc0c5

Browse files
committed
Legend works for "Color by dataset". Fixes #321
1 parent 259f273 commit 39cc0c5

File tree

3 files changed

+108
-31
lines changed

3 files changed

+108
-31
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import javax.swing.Icon;
1616

17+
import org.baderlab.csplugins.enrichmentmap.style.ChartData;
1718
import org.cytoscape.work.AbstractTask;
1819
import org.cytoscape.work.TaskMonitor;
1920
import org.jfree.chart.JFreeChart;
@@ -61,6 +62,7 @@ public void run(TaskMonitor taskMonitor) throws IOException, DocumentException {
6162
addTo(document, createNodeShapeSection(writer));
6263
addTo(document, createChartSection(writer));
6364
addTo(document, createChartColorSection(writer));
65+
addTo(document, createNodeDataSetColorSection(writer));
6466
addTo(document, createEdgeColorSection(writer));
6567

6668
document.close();
@@ -173,15 +175,28 @@ private List<Element> createChartColorSection(PdfWriter writer) throws DocumentE
173175
}
174176

175177

176-
177178
private List<Element> createEdgeColorSection(PdfWriter writer) throws DocumentException {
178179
Map<Object,Paint> edgeColors = content.getEdgeColors();
179180
if(edgeColors == null || edgeColors.isEmpty())
180181
return null;
181-
Paragraph title = new Paragraph(new Chunk(LegendContent.EDGE_COLOR_HEADER));
182-
PdfPTable table = new PdfPTable(new float[] {1,1.2f});
182+
return createColorsSection(writer, edgeColors, LegendContent.EDGE_COLOR_HEADER);
183+
}
184+
185+
private List<Element> createNodeDataSetColorSection(PdfWriter writer) throws DocumentException {
186+
ChartData data = content.getOptions().getChartOptions().getData();
187+
if(data != ChartData.DATA_SET )
188+
return null;
189+
Map<Object,Paint> dataSetColors = content.getDataSetColors();
190+
if(dataSetColors == null || dataSetColors.isEmpty())
191+
return null;
192+
return createColorsSection(writer, dataSetColors, LegendContent.NODE_DATA_SET_COLOR_HEADER);
193+
}
194+
195+
private List<Element> createColorsSection(PdfWriter writer, Map<Object,Paint> colors, String titleText) throws DocumentException {
196+
Paragraph title = new Paragraph(new Chunk(titleText));
197+
PdfPTable table = new PdfPTable(new float[] {1f,10f});
183198
final int width = 40, height = 20;
184-
for(Map.Entry<Object,Paint> entry : edgeColors.entrySet()) {
199+
for(Map.Entry<Object,Paint> entry : colors.entrySet()) {
185200
Image colorRect = drawImage(writer, width, height, graphics -> {
186201
graphics.setPaint(entry.getValue());
187202
graphics.fillRect(0, 0, width, height);
@@ -191,8 +206,6 @@ private List<Element> createEdgeColorSection(PdfWriter writer) throws DocumentEx
191206
return Arrays.asList(title, table);
192207
}
193208

194-
195-
196209
private static Image drawImage(PdfWriter writer, int width, int height, Consumer<PdfGraphics2D> draw) throws DocumentException {
197210
PdfContentByte contentByte = writer.getDirectContent();
198211
PdfTemplate template = contentByte.createTemplate(width, height);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import javax.swing.Icon;
77

8+
import org.baderlab.csplugins.enrichmentmap.style.EMStyleOptions;
89
import org.jfree.chart.JFreeChart;
910

1011
public interface LegendContent {
@@ -13,6 +14,7 @@ public interface LegendContent {
1314
public static final String NODE_CHART_HEADER = "Node Charts";
1415
public static final String NODE_SHAPE_HEADER = "Node Shape";
1516
public static final String NODE_CHART_COLOR_HEADER = "Node Chart Colors";
17+
public static final String NODE_DATA_SET_COLOR_HEADER = "Data Set Color";
1618
public static final String EDGE_COLOR_HEADER = "Edge Stroke Color";
1719

1820
public static final int LEGEND_ICON_SIZE = 18;
@@ -33,4 +35,8 @@ public interface LegendContent {
3335
public ColorLegendPanel getChartNegLegend();
3436

3537
public Map<Object,Paint> getEdgeColors();
38+
39+
public Map<Object,Paint> getDataSetColors();
40+
41+
public EMStyleOptions getOptions();
3642
}

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

Lines changed: 83 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import java.awt.Paint;
5757
import java.text.Collator;
5858
import java.util.Collection;
59+
import java.util.LinkedHashMap;
5960
import java.util.List;
6061
import java.util.Map;
6162
import java.util.Map.Entry;
@@ -122,6 +123,7 @@ public class LegendPanel extends JPanel implements LegendContent {
122123

123124
private final Border DEF_LEGEND_BORDER = BorderFactory.createLineBorder(UIManager.getColor("Separator.foreground"));
124125
private final Color DEF_LEGEND_BG = Color.WHITE;
126+
private final Dimension COLOR_ICON_SIZE = new Dimension(LEGEND_ICON_SIZE, LEGEND_ICON_SIZE / 2);
125127

126128
@Inject private CyApplicationManager applicationManager;
127129
@Inject private VisualMappingManager visualMappingManager;
@@ -144,6 +146,7 @@ public class LegendPanel extends JPanel implements LegendContent {
144146
private final JPanel chartLegendPanel = new JPanel(new BorderLayout());
145147

146148
private JPanel edgeColorPanel;
149+
private JPanel dataSetColorPanel;
147150

148151
private EMStyleOptions options;
149152

@@ -168,6 +171,7 @@ public LegendPanel() {
168171
equalizeSize(nodeShapeDesc1, nodeShapeDesc2);
169172
}
170173

174+
@Override
171175
public EMStyleOptions getOptions() {
172176
return options;
173177
}
@@ -232,6 +236,7 @@ void update(EMStyleOptions options, Collection<EMDataSet> filteredDataSets) {
232236
nodeChartPanel = null;
233237
edgeLegendPanel = null;
234238
edgeColorPanel = null;
239+
dataSetColorPanel = null;
235240

236241
nodeColorLegend = null;
237242
chartPosLegend = null;
@@ -244,6 +249,7 @@ void update(EMStyleOptions options, Collection<EMDataSet> filteredDataSets) {
244249
updateNodeShapePanel();
245250
updateNodeChartPanel(filteredDataSets);
246251
updateNodeChartColorPanel(filteredDataSets);
252+
updateNodeDataSetColorPanel();
247253
updateEdgeColorPanel();
248254

249255
JPanel panel = new JPanel();
@@ -272,7 +278,9 @@ private void updateNodeColorPanel(Collection<EMDataSet> dataSets) {
272278
JPanel p = getNodeColorPanel();
273279
p.removeAll();
274280

275-
if (dataSets != null && dataSets.size() == 1) {
281+
ChartData data = options.getChartOptions().getData();
282+
283+
if (dataSets != null && dataSets.size() == 1 && data == ChartData.NONE) {
276284
EMDataSet ds = dataSets.iterator().next();
277285

278286
nodeColorLegend = new ColorLegendPanel(
@@ -306,7 +314,7 @@ private void updateNodeChartColorPanel(Collection<EMDataSet> dataSets) {
306314
ChartOptions chartOptions = options.getChartOptions();
307315
ChartData data = chartOptions.getData();
308316

309-
if(data != ChartData.NONE) {
317+
if(data != ChartData.NONE && data != ChartData.DATA_SET) {
310318
AbstractColumnDescriptor columnDescriptor = data.getColumnDescriptor();
311319
List<CyColumnIdentifier> columns = ChartUtil.getSortedColumnIdentifiers(options.getAttributePrefix(),
312320
dataSets, columnDescriptor, columnIdFactory);
@@ -393,7 +401,7 @@ private void updateNodeShapePanel() {
393401
p.revalidate();
394402
}
395403

396-
private void updateNodeChartPanel(Collection<EMDataSet> dataSets) {
404+
private void updateNodeChartPanel(Collection<EMDataSet> filteredDataSets) {
397405
JPanel p = getNodeChartPanel();
398406
chartLegendPanel.removeAll();
399407

@@ -409,8 +417,8 @@ private void updateNodeChartPanel(Collection<EMDataSet> dataSets) {
409417
Object cg = vp != null ? style.getDefaultValue(vp) : null;
410418
ChartType chartType = options.getChartOptions() != null ? options.getChartOptions().getType() : null;
411419

412-
if (chartType != null && cg instanceof CyCustomGraphics2 && dataSets != null) {
413-
ChartPanel chart = createChartPanel(dataSets);
420+
if (chartType != null && cg instanceof CyCustomGraphics2 && filteredDataSets != null) {
421+
ChartPanel chart = createChartPanel(filteredDataSets);
414422

415423
if (chart != null) {
416424
JLabel titleLabel = new JLabel(getChartLabel());
@@ -429,11 +437,14 @@ private void updateNodeChartPanel(Collection<EMDataSet> dataSets) {
429437
p.revalidate();
430438
}
431439

432-
private ChartPanel createChartPanel(Collection<EMDataSet> dataSets) {
433-
List<EMDataSet> sortedDataSets = ChartUtil.sortDataSets(dataSets);
440+
private ChartPanel createChartPanel(Collection<EMDataSet> filteredDataSet) {
441+
List<EMDataSet> sortedDataSets = ChartUtil.sortDataSets(filteredDataSet);
434442
ChartType chartType = options.getChartOptions() != null ? options.getChartOptions().getType() : null;
435443

436444
switch (chartType) {
445+
case DATASET_PIE:
446+
chart = ChartUtil.createRadialHeatMapLegend(options.getEnrichmentMap().getDataSetList(), options.getChartOptions());
447+
break;
437448
case RADIAL_HEAT_MAP:
438449
chart = ChartUtil.createRadialHeatMapLegend(sortedDataSets, options.getChartOptions());
439450
break;
@@ -460,31 +471,57 @@ private ChartPanel createChartPanel(Collection<EMDataSet> dataSets) {
460471

461472
private void updateEdgeColorPanel() {
462473
JPanel p = getEdgeColorPanel();
463-
Dimension iconSize = new Dimension(LEGEND_ICON_SIZE, LEGEND_ICON_SIZE / 2);
464-
465474
Map<Object, Paint> dmMap = getEdgeColors();
466475

467476
JComponent[][] entries = new JComponent[dmMap.size()][2];
468477

469478
int i = 0;
470479
for (Entry<?, Paint> e : dmMap.entrySet()) {
471-
Color color = null;
472-
if (e.getValue() instanceof Color)
473-
color = (Color) e.getValue();
480+
Color color = e.getValue() instanceof Color ? (Color)e.getValue(): null;
474481

475-
JLabel iconLabel = createColorLabel(color, iconSize);
482+
JLabel iconLabel = createColorLabel(color, COLOR_ICON_SIZE);
476483
JLabel descLabel = new JLabel("" + e.getKey());
477484

478485
if (Columns.EDGE_DATASET_VALUE_SIG.equals(e.getKey()))
479486
descLabel.setFont(descLabel.getFont().deriveFont(Font.ITALIC));
480487

481-
entries[i++] = new JComponent[]{ iconLabel, descLabel };
488+
entries[i++] = new JComponent[] {iconLabel, descLabel};
482489
}
483490

484491
updateStyleLegendPanel(entries, p);
485492
}
486493

487494

495+
private void updateNodeDataSetColorPanel() {
496+
JPanel p = getNodeDataSetColorPanel();
497+
498+
ChartData data = options.getChartOptions().getData();
499+
500+
if(data == ChartData.DATA_SET) {
501+
Map<Object,Paint> colorMap = getDataSetColors();
502+
503+
JComponent[][] entries = new JComponent[colorMap.size()][2];
504+
505+
int i = 0;
506+
for (Entry<?, Paint> e : colorMap.entrySet()) {
507+
Color color = e.getValue() instanceof Color ? (Color)e.getValue(): null;
508+
509+
JLabel iconLabel = createColorLabel(color, COLOR_ICON_SIZE);
510+
JLabel descLabel = new JLabel("" + e.getKey());
511+
512+
entries[i++] = new JComponent[] {iconLabel, descLabel};
513+
}
514+
515+
updateStyleLegendPanel(entries, p);
516+
517+
p.setVisible(true);
518+
} else {
519+
p.setVisible(false);
520+
}
521+
p.revalidate();
522+
}
523+
524+
488525
@Override
489526
public Map<Object,Paint> getEdgeColors() {
490527
EnrichmentMap map = options.getEnrichmentMap();
@@ -540,6 +577,16 @@ public Map<Object,Paint> getEdgeColors() {
540577
}
541578

542579

580+
@Override
581+
public Map<Object,Paint> getDataSetColors() {
582+
Map<Object,Paint> colorMap = new LinkedHashMap<>();
583+
for(EMDataSet dataset : options.getEnrichmentMap().getDataSetList()) {
584+
colorMap.put(dataset.getName(), dataset.getColor());
585+
}
586+
return colorMap;
587+
}
588+
589+
543590
BasicCollapsiblePanel getNodeLegendPanel() {
544591
if (nodeLegendPanel == null) {
545592
nodeLegendPanel = new BasicCollapsiblePanel("Nodes (Gene Sets)");
@@ -551,16 +598,18 @@ BasicCollapsiblePanel getNodeLegendPanel() {
551598
layout.setAutoCreateGaps(true);
552599

553600
layout.setHorizontalGroup(layout.createParallelGroup(Alignment.CENTER, true)
554-
.addComponent(getNodeColorPanel(), DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)
555-
.addComponent(getNodeShapePanel(), DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)
556-
.addComponent(getNodeChartPanel(), DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)
557-
.addComponent(getNodeChartColorPanel(), DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)
601+
.addComponent(getNodeColorPanel(), DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)
602+
.addComponent(getNodeShapePanel(), DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)
603+
.addComponent(getNodeChartPanel(), DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)
604+
.addComponent(getNodeChartColorPanel(), DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)
605+
.addComponent(getNodeDataSetColorPanel(), DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)
558606
);
559607
layout.setVerticalGroup(layout.createSequentialGroup()
560-
.addComponent(getNodeColorPanel(), PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE)
561-
.addComponent(getNodeShapePanel(), PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE)
562-
.addComponent(getNodeChartPanel(), PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE)
563-
.addComponent(getNodeChartColorPanel(), PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE)
608+
.addComponent(getNodeColorPanel(), PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE)
609+
.addComponent(getNodeShapePanel(), PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE)
610+
.addComponent(getNodeChartPanel(), PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE)
611+
.addComponent(getNodeChartColorPanel(), PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE)
612+
.addComponent(getNodeDataSetColorPanel(), PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE)
564613
);
565614

566615
if (isAquaLAF())
@@ -581,10 +630,10 @@ BasicCollapsiblePanel getEdgeLegendPanel() {
581630
layout.setAutoCreateGaps(true);
582631

583632
layout.setHorizontalGroup(layout.createParallelGroup(Alignment.CENTER, true)
584-
.addComponent(getEdgeColorPanel(), DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)
633+
.addComponent(getEdgeColorPanel(), DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)
585634
);
586635
layout.setVerticalGroup(layout.createSequentialGroup()
587-
.addComponent(getEdgeColorPanel(), PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE)
636+
.addComponent(getEdgeColorPanel(), PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE)
588637
);
589638

590639
if (isAquaLAF())
@@ -674,6 +723,15 @@ private JPanel getEdgeColorPanel() {
674723

675724
return edgeColorPanel;
676725
}
726+
727+
private JPanel getNodeDataSetColorPanel() {
728+
if (dataSetColorPanel == null) {
729+
dataSetColorPanel = createStyleLegendPanel(null);
730+
dataSetColorPanel.setToolTipText(LegendContent.NODE_DATA_SET_COLOR_HEADER);
731+
}
732+
733+
return dataSetColorPanel;
734+
}
677735

678736
private JPanel createStyleLegendPanel(JComponent[][] entries) {
679737
JPanel p = new JPanel();
@@ -685,7 +743,7 @@ private JPanel createStyleLegendPanel(JComponent[][] entries) {
685743
return p;
686744
}
687745

688-
private void updateStyleLegendPanel(JComponent[][] entries, JPanel p) {
746+
private static void updateStyleLegendPanel(JComponent[][] entries, JPanel p) {
689747
p.removeAll();
690748

691749
GroupLayout layout = new GroupLayout(p);

0 commit comments

Comments
 (0)