Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using JfreeChart

Status
Not open for further replies.

puntito

Programmer
Mar 22, 2006
18
I have been working with this library and want to share mi code.
Code:
private static JFreeChart doingChart(String weekIni, String weekFinal, Integer aplication) {
	JFreeChart chart = null;
    	GraficasDAO dao1 = new GraficasDAO(); 
    	ArrayList results = new ArrayList();
    	dataset = new DefaultCategoryDataset();
 
//Asking 4 data information
	results = (ArrayList)dao1.totXEquipo(doingMapa(weekIni, weekFinal,aplication));

//Filling dataset with the results
	datosTotXsem(results,-1);

//creating chart.
// Chart name, X axis label, Y axis value, data set
chart = ChartFactory.createBarChart("Sells by PhoneType", "PERIOD", "QUANTITY",dataset, PlotOrientation.VERTICAL, true, true, true);
    
//get a reference to the plot for further customisation...
         CategoryPlot plot = chart.getCategoryPlot();
         plot.setDomainGridlinePaint(Color.WHITE);
         plot.setDomainGridlinesVisible(true);
         plot.setRangeGridlinePaint(Color.GRAY);
         chart.setBackgroundPaint(new Color(255,255,255));

         //How to show the values
         DecimalFormat decimalformat1 = new DecimalFormat("##,###");
         CategoryItemRenderer renderer = new BarRenderer();

//Changing color to the bars
         renderer.setSeriesPaint(0,new Color(0,204,204));
         renderer.setSeriesPaint(2,new Color(153,204,255));
         renderer.setSeriesPaint(3,new Color(255,102,0));
         renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", decimalformat1));
         renderer.setBaseItemLabelsVisible(true);
         chart.getCategoryPlot().setRenderer(renderer);
        
    	return chart;
	}


public static void datosTotXsem(ArrayList lista, int week){
	//obtaining each result
	for(int i=0; i < lista.size(); i++ ) {
	  DataSetVO vo = (DataSetVO) lista.get(i);

	  String title= "Week"+week;

//data could be a float value or int
	if (vo.isFlag()){
	  dataset.setValue(vo.getYaxisF(),vo.getXAxis(),vo.getSerie()+week);
	  dataFound = true;
	}else{
	  dataset.setValue(vo.getYAxis(),vo.getXAxis(),title);
	  dataFound = true;
	}
	} 
	}

Hope this could help somebody.

More Info at:
 
I have used JFreeChart too and I have tried to modify the renderer in JFreeChart. JFreeChart is very configurable but you
have to code more than normal Java Charting software. After some major version upgrade in the future, I think JFreeChart will be a very good software. JFreeChart is free but you need to pay for the manual.
 
Yes I'm having some problems trying to personalize my charts.
Principaly, how to set the labels, because my chart is positionated Horizontaly.
Can´t place the labels outside the bars.
I'm having a bad time with that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top