What is best and easy to use library for Java graphs? I am working with Swing application and need to integrate a graph for that project
            Asked
            
        
        
            Active
            
        
            Viewed 2.9k times
        
    16
            
            
        - 
                    What do you mean by 'Java graphs' ? – Prashant Bhate Jul 02 '11 at 14:46
- 
                    1I'd recommend the relatively new library, XChart to anyone who is visiting this post closer to 2015 – Adam Hughes Jan 12 '16 at 22:12
3 Answers
14
            
            
        Options
- If graphs you mean like clusters nodes and links visualization have a look at graphviz gallery 
- To generate charts see jfreechart demo Download jfreechart jfreechart-1.0.13 
- Use java advanced imaging - jai see histogram demo source
Steps to create a chart using jfreechart
Create Dataset and pass array of data
HistogramDataset dataset = new HistogramDataset();
dataset.addSeries("series label",arrayOfValues,noOfBins);
Create a chart object
JFreeChart chart = ChartFactory.
createHistogram( "plotTitle", "xaxis label", "yaxis label", 
                dataset, PlotOrientation.VERTICAL, false, false, false);
If swing application use ChartPanel to render chart 
ChartPanel chartPanel = new ChartPanel(chart)
chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
chartPanel.setMouseZoomable(true, false);
If need to write chart to a file/stream use ChartUtilities.saveChartAsPNG(...)
ChartUtilities.saveChartAsPNG(new File("histogram.PNG"), chart, width, height);
 
    
    
        Prashant Bhate
        
- 10,907
- 7
- 47
- 82
- 
                    
- 
                    oh i see , I have updated the answer to reflect what you might need to use – Prashant Bhate Jul 02 '11 at 15:11
- 
                    
- 
                    How can i use that library to create a histogram (or bar char) – Keshan De Silva Jul 02 '11 at 15:31
- 
                    the sample code that given is too hard to under stand. because i am new to Java. So can you please help me to create a chart. I have the data in 2D array and a JPanel to display chart. – Keshan De Silva Jul 02 '11 at 15:39
- 
                    DefaultCategoryDataset dataset = new DefaultCategoryDataset(); is not working... ask for the class even i linked the JAR file and import the import org.jfree.data.category.DefaultCategoryDataset; – Keshan De Silva Jul 02 '11 at 16:04
- 
                    how can i link the JAR file in to my net-beans project. I already add the JAR file in to the library. But when compiling it says that 'HistogramDataset' class can't find??? – Keshan De Silva Jul 02 '11 at 16:11
- 
                    make sure that you have both jfreechart-1.0.13.jar and jcommons-1.0.13.jar on the build path – Prashant Bhate Jul 02 '11 at 16:19
- 
                    addSeries(Comparable key, double[] values, int bins) what is the parameter for 'key' it is not accepting the String – Keshan De Silva Jul 02 '11 at 16:19
9
            One commonly used charting API is JFreeChart.
 
    
    
        Andrew Thompson
        
- 168,117
- 40
- 217
- 433
- 
                    can you give me sample code for how to use that Library. i Try to find. But i fail. Only i get it the API. But i can't find any sample code. – Keshan De Silva Jul 02 '11 at 14:40
- 
                    3See also the `JFreeChart` [tag](http://stackoverflow.com/tags/jfreechart/info). – trashgod Jul 02 '11 at 16:12
- 
                    1
 
     
    