Skip to content

Add PlotService to SciJava #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ Wisconsin-Madison.</license.copyrightOwners>
<groupId>org.scijava</groupId>
<artifactId>scijava-table</artifactId>
</dependency>
<dependency>
<groupId>org.scijava</groupId>
<artifactId>scijava-plot</artifactId>
<version>0.1.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.scijava</groupId>
<artifactId>scijava-ui-awt</artifactId>
Expand All @@ -144,6 +149,15 @@ Wisconsin-Madison.</license.copyrightOwners>
<artifactId>jdatepicker</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
</dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreesvg</artifactId>
<version>3.2</version>
</dependency>

<!-- Test scope dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.scijava.ui.swing.plot.converter;

import org.jfree.chart.JFreeChart;
import org.scijava.Priority;
import org.scijava.convert.AbstractConverter;
import org.scijava.convert.ConversionRequest;
import org.scijava.convert.ConvertService;
import org.scijava.convert.Converter;
import org.scijava.plot.Plot;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

/**
* Converter plugin, that converts an {@link Plot} to {@link BufferedImage}.
*
* @author Matthias Arzt
* @see ConvertService
*/
@Plugin(type = Converter.class, priority = Priority.NORMAL_PRIORITY)
public class PlotToBufferedImageConverter extends AbstractConverter<Plot, BufferedImage>
{

@Parameter
ConvertService convertService;

@Override
public boolean canConvert(ConversionRequest request) {
return request.destClass().isAssignableFrom( BufferedImage.class ) &&
Plot.class.isAssignableFrom( request.sourceClass() ) &&
convertService.supports(new ConversionRequest(
request.sourceObject(), request.sourceType(), JFreeChart.class));
}

@Override
public <T> T convert(Object o, Class<T> aClass) {
if(o instanceof Plot && BufferedImage.class.equals(aClass)) {
@SuppressWarnings("unchecked")
T t = (T) toBufferedImage((Plot) o);
return t;
}
return null;
}

private BufferedImage toBufferedImage(Plot plot) {
BufferedImage image = new BufferedImage( plot.getPreferredWidth(), plot.getPreferredHeight(), BufferedImage.TYPE_INT_ARGB );
JFreeChart chart = convertService.convert(plot, JFreeChart.class);
chart.draw(image.createGraphics(), new Rectangle2D.Float(0, 0, image.getWidth(), image.getHeight()));
return image;
}

@Override
public Class<BufferedImage> getOutputType() {
return BufferedImage.class;
}

@Override
public Class<Plot> getInputType() {
return Plot.class;
}
}
64 changes: 64 additions & 0 deletions src/main/java/org/scijava/ui/swing/plot/io/PlotToSvgIOPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.scijava.ui.swing.plot.io;

import org.scijava.plot.Plot;
import org.jfree.chart.JFreeChart;
import org.jfree.graphics2d.svg.SVGGraphics2D;
import org.jfree.graphics2d.svg.SVGUtils;
import org.scijava.convert.ConvertService;
import org.scijava.io.AbstractIOPlugin;
import org.scijava.io.IOPlugin;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

import java.awt.*;
import java.io.File;
import java.io.IOException;

/**
* Plugin that can write {@link Plot} as SVG file.
*
* @author Matthias Arzt
*/
@Plugin(type = IOPlugin.class)
public class PlotToSvgIOPlugin extends AbstractIOPlugin<Plot> {

@Parameter
ConvertService convertService;

@Override
public boolean supportsOpen(String source) {
return false;
}

@Override
public boolean supportsSave(String destination) {
return destination.endsWith(".svg");
}

@Override
public boolean supportsSave(Object data, String destination) {
return supportsSave(destination) &&
data instanceof Plot &&
convertService.supports(data, JFreeChart.class);
}

@Override
public Plot open(String source) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void save(Plot data, String destination) throws IOException {
if(!supportsSave(data, destination))
throw new IllegalArgumentException();
JFreeChart chart = convertService.convert(data, JFreeChart.class);
SVGGraphics2D g = new SVGGraphics2D(data.getPreferredWidth(), data.getPreferredWidth());
chart.draw(g, new Rectangle(0, 0, g.getWidth(), g.getHeight()));
SVGUtils.writeToSVG(new File(destination), g.getSVGElement());
}

@Override
public Class<Plot> getDataType() {
return Plot.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* #%L
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2009 - 2016 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package org.scijava.ui.swing.viewer.plot;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.scijava.convert.ConvertService;
import org.scijava.plot.Plot;
import org.scijava.ui.viewer.DisplayWindow;
import org.scijava.ui.viewer.plot.PlotDisplay;
import org.scijava.ui.viewer.plot.PlotDisplayPanel;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;

/**
* A JFreeChart-driven display panel for {@link Plot}s.
*
* @author Curtis Rueden
*/
public class SwingPlotDisplayPanel extends JPanel implements PlotDisplayPanel
{

// -- instance variables --

private final DisplayWindow window;
private final PlotDisplay display;
private final ConvertService convertService;
private Dimension prefferedSize;

// -- constructor --

public SwingPlotDisplayPanel(final PlotDisplay display,
final DisplayWindow window, final ConvertService convertService)
{
this.display = display;
this.window = window;
this.convertService = convertService;
setLayout(new BorderLayout());
initPreferredSize();
setupChart();
window.setContent(this);
}

private void initPreferredSize() {
Plot plot = display.get(0);
prefferedSize = new Dimension(plot.getPreferredWidth(), plot.getPreferredHeight());
}

private void setupChart() {
final JFreeChart chart = convertToJFreeChart(display.get(0));
add(new ChartPanel(chart));
}

private JFreeChart convertToJFreeChart(Plot plot) {
return Objects.requireNonNull(convertService.convert(plot, JFreeChart.class));
}

public static boolean supports(Plot abstractPlot, ConvertService convertService) {
return convertService.supports(abstractPlot, JFreeChart.class);
}

// -- PlotDisplayPanel methods --

@Override
public PlotDisplay getDisplay() {
return display;
}

// -- DisplayPanel methods --

@Override
public DisplayWindow getWindow() {
return window;
}

@Override
public void redoLayout() { }

@Override
public void setLabel(final String s) { }

@Override
public void redraw() { }

@Override
public Dimension getPreferredSize() {
return prefferedSize;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* #%L
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2009 - 2016 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package org.scijava.ui.swing.viewer.plot;

import org.scijava.plot.Plot;
import org.scijava.ui.viewer.plot.AbstractPlotDisplayViewer;
import org.scijava.convert.ConvertService;
import org.scijava.display.Display;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.ui.UserInterface;
import org.scijava.ui.swing.SwingUI;
import org.scijava.ui.viewer.DisplayViewer;
import org.scijava.ui.viewer.DisplayWindow;
import org.scijava.ui.viewer.plot.PlotDisplay;

/**
* A Swing {@link Plot} display viewer, which displays plots using JFreeChart.
*
* @author Curtis Rueden
*/
@Plugin(type = DisplayViewer.class)
public class SwingPlotDisplayViewer extends AbstractPlotDisplayViewer {

@Parameter
ConvertService convertService;

@Override
public boolean isCompatible(final UserInterface ui) {
return ui instanceof SwingUI;
}

@Override
public boolean canView(final Display<?> d) {
if(! (d instanceof PlotDisplay ))
return false;
Plot plot = ((PlotDisplay) d).get(0);
return SwingPlotDisplayPanel.supports(plot, convertService);
}

@Override
public void view(final DisplayWindow w, final Display<?> d) {
super.view(w, d);
setPanel(new SwingPlotDisplayPanel(getDisplay(), w, convertService));
}

}
Loading