Skip to content

Commit b97db2f

Browse files
committed
Node view will be used for exporting network view.
1 parent ca219bc commit b97db2f

File tree

8 files changed

+44
-34
lines changed

8 files changed

+44
-34
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
Cytoscape to D3.js Exporter
22
============
3+
## Release history
4+
* 9/15/2014 - Version 1.0.1 released. Node locations (x, y) will be added as node property when user select Network and View.
5+
36

47
## What is this for?
5-
This is a [Cytoscape 3]() App for exporting network and its data table to [D3.js](http://d3js.org/) compatible JSON. D3.js is a general data visualization JavaScript library, and some of its presets support network/tree style data. This App directly creates JSON file for those presets from Cytoscape network and data tables.
8+
This is a [Cytoscape](http://www.cytoscape.org/) App for exporting network and its data table to [D3.js](http://d3js.org/) compatible JSON. D3.js is a general data visualization JavaScript library, and some of its presets support network/tree style data. This App directly creates JSON file for those presets from Cytoscape network and data tables.
69

710
Currently, this App supports:
811

912
* Network JSON for force layout ([example](http://bl.ocks.org/mbostock/4062045))
1013
* Tree JSON ([example](http://mbostock.github.io/d3/talk/20111018/tree.html))
1114

1215
## How to Use
13-
Build, and install the JAR file from Cytoscape's App manager. You can export your network and its data table as JSON from *File-->Export-->Network*.
16+
Build, and install the JAR file from Cytoscape's App manager or simply install from Cytoscape App Store. You can export your network and its data table as JSON from *File → Export → Network*.
1417

1518
**For Tree data, you need to select root node manually (simply click the root node before exporting)**.
1619

pom.xml

+6-9
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
<bundle.symbolicName>org.cytoscape.d3js-exporter</bundle.symbolicName>
88
<bundle.namespace>org.cytoscape.d3</bundle.namespace>
99
<cytoscape.api.version>3.1.0</cytoscape.api.version>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1011
</properties>
1112

1213
<modelVersion>4.0.0</modelVersion>
1314
<groupId>org.cytoscape</groupId>
1415
<artifactId>d3js-exporter</artifactId>
15-
<version>1.0.0</version>
16+
<version>1.0.1</version>
1617

1718
<name>D3.js Exporter</name>
1819

@@ -69,7 +70,7 @@
6970
<plugin>
7071
<groupId>org.apache.felix</groupId>
7172
<artifactId>maven-bundle-plugin</artifactId>
72-
<version>2.4.0</version>
73+
<version>2.5.3</version>
7374
<extensions>true</extensions>
7475
<configuration>
7576
<instructions>
@@ -78,7 +79,7 @@
7879
<Export-Package>${bundle.namespace}</Export-Package>
7980
<Private-Package>${bundle.namespace}.internal.*</Private-Package>
8081
<Bundle-Activator>${bundle.namespace}.internal.CyActivator</Bundle-Activator>
81-
<Embed-Dependency>*;scope=!provided;groupId=!org.cytoscape</Embed-Dependency>
82+
<Embed-Dependency>*;scope=!provided;scope=!test;groupId=!org.cytoscape</Embed-Dependency>
8283
<Embed-Transitive>true</Embed-Transitive>
8384
<Import-Package>*;resolution:=optional</Import-Package>
8485
</instructions>
@@ -120,6 +121,7 @@
120121
<groupId>org.osgi</groupId>
121122
<artifactId>org.osgi.core</artifactId>
122123
<version>4.2.0</version>
124+
<scope>provided</scope>
123125
</dependency>
124126
<dependency>
125127
<groupId>org.ops4j.pax.logging</groupId>
@@ -161,15 +163,10 @@
161163
</dependency>
162164

163165
<!-- 3rd party libraries -->
164-
<dependency>
165-
<groupId>com.fasterxml.jackson.core</groupId>
166-
<artifactId>jackson-core</artifactId>
167-
<version>2.2.2</version>
168-
</dependency>
169166
<dependency>
170167
<groupId>com.fasterxml.jackson.core</groupId>
171168
<artifactId>jackson-databind</artifactId>
172-
<version>2.2.2</version>
169+
<version>2.4.2</version>
173170
</dependency>
174171

175172
<!-- Testing -->

src/main/java/org/cytoscape/d3/internal/serializer/D3CyNetworkSerializer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class D3CyNetworkSerializer extends JsonSerializer<CyNetwork> {
1515
public void serialize(final CyNetwork network, JsonGenerator jgen, SerializerProvider provider)
1616
throws IOException, JsonProcessingException {
1717
final D3JsonBuilder builder = new D3JsonBuilder();
18-
builder.serializeNetwork(network, jgen, provider);
18+
builder.serializeNetwork(network, null, jgen, provider);
1919
}
2020

2121
@Override

src/main/java/org/cytoscape/d3/internal/serializer/D3CyNetworkViewSerializer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void serialize(final CyNetworkView networkView, JsonGenerator jgen, Seria
1919

2020
final D3JsonBuilder builder = new D3JsonBuilder();
2121
final CyNetwork network = networkView.getModel();
22-
builder.serializeNetwork(network, jgen, provider);
22+
builder.serializeNetwork(network, networkView, jgen, provider);
2323
}
2424

2525

src/main/java/org/cytoscape/d3/internal/serializer/D3JsonBuilder.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import org.cytoscape.model.CyEdge;
99
import org.cytoscape.model.CyNetwork;
1010
import org.cytoscape.model.CyNode;
11+
import org.cytoscape.view.model.CyNetworkView;
12+
import org.cytoscape.view.model.View;
13+
import org.cytoscape.view.presentation.property.BasicVisualLexicon;
1114

1215
import com.fasterxml.jackson.core.JsonGenerator;
1316
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -20,8 +23,10 @@ public class D3JsonBuilder {
2023
private static final String SOURCE = "source";
2124
private static final String TARGET= "target";
2225
private static final String ID = "id";
26+
private static final String X = "x";
27+
private static final String Y = "y";
2328

24-
protected final void serializeNetwork(final CyNetwork network, JsonGenerator jgen, SerializerProvider provider)
29+
protected final void serializeNetwork(final CyNetwork network, final CyNetworkView view, JsonGenerator jgen, SerializerProvider provider)
2530
throws IOException, JsonProcessingException {
2631

2732
// Write array
@@ -40,6 +45,12 @@ protected final void serializeNetwork(final CyNetwork network, JsonGenerator jge
4045
jgen.writeStartObject();
4146

4247
jgen.writeStringField(ID, node.getSUID().toString());
48+
if(view != null) {
49+
// View is available. Pick (x,y)
50+
final View<CyNode> nodeView = view.getNodeView(node);
51+
jgen.writeNumberField(X, nodeView.getVisualProperty(BasicVisualLexicon.NODE_X_LOCATION));
52+
jgen.writeNumberField(Y, nodeView.getVisualProperty(BasicVisualLexicon.NODE_Y_LOCATION));
53+
}
4354

4455
// Write CyRow in "data" field
4556
jgen.writeObject(network.getRow(node));

src/main/java/org/cytoscape/d3/internal/writer/D3NetworkViewWriter.java

+8-11
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
import java.io.OutputStream;
44
import java.io.OutputStreamWriter;
5-
import java.nio.charset.Charset;
6-
import java.nio.charset.CharsetEncoder;
75

86
import org.cytoscape.io.write.CyWriter;
97
import org.cytoscape.task.AbstractNetworkViewTask;
108
import org.cytoscape.view.model.CyNetworkView;
119
import org.cytoscape.work.TaskMonitor;
12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
1410

1511
import com.fasterxml.jackson.databind.ObjectMapper;
1612

@@ -27,25 +23,26 @@ public D3NetworkViewWriter(final OutputStream outputStream, final CyNetworkView
2723
final ObjectMapper networkView2jsonMapper) {
2824
super(networkView);
2925

30-
if(outputStream == null)
26+
if (outputStream == null)
3127
throw new NullPointerException("Output Stream is null.");
32-
33-
if(networkView2jsonMapper == null)
28+
29+
if (networkView2jsonMapper == null)
3430
throw new NullPointerException("Object Mapper is null.");
35-
31+
3632
this.outputStream = outputStream;
3733
this.networkView2jsonMapper = networkView2jsonMapper;
3834
}
3935

4036
@Override
4137
public void run(TaskMonitor taskMonitor) throws Exception {
4238
if (taskMonitor != null) {
43-
taskMonitor.setTitle("Writing Network View to D3.js Style JSON...");
44-
taskMonitor.setProgress(0);
39+
taskMonitor.setTitle("Writing Network View to D3-Style JSON");
40+
taskMonitor.setStatusMessage("Writing D3 style JSON...");
41+
taskMonitor.setProgress(-1.0);
4542
}
4643
networkView2jsonMapper.writeValue(new OutputStreamWriter(outputStream, EncodingUtil.getEncoder()), view);
4744
outputStream.close();
48-
45+
4946
if (taskMonitor != null) {
5047
taskMonitor.setStatusMessage("Success.");
5148
taskMonitor.setProgress(1.0);

src/main/java/org/cytoscape/d3/internal/writer/D3NetworkWriter.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package org.cytoscape.d3.internal.writer;
22

33
import java.io.OutputStream;
4-
import java.nio.charset.Charset;
5-
import java.nio.charset.CharsetEncoder;
4+
import java.io.OutputStreamWriter;
65

76
import org.cytoscape.io.write.CyWriter;
87
import org.cytoscape.model.CyNetwork;
98
import org.cytoscape.task.AbstractNetworkTask;
109
import org.cytoscape.work.TaskMonitor;
11-
import org.slf4j.Logger;
12-
import org.slf4j.LoggerFactory;
1310

1411
import com.fasterxml.jackson.databind.ObjectMapper;
1512

@@ -33,14 +30,16 @@ public D3NetworkWriter(final OutputStream outputStream, final CyNetwork network,
3330
@Override
3431
public void run(TaskMonitor taskMonitor) throws Exception {
3532
if (taskMonitor != null) {
36-
taskMonitor.setTitle("Writing to D3.js style JSON...");
37-
taskMonitor.setProgress(0);
33+
taskMonitor.setTitle("Writing to D3-Style JSON");
34+
taskMonitor.setStatusMessage("Writing network in D3.js format...");
35+
taskMonitor.setProgress(-1.0);
3836
}
39-
network2jsonMapper.writeValue(outputStream, network);
37+
38+
network2jsonMapper.writeValue(new OutputStreamWriter(outputStream, EncodingUtil.getEncoder()), network);
4039
outputStream.close();
41-
40+
4241
if (taskMonitor != null) {
43-
taskMonitor.setTitle("Success.");
42+
taskMonitor.setStatusMessage("Success.");
4443
taskMonitor.setProgress(1.0);
4544
}
4645
}

src/test/java/org/cytoscape/d3/D3WriterTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,8 @@ private void readAndTest(final File outFile, CyNetwork network) throws IOExcepti
136136
assertTrue(nodes.isArray());
137137
assertNotNull(edges);
138138
assertTrue(edges.isArray());
139+
140+
assertEquals(5, nodes.size());
141+
assertEquals(4, edges.size());
139142
}
140143
}

0 commit comments

Comments
 (0)