Chart| Statistics | Column chart
We introduce using Jfreechart to generate a histogram, starting with one of the simplest examples.
One of the simplest examples
In order to reduce the threshold, so that people have a psychological bottom, first introduced a simple can not be a simple example, all kinds of attributes in the picture are the default values.
<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%@ page import= "Org.jfree.chart.ChartFactory,
Org.jfree.chart.JFreeChart,
Org.jfree.chart.plot.PlotOrientation,
Org.jfree.chart.servlet.ServletUtilities,
Org.jfree.data.DefaultCategoryDataset "%>
<%
Defaultcategorydataset DataSet = new Defaultcategorydataset ();
Dataset.addvalue (300, "Guangzhou", "Apple");
Dataset.addvalue (200, "Guangzhou", "pear");
Dataset.addvalue (500, "Guangzhou", "Grape");
Dataset.addvalue (340, "Guangzhou", "Mango");
Dataset.addvalue (280, "Guangzhou", "Litchi");
Jfreechart chart = Chartfactory.createbarchart3d ("Fruit Sales statistics Map",
"Fruit",
"Sales",
DataSet
Plotorientation.vertical,
False
False
FALSE);
String filename = servletutilities.savechartaspng (chart, MB, null, session);
String Graphurl = Request.getcontextpath () + "/servlet/displaychart?filename=" + filename;
%>
The results of this JSP program are shown below
Figure I
Advanced properties of two-column graphs
The above procedure is simple, but the resulting histogram is also very simple. More often, we may need different effects. Org.jfree.chart.ChartFactory this factory class has Createbarchart, Createstackedbarchart, Createbarchart3d, Createstackedbarchart3d these several factory methods to create different types of columnar graphs. The Jfreechart Java Doc API documentation for these four methods is described in detail, and it is important to plotorientation.vertical that parallel columns are displayed vertically, while Plotorientation.horizontal Let the parallel column be displayed horizontally.
Several classes that have a larger impact on the histogram, respectively, are:
Org.jfree.chart.axis.CategoryAxis
Org.jfree.chart.axis.ValueAxis
Org.jfree.chart.renderer.BarRenderer
Org.jfree.chart.renderer.BarRenderer3D
Let's use examples to illustrate these classes, first to assume a data table that needs to be counted:
|
Beijing |
Shanghai |
Guangzhou |
Chengdu |
Shenzhen |
Apple |
672 |
766 |
223 |
540 |
126 |
Pears |
325 |
521 |
210 |
340 |
106 |
Grape |
332 |
256 |
523 |
240 |
526 |
Based on the above table data, first constructs the Categorydataset, here no longer uses the Defaultcategorydataset class in the above simple example, but datasetutilities more effective construction categorydataset, such as the following code:
double[][] data = new double[][] {{672, 766, 223, 540, 126}, {325, 521, 210, 340, 106}, {332, 256, 523, 240, 526}};
String[] Rowkeys = {"Apple", "pear", "grape"};
String[] Columnkeys = {"Beijing", "Shanghai", "Guangzhou", "Chengdu", "Shenzhen"};
Categorydataset DataSet = Datasetutilities.createcategorydataset (Rowkeys, Columnkeys, data);
3D histogram generated with the dataset above
Figure II
Org.jfree.chart.axis.CategoryAxis
Categoryaxis Domainaxis = Plot.getdomainaxis ();
Set whether Columnkey is displayed vertically
Domainaxis.setverticalcategorylabels (TRUE);
Set the distance from the left end of the picture
Domainaxis.setlowermargin (0.1);
Set the distance from the right end of the picture
Domainaxis.setuppermargin (0.1);
Set whether the Columnkey is displayed at intervals
Domainaxis.setskipcategorylabelstofit (TRUE);
Plot.setdomainaxis (Domainaxis);
The above code produces the effect shown below, notice the difference with figure two.
Figure Three
Org.jfree.chart.axis.ValueAxis
Valueaxis Rangeaxis = Plot.getrangeaxis ();
Sets the maximum distance between one column and the top of the picture
Rangeaxis.setuppermargin (0.15);
Sets the minimum distance between one column and the bottom of the picture
Rangeaxis.setlowermargin (0.15);
Plot.setrangeaxis (Rangeaxis);
The above code produces the effect shown below, notice the difference with figure two.
Figure Four
Org.jfree.chart.renderer.BarRenderer3D
Barrenderer3d renderer = new Barrenderer3d ();
Renderer.setbaseoutlinepaint (Color.Black);
Set the color of the Wall
Renderer.setwallpaint (Color.gray);
Set the color of the column to be represented by each type of fruit
Renderer.setseriespaint (0, new Color (0, 0, 255));
Renderer.setseriespaint (1, New Color (0, 100, 255));
Renderer.setseriespaint (2, Color.green);
Set the Outline color of the column to be represented by each type of fruit
Renderer.setseriesoutlinepaint (0, Color.Black);
Renderer.setseriesoutlinepaint (1, Color.Black);
Renderer.setseriesoutlinepaint (2, Color.Black);
Sets the distance between the parallel columns contained in each region
Renderer.setitemmargin (0.1);
Displays the value of each column and modifies the font properties of the value
Renderer.setitemlabelgenerator (New Standardcategoryitemlabelgenerator ());
Renderer.setitemlabelfont (New Font ("bold", font.plain,12));
Renderer.setitemlabelsvisible (TRUE);
The above code produces the effect shown below, notice the difference with figure two.
Figure Five
Add two useful methods
Two ways to supplement Org.jfree.chart.plot.CategoryPlot, both of which are useful for all types of charts, because they are not described in the previous article, which is added here.
Set the location where sales are displayed
Plot.setdomainaxislocation (Axislocation.top_or_right);
Plot.setrangeaxislocation (Axislocation.bottom_or_right);
The above code produces the effect shown below, notice the difference with figure two.
Figure Six
three complete examples
There are a few snippets of code in front of them, now combining these fragments into a complete example.
<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%@ page import= "Java.awt.Color,
Java.awt.Font,
Org.jfree.chart.ChartFactory,
Org.jfree.chart.JFreeChart,
Org.jfree.chart.plot.PlotOrientation,
Org.jfree.chart.servlet.ServletUtilities,
Org.jfree.data.CategoryDataset,
Org.jfree.data.DatasetUtilities,
Org.jfree.chart.plot.CategoryPlot,
Org.jfree.chart.axis.CategoryAxis,
Org.jfree.chart.axis.ValueAxis,
Org.jfree.chart.renderer.BarRenderer3D,
Org.jfree.chart.labels.StandardCategoryItemLabelGenerator,
Org.jfree.chart.axis.AxisLocation "%>
<%
double[][] data = new double[][] {{672, 766, 223, 540, 126},{325, 521, 210, 340, 106},{332, 256, 523, 240, 526}};
String[] Rowkeys = {"Apple", "pear", "grape"};
String[] Columnkeys = {"Beijing", "Shanghai", "Guangzhou", "Chengdu", "Shenzhen"};
Categorydataset DataSet = Datasetutilities.createcategorydataset (Rowkeys, Columnkeys, data);
Jfreechart chart = Chartfactory.createbarchart3d ("Statistics of fruit sales figures",
Null
Null
DataSet
Plotorientation.vertical,
True,false,false);
Chart.setbackgroundpaint (Color.White);
Categoryplot plot = Chart.getcategoryplot ();
Categoryaxis Domainaxis = Plot.getdomainaxis ();
Domainaxis.setverticalcategorylabels (FALSE);
Plot.setdomainaxis (Domainaxis);
Valueaxis Rangeaxis = Plot.getrangeaxis ();
Sets the maximum distance of one Item to the top of the picture
Rangeaxis.setuppermargin (0.15);
Set the minimum one Item to the bottom of the picture
Rangeaxis.setlowermargin (0.15);
Plot.setrangeaxis (Rangeaxis);
Barrenderer3d renderer = new Barrenderer3d ();
Renderer.setbaseoutlinepaint (Color.Black);
Set the color of the Wall
Renderer.setwallpaint (Color.gray);
Set the color of the column to be represented by each type of fruit
Renderer.setseriespaint (0, new Color (0, 0, 255));
Renderer.setseriespaint (1, New Color (0, 100, 255));
Renderer.setseriespaint (2, Color.green);
Sets the distance between the parallel columns contained in each region
Renderer.setitemmargin (0.1);
Displays the value of each column and modifies the font properties of the value
Renderer.setitemlabelgenerator (New Standardcategoryitemlabelgenerator ());
Renderer.setitemlabelsvisible (TRUE);
Plot.setrenderer (renderer);
Set the transparency of a column
Plot.setforegroundalpha (0.6f);
Set the location where sales are displayed
Plot.setdomainaxislocation (Axislocation.top_or_right);
Plot.setrangeaxislocation (Axislocation.bottom_or_right);
String filename = servletutilities.savechartaspng (chart, MB, null, session);
String Graphurl = Request.getcontextpath () + "/servlet/displaychart?filename=" + filename;
%s>
Look at the results of the program running:
Figure Seven
Three summary
I have only introduced a small number of methods, please refer to Jfreechart's Java Doc API documentation and Sample Code.