JFreechart - histogram

xiaoxiao2021-03-06  43

I don't know what purpose for JFreeChart projects. The examples of the Sample Code they provide are fascinating, the same result can be used in different ways, the use is not used. Plus the old use of JFreechart, so that friends who have just contacted JFreechart into JFreechart don't spend some time.

The JFreeChart generated pie chart has been introduced in front, which may be introduced to how JFreeChart generates how to generate a histogram. Copying others: Help Others as well as to help myself. This time we introduce to generate a histogram using JFreeChart, starting with a simplest example.

One simplest example

In order to reduce the threshold, let everyone have a bottom, first introduce a simple example of a simple example, and the various properties in the picture use the default value.

<% @ 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", "pears"); Dataset.addvalue (500, "Guangzhou", "grape"); Dataset.addValue (340, "Guangzhou", "Mango"); Dataset.AddValue (280, "Guangzhou", "lychee"); JFreeChart chart = ChartFactory.createBarChart3D ( "fruit sales charts," "fruit", "sales", dataset, PlotOrientation.VERTICAL, false, false, false); String filename = ServletUtilities.saveChartAsPNG (chart, 500 , 300, null, session; string graphURL = Request.getContextPath () "/ servlet / displaychart? Filename =" filename;%> >

The result of this JSP program is as follows

Advanced characteristics of two pillar maps

The above procedure is simple, but the generated column graph is also very simple. More time, we may need a different effect. Org.jfree.Chart.ChartFactory This factory has CreateBarchart, CreateStackedBarchart, CreateBarchart3D, CreateStackedBarchart3d. Several factory methods create different types of histograms. The Java Doc API document for JFreeChart on these four methods has a detailed description, and more important is that Plotorientation.Vertical makes the parallel column vertically, while Plotorientation.horizontal makes the parallel column horizontally.

Several of several classes have a larger class, they are: org.jfree.chart.axis.categoryaxisorg.jfree.chart.axis.Valueaxisorg.jfree.Chart.Ree.Chart.Renderer.Barree.Chart.Renderer.barrenderer3D

We still explain these classes with examples, first to assume a data sheet that requires statistics:

Beijing, Guangzhou, Chengdu, Shenzhen Apple 672766223540126 Pear 325521210340106 Grape 332256523240526

According to the above table data, first construct CategoryDataSet, here no longer use the defaultcategoryDataSet class in the above simple example, but DataSetUtilities more efficiently constructed 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 DataSet above

Figure II

Org.jfree.Chart.axis.categoryaxis

CategoryAxis domainAxis = plot.getDomainAxis (); // set whether columnKey vertical display domainAxis.setVerticalCategoryLabels (true); // Sets the distance from the left end of the image domainAxis.setLowerMargin (0.1); // set the distance from the right end of the image domainAxis.setUpperMargin (0.1) ; // Set whether columnkey is interval to display domainaxis.setskipcategorylabelstofit (TRUE); Plot.SetDomainAxis (Domainax);

The results produced by the above code are as follows, pay attention to the difference from Figure II.

Figure three

Org.jfree.Chart.axis.valueaxis

Valueaxis Rangeaxis = Plot.getRangeaxis (); // Set the highest number of columns and pictures Top Rangeaxis.SetupPermargin (0.15); // Set the lowest a column and the bottom of the picture //RANGEAXIS.SETLOWERMARGIN (0.15 );plot .SetRangeaxis (Rangeaxis); The effect of the above code is as follows, pay attention to the difference from Figure II.

Figure four

Org.jfree.Chart.renderer.barrenderer3D

Barrenderer3D renderer = new barrendrer3d (); renderer.setBaseOutlinePaint (color.black); // Set the color renderer.setwallpaint (color.gray); // Set the color renderer.setSeriespaint of each fruit representative of the column (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 representative of each fruit RENDERER.SETSERIESOUTLINEPAINT (0, color.black); renderer.setSeriesoutLinePaint (1, color.black); renderer.setSeriesoutLinePaint (2, color.black); // Setting between the parallel columns included in each region. SetItemMargin (0.1); // Displays the value of each column and modifies the font attribute of the value renderer.setitemlabelgenerator (New StandardcategoryItemLabelgenerator ()); renderer.SetItemlabelfont (New Font ("Black Body, Font.Plain, 12)); Renderer.SetItemlabelsvisible (TRUE);

The results produced by the above code are as follows, pay attention to the difference from Figure II.

Figure 5

Supplement two useful methods

Supplementary org.jfree.chart.plot.categoryPlot's two methods, these two methods have a role in all types of charts, because there is no introduction in front, here added.

// Set the area, sales display position plot.setdomainaxislocation (axislocation.top_or_right); plot.setRangeaxisLocation (AxisLocation.bottom_or_right);

The results produced by the above code are as follows, pay attention to the difference from Figure II.

Figure 6

Three complete examples

The front is some code snippet, and now combines these pieces 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 (Rowkey s, columnKeys, data); JFreeChart chart = ChartFactory.createBarChart3D ( "FIG fruit sales statistics", null, null, dataset, PlotOrientation.VERTICAL, true, false, false); chart.setBackgroundPaint (Color.WHITE); CategoryPlot plot = chart.getCategoryPlot (); CategoryAxis domainAxis = plot.getDomainAxis (); // set the maximum distance of the image at the top of an Item; domainAxis.setVerticalCategoryLabels (false); plot.setDomainAxis (domainAxis); ValueAxis rangeAxis = plot.getRangeAxis () Rangeaxis.SetupPermargin (0.15); // Set the lowest Item and the bottom distance of the picture Rangeaxis.SetLowerMargin (0.15);

Plot.setRangeaxis; barrendrer3d renderer = new barrendrer3d (); renderer.setBaseOutlinePaint (Color.Black); // Set the color renderer.SetwallPaint (color.gray); // Set the column of each fruit representative RENDERER.SETSERIESPAINT (0, New Color (0, 0, 255)); renderer.setSeriesPaint (1, New Color (0, 100, 255)); renderer.setSeriesPaint (2, color.green); // Setting each The distance between the parallel columns included in the region retrerer.setitemmargin (0.1); // displays the value of each column, and modifies the font attribute of the value renderer.SetItemlabelgenerator (); renderer.setitemlabelsvisible (TRUE); plot.setRenderer (renderer); // set the transparency plot.setForegroundAlpha column (0.6f); // set area, sales of display position plot.setDomainAxisLocation (AxisLocation.TOP_OR_RIGHT); plot.setRangeAxisLocation (AxisLocation.BOTTOM_OR_RIGHT); String filename = ServletUtilities.saveChartAsPNG (chart, 500, 300, null, session); String graphURL = request.getContextPath () "? / servlet / DisplayChart filename =" filename;% s> Look at the result of the program run:

Figure 7

Summary

I only introduce a small number of methods, more, please refer to JFreechart's Java Doc API documentation and Sample Code. Some of the articles about the chart don't know how to express it correctly. If you have any ideas about this article, you can contact me Wayne@sentom.net.

转载请注明原文地址:https://www.9cbs.com/read-58873.html

New Post(0)