How to use an external JAR library with Beanshell

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to use an external JAR library with Beanshell

Gwendall Petit
Administrator
Hi everybody,

Here is a small video to present how to use an external JAR library with Beanshell.

In this example we use JFreeChart to create charts in OrbisGIS V4.0 beta.



Below is the corresponding code :
addJAR("/home/ebocher/applications/jfreechart-1.0.14/lib/jfreechart-1.0.14.jar");
addJAR("/home/ebocher/applications/jfreechart-1.0.14/lib/jcommon-1.0.17.jar");

import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.time.Minute;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(34.0, "Series 1", "Category 1");
dataset.addValue(23.0, "Series 1", "Category 2");
dataset.addValue(54.0, "Series 1", "Category 3");
JFreeChart chart = ChartFactory.createBarChart(
            "Bar Chart",
            "Category",
            "Series",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(200, 100));

JFrame frame = new JFrame();

frame.add(chartPanel);
frame.setVisible(true);
Feel free to use this mailing list if you have any questions.

Gwendall
Reply | Threaded
Open this post in threaded view
|

Re: How to use an external JAR library with Beanshell

Gwendall Petit
Administrator
Here is a new example to create a xychart based on a datasource loaded in OrbisGIS V4.0 beta.



import java.awt.Color;
import java.io.File;
import java.util.Iterator;
import javax.swing.JFrame;
import org.gdms.data.DataSource;
import org.gdms.data.values.Value;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.LogarithmicAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.orbisgis.core.DataManager;
import org.orbisgis.core.Services;

DataManager dm = Services.getService(DataManager.class);

DataSource ds = dm.getDataSourceFactory().getDataSource("densite_27_47_67");
ds.open();

Iterator  iter = ds.iterator();

XYSeries s1 = new XYSeries("densite202");
       
while(iter.hasNext()){
    Value[] values = iter.next();
    gid = values[1].getAsInt();
    val1 = values[2].getAsInt();
    s1.add(gid, val1 );
}


XYSeriesCollection dataset = new XYSeriesCollection(s1);


 JFreeChart chart = ChartFactory.createXYLineChart(
            "Densité de population",          // chart title
            "Cell gid",               // x axis label
            "Population value",       // y axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            true,                     // include legend
            true,
            false
        );

chart.setBackgroundPaint(Color.white);

ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));    

JFrame frame = new JFrame();
frame.add(chartPanel);
frame.pack();
frame.setVisible(true);