Statistical Bar Chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
conala
Posts: 3
Joined: Mon May 25, 2015 10:00 pm
antibot: No, of course not.

Statistical Bar Chart

Post by conala » Mon May 25, 2015 10:09 pm

Hello, i was trying to make my first JFreeChart Statiscal bar Chart but I can't find the way I can display ir, so long I have this, with all the imports and such:

Code: Select all

				DefaultStatisticalCategoryDataset dataset = new DefaultStatisticalCategoryDataset();
		        dataset.add(10.0, 2.4, "Series 1", "C1(Mean,StdDev)");
		        dataset.add(15.0, 4.4, "Series 1", "C2(Mean,StdDev)");
		        dataset.add(13.0, 2.1, "Series 1", "C3(Mean,StdDev)");
		        dataset.add(7.0, 1.3, "Series 1", "C4(Mean,StdDev)");
		        
	       		JFreeChart chart = ChartFactory.createLineChart(
	       				"Stadisticas del programa", // chart title
		            "Tiempo Ejecucion",                         // domain axis label
		            "Numero de nodos",                        // range axis label
		            dataset,                        // data
		            PlotOrientation.VERTICAL,       // orientation
		            true,                           // include legend
		            true,                           // tooltips
		            false                           // urls
		        );
	       		
	       		chart.setBackgroundPaint(Color.white);

	            CategoryPlot plot = (CategoryPlot) chart.getPlot();
	            plot.setBackgroundPaint(Color.lightGray);
	            plot.setRangeGridlinePaint(Color.white);

	            NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
	            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
	            rangeAxis.setAutoRangeIncludesZero(true);
	            

//Here comes my problem, because I can't find the way to display it
	            BarChartDemo1 frame = new BarChartDemo1("Hello");
	            frame.pack();
	            RefineryUtilities.centerFrameOnScreen(frame);
	            frame.setVisible(true);
	     
I know that maybe it's just 3 or 4 lines of code what I need, but I cant really find which ones I need :cry:


If someone could help me with this, I would greatly appreciate.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Statistical Bar Chart

Post by paradoxoff » Tue May 26, 2015 12:11 pm

Your code is not compilable (no method or class declaration).
I agree that you probably need only a few more lines of code, but I would prefer to place those in a code snippet that it running otherwise.

conala
Posts: 3
Joined: Mon May 25, 2015 10:00 pm
antibot: No, of course not.

Re: Statistical Bar Chart

Post by conala » Tue May 26, 2015 2:31 pm

Here's the compilable code:

Code: Select all

import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.demo.BarChartDemo1;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StatisticalBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;
import org.jfree.ui.RefineryUtilities;

/**
 * A simple demonstration application showing how to create a bar chart.
 */
public class First  {
	public static void main(String[] args) {
		DefaultStatisticalCategoryDataset dataset = new DefaultStatisticalCategoryDataset();
	    dataset.add(10.0, 2.4, "Series 1", "C1(Mean,StdDev)");
	    dataset.add(15.0, 4.4, "Series 1", "C2(Mean,StdDev)");
	    dataset.add(13.0, 2.1, "Series 1", "C3(Mean,StdDev)");
	    dataset.add(7.0, 1.3, "Series 1", "C4(Mean,StdDev)");
	    //dataset.add(22.0, 2.4, "Series 2", "C1(Mean,StdDev)");
	    //dataset.add(18.0, 4.4, "Series 2", "C2(Mean,StdDev)");
	    //dataset.add(28.0, 2.1, "Series 2", "C3(Mean,StdDev)");
	    //dataset.add(17.0, 1.3, "Series 2", "C4(Mean,StdDev)");
	    
		JFreeChart chart = ChartFactory.createLineChart(
					"Stadisticas del programa", // chart title
	        "Tiempo Ejecucion",                         // domain axis label
	        "Numero de nodos",                        // range axis label
	        dataset,                        // data
	        PlotOrientation.VERTICAL,       // orientation
	        true,                           // include legend
	        true,                           // tooltips
	        false                           // urls
	    );
			
			chart.setBackgroundPaint(Color.white);

	    CategoryPlot plot = (CategoryPlot) chart.getPlot();
	    plot.setBackgroundPaint(Color.lightGray);
	    plot.setRangeGridlinePaint(Color.white);

	    // customise the range axis...
	    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
	    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
	    rangeAxis.setAutoRangeIncludesZero(true);
	    
	    //Here comes my problem, because I can't find the way to display it
	    
	    BarChartDemo1 frame = new BarChartDemo1("Hello");
	    frame.pack();
	    RefineryUtilities.centerFrameOnScreen(frame);
	    frame.setVisible(true);
	    
	    

	    //ChartFrame frame = new ChartFrame("First", chart);
	    //frame.pack();
	    //
	}
}

With this i geyt this plot

Image

But this is the plot I'm looking for:

Image

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Statistical Bar Chart

Post by paradoxoff » Tue May 26, 2015 3:43 pm

I see two major issues with your code:
1. You are not using the chart that your are creating. Instead, you show the BarChartDemo1. Interestingly, you already have two lines in your code that should display your data.
2. Even if you used your own chart: you are creating a line chart. This chart type ignores the sub type of CategoryDataset that you use (a StatisticalCategoryDataset inh your case), and treats the data as a plain CategoryDataset. Unfortunately, you cannot create a "statistical bar chart" or a "statistical line chart" using the ChartFactory.
I suggest to create a CategoryPlot from your dataset, an instance of StatisticalBarRenderer as renderer, a CatetgoryAxis and a NumberAxis, the create an instance of JFreeChart from your plot, an finally show the JFreeChart in a ChartFrame.

conala
Posts: 3
Joined: Mon May 25, 2015 10:00 pm
antibot: No, of course not.

Re: Statistical Bar Chart

Post by conala » Tue May 26, 2015 5:49 pm

Thanks a lot, it really help me a lot :P

But now I have some little problem, I want the chart to be visible within my main window (pictured in the red zone) and not create a new window to display the graph. Is there any way I can do that? Thanks

Image

Code:

Code: Select all

DefaultStatisticalCategoryDataset dataset = new DefaultStatisticalCategoryDataset();
			    dataset.add(10.0, 2.4, "Series 1", "C1(Mean,StdDev)");
			    dataset.add(15.0, 4.4, "Series 1", "C2(Mean,StdDev)");
			    dataset.add(13.0, 2.1, "Series 1", "C3(Mean,StdDev)");
			    dataset.add(7.0, 1.3, "Series 1", "C4(Mean,StdDev)");

			    

			    
			    
			    CategoryPlot plot = new CategoryPlot(
			    		dataset,
			    		new CategoryAxis("Numero de Nodos"),
			    		new NumberAxis("Tiempo de ejecucion"),
			    		new StatisticalBarRenderer());
			    plot.setOrientation(PlotOrientation.VERTICAL);
			    plot.setBackgroundPaint(Color.lightGray);

			    JFreeChart chart = new JFreeChart("Prueba",JFreeChart.DEFAULT_TITLE_FONT,plot,true);

			    chart.setBackgroundPaint(Color.white);
			    
			    ChartFrame frame = new ChartFrame("Prueba2", chart);
			    frame.pack();
			    RefineryUtilities.centerFrameOnScreen(frame);
			    frame.setVisible(true);

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Statistical Bar Chart

Post by paradoxoff » Tue May 26, 2015 8:10 pm

You can place your chart in a ChartPanel (which extends JPanel )and add that to your GUI.

Locked