how to get value of value axis on the top of bar chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
samaeinder
Posts: 10
Joined: Thu Jun 12, 2008 6:34 am

how to get value of value axis on the top of bar chart

Post by samaeinder » Fri Jul 04, 2008 9:35 am

how to get value of value axis on the top of bar chart...............
plz plz help me out

like in this example

http://www.java2s.com/Code/Java/Chart/ChartBarChart.htm[/img][/code]package src;

import java.awt.Color;
import java.awt.Paint;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

import javax.servlet.http.HttpSession;


import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import java.awt.Color;
import java.awt.Paint;

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.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
//import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.*;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;

import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.BarRenderer;
import org.jfree.chart.renderer.LayeredBarRenderer;
import org.jfree.chart.renderer.StandardXYItemRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.chart.urls.TimeSeriesURLGenerator;

import org.jfree.data.DefaultCategoryDataset;
import org.jfree.data.XYDataset;
import org.jfree.data.XYSeries;
import org.jfree.data.XYSeriesCollection;
import org.jfree.data.category.CategoryDataset;
import org.jfree.ui.TextAnchor;

class CustomRenderer extends LayeredBarRenderer {

/**
*
*/
private static final long serialVersionUID = 1L;
/** The colors. */
private Paint[] colors;

/**
* Creates a new renderer.
*
* @param colors the colors.
*/
public CustomRenderer(final Paint[] colors) {
this.colors = colors;
}

/**
* Returns the paint for an item. Overrides the default behaviour inherited from
* AbstractSeriesRenderer.
*
* @param row the series.
* @param column the category.
*
* @return The item color.
*/
public Paint getItemPaint(final int row, final int column) {
return this.colors[column % this.colors.length];
}
}
public class barTool{
public static String generateXYChart(String section, HttpSession session, PrintWriter pw) {
String filename = null;
try{
Connection con;
ResultSet total;
Statement stmt;
int tcount=0,fcount=0,icount=0,wcount=0,dcount=0,wfcount=0;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con= DriverManager.getConnection("jdbc:odbc:bugzilla","bzquery","bzquery");
stmt = con.createStatement();
total = stmt.executeQuery("select target_milestone from bugs where product_id in ('114','115') and target_milestone='3.6.2'");
while(total.next())
{
total.getString("target_milestone");
tcount=tcount+1;
}
total.close();
ResultSet fixed=stmt.executeQuery("select target_milestone from bugs where resolution='fixed' and product_id in ('114','115') and target_milestone='3.6.2'");
while(fixed.next())
{
fixed.getString("target_milestone");
fcount=fcount+1;
}
fixed.close();
ResultSet invalid=stmt.executeQuery("select target_milestone from bugs where resolution='INVALID' and product_id in ('114','115') and target_milestone='3.6.2'");
while(invalid.next())
{
invalid.getString("target_milestone");
icount=icount+1;
}
invalid.close();
ResultSet wont=stmt.executeQuery("select target_milestone from bugs where resolution='WONTFIX' and product_id in ('114','115') and target_milestone='3.6.2'");
while(wont.next())
{
wont.getString("target_milestone");
wcount=wcount+1;
}
wont.close();
ResultSet duplicate=stmt.executeQuery("select target_milestone from bugs where resolution='DUPLICATE' and product_id in ('114','115') and target_milestone='3.6.2'");
while(duplicate.next())
{
duplicate.getString("target_milestone");
dcount=dcount+1;
}
duplicate.close();
ResultSet wfme=stmt.executeQuery("select target_milestone from bugs where resolution='WORKSFORME' and product_id in ('114','115') and target_milestone='3.6.2'");
while(wfme.next())
{
wfme.getString("target_milestone");
wfcount=wfcount+1;
}
wfme.close();
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
final String type1= "Total Bugs";
final String type2 = "Bugs";
final String type3 = "Invalid Bugs";
final String type4 = "Wont Fix bugs";
final String type5 = "Duplicate Bugs";
final String type6 = "WorksForMe Bugs";


/* while(rs.next()){
dataset.addValue(rs.getInt(1),type1, rs.getString(2));
}*/
dataset.addValue(tcount, "Bugs", "Total");
dataset.addValue(fcount, "Bugs", "Valid");
dataset.addValue(icount, "Bugs", "Invalid");
dataset.addValue(wcount, "Bugs", "Wont Fix");
dataset.addValue(dcount, "Bugs", "Duplicate");
dataset.addValue(wfcount, "Bugs", "WorksForMe");

/* final JFreeChart chart = ChartFactory.createBarChart(
"Bar Chart Demo 3", // chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // the plot orientation
true, // include legend
true,
true
);

*/final CustomRenderer renderer = new CustomRenderer(
new Paint[] {Color.red, Color.blue, Color.green,
Color.yellow, Color.orange, Color.cyan,
Color.magenta, Color.blue}
);
renderer.setItemURLGenerator(new StandardCategoryURLGenerator("wsmlatest.jsp","series","section"));
renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
renderer.setItemLabelsVisible(true);
final ItemLabelPosition p = new ItemLabelPosition(
ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, 45.0
);
renderer.setPositiveItemLabelPosition(p);
CategoryAxis categoryAxis = new CategoryAxis("Bug Types");
ValueAxis valueAxis = new NumberAxis("No. of bugs raised");
DecimalFormat decimalformat1 = new DecimalFormat("$##,###.00");

renderer.setBaseItemLabelsVisible(true);

Plot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);
final CategoryAxis domainAxis = ((CategoryPlot) plot).getDomainAxis();
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
);
JFreeChart chart = new JFreeChart("WSM 3.6.2 Release Bugs Graph",JFreeChart.DEFAULT_TITLE_FONT, plot, false);


// get a reference to the plot for further customisation...
// final CategoryPlot plot = chart.getCategoryPlot();
plot.setNoDataMessage("NO DATA!");

/* final CustomRenderer renderer = new CustomRenderer(
new Paint[] {Color.red, Color.blue, Color.green,
Color.yellow, Color.orange, Color.cyan,
Color.magenta, Color.blue}
);
renderer.setItemURLGenerator(new StandardCategoryURLGenerator("wsmlatest.jsp","series","section"));
renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
renderer.setItemLabelsVisible(true);
final ItemLabelPosition p = new ItemLabelPosition(
ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, 45.0
);
renderer.setPositiveItemLabelPosition(p);

plot.setRenderer(renderer);*/
// change the margin at the top of the range axis...
// final ValueAxis rangeAxis = plot.getRangeAxis();
// rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// rangeAxis.setLowerMargin(0.15);
// rangeAxis.setUpperMargin(0.15);

ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);

// Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, filename, info, false );
pw.flush();


} catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = "public_error_500x300.png";
}
return filename;
}
}

[/code]

plzzzzzzzzzzzzzz help me

samaeinder
Posts: 10
Joined: Thu Jun 12, 2008 6:34 am

Post by samaeinder » Fri Jul 04, 2008 12:06 pm

even i have write the code for displaying the value on the m not able to get

Code: Select all

package src;

import java.awt.Color;
import java.awt.Paint;
import java.io.PrintWriter;
	import java.sql.Connection;
	import java.sql.DriverManager;
	import java.sql.ResultSet;
	import java.sql.Statement;
import java.text.DecimalFormat;
	import java.text.NumberFormat;
	import java.text.SimpleDateFormat;
	import java.util.Locale;

	import javax.servlet.http.HttpSession;


	import org.jfree.chart.ChartFactory;
	import org.jfree.chart.ChartRenderingInfo;
	import org.jfree.chart.ChartUtilities;
	import org.jfree.chart.JFreeChart;
	import java.awt.Color;
	import java.awt.Paint;

	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.axis.ValueAxis;
	import org.jfree.chart.labels.ItemLabelAnchor;
	import org.jfree.chart.labels.ItemLabelPosition;
	import org.jfree.chart.plot.CategoryPlot;
	import org.jfree.chart.plot.PlotOrientation;
	//import org.jfree.chart.renderer.category.BarRenderer;
	import org.jfree.chart.renderer.category.*;
	import org.jfree.data.category.CategoryDataset;
	import org.jfree.data.general.DatasetUtilities;
	import org.jfree.ui.ApplicationFrame;
	import org.jfree.ui.RefineryUtilities;
	import org.jfree.ui.TextAnchor;

	import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
	import org.jfree.chart.axis.DateAxis;
	import org.jfree.chart.axis.NumberAxis;
	import org.jfree.chart.axis.ValueAxis;
	import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
	import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
	import org.jfree.chart.plot.PlotOrientation;
	import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.BarRenderer;
import org.jfree.chart.renderer.LayeredBarRenderer;
	import org.jfree.chart.renderer.StandardXYItemRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
	import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
	import org.jfree.chart.urls.TimeSeriesURLGenerator;

import org.jfree.data.DefaultCategoryDataset;
	import org.jfree.data.XYDataset;
	import org.jfree.data.XYSeries;
import org.jfree.data.XYSeriesCollection;
import org.jfree.data.category.CategoryDataset;
import org.jfree.ui.TextAnchor;

class CustomRenderer extends LayeredBarRenderer {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** The colors. */
    private Paint[] colors;

    /**
     * Creates a new renderer.
     *
     * @param colors  the colors.
     */
    public CustomRenderer(final Paint[] colors) {
        this.colors = colors;
    }

    /**
     * Returns the paint for an item.  Overrides the default behaviour inherited from
     * AbstractSeriesRenderer.
     *
     * @param row  the series.
     * @param column  the category.
     *
     * @return The item color.
     */
    public Paint getItemPaint(final int row, final int column) {
        return this.colors[column % this.colors.length];
    }
}
public class barTool{
	public static String generateXYChart(String section, HttpSession session, PrintWriter pw) {
	String filename = null;
	try{
		Connection con;
		ResultSet total;
		Statement stmt;
		int tcount=0,fcount=0,icount=0,wcount=0,dcount=0,wfcount=0;
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		con= DriverManager.getConnection("jdbc:odbc:bugzilla","bzquery","bzquery");
		stmt = con.createStatement();
		total = stmt.executeQuery("select target_milestone from bugs where product_id in ('114','115') and target_milestone='3.6.2'");
		while(total.next())
		{
			total.getString("target_milestone");
			tcount=tcount+1;
		}
		total.close();
		ResultSet fixed=stmt.executeQuery("select target_milestone from bugs where resolution='fixed' and product_id in ('114','115') and target_milestone='3.6.2'");
		while(fixed.next())
		{
			fixed.getString("target_milestone");
			fcount=fcount+1;
		}
		fixed.close();
		ResultSet invalid=stmt.executeQuery("select target_milestone from bugs where resolution='INVALID' and product_id in ('114','115') and target_milestone='3.6.2'");
		while(invalid.next())
		{
			invalid.getString("target_milestone");
			icount=icount+1;
		}
		invalid.close();
		ResultSet wont=stmt.executeQuery("select target_milestone from bugs where resolution='WONTFIX' and product_id in ('114','115') and target_milestone='3.6.2'");
		while(wont.next())
		{
			wont.getString("target_milestone");
			wcount=wcount+1;
		}
		wont.close();
		ResultSet duplicate=stmt.executeQuery("select target_milestone from bugs where resolution='DUPLICATE' and product_id in ('114','115') and target_milestone='3.6.2'");
		while(duplicate.next())
		{
			duplicate.getString("target_milestone");
			dcount=dcount+1;
		}
		duplicate.close();
		ResultSet wfme=stmt.executeQuery("select target_milestone from bugs where resolution='WORKSFORME' and product_id in ('114','115') and target_milestone='3.6.2'");
		while(wfme.next())
		{
			wfme.getString("target_milestone");
			wfcount=wfcount+1;
		}
		wfme.close();
	final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	final String type1= "Total Bugs";
	final String type2 = "Bugs";
	final String type3 = "Invalid Bugs";
	final String type4 = "Wont Fix bugs";
	final String type5 = "Duplicate Bugs";
	final String type6 = "WorksForMe Bugs";
	
	
/*	while(rs.next()){
		dataset.addValue(rs.getInt(1),type1, rs.getString(2));
	}*/
	dataset.addValue(tcount, "Bugs", "Total");
	dataset.addValue(fcount, "Bugs", "Valid");
	dataset.addValue(icount, "Bugs", "Invalid");
	dataset.addValue(wcount, "Bugs", "Wont Fix");
	dataset.addValue(dcount, "Bugs", "Duplicate");
	dataset.addValue(wfcount, "Bugs", "WorksForMe");	

	      /*  final JFreeChart chart = ChartFactory.createBarChart(
	            "Bar Chart Demo 3",       // chart title
	            "Category",               // domain axis label
	            "Value",                  // range axis label
	             dataset,                  // data
	            PlotOrientation.VERTICAL, // the plot orientation
	            true,                    // include legend
	            true,
	            true
	        );
	        
	        */final CustomRenderer renderer =   new CustomRenderer(
	                new Paint[] {Color.blue, Color.green, Color.red,
		                    Color.magenta, Color.orange, Color.cyan,
		                    Color.yellow, Color.blue}
		            );
		     //       renderer.setItemURLGenerator(new StandardCategoryURLGenerator("wsmlatest.jsp","series","section"));
	           // renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
	           renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
	            //DecimalFormat dc = 	new DecimalFormat( "(###.##)" );
	    		//StandardCategoryLabelGenerator categoryLabel = new StandardCategoryLabelGenerator("{2}",dc); 

	        [b]StandardCategoryLabelGenerator categoryLabel = new StandardCategoryLabelGenerator("{2}",new DecimalFormat( "(###.##)" )); 

	        renderer.setSeriesLabelGenerator(0,categoryLabel); 
	        renderer.setItemLabelsVisible(true); 
		          [/b]		            CategoryAxis categoryAxis = new CategoryAxis("Bug Types");
		    		ValueAxis valueAxis = new NumberAxis("No. of bugs raised");
		    	
		    		
	Plot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);
	
	final CategoryAxis domainAxis = ((CategoryPlot) plot).getDomainAxis();
    domainAxis.setCategoryLabelPositions(
        CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
    );
	        JFreeChart chart = new JFreeChart("WSM 3.6.2 Release Bugs Graph",JFreeChart.DEFAULT_TITLE_FONT, plot, false);
			chart.setBackgroundPaint(java.awt.Color.white);
	        
	        
	           

	            // get a reference to the plot for further customisation...
	        //    final CategoryPlot plot = chart.getCategoryPlot();
	            plot.setNoDataMessage("NO DATA!");

	          /*  final CustomRenderer renderer =   new CustomRenderer(
	                new Paint[] {Color.red, Color.blue, Color.green,
	                    Color.yellow, Color.orange, Color.cyan,
	                    Color.magenta, Color.blue}
	            );
	            renderer.setItemURLGenerator(new StandardCategoryURLGenerator("wsmlatest.jsp","series","section"));
            renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
            renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
	            renderer.setItemLabelsVisible(true);
	            final ItemLabelPosition p = new ItemLabelPosition(
	                ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, 45.0
	            );
	            renderer.setPositiveItemLabelPosition(p);
	            
	         plot.setRenderer(renderer);*/
           // change the margin at the top of the range axis...
	          //  final ValueAxis rangeAxis = plot.getRangeAxis();
	           // rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
	           // rangeAxis.setLowerMargin(0.15);
	          //  rangeAxis.setUpperMargin(0.15);
	        
	            ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
	    		filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);

	    		//  Write the image map to the PrintWriter
	    		ChartUtilities.writeImageMap(pw, filename, info, false );
	    		pw.flush();

	    	
	    	} catch (Exception e) {
	    		System.out.println("Exception - " + e.toString());
	    		e.printStackTrace(System.out);
	    	 filename = "public_error_500x300.png";
	    	}
	    	return filename;
	    	}
	    }
	        	

samaeinder
Posts: 10
Joined: Thu Jun 12, 2008 6:34 am

this i have written newly for that

Post by samaeinder » Fri Jul 04, 2008 12:07 pm

StandardCategoryLabelGenerator categoryLabel = new StandardCategoryLabelGenerator("{2}",new DecimalFormat( "(###.##)" ));

renderer.setSeriesLabelGenerator(0,categoryLabel);
renderer.setItemLabelsVisible(true);

Pellegrino
Posts: 16
Joined: Wed Jun 17, 2009 5:00 pm

Re: how to get value of value axis on the top of bar chart

Post by Pellegrino » Tue Jul 28, 2009 1:21 pm

Salut!

I have a problem with LayeredBarRenderer! It doesn't want to show the labels of each bar!

Code: Select all

               LayeredBarRenderer renderer = new LayeredBarRenderer();
               StandardCategoryItemLabelGenerator categoryLabel = new StandardCategoryItemLabelGenerator("{2}",new DecimalFormat( "(###.##)" ));

		renderer.setSeriesItemLabelGenerator(0,categoryLabel);
		renderer.setSeriesItemLabelsVisible(0,true);
Thanx

Locked