Skip to content

Commit c752a50

Browse files
maarztctrueden
authored andcommitted
Add PlotToSvgIOPlugin
This allows to store a plot as SVG with the SciJava IOService.
1 parent 0f58ac2 commit c752a50

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.scijava.ui.swing.plot.io;
2+
3+
import org.scijava.plot.Plot;
4+
import org.jfree.chart.JFreeChart;
5+
import org.jfree.graphics2d.svg.SVGGraphics2D;
6+
import org.jfree.graphics2d.svg.SVGUtils;
7+
import org.scijava.convert.ConvertService;
8+
import org.scijava.io.AbstractIOPlugin;
9+
import org.scijava.io.IOPlugin;
10+
import org.scijava.plugin.Parameter;
11+
import org.scijava.plugin.Plugin;
12+
13+
import java.awt.*;
14+
import java.io.File;
15+
import java.io.IOException;
16+
17+
/**
18+
* Plugin that can write {@link Plot} as SVG file.
19+
*
20+
* @author Matthias Arzt
21+
*/
22+
@Plugin(type = IOPlugin.class)
23+
public class PlotToSvgIOPlugin extends AbstractIOPlugin<Plot> {
24+
25+
@Parameter
26+
ConvertService convertService;
27+
28+
@Override
29+
public boolean supportsOpen(String source) {
30+
return false;
31+
}
32+
33+
@Override
34+
public boolean supportsSave(String destination) {
35+
return destination.endsWith(".svg");
36+
}
37+
38+
@Override
39+
public boolean supportsSave(Object data, String destination) {
40+
return supportsSave(destination) &&
41+
data instanceof Plot &&
42+
convertService.supports(data, JFreeChart.class);
43+
}
44+
45+
@Override
46+
public Plot open(String source) throws IOException {
47+
throw new UnsupportedOperationException();
48+
}
49+
50+
@Override
51+
public void save(Plot data, String destination) throws IOException {
52+
if(!supportsSave(data, destination))
53+
throw new IllegalArgumentException();
54+
JFreeChart chart = convertService.convert(data, JFreeChart.class);
55+
SVGGraphics2D g = new SVGGraphics2D(data.getPreferredWidth(), data.getPreferredWidth());
56+
chart.draw(g, new Rectangle(0, 0, g.getWidth(), g.getHeight()));
57+
SVGUtils.writeToSVG(new File(destination), g.getSVGElement());
58+
}
59+
60+
@Override
61+
public Class<Plot> getDataType() {
62+
return Plot.class;
63+
}
64+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.scijava.ui.swing.plot.io;
2+
3+
import org.scijava.Context;
4+
import org.scijava.io.IOService;
5+
import org.scijava.plot.Plot;
6+
import org.scijava.plot.PlotService;
7+
import org.scijava.plot.XYPlot;
8+
import org.scijava.plot.XYSeries;
9+
import org.scijava.plugin.Parameter;
10+
11+
import java.io.IOException;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
14+
import java.util.List;
15+
import java.util.stream.Collectors;
16+
import java.util.stream.IntStream;
17+
18+
/**
19+
* @author
20+
*/
21+
public class PlotToSvgDemo {
22+
23+
@Parameter
24+
private PlotService plotService;
25+
26+
@Parameter
27+
private IOService ioService;
28+
29+
public static void main(String... args) throws IOException {
30+
PlotToSvgDemo demo = new PlotToSvgDemo();
31+
new Context().inject(demo);
32+
demo.run();
33+
}
34+
35+
private void run() throws IOException {
36+
Path path = Paths.get(System.getProperty("user.home"), "chart.svg");
37+
Plot plot = getExamplePlot();
38+
ioService.save(plot, path.toString());
39+
System.out.println("Plot saved as " + path.toString());
40+
}
41+
42+
private Plot getExamplePlot() {
43+
XYPlot plot = plotService.newXYPlot();
44+
plot.setTitle("Hello World!");
45+
plot.xAxis().setLabel("x");
46+
plot.yAxis().setLabel("y");
47+
List<Double> xs = IntStream.rangeClosed(0, 100).mapToObj(x -> (double) x * 2. * Math.PI / 100.).collect(Collectors.toList());
48+
List<Double> ys = xs.stream().map(Math::sin).collect(Collectors.toList());
49+
XYSeries series = plot.addXYSeries();
50+
series.setLabel("y = sin(x)");
51+
series.setValues( xs, ys );
52+
return plot;
53+
}
54+
}

0 commit comments

Comments
 (0)