Skip to content

Instantly share code, notes, and snippets.

@tferr
Created September 15, 2016 13:12
Show Gist options
  • Save tferr/2d7525ab2f8614889a4e07861067277e to your computer and use it in GitHub Desktop.
Save tferr/2d7525ab2f8614889a4e07861067277e to your computer and use it in GitHub Desktop.
A Demo Polar Plot Rendered in ImageJ
/* Polar_Plot_Demo.bsh
* IJ BAR: https://github.com/tferr/Scripts#scripts
*
* Beanshell implementation of JFreeChart[1] PolarChartDemo[2]
*
* [1] http://www.jfree.org/jfreechart/api/javadoc/
* [2] http://www.java2s.com/Code/Java/Chart/JFreeChartPolarChartDemo.htm
*/
import ij.gui.GUI;
import ij.plugin.frame.PlugInFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.PolarChartPanel;
import org.jfree.chart.plot.PolarPlot;
import org.jfree.chart.renderer.DefaultPolarItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/** Creates a sample dataset */
XYDataset createDataset() {
XYSeriesCollection data = new XYSeriesCollection();
XYSeries series1 = createRandomData("Series 1", 75.0, 10.0);
XYSeries series2 = createRandomData("Series 2", 50.0, 5.0);
XYSeries series3 = createRandomData("Series 3", 25.0, 1.0);
data.addSeries(series1);
data.addSeries(series2);
data.addSeries(series3);
return data;
}
/** Creates a sample chart */
JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createPolarChart(
"Polar Chart Demo", dataset, true, true, false
);
PolarPlot plot = (PolarPlot) chart.getPlot();
DefaultPolarItemRenderer renderer = (DefaultPolarItemRenderer) plot.getRenderer();
renderer.setSeriesFilled(2, true);
return chart;
}
/**
* Creates a series containing random data.
*
* @param name the series name.
* @param baseRadius the base radius.
* @param thetaInc the angle increment.
*
* @return The series.
*/
XYSeries createRandomData(String name, double baseRadius, double thetaInc) {
XYSeries series = new XYSeries(name);
for (double theta = 0.0; theta < 360.0; theta += thetaInc) {
double radius = baseRadius * (1.0 + Math.random());
series.add(theta, radius);
}
return series;
}
JFreeChart chart = createChart(createDataset());
ChartPanel chartPanel = new PolarChartPanel(chart);
// Display analysis in IJ
PlugInFrame frame = new PlugInFrame("Polar Plot Demo");
frame.add(chartPanel);
frame.pack();
GUI.center(frame);
frame.setVisible(true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment