Data Visualization with Java

From Self-Organization Wiki
Revision as of 09:17, 5 July 2010 by Tdittric (talk | contribs) (Code Examples)
Jump to: navigation, search

JChart2D Library

  • License Model: open source (LGPL license)
  • Webpage and download: [1]

The library is very easy to use because you don't have to spend a lot of time on making adjustments. You can create for example static line charts and dynamic line charts but you cannot create Pie Charts or Bar Charts.

Code Examples

The follwing code snipped creates a static XY Diagramm

 import info.monitorenter.gui.chart.Chart2D;
 import info.monitorenter.gui.chart.ITrace2D;
 import info.monitorenter.gui.chart.traces.Trace2DSimple;
 import javax.swing.JFrame;
 public class MinimalStaticChart {
 private MinimalStaticChart() {
   super();
 }
 public static void main(String[]args){
   // Create a chart:  
   Chart2D chart = new Chart2D();
   // Create an ITrace: 
   ITrace2D trace = new Trace2DSimple(); 
   // Add the trace to the chart: 
   chart.addTrace(trace); 
   // Create the Points and add them to the trace
   for(double i=0;i<=10;i+=0.001){
     trace.addPoint(Math.cos(3*i),Math.sin(4*i));
   }  
   // Create a frame.
   JFrame frame = new JFrame("MinimalStaticChart");
   // add the chart to the frame: 
   frame.getContentPane().add(chart);
   // make a few adjustments for the Frame
   frame.setSize(400,300);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setVisible(true);
   }
 }

which looks like this:

Screenshot

JFreeChart Library

  • License Model: Open Source (LGPL)
  • Webpage and download: [2]

JFreeChart is a useful package. You can create Line Charts, Bar Charts, Pie Charts and many more with it. There is a Developer Guide available for it. Using this Developer Guide working with JFreeChart is very easy.

Code Examples

The follwing code snipped creates a static XY Diagramm

 import java.awt.Color;
 import java.awt.Dimension;
 import javax.swing.JPanel;
 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.xy.XYDataset;
 import org.jfree.data.xy.XYSeries;
 import org.jfree.data.xy.XYSeriesCollection;
 import org.jfree.ui.ApplicationFrame;
 import org.jfree.ui.RefineryUtilities;
 public class LineChartTest extends ApplicationFrame{	
   public LineChartTest(String title){
     super(title);
     XYDataset data = createDataset();
     JFreeChart chart = createChart(data);
     ChartPanel chartPanel = new ChartPanel(chart);
     chartPanel.setPreferredSize(new Dimension(500,270));
     setContentPane(chartPanel);
   }
   //Create Data	
   private static XYDataset createDataset(){
     //Create data series and add all the points
     XYSeries data1 = new XYSeries("Sin(t)");
     XYSeries data2 = new XYSeries("Cos(t)");
     for (int i=0;i<100;i++) {
       data1.add(i/10.0,Math.sin(i/10.0));
       data2.add(i/10.0,Math.cos(i/10.0));
     }	
     //Create a collection for the series and add them to the collection	  
     XYSeriesCollection dataset = new XYSeriesCollection();
     dataset.addSeries(data1);
     dataset.addSeries(data2);
     return dataset;
   }
   //Create a Chart with the XYDataset "data" and a few ettings
   private static JFreeChart createChart(XYDataset data){
     //Create a chart with the title "Sin/Cos", the xAxisLable "t", the yAxisLable "Value"
     JFreeChart chart = ChartFactory.createXYLineChart("Sin/Cos", "t", "Value", data, PlotOrientation.VERTICAL, true, false, false);
     chart.setBackgroundPaint(Color.white);
     chart.getPlot().setBackgroundPaint(Color.gray);
     return chart;
   }
   public static void main(String[] args) {
     LineChartTest demo = new LineChartTest("LineChartTest");
     demo.pack();
     RefineryUtilities.centerFrameOnScreen(demo);
     demo.setVisible(true);
   }
 }

which looks like this:

Screenshot

PTPlot Library

Ptolemy II is a library that can be used for simulating different things either in real time or just static. It creates XY-LineCharts, BarCharts, Histogramms Live Plots. It is easy to work with it.

Code Examples

The follwing code snipped creates a non-static XY Diagramm

 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import javax.swing.JFrame;
 import ptolemy.plot.PlotLive;
 public class LivePlotTest extends PlotLive{
   public LivePlotTest() {
     //Set the range of the axes
     setYRange(-1, 1);
     setXRange(-1, 1);
     //Set the amount of points on the screen
     setPointsPersistence(500);
     //Set the shape of the points, other possibilities are "points" or "dots"
     setMarksStyle("dots");        
   }
   double i = 0;
   public static void main(String[] args) {
     //Create a plot
     final LivePlotTest test = new LivePlotTest();
     //Enable the exit button
     JFrame frame = new JFrame("Test");
     frame.addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent event) {
         test.stop();
         System.exit(0);
       }
     });
     //Make it visible
     frame.add("Center", test);
     frame.setVisible(true);
     //Enable the start and the stop button
     test.setButtons(true);
     //Start the plot
     test.start();
     frame.pack();
   }
   //Create the points and add them to the plot
   public void addPoints() {
     addPoint(0, Math.sin(2*i),Math.cos(3*i), false);
     i+=0.01;
     try{
       Thread.sleep(5);		
     }catch(InterruptedException ex){
       ex.printStackTrace();
     }
   }
 }

which looks like this:

Screenshot

JCCKit Library

  • License Model: open source (LGPL license)
  • Webpage and download: [4]

JCCKit is is a package, that is a little bit difficult to use because you need a lot of time for making adjustments. But the result looks nice.

Code Examples

The follwing code snipped creates a static XY Diagramm

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;
import javax.swing.JFrame;
import jcckit.GraphicsPlotCanvas;
import jcckit.data.DataCurve;
import jcckit.data.DataPlot;
import jcckit.data.DataPoint;
import jcckit.util.ConfigParameters;
import jcckit.util.PropertiesBasedConfigData;
public class JCCKitTest extends Applet {
  private DataPlot _dataPlot;
  public void init() {
    GraphicsPlotCanvas plotCanvas = createPlotCanvas();
    //Create a new dataPlot
    _dataPlot = new DataPlot();
    _dataPlot.addElement(new DataCurve(""));
    //Show it on the screen
    plotCanvas.connect(_dataPlot);
    setLayout(new BorderLayout());
    add(plotCanvas.getGraphicsCanvas(), BorderLayout.CENTER);
    add(createControlPanel(), BorderLayout.SOUTH);
  }
  private GraphicsPlotCanvas createPlotCanvas() {
    //Adjustments:
    Properties props = new Properties();
    ConfigParameters config = new ConfigParameters(new PropertiesBasedConfigData(props));
    props.put("plot/legendVisible", "false");
    props.put("plot/coordinateSystem/xAxis/minimum", "0");
    props.put("plot/coordinateSystem/xAxis/maximum", "10");
    props.put("plot/coordinateSystem/xAxis/axisLabel", "");
    props.put("plot/coordinateSystem/xAxis/ticLabelFormat/map", "%d");    
    props.put("plot/coordinateSystem/yAxis/axisLabel", "growth rate");
    props.put("plot/coordinateSystem/yAxis/maximum", "1");
    props.put("plot/coordinateSystem/yAxis/minimum", "-1");
    props.put("plot/coordinateSystem/yAxis/ticLabelFormat", "%d");    
    props.put("plot/curveFactory/definitions", "curve, curve2");
    props.put("plot/curveFactory/curve/withLine", "true");
    props.put("plot/curveFactory/curve/symbolFactory/className", "jcckit.plot.CircleSymbolFactory");
    props.put("plot/curveFactory/curve/symbolFactory/attributes/className", "jcckit.graphic.ShapeAttributes");
    props.put("plot/curveFactory/curve/symbolFactory/attributes/fillColor", "0xfe8000");
    props.put("plot/curveFactory/curve/symbolFactory/attributes/lineColor", "0");
    props.put("plot/curveFactory/curve/symbolFactory/size", "0.01");
    props.put("plot/initialHintForNextCurve/className", "jcckit.plot.PositionHint");
    props.put("plot/initialHintForNextCurve/position", "0 0.1");    
    return new GraphicsPlotCanvas(config);
  }  
  private Panel createControlPanel() {
    Panel controlPanel = new Panel();
    Button startButton = new Button("start");
    //Starts to plot the chart
    startButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                new Thread() {
                        public void run() {
                          animate();
                        }
                      }.start();
              }
            });
    controlPanel.add(startButton);    
    return controlPanel;
  }  
  private void animate() {
    //Create Points and add them to the dataPlot
    DataCurve curve = new DataCurve("");
    DataCurve curve2 = new DataCurve("");    
    for (int i = 0; i <= 100; i++) {
      //x=i/10.0 , y=sin(i/10.0)
      curve.addElement(new DataPoint(i/10.0, Math.sin(i/10.0)));
      //x=i/10.0 , y=cos(i/10.0)
      curve2.addElement(new DataPoint(i/10.0, Math.cos(i/10.0)));
    }
    _dataPlot.addElement(curve);
    _dataPlot.addElement(curve2);
  }  
  public static void main(String[] args) {
    JFrame frame = new JFrame("Animated Chart");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Applet applet = new JCCKitTest();
    applet.init();
    applet.setBackground(Color.white);
    frame.add(applet);
    frame.setSize(600, 500);
    frame.setVisible(true);
  }
}

which looks like this: Screenshot

ExampleChart Library

  • License Model: open source (LGPL license)
  • Webpage and download: [5]

The library bla bla bla .....

Code Examples

The follwing code snipped creates a pie chart together with a life update Venn Diagramm

code

code code
code code? // comment
code code code !!

which looks like this:

Screenshot

Other Libraries

The following Java libraries looked promising, but turned out to be not recommendable:

  • M$$-Chart commercial license (500 € per license)
  • Crash-Chart beta version, still needs a lot of development, crashes frequently
  • Bad-Chart bad performance, missing important chart types like xy-Diagrams