I. JFreeChart Project Introduction JFreeChart is a Java project on SourceForge.NET, which is open source site, which is mainly used to include: pie chart, histogram (ordinary column chart, and stack post chart), Line diagram, regional diagram, distribution map, mixed chart, Gantt chart, and some instrument panels, etc. These different graphs can basically meet the current requirements. In order to reduce the context, this paper mainly introduces the previous three types of charts, the reader can touch the graph to develop other styles. The following is the result of these three types of charts generated by JFreeChart:
figure 1
figure 2
image 3
The above three figures are the sales information of a certain product in four quarters. You must first prepare the development environment before proceeding with the following section, because it is a graphic display based on a web browser, so you need a servlet engine or a J2EE application server (such as WebSphere, Tomcat, etc.). The construction of the web environment is not cumbersome, and the reader is installed according to the preferences. The JFreeChart engine itself needs to be downloaded on the sourceforge.net, the address is as follows:
JFreechart Home: http://www.jfree.org/jfreechart/index.html jfreechart Download Page: http://sourceforge.net/projects/jfreechart/
When downloading, you need to pay attention to you must download two files: JFreeChart and JCommon. At present, the latest version is: jfreechart 0.9.11 JCommon 0.8.6
It is aware of the problem that I meet in the development. It should be noted that when using Eclipse development, it will report an inexplicable error, and the error may point to the first line of a class file. This problem is generally because there is no reason for the JCMMON's JAR package to the project's class path. Specific reasons are immune.
Second, interpretation of JFreeChart Structure Before starting using JFreeChart, we must first understand the structure of JFreeChart itself and some examples thereof, which helps us to develop next yourself. Downloading the JFreeChart package has already had a very rich example, because JFreeChart this project itself is very small, so learning it is the best way to learn the example source code it belongs. There are dozens of files in the package org.jfree.chart.demo to show all the results of all charts that JFreeChart can support. If your JDK is more new, there will be problems when running these examples, and the phenomenon is as follows:
java.lang.UnsatisfiedLinkError:. initDDraw at sun.awt.windows.Win32OffScreenSurfaceData.initDDraw (Native Method) at sun.awt.windows.Win32OffScreenSurfaceData
At this time, you may be wondering again, don't say it is a web-based chart, how can I pull Swing? This is because the developers are easy to get started without configuring any operating environment, so these examples are based on the GUI mode for display to the developer if they have to generate a chart, how do we learn how to use this engine to generate a chart instead of How to display a chart. When we use the generated chart object export to an image file to be released on the web.
Let's introduce several core objects in JFreechart:
The role of the class name class and the simple description of the JFreeChart chart object, and the final expression of any type of chart is customized in this object. The JFreeChart engine itself provides a factory class for creating different types of chart object XXXXDataSet dataset objects for providing data used in the chart. According to different types of charts, there should be many types of dataset object class XXXXXPLOT chart area objects. Basically this object determines what styles of charts. When you create this object, you need AXIS, RENDERER, and data set objects to support XXXXXAXIS for processing charts. Two axes: longitudinal axis and horizontal axis xxxxrenderer How to display a chart object XXXXXURLGENERATOR to generate a mouse for each project in a web chart Click the link XXXXTooltipGenerator to generate a help prompt for image, different types of charts correspond to different types of tool tips. Basically I think the design of the class structure of the JFreeChart project itself is not very good. First, use a lot of factory methods when you create a chart, so although you can simplify the code of the chart object, but the project itself or developers come It is still a very troublesome thing that it is still a very troublesome. Secondly, in addition to the chart object itself, the user must understand which Axis, Plot, Renderer class should know each type of chart object. And must be very familiar with the specific meaning of each parameter in these classes. These issues have greatly plagued many beginners. However, although there are many problems, JFreeChart itself is still a very good chart engine, and the project itself is gradually developing.
After very briefly introduced the code structure of JFreeChart itself, let's start trial a few common charts and put them on the web.
Third, using JFreeChart to generate various styles of charts to the problem of space, we only achieve two common charts here, other types of chart readers can touch bypass. We first give the cosmication map, and the achievement of the pie chart is again compared.
1 column map
Package Lius.Chart.Demo; import java.io. *; import org.jfree.data. *; import org.jfree.chart. *; import org.jfree.chart.plot. *; / ** * This class the simplest presentation to the histogram generating * @author Winter Lau * / public class BarChartDemo {public static void main (String [] args) throws IOException {CategoryDataset dataset = getDataSet2 (); JFreeChart chart = ChartFactory.createBarChart3D ( "FIG fruit production " Whether to display the legend (for simple column chart must be false) false, / / whether tool false // is generated to generate a URL link); fileOutputStream fos_jpg = null; try {fos_jpg = new fileoutputstream ("d: //fruit.jpg" CHARTUTILITIES.WRITECHARTASJPEG (FOS_JPG, 100, CHART, 400, 300, NULL);} Finally {Try {fos_jpg.close ();} catch (exception e) {}}} / ** * Get a demonstration simple data set Object * @return * / private static CategoryDataset getDataSet () {DefaultCategoryDataset dataset = new DefaultCategoryDataset (); dataset.addValue (100, null, "apple"); dataset.addValue (200, null, "pear"); dataset.addValue (300, NULL, "Grape"); DAT ASET.ADDVALUE (400, NULL, "Banana"); Dataset.addValue (500, NULL, "Litchi"); Return DataSet;} / ** * Get a combination of delivering data set object * @Return * / private static CategoryDataSet GetDataSet2 () {defaultcategoryDataSet Dataset = New DefaultcategoryDataSet (); Dataset.addValue (100, "Beijing", "Apple"); Dataset.addValue (100, "Shanghai", "Apple"); Dataset.AddValue (100, " Guangzhou, "Apple"); Dataset.addvalue (200, "Beijing", "Peer"); Dataset.addValue (200, "Shanghai", "Peer"); Dataset.AddValue (200, "Guangzhou", "Pear "); Dataset.addValue (300," Beijing "," Grape "); Dataset.AddValue (300," Shanghai "," Grape "
DataSet.addValue (300, "Guangzhou", "Grape"); Dataset.addValue (400, "Beijing", "Banana"); Dataset.addValue (400, "Shanghai", "Banana"); Dataset.AddValue (400, "Guangzhou", "Banana"); Dataset.Addvalue (500, "Beijing", "Litchi"); Dataset.addValue (500, "Shanghai", "Litchi"); Dataset.AddValue (500, "Guangzhou "," Lychee "); return dataset;} The image file effect generated after the program is running, as shown below:
Figure 4 If you are using simple data, you can use the getDataSet method to get the image file generated when you get the data set as follows:
Figure 5
2 pie chart
For pie charts, the acquisition of the data set is not the same data set, and the other pie chart does not support data such as subjects in the same category. We only give the code that created the pie chart, as for the graph to a file, consistent with the histogram without repeating.
Package Lius.Chart.Demo; import java.io. *; import org.jfree.data. *; import org.jfree.chart. *; / ** * The generation of the presentation pie chart * @Author Winter Lau * / public class PieChartDemo {public static void main (String [] args) throws IOException {DefaultPieDataset data = getDataSet (); JFreeChart chart = ChartFactory.createPie3DChart ( "fruit yield map", // chart title data, true, // whether the legend False, false); // Write chart object to file, refer to the column graph generation source code} / ** * Get a deliberate dataset object * @return * / private static defaultpiedataset getDataSet () {defaultpiedataset dataset = new defaultpiedataset ( DataSet.SetValue ("Apple", 100); Dataset.SetValue ("Pears", 200); Dataset.SetValue ("Grape", 300); Dataset.SetValue ("Banana", 400); Dataset.SetValue "Lychee", 500); return dataset;}}
The generated pie chart file is as follows:
Figure 6
Fourth, move the generated chart to the browser to pass the generated chart to the client browser, just need to stream the file in the previous two examples into the output stream acquired by the HTTPSERVLETRESPONSE object, detailed code list as follows:
Package lius.chart.de; import java.ioException; import javax.servlet. *; import javax.servlet.http.httpservlet; import org.jfree.data. *; import org.jfree.chart. *; / * * * servlet graph demonstrates direct outputs * @author Winter Lau * / public class ChartDemoServlet extends HttpServlet {public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException {res.setContentType ( "image / jpeg"); DefaultPieDataset data = GetDataSet (); jfreechart chart = chartfactory.createpie3dchart ("Fruit Yield Diagram", DATA, TRUE, FALSE, FALSE); Chartutilities.writeChartasjpeg (Res. GetputStream (), 100, Chart, 400, 300, Null);} / ** * Get a demonstration of simple data set object * @Return * / private static defaultpiedataset getDataSet () {defaultpiedataset dataset = new defaultpiedataset (); Dataset.SetValue ("Apple", 100); Dataset.SetValue ("Pear", 200) DataSet.SetValue ("Grape", 300); Dataset.SetValue ("Banana", 400); Dataset.SetValue ("Litchi", 500); Return Dataset;} A chart is displayed on the server, and we need to do more on the chart, such as obtaining information prompts, and click on a portion of the chart to make more details. For example, the simple column chart of the previously generated, the user needs to see some fruits after seeing the histogram, for example, Apple can see the apple production in each region. This graphic is required to have an interactive function for this purpose. In HTML, in order to make an image have a function of interactive, you must define a MAP object. The following table excerpts an HTML code with this feature