Why doesn't itemMargin in BarRenderer work?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
MasterJ
Posts: 19
Joined: Fri Jun 23, 2006 8:12 am

Why doesn't itemMargin in BarRenderer work?

Post by MasterJ » Fri Jul 21, 2006 4:36 am

Hi! Everybody,

I need to set the spacing between the bars with in a single category

I'm using the following code...


final CategoryItemRenderer renderer = plot.getRenderer();
final BarRenderer3D ren = (BarRenderer3D) renderer;
ren.setItemMargin(0.1);

And also I need to set the amount of space reserved at the start of the axis.
and the amount of space reserved at the end of the axis. For this I'm using

final CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLowerMargin(0.0);
domainAxis.setUpperMargin(0.0);



why am I not getting the changes?
Is there any other way
or am I doing anything wrong here?
Master Java

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Fri Jul 21, 2006 3:22 pm

This seems to work for me. This only has an effect if you have more than one series (that is, more than one bar per category), but you said you did so I can't see why it doesn't work. Can you post a small demo that reproduces the problem?
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

MasterJ
Posts: 19
Joined: Fri Jun 23, 2006 8:12 am

Eliminating the space in the plot area after the last bar

Post by MasterJ » Sat Jul 22, 2006 4:36 am

Hi! David,
I could manage to set the pace between the bars, and that between the fierst ba and the Range Axis. .So now all the bars are aligned to the left of the polt Area. That's what I want.
But there still reamins an excess of plot beyonnd the last bar ,which I don't need.
So how to eliminate the trailing plot area after the last bar.

Could you please help me?

Anyway, Thankyou..
Master Java

MasterJ
Posts: 19
Joined: Fri Jun 23, 2006 8:12 am

Please David.

Post by MasterJ » Mon Jul 24, 2006 2:41 pm

Dear David,

Please look into my problem.
I need to remove the excess of plot area after the last bar.

May be like pieplot.setInteriorGap() method does for pie Graph.
Could you please help me.
I need it urgently.
Thanking you,
MJ
Master Java

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Jul 25, 2006 10:53 am

david.gilbert wrote:Can you post a small demo that reproduces the problem?
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

FBueckert
Posts: 5
Joined: Thu Jul 20, 2006 5:58 pm

Post by FBueckert » Tue Jul 25, 2006 3:41 pm

I had been having the same sort of problem. I set the ItemMargin to a negative value, and it widens out my bars. Of course, once you do that, your series labels don't tend to line up with the bars, so you need to tweak with those, too.

MasterJ
Posts: 19
Joined: Fri Jun 23, 2006 8:12 am

Please help me!

Post by MasterJ » Wed Aug 02, 2006 7:15 am

Dear David,
I tried my best to find out a solution but could not; ad so have come to you for help.


Tell me how to remove the area of the plot after the laft baras I am pulling all the bars together to the left , there is a lot of space left on the right of the plot which I don't need.
But the legend width should not be changed. It should be independent of plt width, and equal to chart width.
Thanks in advance,
MJ

Here is entire Servlet that I'm using Please help me out.
It has to be edited to remove unnecessary duplicating,ofcourse.
package zenith;

//~--- non-JDK imports --------------------------------------------------------

import org.jfree.chart.ChartFactory;

//import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.LegendItemSource;
import org.jfree.chart.axis.AxisSpace;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;

//import org.jfree.chart.axis.NumberAxis;
//import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.block.BlockBorder;

//import org.jfree.chart.labels.CategoryToolTipGenerator;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;

//import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;

//import org.jfree.chart.renderer.category.AbstractCategoryItemRenderer;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.IntervalBarRenderer;
import org.jfree.chart.renderer.category.StatisticalBarRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.category.CategoryDataset;

//import org.jfree.data.category.DefaultCategoryDataset;
//import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.jdbc.JDBCCategoryDataset;
import org.jfree.data.jdbc.JDBCPieDataset;
import org.jfree.data.jdbc.JDBCXYDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.date.SerialDate;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;

//~--- JDK imports ------------------------------------------------------------

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;

import java.io.IOException;
import java.io.OutputStream;

//import java.io.PrintWriter;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.text.NumberFormat;

import java.util.Iterator;

import javax.servlet.*;
import javax.servlet.http.*;

//~--- classes ----------------------------------------------------------------

public class ChartModifier extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=UTF-8";

//~--- fields -------------------------------------------------------------

String user = "PRASCHEMA";
String url = "jdbc:oracle:thin:@192.168.10.11:1521:devdb01";
String password = "oracle123";
String driver = "oracle.jdbc.OracleDriver";
Connection logincon;
String type;

//~--- constructors -------------------------------------------------------

public ChartModifier() {
}

//~--- methods ------------------------------------------------------------

/**
* Creates a sample bar chart.
*
* @return a bar chart.
*/
private JFreeChart createBarChart(String SQLQuery, String title,
String domain, String range) {
String query = SQLQuery;
CategoryDataset dataset = readBarData(query);
JFreeChart chart =
ChartFactory.createBarChart3D(title, domain, range, dataset,
PlotOrientation.VERTICAL, true, true,
false);

// chart.setBackgroundPaint(new Color(0xbb,0x88,0x33));
final CategoryPlot plot = chart.getCategoryPlot();

// plot.setBackgroundPaint(new Color(0xcc,0xdd,0x55));
final CategoryAxis axis = plot.getDomainAxis();

axis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI /
8.0));

final CategoryItemRenderer renderer = plot.getRenderer();

renderer.setItemLabelsVisible(true);

final BarRenderer3D ren = (BarRenderer3D)renderer;

ren.setMaximumBarWidth(0.03);

/*
*
* LegendItemCollection legendItemCollection=new LegendItemCollection();
*
* LegendItemCollection licollection= renderer.getLegendItems();
* Iterator iter= licollection.iterator();
* int item=-1;
* do{
* ++item;
* if(item==licollection.getItemCount())
* break;
*
* LegendItem li =licollection.get( item);
*
*
* System.out.println(" List Itemcount="+ licollection.getItemCount());
* System.out.println("Column Count="+ dataset.getColumnCount());
* System.out.println("Column Key="+ dataset.getColumnKey(0));
*
* System.out.println(" Row Key="+ dataset.getRowKey(item));
* System.out.println(" Legend Label="+ li.getLabel());
* System.out.println(" Legend Description="+ li.getDescription());
*
*
*
* java.lang.Number datavalue=dataset.getValue(dataset.getRowKey(item),dataset.getColumnKey(0));
* if(datavalue==null)
* datavalue=0;
*
* System.out.println(" Data VAlue="+ datavalue);
*
* String label=li.getLabel()+"="+datavalue;
* LegendItem legendItem=new LegendItem(label,li.getDescription(),li.getToolTipText(),li.getURLText(),li.getShape(),li.getFillPaint());
* legendItemCollection.add(legendItem);
*
* }while(iter.hasNext());
*
*
* LegendSource legendSource=new LegendSource(legendItemCollection);
*
* LegendTitle legendTitle=new LegendTitle(legendSource);
*
* legendTitle.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
* legendTitle.setBorder(new BlockBorder());
* legendTitle.setBackgroundPaint(new Color(200,150,100));
* legendTitle.setPosition(RectangleEdge.BOTTOM);
*
* chart.addLegend(legendTitle);
*/
return chart;
}

private JFreeChart createCustomBar2Chart(String SQLQuery, String title,
String domain, String range) {
String query = SQLQuery;
CategoryDataset dataset = readCustomBarData(query);
JFreeChart chart =
ChartFactory.createBarChart3D(title, domain, range, dataset,
PlotOrientation.VERTICAL, false,
true, false);

// chart.setBackgroundPaint();
final CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();

domainAxis.setLowerMargin(-0.5);

// domainAxis.setUpperMargin(-0.2);
domainAxis.setCategoryMargin(0.0);
plot.setDomainAxis(domainAxis);

/*
* plot.setOutlinePaint(new Color(0x0,0x0,0x0);
* domainAxis.setCategoryLabelPositions(
* CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 8.0)
* );
*
* BasicStroke stroke=new BasicStroke();
* plot.setOutlineStroke(stroke);
*
*
* plot.setBackgroundPaint(Color.BLUE);
* plot.setForegroundAlpha(0.65f);
* plot.setDomainGridlinePaint(Color.RED);
* plot.setRangeGridlinePaint(Color.GREEN);
* // plot.setDomainCrosshairVisible(true);
* plot.setRangeCrosshairVisible(true);
*
*
* domainAxis.setTickMarkPaint(Color.MAGENTA);
* domainAxis.setLowerMargin(0.0);
* domainAxis.setUpperMargin(0.0);
* domainAxis.setAxisLineStroke(stroke);
* final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
* rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
* rangeAxis.setTickMarkPaint(Color.PINK);
* rangeAxis.setAxisLineStroke(stroke);
*
*
*/
final CategoryItemRenderer renderer = plot.getRenderer();

// AxisSpace axisSpace=new AxisSpace();
// axisSpace.setRight(0.0);
// axisSpace.setLeft(1.0);
// plot.setFixedRangeAxisSpace(axisSpace);
renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
renderer.setItemLabelsVisible(true);

LegendItemCollection legendItemCollection = new LegendItemCollection();
LegendItemCollection licollection = renderer.getLegendItems();
Iterator iter = licollection.iterator();
int item = -1;

do {
++item;

if (item == licollection.getItemCount()) {
break;
}

LegendItem li = licollection.get(item);
java.lang.Number datavalue =
dataset.getValue(dataset.getRowKey(item),
dataset.getColumnKey(0));

if (datavalue == null) {
datavalue = 0;
}

String label = li.getLabel() + "=" + datavalue;
LegendItem legendItem =
new LegendItem(label, li.getDescription(), li.getToolTipText(),
li.getURLText(), li.getShape(),
li.getFillPaint());

legendItemCollection.add(legendItem);
} while (iter.hasNext());

LegendSource legendSource = new LegendSource(legendItemCollection);
LegendTitle legendTitle = new LegendTitle(legendSource);

legendTitle.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
legendTitle.setBorder(new BlockBorder());
legendTitle.setBackgroundPaint(new Color(200, 150, 100));
legendTitle.setPosition(RectangleEdge.BOTTOM);
legendTitle.setItemFont(new Font("Centuary Gothic", Font.ITALIC, 11));
chart.addLegend(legendTitle);
chart.setBackgroundPaint(new Color(0xbb, 0x88, 0x33));
chart.getBackgroundPaint();

final BarRenderer3D ren = (BarRenderer3D)renderer;

ren.setMaximumBarWidth(0.04);
ren.setItemMargin(-1.2);

// renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
// ren.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
// plot.setRenderer(renderer);
// ren.setDrawBarOutline(false);
RectangleInsets ri = chart.getPadding();

ri =
new RectangleInsets(ri.getTop(), ri.getBottom(), ri.getLeft(), ri.getRight() +
50.0);
chart.setPadding(ri);

return chart;
}

private JFreeChart createCustomBar3Chart(String SQLQuery, String title,
String domain, String range) {
String query = SQLQuery;
CategoryDataset dataset = readCustomBarData(query);
JFreeChart chart =
ChartFactory.createBarChart3D(title, domain, range, dataset,
PlotOrientation.VERTICAL, false,
true, false);

// chart.setBackgroundPaint();
final CategoryPlot plot = chart.getCategoryPlot();
final CategoryAxis domainAxis = plot.getDomainAxis();

domainAxis.setLowerMargin(-0.55);

// domainAxis.setUpperMargin(-0.2);
plot.setDomainAxis(domainAxis);

/*
* plot.setOutlinePaint(new Color(0x0,0x0,0x0);
* domainAxis.setCategoryLabelPositions(
* CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 8.0)
* );
*
* BasicStroke stroke=new BasicStroke();
* plot.setOutlineStroke(stroke);
*
*
* plot.setBackgroundPaint(Color.BLUE);
* plot.setForegroundAlpha(0.65f);
* plot.setDomainGridlinePaint(Color.RED);
* plot.setRangeGridlinePaint(Color.GREEN);
* // plot.setDomainCrosshairVisible(true);
* plot.setRangeCrosshairVisible(true);
*
*
* domainAxis.setTickMarkPaint(Color.MAGENTA);
* domainAxis.setLowerMargin(0.0);
* domainAxis.setUpperMargin(0.0);
* domainAxis.setAxisLineStroke(stroke);
* final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
* rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
* rangeAxis.setTickMarkPaint(Color.PINK);
* rangeAxis.setAxisLineStroke(stroke);
*
*
*/
final CategoryItemRenderer renderer = plot.getRenderer();

// AxisSpace axisSpace=new AxisSpace();
// axisSpace.setRight(0.0);
// axisSpace.setLeft(1.0);
// plot.setFixedRangeAxisSpace(axisSpace);
renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
renderer.setItemLabelsVisible(true);

LegendItemCollection legendItemCollection = new LegendItemCollection();
LegendItemCollection licollection = renderer.getLegendItems();
Iterator iter = licollection.iterator();
int item = -1;

do {
++item;

if (item == licollection.getItemCount()) {
break;
}

LegendItem li = licollection.get(item);
java.lang.Number datavalue =
dataset.getValue(dataset.getRowKey(item),
dataset.getColumnKey(0));

if (datavalue == null) {
datavalue = 0;
}

String label = li.getLabel() + "=" + datavalue;
LegendItem legendItem =
new LegendItem(label, li.getDescription(), li.getToolTipText(),
li.getURLText(), li.getShape(),
li.getFillPaint());

legendItemCollection.add(legendItem);
} while (iter.hasNext());

LegendSource legendSource = new LegendSource(legendItemCollection);
LegendTitle legendTitle = new LegendTitle(legendSource);

legendTitle.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
legendTitle.setBorder(new BlockBorder());
legendTitle.setBackgroundPaint(new Color(200, 150, 100));
legendTitle.setPosition(RectangleEdge.BOTTOM);
legendTitle.setItemFont(new Font("Centuary Gothic", Font.ITALIC, 11));
chart.addLegend(legendTitle);
chart.setBackgroundPaint(new Color(0xbb, 0x88, 0x33));
chart.getBackgroundPaint();

final BarRenderer3D ren = (BarRenderer3D)renderer;

ren.setMaximumBarWidth(0.04);
ren.setItemMargin(-2.2);

// renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
// ren.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
// plot.setRenderer(renderer);
// ren.setDrawBarOutline(false);
RectangleInsets ri = chart.getPadding();

ri =
new RectangleInsets(ri.getTop(), ri.getBottom(), ri.getLeft(), ri.getRight() +
50.0);
chart.setPadding(ri);

return chart;
}

private JFreeChart createCustomBar5Chart(String SQLQuery, String title,
String domain, String range) {
String query = SQLQuery;
CategoryDataset dataset = readCustomBarData(query);
JFreeChart chart =
ChartFactory.createBarChart3D(title, domain, range, dataset,
PlotOrientation.VERTICAL, false,
true, false);

// chart.setBackgroundPaint();
final CategoryPlot plot = chart.getCategoryPlot();
final CategoryAxis domainAxis = plot.getDomainAxis();

domainAxis.setLowerMargin(-0.2);

// domainAxis.setUpperMargin(-0.2);
plot.setDomainAxis(domainAxis);

/*
* plot.setOutlinePaint(new Color(0x0,0x0,0x0);
* domainAxis.setCategoryLabelPositions(
* CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 8.0)
* );
*
* BasicStroke stroke=new BasicStroke();
* plot.setOutlineStroke(stroke);
*
*
* plot.setBackgroundPaint(Color.BLUE);
* plot.setForegroundAlpha(0.65f);
* plot.setDomainGridlinePaint(Color.RED);
* plot.setRangeGridlinePaint(Color.GREEN);
* // plot.setDomainCrosshairVisible(true);
* plot.setRangeCrosshairVisible(true);
*
*
* domainAxis.setTickMarkPaint(Color.MAGENTA);
* domainAxis.setLowerMargin(0.0);
* domainAxis.setUpperMargin(0.0);
* domainAxis.setAxisLineStroke(stroke);
* final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
* rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
* rangeAxis.setTickMarkPaint(Color.PINK);
* rangeAxis.setAxisLineStroke(stroke);
*
*
*/
final CategoryItemRenderer renderer = plot.getRenderer();

// AxisSpace axisSpace=new AxisSpace();
// axisSpace.setRight(0.0);
// axisSpace.setLeft(1.0);
// plot.setFixedRangeAxisSpace(axisSpace);
renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
renderer.setItemLabelsVisible(true);

LegendItemCollection legendItemCollection = new LegendItemCollection();
LegendItemCollection licollection = renderer.getLegendItems();
Iterator iter = licollection.iterator();
int item = -1;

do {
++item;

if (item == licollection.getItemCount()) {
break;
}

LegendItem li = licollection.get(item);
java.lang.Number datavalue =
dataset.getValue(dataset.getRowKey(item),
dataset.getColumnKey(0));

if (datavalue == null) {
datavalue = 0;
}

String label = li.getLabel() + "=" + datavalue;
LegendItem legendItem =
new LegendItem(label, li.getDescription(), li.getToolTipText(),
li.getURLText(), li.getShape(),
li.getFillPaint());

legendItemCollection.add(legendItem);
} while (iter.hasNext());

LegendSource legendSource = new LegendSource(legendItemCollection);
LegendTitle legendTitle = new LegendTitle(legendSource);

legendTitle.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
legendTitle.setBorder(new BlockBorder());
legendTitle.setBackgroundPaint(new Color(200, 150, 100));
legendTitle.setPosition(RectangleEdge.BOTTOM);
legendTitle.setItemFont(new Font("Centuary Gothic", Font.ITALIC, 11));
chart.addLegend(legendTitle);
chart.setBackgroundPaint(new Color(0xbb, 0x88, 0x33));
chart.getBackgroundPaint();

final BarRenderer3D ren = (BarRenderer3D)renderer;

ren.setMaximumBarWidth(0.04);
ren.setItemMargin(-1.5);

// renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
// ren.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
// plot.setRenderer(renderer);
// ren.setDrawBarOutline(false);
RectangleInsets ri = chart.getPadding();

ri =
new RectangleInsets(ri.getTop(), ri.getBottom(), ri.getLeft(), ri.getRight() +
50.0);
chart.setPadding(ri);

return chart;
}

private JFreeChart createCustomBarChart(String SQLQuery, String title,
String domain, String range) {
String query = SQLQuery;
CategoryDataset dataset = readCustomBarData(query);
JFreeChart chart =
ChartFactory.createBarChart3D(title, domain, range, dataset,
PlotOrientation.VERTICAL, false,
true, false);

// chart.setBackgroundPaint();
final CategoryPlot plot = chart.getCategoryPlot();
final CategoryAxis domainAxis = plot.getDomainAxis();

domainAxis.setLowerMargin(0.0);

// domainAxis.setUpperMargin(-0.2);
// plot.setDomainAxis(domainAxis);

/*
* plot.setOutlinePaint(new Color(0x0,0x0,0x0);
* domainAxis.setCategoryLabelPositions(
* CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 8.0)
* );
*
* BasicStroke stroke=new BasicStroke();
* plot.setOutlineStroke(stroke);
*
*
* plot.setBackgroundPaint(Color.BLUE);
* plot.setForegroundAlpha(0.65f);
* plot.setDomainGridlinePaint(Color.RED);
* plot.setRangeGridlinePaint(Color.GREEN);
* // plot.setDomainCrosshairVisible(true);
* plot.setRangeCrosshairVisible(true);
*
*
* domainAxis.setTickMarkPaint(Color.MAGENTA);
* domainAxis.setLowerMargin(0.0);
* domainAxis.setUpperMargin(0.0);
* domainAxis.setAxisLineStroke(stroke);
* final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
* rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
* rangeAxis.setTickMarkPaint(Color.PINK);
* rangeAxis.setAxisLineStroke(stroke);
*
*
*/
final CategoryItemRenderer renderer = plot.getRenderer();

// AxisSpace axisSpace=new AxisSpace();
// axisSpace.setRight(0.0);
// axisSpace.setLeft(1.0);
// plot.setFixedRangeAxisSpace(axisSpace);
renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
renderer.setItemLabelsVisible(true);

LegendItemCollection legendItemCollection = new LegendItemCollection();
LegendItemCollection licollection = renderer.getLegendItems();
Iterator iter = licollection.iterator();
int item = -1;

do {
++item;

if (item == licollection.getItemCount()) {
break;
}

LegendItem li = licollection.get(item);
java.lang.Number datavalue =
dataset.getValue(dataset.getRowKey(item),
dataset.getColumnKey(0));

if (datavalue == null) {
datavalue = 0;
}

String label = li.getLabel() + "=" + datavalue;
LegendItem legendItem =
new LegendItem(label, li.getDescription(), li.getToolTipText(),
li.getURLText(), li.getShape(),
li.getFillPaint());

legendItemCollection.add(legendItem);
} while (iter.hasNext());

LegendSource legendSource = new LegendSource(legendItemCollection);
LegendTitle legendTitle = new LegendTitle(legendSource);

legendTitle.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
legendTitle.setBorder(new BlockBorder());
legendTitle.setBackgroundPaint(new Color(200, 150, 100));
legendTitle.setPosition(RectangleEdge.BOTTOM);
legendTitle.setItemFont(new Font("Centuary Gothic", Font.ITALIC, 11));
chart.addLegend(legendTitle);
chart.setBackgroundPaint(new Color(0xbb, 0x88, 0x33));
chart.getBackgroundPaint();

final BarRenderer3D ren = (BarRenderer3D)renderer;

ren.setMaximumBarWidth(0.03);

// ren.setItemMargin(-0.6);
// renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
// ren.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
// plot.setRenderer(renderer);
// ren.setDrawBarOutline(false);
// RectangleInsets ri =chart.getPadding();
// ri=new RectangleInsets(ri.getTop(),ri.getBottom(),ri.getLeft(),ri.getRight()+50.0);
// chart.setPadding(ri);
chart.getCategoryPlot().getDomainAxis().setLowerMargin(0.0);
chart.getCategoryPlot().getDomainAxis().setUpperMargin(0.1);

// StatisticalBarRenderer statisticalbarrenderer=new StatisticalBarRenderer();
// statisticalbarrenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", NumberFormat.getPercentInstance()));
// plot.setRenderer(statisticalbarrenderer);
return chart;
}

private JFreeChart createCustomPieChart(String SQLQuery, String title) {

// create a dataset...
String query = SQLQuery;
PieDataset data = (PieDataset)readCustomPieData(query);

// create the chart...
final JFreeChart chart = // chart title
// data
// include legend
ChartFactory.createPieChart3D(title, data, false, true, false);

chart.setBackgroundPaint(Color.ORANGE);

final PiePlot3D plot = (PiePlot3D)chart.getPlot();
LegendTitle legendTitle = new LegendTitle(plot);

legendTitle.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
legendTitle.setBorder(new BlockBorder());
legendTitle.setBackgroundPaint(new Color(200, 150, 100));
legendTitle.setPosition(RectangleEdge.BOTTOM);
legendTitle.setItemFont(new Font("Centuary Gothic", Font.ITALIC, 10));
chart.addLegend(legendTitle);
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}",
NumberFormat.getNumberInstance(),
NumberFormat.getPercentInstance()));

// plot.setCircular(false);
plot.setLabelFont(new Font("Centuary Gothic", Font.ITALIC, 12));

// plot.setLabelGap(0.3);
plot.setStartAngle(Math.PI / 8);
plot.setForegroundAlpha(0.9f);
plot.setInteriorGap(0.15);
plot.setBackgroundPaint(Color.WHITE);

return chart;
}

/**
* Creates a sample bar chart.
*
* @return a bar chart.
*/
private JFreeChart createLineChart(String SQLQuery) {
String query = SQLQuery;
CategoryDataset dataset = readBarData(query);
JFreeChart chart =
ChartFactory.createBarChart3D("Bar Chart", "School", "Students",
dataset, PlotOrientation.VERTICAL,
true, true, false);
final CategoryPlot plot = chart.getCategoryPlot();
final CategoryAxis axis = plot.getDomainAxis();

axis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI /
8.0));

final CategoryItemRenderer renderer = plot.getRenderer();

renderer.setItemLabelsVisible(true);

final BarRenderer3D ren = (BarRenderer3D)renderer;

ren.setMaximumBarWidth(0.05);

return chart;
}

private JFreeChart createPieChart(String SQLQuery, String title) {

// create a dataset...
String query = SQLQuery;
PieDataset data = readPieData(query);

// create the chart...
final JFreeChart chart = // chart title
// data
// include legend
ChartFactory.createPieChart3D(title, data, true, true, false);

chart.setBackgroundPaint(Color.lightGray);

final PiePlot3D plot = (PiePlot3D)chart.getPlot();

plot.setExplodePercent(1, 0.25);
plot.setForegroundAlpha(0.60f);
plot.setInteriorGap(0.15);
plot.setInteriorGap(0.15);
return chart;
}

/**
* Creates a sample bar chart.
*
* @return a Time chart.
*/
private JFreeChart createTimeSeriesChart(String SQLQuery) {

// here we just populate a series with random data...
String query = SQLQuery;

// TimeSeries series = new TimeSeries("Random Data");
XYDataset timedata = readTimeData(query);
JFreeChart chart =
ChartFactory.createTimeSeriesChart("", "YEAR", "STUDENTS",
timedata, true, true, false);

return chart;
}

public void destroy() {
try {
logincon.close();
} catch (SQLException se) {
System.out.println(se.getMessage());
}
}

public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);

OutputStream out = response.getOutputStream();
String type = null;
String title = null;
String domain = null;
String range = null;
String sqlquery = null;

if (null == (type = request.getParameter("type"))) {
throw new NullPointerException("You Must Speify the type of the Graph you would like to draw");
} else {
type = request.getParameter("type");
}

if (null == (sqlquery = request.getParameter("sqlquery"))) {
throw new NullPointerException("You Must Speify the type of the Graph you would like to draw");
} else {
sqlquery = request.getParameter("sqlquery");
}

try {
title = request.getParameter("title");
} catch (NullPointerException nfe) {
title = "";
System.out.println(nfe.getMessage());
}

try {
domain = request.getParameter("domain");
} catch (NullPointerException nfe) {
domain = "";
System.out.println(nfe.getMessage());
}

try {
range = request.getParameter("range");
} catch (NullPointerException nfe) {
range = "";
System.out.println(nfe.getMessage());
}

System.out.println(type);
System.out.println(title);
System.out.println(domain);
System.out.println(range);
System.out.println(sqlquery);

try {
if (type.equals("pie")) {
JFreeChart pieChart = null;

pieChart = createPieChart(sqlquery, title);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, pieChart, 400, 300, false,
0);

return;
}

if (type.equals("custompie")) {
JFreeChart pieChart = null;

pieChart = createCustomPieChart(sqlquery, title);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, pieChart, 400, 300, false,
0);

return;
}

if (type.equals("bar")) {
JFreeChart barChart = null;

barChart = createBarChart(sqlquery, title, domain, range);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, barChart, 400, 300, false,
0);

return;
}

if (type.equals("custombar")) {
JFreeChart barChart = null;

barChart =
createCustomBarChart(sqlquery, title, domain, range);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, barChart, 400, 330, false,
0);

return;
}

if (type.equals("custombar2")) {
JFreeChart barChart = null;

barChart =
createCustomBar2Chart(sqlquery, title, domain, range);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, barChart, 400, 330, false,
0);

return;
}

if (type.equals("custombar3")) {
JFreeChart barChart = null;

barChart =
createCustomBar3Chart(sqlquery, title, domain, range);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, barChart, 400, 330, false,
0);

return;
}

if (type.equals("custombar5")) {
JFreeChart barChart = null;

barChart =
createCustomBar5Chart(sqlquery, title, domain, range);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, barChart, 400, 330, false,
0);

return;
}

if (type.equals("time")) {
JFreeChart timeChart = null;

timeChart = createTimeSeriesChart(sqlquery);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, timeChart, 400, 300);

return;
}

if (type.equals("line")) {
JFreeChart lineChart = null;

lineChart = createTimeSeriesChart(sqlquery);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, lineChart, 400, 300);

return;
}
} catch (Exception e) {
System.err.println(e.toString());
} finally {
if (out != null) {
out.close();
}
}
}

public void init(ServletConfig config) throws ServletException {
super.init(config);
logincon = getConnection();
}

private CategoryDataset readBarData(String SQLQuery) {
JDBCCategoryDataset dataset = null;

try {
dataset = new JDBCCategoryDataset(logincon);

String query = SQLQuery;

dataset.executeQuery(query);
} catch (SQLException e) {
System.err.print("SQLException: ");
System.err.println(e.getMessage());
}

return dataset;
}

private CategoryDataset readCustomBarData(String SQLQuery) {
JDBCCustomCategoryDataset dataset = null;

try {
dataset = new JDBCCustomCategoryDataset(logincon);

String query = SQLQuery;

dataset.executeQuery(query);
} catch (SQLException e) {
System.err.print("SQLException: ");
System.err.println(e.getMessage());
}

return dataset;
}

private PieDataset readCustomPieData(String SQLQuery) {
JDBCCustomPieDataSet data = null;

try {
data = new JDBCCustomPieDataSet(logincon);

String query = SQLQuery;

data.executeQuery(query);
} catch (SQLException e) {
System.err.print("SQLException: ");
System.err.println(e.getMessage());
}

return (PieDataset)data;
}

private PieDataset readPieData(String SQLQuery) {
JDBCPieDataset data = null;

try {
data = new JDBCPieDataset(logincon);

String query = SQLQuery;

data.executeQuery(query);
} catch (SQLException e) {
System.err.print("SQLException: ");
System.err.println(e.getMessage());
}

return data;
}

private XYDataset readTimeData(String SQLQuery) {
JDBCXYDataset dataset = null;

try {
dataset = new JDBCXYDataset(logincon);

String query = SQLQuery;

dataset.executeQuery(query);
} catch (SQLException e) {
System.err.print("SQLException: ");
System.err.println(e.getMessage());
}

return dataset;
}

//~--- get methods --------------------------------------------------------

private Connection getConnection() {
try {
Class.forName(driver);
} catch (ClassNotFoundException cnfe) {
System.out.println(cnfe.getMessage());
}

try {
logincon = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
System.err.print("SQLException: ");
System.err.println(e.getMessage());
}

return logincon;
}
}


class LegendSource implements LegendItemSource {
LegendItemCollection lic;

//~--- constructors -------------------------------------------------------

public LegendSource(LegendItemCollection lic) {
this.lic = lic;
}

//~--- get methods --------------------------------------------------------

public LegendItemCollection getLegendItems() {
return this.lic;
}
}
Master Java

Locked