The codepackage chart;
import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.axis.SubCategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.GroupedStackedBarRenderer;
import org.jfree.data.KeyToGroupMap;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
public class StackedBarChartTest {
private static CategoryDataset getData() {
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
dataSet.addValue(100, "Series1 G1", "Jan 04");
dataSet.addValue(150, "Series1 G2", "Jan 04");
dataSet.addValue(60, "Series2 G1", "Feb 04");
dataSet.addValue(90, "Series2 G2", "Feb 04");
dataSet.addValue(70, "Series3 G2", "Feb 04");
dataSet.addValue(99, "Series3 G1", "Feb 04");
dataSet.addValue(100, "Series4 G1", "Feb 04");
dataSet.addValue(80, "Series4 G2", "Feb 04");
dataSet.addValue(20, "Series1 G1", "Feb 04");
dataSet.addValue(50, "Series1 G2", "Feb 04");
dataSet.addValue(10, "Series2 G1", "Jan 04");
dataSet.addValue(40, "Series2 G2", "Jan 04");
dataSet.addValue(10, "Series3 G2", "Jan 04");
dataSet.addValue(50, "Series3 G1", "Jan 04");
dataSet.addValue(10, "Series4 G1", "Jan 04");
dataSet.addValue(60, "Series4 G2", "Jan 04");
// dataSet.addValue(100, "Series1 G1", "Jan 04");
// dataSet.addValue(20, "Series1 G1", "Feb 04");
// dataSet.addValue(10, "Series2 G1", "Jan 04");
// dataSet.addValue(60, "Series2 G1", "Feb 04");
// dataSet.addValue(99, "Series3 G1", "Feb 04");
// dataSet.addValue(50, "Series3 G1", "Jan 04");
// dataSet.addValue(100, "Series4 G1", "Feb 04");
// dataSet.addValue(10, "Series4 G1", "Jan 04");
//
//
// dataSet.addValue(150, "Series1 G2", "Jan 04");
// dataSet.addValue(50, "Series1 G2", "Feb 04");
// dataSet.addValue(40, "Series2 G2", "Jan 04");
// dataSet.addValue(90, "Series2 G2", "Feb 04");
// dataSet.addValue(10, "Series3 G2", "Jan 04");
// dataSet.addValue(70, "Series3 G2", "Feb 04");
// dataSet.addValue(60, "Series4 G2", "Jan 04");
// dataSet.addValue(80, "Series4 G2", "Feb 04");
return dataSet;
}
private static JFreeChart createChart(CategoryDataset categorydataset) {
JFreeChart jfreechart = ChartFactory.createStackedBarChart("Sample", null, "value", categorydataset, PlotOrientation.VERTICAL, true, true, false);
GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer();
renderer.setItemMargin(0.01);
renderer.setDrawBarOutline(false);
renderer.setSeriesPaint(0, Color.blue);
renderer.setSeriesPaint(1, Color.blue);
renderer.setSeriesPaint(2, Color.cyan);
renderer.setSeriesPaint(3, Color.cyan);
renderer.setSeriesPaint(4, Color.green);
renderer.setSeriesPaint(5, Color.green);
renderer.setSeriesPaint(6, Color.yellow);
renderer.setSeriesPaint(7, Color.yellow);
// renderer.setSeriesPaint(0, Color.blue);
// renderer.setSeriesPaint(4, Color.blue);
//
// renderer.setSeriesPaint(1, Color.cyan);
// renderer.setSeriesPaint(5, Color.cyan);
//
// renderer.setSeriesPaint(2, Color.green);
// renderer.setSeriesPaint(6, Color.green);
//
// renderer.setSeriesPaint(3, Color.yellow);
// renderer.setSeriesPaint(7, Color.yellow);
KeyToGroupMap keytogroupmap = new KeyToGroupMap("G1");
keytogroupmap.mapKeyToGroup("Series1 G1", "G1");
keytogroupmap.mapKeyToGroup("Series2 G1", "G1");
keytogroupmap.mapKeyToGroup("Series3 G1", "G1");
keytogroupmap.mapKeyToGroup("Series4 G1", "G1");
keytogroupmap.mapKeyToGroup("Series1 G2", "G2");
keytogroupmap.mapKeyToGroup("Series2 G2", "G2");
keytogroupmap.mapKeyToGroup("Series3 G2", "G2");
keytogroupmap.mapKeyToGroup("Series4 G2", "G2");
renderer.setSeriesToGroupMap(keytogroupmap);
SubCategoryAxis subcategoryaxis = new SubCategoryAxis("Series/Month");
subcategoryaxis.setCategoryMargin(0.03);
subcategoryaxis.addSubCategory("G1");
subcategoryaxis.addSubCategory("G2");
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
categoryplot.setDomainAxis(subcategoryaxis);
categoryplot.setRenderer(renderer);
categoryplot.setFixedLegendItems(createLegendItems());
return jfreechart;
}
private static LegendItemCollection createLegendItems() {
LegendItemCollection legenditemcollection = new LegendItemCollection();
LegendItem legenditem = new LegendItem("Series1", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.blue);
LegendItem legenditem1 = new LegendItem("Series2", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.cyan);
LegendItem legenditem2 = new LegendItem("Series3", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.green);
LegendItem legenditem3 = new LegendItem("Series4", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.yellow);
legenditemcollection.add(legenditem);
legenditemcollection.add(legenditem1);
legenditemcollection.add(legenditem2);
legenditemcollection.add(legenditem3);
return legenditemcollection;
}
public static void main(String args[]) {
StackedBarChartTest demo = new StackedBarChartTest();
ChartFrame frame = new ChartFrame("First", demo.createChart(demo.getData()));
frame.pack();
frame.setVisible(true);
}
}
Note:- The SubCategoryAxis is used to assign labels to Category Axis' subcategories (i.e. groups).
- The ChartFrame is used to display the results. The results could also be written to an output stream using the ChartUtilities.writeChartAsJPEG or ChartUtilities.writeChartAsPNG methods.
- The commented lines show how the series color is affected by the order in which the data is entered into the Dataset.
Hi.. Abhi...
ReplyDeleteCan u help me regarding the way of running the Jfreechart code??? It will be very much helpful for me if u can help me out.. I am stuck with it for many days.. My email id is : abh.kol@gmail.com. . .
With regards,
Abhisek
hi Abhi
ReplyDeletei am a new user of JFreechart and i wud like to create an Image Map for the Horizontal Stacked Bar chart.
i did that too. but mapping was a problem. how to set the tooltip, etc...
bablu
ReplyDeletehi abhi
can u tell me how to create the
legends in square/circle shape in pie
and bar charts. Its very urgent.
Bye
I want my bar to be clickable like each different bar can point to some different url. Would you plz tell me how to do that?Any help/hint or resource reference from you would be appreciated.
ReplyDeleteThanks,
Suvoraj
hi Abhi
ReplyDeleteCan you please send me the jar that have org.jfree.chart.renderer.category
Thanks
With Regards
Pankaj Bansal
pankaj.bansal@sdgc.com
hi abhi ,
ReplyDeletei am not able to run the javachart it is giving me the error package org.jfree.util does not exit.
Hi.. Abhi...
ReplyDeleteCan u help me regarding the way of running the Jfreechart code??? It will be very much helpful for me if u can help me out.. I am stuck with it for many days.. My email id is ravi.k.choudhari@gmail.com
thnks.
Hi Abhi
ReplyDeleteIt would be of great help if you could pls tell me how to assign the series color based on some field value which is being queried from database. I am using JasperReports for the same.
Hi Abhi,
ReplyDeleteIs there a way to change the orientation of the SubCategroyAxis, for rg: In your case change the orientation of G1 , G2 label by 90 degrees.
Regards
Naveen
Hi Abhi....
ReplyDeleteI need some hlep regarding charts, please help me i need financial charts like line charts,candlestick charts and like that
With Regards,
Dhanavardhan
Hi Abhi,
ReplyDeletei'm working on bar chart using JFREEChart.
i'm able to get dynamic data but i'm unable to run on browser if i'm runing on browser its giving default image.Help me out
my mail id is rajnesh.thakore@gmail.com
Hi Abhi
ReplyDeletei am a new user of JFreechart and i wud like to create a pie chart in IBM WebSphere Studio Application Developer 5.1.1
Any help/hint or resource reference from you would be appreciated.
Thanks,
Anamika
HI Abhi...
ReplyDeleteI have an urgent requirement to plott bar chart graphs.
I'm new to plotting graphs can u please explain me how to plott graphs using java...
thanks inadvance..
Hi Abhi,
ReplyDeleteWhich version of JFreeChart do you have? I have v1.0.5 and your code does not work on it.
hi abhi
ReplyDeletehow did u create a categories archive in ur blog at the left side, i want to do the same for my blog but don't know how to do this.
thanks
Hi Abhi,
ReplyDeleteThank you so much for the Legend items code.It has helped me a lot and saved a lot of my API research time. I need one more help, can you tell me how can i put a image title to a bar chart instead of default text title.
Thank you sooo much,
Prashant
The explanation is correct but the code is not working,in Jfree Chart
ReplyDeleteThere are some limitations with respect to the item labels in the current release of JFreeChart:
• some renderers do not support item labels;
• axis ranges are not automatically adjusted to take into account the item labels—some labels may disappear off the chart if sufficient margins are not set (use the setUpperMargin() and/or setLowerMargin() methods in the relevant axis to adjust this).