Search code examples
javavisualizationprefusestacked-area-chart

How do I make a stacked area chart with Prefuse?


I would like to make a stacked area chart with prefuse similar to the folowing: http://prefuse.org/gallery/namevoyager/

I'm not quite sure where to start however, and there's no sample code for these charts. I did find prefuse.action.layout.StackedAreaChart, but am not sure what to do with it.


Solution

  • Have you checked the Prefuse manual? (not too complete, but it's something to start with).

    In it, you can find an sample application which shows you how to load some data on a Graph element, and how to deploy it to a Visualization item.

    For generating a StackedAreaChart you'll need to load your data into a prefuse.data.Table object, which you could load, by the example, from a CSV file:

    CSVTableReader reader=new CSVTableReader();
    Table myTable=reader.readTable("/myDataFile.csv");
    

    Then, add the table to the visualization as a data group, i.e "table"

    Visualization vis = new Visualization();
    vis.add("table", myTable);
    

    Then, create the StackedAreaChart, and add it to the visualization actions collection:

    //params: name of the data group to layout, name of the data field in which to store computed polygons, and an array containing the names of the various data fields, in sorted order, that should be referenced for each consecutive point of a stack layer
    StackedAreaChart chart=new StackedAreaChart ("table", fieldName, csvColumnsToCompute);
    //add the layout action with a unique key
     vis.putAction("myChartLayout", chart);
    

    Then, you can config various layout actions,or other visual aspects (see the linked example).

    At last, for displaying the chart, you'll have to create a Display object, bind the visualization, and run the layout actions on it:

    //this Display initialization is extracted from the Example app
    Display d = new Display(vis);
    d.setSize(720, 500); // set display size
    // drag individual items around
    d.addControlListener(new DragControl());
    // pan with left-click drag on background
    d.addControlListener(new PanControl()); 
    // zoom with right-click drag
    d.addControlListener(new ZoomControl());
    
    // create a new window to hold the visualization
    JFrame frame = new JFrame("prefuse example");
    // ensure application exits when window is closed
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(d);
    frame.pack();           // layout components in window
    frame.setVisible(true); // show the window
    
    //At the end: RUN THE CHART ACTION:
    vis.run("myChartLayout");
    

    Hope this helps, at least as a first start (the code snippets are not intended for copy-paste and can contain some compile errors).

    Good luck.