Jfreechart + SQL implementation time Graph

Source: Internet
Author: User

Not long ago, I wrote a program for the company's customers.

I encountered a lot of problems when using JSP for implementation. Later, I finally completed the program with some friends of itpub and their own hard work.

I learned to read the API to write programs. As an old itpub veteran said, if I didn't read the API, I still gave up programming. After this event, I did find that, API is an indispensable "tool" for programming, hoho

Here, I will write out the examples I used. The parameters here are for reference only.

<Reprinted please indicate the source-Author: Xin Shui fable Email: fableking17@163.com>

1. Obtain jfreechart.
Jfreechart is a project of jfreechart on the open source website sourceforge.net. Its main products include:
1. jfreereport: Report solution Tool
2. jfreechart: Java graphics solution (Application/applet/servlet/jsp)
3. jcommon: public class libraries of jfreereport and jfreechart
4. jfreedesigner: jfreereport report design tool

We can get the latest version and related materials from the jfree official website (but jfree document takes US $40 ),
Get address: http://www.jfree.org/jfreechart/index.html (for a brief introduction)
The latest version: jfreechart_0.9.21.zip is used as an example.

Ii. jfreechart configuration and installation
1、decompress jfreechart_0.9.21.zip to the specified location, where source is the source code of jfreechart, jfreechart-0.9.21-demo.jar
Is the example Program (this part is left for everyone to study)
2, in order to configure successfully, we need to pay attention to the following three files: jfreechart-0.9.21.jar, lib/jcommon-0.9.6.jar,
LIB/gnujaxp. Jar
3. For application development, copy the preceding three files to % java_home %/lib and add them to the environment variable classpath.
For web development, take a WEB Project Test in Tomcat as an example:
Copy the above three files to test/WEB-INF/lib, then modify the test/WEB-INF/Web. xml file, add the following code in it:
<Servlet>
<Servlet-Name> displaychart </servlet-Name>
<Servlet-class> org. jfree. Chart. servlet. displaychart </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> displaychart </servlet-Name>
<URL-pattern>/servlet/displaychart </url-pattern>
</Servlet-mapping>
Now the configuration of jfreechart is complete. Now we can develop jfreechart. The jfreechart class is worth mentioning here.
The compatibility before and after structure design is not very good. The structure of the class library in different versions of jfreechart may be different, and sometimes the source code may need to be checked. If
The source code font may need to be changed based on the perception when displaying Chinese characters, but I personally think this version is better than the previous version.

Iii. Demo
<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Page import = "Java. AWT .*,
Java. Text. simpledateformat,
Javax. Swing. jpanel,
Org. jfree. Chart. chartfactory,
Org. jfree. Chart. chartpanel,
Org. jfree. Chart. jfreechart,
Org. jfree. Chart. axis. dateaxis,
Org. jfree. Chart. Plot. xyplot,
Org. jfree. Chart. Renderer. XY. xyitemrenderer,
Org. jfree. Chart. Renderer. XY. xylineandshaperenderer,
Org. jfree. Data. time .*,
Org. jfree. Data. Time. Timeseries,
Org. jfree. Data. Time. timeseriescollection,
Org. jfree. Data. XY. xydataset "%>
<% @ Page import = "org. jfree. Chart. servlet. servletutilities" %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "org. jfree. UI. rectangleinsets" %>
<% @ Page import = "Java. SQL. *" %>
<%
// Connect to the database
Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver"). newinstance ();
String url = "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = jfree ";
String USR = "sa ";
String Pwd = "";
Connection conn = drivermanager. getconnection (URL, USR, PWD );
Statement stmt = conn. createstatement ();
String SQL = "select * From jfree order by ID ";
Resultset rs1_stmt.exe cutequery (SQL );

// Set the curve. There are three curves in total.
Timeseries = new Timeseries ("average", minute. Class); // The minimum unit of time is minute, and the line value name is "average"
Timeseries timeseries1 = new Timeseries ("usl", minute. Class );
Timeseries timeseries2 = new Timeseries ("LSL", minute. Class );

// Values, data, and time from the database
While (Rs. Next ())
{
Float Y0 = Rs. getfloat ("lengthavg ");
Float Y1 = Rs. getfloat ("lengthusl ");
Float y2 = Rs. getfloat ("lengthlsl ");
String S = Rs. getstring ("addtime ");
// Retrieve the year, month, day, and hour from Char in the yyyymmddhhmm time format and set it to int.
Int x = integer. parseint (S. substring (0, 4 ));
Int y = integer. parseint (S. substring (4, 6 ));
Int z = integer. parseint (S. substring (6, 8 ));
Int A = integer. parseint (S. substring (8, 10 ));
Int B = integer. parseint (S. substring (10, 12 ));

Timeseries. Add (new minute (B, new hour (A, New Day (z, Y, X), y0 );
Timeseries1.add (new minute (B, new hour (A, New Day (z, Y, X), Y1 );
Timeseries2.add (new minute (B, new hour (A, New Day (z, Y, X), Y2 );

}

// Connection Curve
Timeseriescollection dataset = new timeseriescollection ();
Dataset. addseries (Timeseries );
Dataset. addseries (timeseries1 );
Dataset. addseries (timeseries2 );
Dataset. setdomainispointsintime (true );

// Set the Graph
Xydataset = (xydataset) dataset;
Jfreechart chart = chartfactory. createtimeserieschart (
"Trend chart ",
"Time ",
"Value ",
Xydataset,
True,
True,
True
);
Chart. setbackgroundpaint (color. White); // you can specify the background color of a curve.

Xyplot plot = (xyplot) Chart. getplot ();
Xylineandshaperenderer = (xylineandshaperenderer) plot. getrenderer ();
Plot. setbackgroundpaint (color. White); // set the grid background color.
Plot. setdomaingridlinepaint (color. Pink); // you can specify the grid vertical line color.
Plot. setrangegridlinepaint (color. Pink); // you can specify the grid horizontal line color.
Plot. setaxisoffset (New rectangleinsets (0d, 0d, 0d, 10d); // you can specify the distance between the curve and the XY axis.
Xylineandshaperenderer. setbaseshapesvisible (true); // you can specify whether the curve displays data points.

String filename = servletutilities. savechartaspng (chart, 500,300, null, session );
String graphurl = request. getcontextpath () + "/servlet/displaychart? Filename = "+ filename;
%>

In addition, many people have previously said that they cannot display a graph and come up with a cross. Generally, this is a servlet error. I have seen that some people use servlets.
So we need to go to string graphurl = request. getcontextpath () + "/servlet/displaychart? Filename = "+ filename;
Change to string graphurl = request. getcontextpath () + "/Servlets/displaychart? Filename = "+ filename;
This should be taken into account. If it comes out of the fork, it is certain that there is no program error in the program code, but it is just a path error.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.