Jfreechart tutorial (2)

Source: Internet
Author: User

I. jfreechart Graphic Generation Process

Create a data source (Dataset) to include the data to be displayed in the graph> Create a jfreechart object to represent the graph to be displayed
> Output the image
Important classes and interfaces:
Org. jfree. Data. General. dataset interfaces to be implemented by all data source classes
Org. jfree. Chart. chartfactory is used to generate a jfreechart object.
Org. jfree. Chart. jfreechart all the adjustments to the image are made through it. Oh !!
Org. jfree. Chart. Plot. Plot obtains it through the jfreechart object, and then adjusts the external part of the image (for example, axis) through it.
Note: It has many sub-classes, and is generally shaped to its sub-classes under the category!
Org. jfree. Chart. Renderer. abstractrenderer obtains it through the jfreechart object, and then uses it
(For example, line type) adjustment. For different types of report charts
Different subclass implementations! In this example, we call it Renderer.
Next we will analyze this process with different types of images.

Ii. Pie Chart
Dataset of a pie chart generally uses the piedataset interface. The specific implementation class is defaultpiedataset.
1. Create a data source (Dataset ):
Private Static piedataset createdataset ()
{
Defaultpiedataset defapipiedataset = new defaultpiedataset (); // pay attention to defaultpiedataset !!
Defaultpiedataset. setvalue ("one", new double (43.200000000000003d ));
Defaultpiedataset. setvalue ("two", New Double (10d ));
Defaultpiedataset. setvalue ("three", New Double (27.5D ));
Defaultpiedataset. setvalue ("four", new double (17.5d ));
Return defaultpiedataset;
}
2. jfreechart object generated by chartfactory
Private Static jfreechart createchart (piedataset)
{
Jfreechart = chartfactory. createpiechart ("pie chart demo 1", // graphic title name
Piedataset, // Dataset
True, // legend?
True, // tooltips?
False); // URLs?
Pieplot = (pieplot) jfreechart. getplot (); // get the plot: pieplot through the jfreechart object !!
Pieplot. setnodatamessage ("No data available"); // The content displayed when no data exists.
Return jfreechart;
}
Some important methods:
Pieplot. setexplodepercent (0, 0.3d) // "dig" the one with lable as "one" to 30%
3. Output

3. Bar Chart
Dataset of the bar chart generally uses the catagorydataset interface (the specific implementation class is defaultcategorydataset), and intervalxydataset is also used.
Interface
1. Create a data source (Dataset ):
Private Static categorydataset createdataset ()
{
String Series1 = "first ";
String series2 = "second ";
String series3 = "third ";
String category1 = "Category 1 ″;
String category2 = "Category 2 ″;
String category3 = "Category 3 ″;
String category4 = "Category 4 ″;
String category5 = "Category 5 ″;
Defaultcategorydataset defacategcategorydataset = new defaultcategorydataset ();
Defaultcategorydataset. addvalue (1.0d, Series1, category1 );
Defaultcategorydataset. addvalue (4d, Series1, category2 );
Defaultcategorydataset. addvalue (3d, Series1, category3 );
Defaultcategorydataset. addvalue (5d, Series1, category4 );
Defaultcategorydataset. addvalue (5d, Series1, category5 );

Defaultcategorydataset. addvalue (5d, series2, category1 );
Defaultcategorydataset. addvalue (7D, series2, category2 );
Defaultcategorydataset. addvalue (6d, series2, category3 );
Defaultcategorydataset. addvalue (8d, series2, category4 );
Defaultcategorydataset. addvalue (4d, series2, category5 );

Defaultcategorydataset. addvalue (4d, series3, category1 );
Defaultcategorydataset. addvalue (3d, series3, category2 );
Defaultcategorydataset. addvalue (2d, series3, category3 );
Defaultcategorydataset. addvalue (3d, series3, category4 );
Defaultcategorydataset. addvalue (6d, series3, category5 );
Return defaultcategorydataset;
}
2. jfreechart object generated by chartfactory
Private Static jfreechart createchart (categorydataset)
{
Jfreechart = chartfactory. createbarchart ("bar chart Demo", // graphic title name
"Category", // domain axis lable
Here, we can simply understand the x-axis lable.
"Value", // range axis lable
Here, we can simply understand it as the vertical coordinate lable.
Categorydataset, // Dataset
Plotorientation. Vertical, // vertical display
True, // legend?
True, // tooltips?
False); // URLs?
Jfreechart. setbackgroundpaint (color. White); // set the background color to white.
Categoryplot = jfreechart. getcategoryplot (); // obtain the plot: categoryplot !!
Categoryplot. setbackgroundpaint (color. lightgray); // you can specify the background color for displaying certain chart data.
Categoryplot. setdomaingridlinepaint (color. White); // the x-axis gridline is white.
Categoryplot. setdomaingridlinesvisible (true); // visible
Categoryplot. setrangegridlinepaint (color. White); // the x-axis is white.
// The minimum unit of the following two Y coordinates is an integer.
Numberaxis = (numberaxis) categoryplot. getrangeaxis ();
Numberaxis. setstandardtickunits (numberaxis. createintegertickunits ());
Barrenderer = (barrenderer) categoryplot. getrenderer (); // obtain the Renderer. Note that this is the lower render shape.
To barrenderer !!
Barrenderer. setdrawbaroutline (false); // do not draw the bar contour.
Gradientpaint = new gradientpaint (0.0f, 0.0f, color. Blue,
0.0f, 0.0f, new color (0, 0, 64); // you can specify a color.
Gradientpaint gradientpaint1 = new gradientpaint (0.0f, 0.0f, color. Green,
0.0f, 0.0f, new color (0, 64, 0 ));
Gradientpaint gradientpaint2 = new gradientpaint (0.0f, 0.0f, color. Red,
0.0f, 0.0f, new color (64, 0, 0 ));
Barrenderer. setseriespaint (0, gradientpaint); // set the color defined above for the Series1 bar
Barrenderer. setseriespaint (1, gradientpaint1); // set the color defined above for the series2 bar
Barrenderer. setseriespaint (2, gradientpaint2); // set the color defined above for the series3 bar
Categoryaxis = categoryplot. getdomainaxis (); // lable 45 degree skew on the horizontal axis
Categoryaxis. setcategorylabelpositions (categorylabelpositions. up_45 );
Return jfreechart;
}
Some important methods: (add a tag)
Intervalmarker = new intervalmarker (4.5d, 7.5d );
Intervalmarker. setlabel ("target range ");
Intervalmarker. setlabelfont (new font ("sansserif", 2, 11 ));
Intervalmarker. setlabelanchor (rectangleanchor. Left );
Intervalmarker. setlabeltextanchor (textanchor. center_left );
Intervalmarker. setpaint (new color (222,222,255,128 ));
Categoryplot. addrangemarker (intervalmarker, layer. Background );

Iv. line chart
Dataset and xydataset interfaces of line charts (The implementation class is defaultcategorydataset)
1. catagorydataset interface:
A. Create a data source (Dataset ):
Private Static categorydataset createdataset ()
{
String Series1 = "first ";
String series2 = "second ";
String series3 = "third ";
String type1 = "Type 1 ″;
String type2 = "Type 2 ″;
String type3 = "Type 3 ″;
String type4 = "type 4 ″;
String type5 = "type 5 ″;
String type6 = "Type 6 ″;
String type7 = "type 7 ″;
String type8 = "type 8 ″;
Defaultcategorydataset defacategcategorydataset = new defaultcategorydataset ();
Defaultcategorydataset. addvalue (1.0d, Series1, type1 );
Defaultcategorydataset. addvalue (4d, Series1, type2 );
Defaultcategorydataset. addvalue (3d, Series1, type3 );
Defaultcategorydataset. addvalue (5d, Series1, type4 );
Defaultcategorydataset. addvalue (5d, Series1, type5 );
Defaultcategorydataset. addvalue (7D, Series1, type6 );
Defaultcategorydataset. addvalue (7D, Series1, type7 );
Defaultcategorydataset. addvalue (8d, Series1, type8 );

Defaultcategorydataset. addvalue (5d, series2, type1 );
Defaultcategorydataset. addvalue (7D, series2, type2 );
Defaultcategorydataset. addvalue (6d, series2, type3 );
Defaultcategorydataset. addvalue (8d, series2, type4 );
Defaultcategorydataset. addvalue (4d, series2, type5 );
Defaultcategorydataset. addvalue (4d, series2, type6 );
Defaultcategorydataset. addvalue (2d, series2, type7 );
Defaultcategorydataset. addvalue (1.0d, series2, type8 );

Defaultcategorydataset. addvalue (4d, series3, type1 );
Defaultcategorydataset. addvalue (3d, series3, type2 );
Defaultcategorydataset. addvalue (2d, series3, type3 );
Defaultcategorydataset. addvalue (3d, series3, type4 );
Defaultcategorydataset. addvalue (6d, series3, type5 );
Defaultcategorydataset. addvalue (3d, series3, type6 );
Defaultcategorydataset. addvalue (4d, series3, type7 );
Defaultcategorydataset. addvalue (3d, series3, type8 );
Return defaultcategorydataset;
}
B. jfreechart objects are generated by chartfactory (the repeated parts are not commented out)
Private Static jfreechart createchart (categorydataset)
{
Jfreechart = chartfactory. createlinechart ("line chart demo 1 ″,
"Type ",
"Value ",
Categorydataset,
Plotorientation. Vertical,
True,
True,
False );
Jfreechart. setbackgroundpaint (color. White );
Categoryplot = (categoryplot) jfreechart. getplot ();
Categoryplot. setbackgroundpaint (color. lightgray );
Categoryplot. setrangegridlinepaint (color. White );
Numberaxis = (numberaxis) categoryplot. getrangeaxis ();
Numberaxis. setstandardtickunits (numberaxis. createintegertickunits ());
Numberaxis. setautorangeincludeszero (true );
// Get the Renderer. Note that the following figure shows the render styling to lineandshaperenderer !!
Lineandshaperenderer = (lineandshaperenderer) categoryplot. getrenderer ();
Lineandshaperenderer. setshapesvisible (true); // visible to series points (that is, data points)
Lineandshaperenderer. setseriesstroke (0, new basicstroke (2.0f, 1, 1, 1.0f, new float [] {
10f, 6f
}, 0.0f); // the line between the Series1 points that define series as "first". The line is a dotted line. The default line is a straight line.
Lineandshaperenderer. setseriesstroke (1, new basicstroke (2.0f, 1, 1, 1.0f, new float [] {
6f, 6f
}, 0.0f); // the line between the series2 points that define series as "second"
Lineandshaperenderer. setseriesstroke (2, new basicstroke (2.0f, 1, 1, 1.0f, new float [] {
2.0f, 6f
}, 0.0f); // the line between the series3 points that define series as "third"
Return jfreechart;
}
Some important methods:
Lineandshaperenderer. setlinevisible (true) // a line between series points (that is, data points) is visible.
2. xydataset interface:
A. Create a data source (Dataset ):
Private Static xydataset createdataset ()
{
Xyseries = new xyseries ("first"); // generates a xyseries object first.
Xyseries. Add (1.0d, 1.0d );
Xyseries. Add (2d, 4D );
Xyseries. Add (3d, 3d );
Xyseries. Add (4d, 5d );
Xyseries. Add (5d, 5d );
Xyseries. Add (6d, 7d );
Xyseries. Add (7D, 7d );
Xyseries. Add (8d, 8D );

Xyseries xyseries1 = new xyseries ("second ");
Xyseries1.add (1.0d, 5d );
Xyseries1.add (2d, 7d );
Xyseries1.add (3d, 6d );
Xyseries1.add (4d, 8D );
Xyseries1.add (5d, 4D );
Xyseries1.add (6d, 4D );
Xyseries1.add (7D, 2d );
Xyseries1.add (8d, 1.0d );

Xyseries xyseries2 = new xyseries ("third ");
Xyseries2.add (3d, 4D );
Xyseries2.add (4d, 3d );
Xyseries2.add (5d, 2d );
Xyseries2.add (6d, 3d );
Xyseries2.add (7D, 6d );
Xyseries2.add (8d, 3d );
Xyseries2.add (9d, 4D );
Xyseries2.add (10d, 3d );

Xyseriescollection = new xyseriescollection (); // use xyseriescollection to add a xyseries object
Xyseriescollection. addseries (xyseries );
Xyseriescollection. addseries (xyseries1 );
Xyseriescollection. addseries (xyseries2 );
Return xyseriescollection;
}
B. jfreechart object generated by chartfactory
Private Static jfreechart createchart (xydataset)
{
Jfreechart = chartfactory. createxylinechart ("line chart DEMO 2 ″,
"X ",
"Y ",
Xydataset,
Plotorientation. Vertical,
True,
True,
False );
Jfreechart. setbackgroundpaint (color. White );
Xyplot = (xyplot) jfreechart. getplot (); // obtain the plot: xyplot !!
Xyplot. setbackgroundpaint (color. lightgray); // you can specify the background color for displaying certain chart data.
Xyplot. setaxisoffset (New rectangleinsets (5d, 5d, 5d, 5d); // set the distance between the coordinate axis and the display part of the chart data
Xyplot. setdomaingridlinepaint (color. White); // gridline color
Xyplot. setrangegridlinepaint (color. White );
// Obtain Renderer. Note that this is xylineandshaperenderer !!
Xylineandshaperenderer = (xylineandshaperenderer) xyplot. getrenderer ();
Xylineandshaperenderer. setshapesvisible (true); // visible to data points
Xylineandshaperenderer. setshapesfilled (true); // The data point is filled, which is not a hollow point.
Numberaxis = (numberaxis) xyplot. getrangeaxis ();
Numberaxis. setstandardtickunits (numberaxis. createintegertickunits ());
Return jfreechart;
}
Some important methods:
Xylineandshaperenderer = new xylineandshaperenderer ();
Xylineandshaperenderer. setserieslinesvisible (0, false); // the line between the first xyseries data points is invisible.
Xylineandshaperenderer. setseriesshapesvisible (1, false); // The second xyseries data point is invisible.
Xyplot. setrenderer (xylineandshaperenderer );

V. Time Sequence Diagram
The time sequence diagram is similar to the line chart. The difference is that the data in the domain axis is time rather than number. Dataset of the time sequence diagram is
Xydataset interface. The specific implementation class is timeseriescollection. Similar to the above, there is a Timeseries object, which is added
Timeseriescollection.
1. Create a data source (Dataset ):
Private Static xydataset createdataset ()
{
Timeseries = new Timeseries ("L & G European index trust", month. Class );
Timeseries. Add (new month (2, 2001), 181.8d); // here month. Class is used, and day. Class year. Class is also used.
Timeseries. Add (new month (3, 2001), 167.3d );
Timeseries. Add (new month (4, 2001), 153.8d );
Timeseries. Add (new month (5, 2001), 167.6d );
Timeseries. Add (new month (6, 2001), 158.8d );
Timeseries. Add (new month (7, 2001), 148.3d );
Timeseries. Add (new month (8, 2001), 153.9d );
Timeseries. Add (new month (9, 2001), 142.7d );
Timeseries. Add (new month (10,200 1), 123.2d );
Timeseries. Add (new month (11,200 1), 131.8d );
Timeseries. Add (new month (1, 12,200), 139.6d );
Timeseries. Add (new month (1, 2002), 142.9d );
Timeseries. Add (new month (2, 2002), 138.7d );
Timeseries. Add (new month (3, 2002), 137.3d );
Timeseries. Add (new month (4, 2002), 143.9d );
Timeseries. Add (new month (5, 2002), 139.8d );
Timeseries. Add (new month (6, 2002), 137d );
Timeseries. Add (new month (7, 2002), 132.8d );

Timeseries timeseries1 = new Timeseries ("L & g uk index trust", month. Class );
Timeseries1.add (new month (2, 2001), 129.6d );
Timeseries1.add (new month (3, 2001), 123.2d );
Timeseries1.add (new month (4, 2001), 117.2d );
Timeseries1.add (new month (5, 2001), 124.1d );
Timeseries1.add (new month (6, 2001), 122.6d );
Timeseries1.add (new month (7, 2001), 119.2d );
Timeseries1.add (new month (8, 2001), 116.5d );
Timeseries1.add (new month (9, 2001), 112.7d );
Timeseries1.add (new month (10,200 1), 101.5d );
Timeseries1.add (new month (1, 11,200), 106.1d );
Timeseries1.add (new month (12,200 1), 110.3d );
Timeseries1.add (new month (1, 2002), 111.7d );
Timeseries1.add (new month (2, 2002), 111d );
Timeseries1.add (new month (3, 2002), 109.6d );
Timeseries1.add (new month (4, 2002), 113.2d );
Timeseries1.add (new month (5, 2002), 111.6d );
Timeseries1.add (new month (6, 2002), 108.8d );
Timeseries1.add (new month (7, 2002), 101.6d );
Timeseriescollection = new timeseriescollection ();
Timeseriescollection. addseries (Timeseries );
Timeseriescollection. addseries (timeseries1 );
Timeseriescollection. setdomainispointsintime (true); // The scale point on the domain axis indicates the time point instead of the time period.
Return timeseriescollection;
}
2. jfreechart object generated by chartfactory
Private Static jfreechart createchart (xydataset)
{
Jfreechart = chartfactory. createtimeserieschart ("Legal & General unit trust prices ",
"Date ",
"Price per unit ",
Xydataset,
True,
True,
False );
Jfreechart. setbackgroundpaint (color. White );
Xyplot = (xyplot) jfreechart. getplot (); // obtain the plot: xyplot !!
Xyplot. setbackgroundpaint (color. lightgray );
Xyplot. setdomaingridlinepaint (color. White );
Xyplot. setrangegridlinepaint (color. White );
Xyplot. setaxisoffset (New rectangleinsets (5d, 5d, 5d, 5d ));
Xyplot. setdomaincrosshairvisible (true );
Xyplot. setrangecrosshairvisible (true );
Org. jfree. Chart. Renderer. XY. xyitemrenderer = xyplot. getrenderer ();
If (xyitemrenderer instanceof xylineandshaperenderer)
{
Xylineandshaperenderer = (xylineandshaperenderer) xyitemrenderer;
Xylineandshaperenderer. setdefashapshapesvisible (true); // visible to data points
Xylineandshaperenderer. setdefashapshapesfilled (true); // The data point is a solid point.
}
Dateaxis = (dateaxis) xyplot. getdomainaxis (); // defines the date display format on the domain axis
Dateaxis. setdateformatoverride (New simpledateformat ("mmm-yyyy "));
Return jfreechart;
}
Some important methods:
A. Add a marking line:
Xyplot. addrangemarker (New valuemarker (550d); // numerical axis
Quarter quarter = new quarter (2, 2002 );
Xyplot. adddomainmarker (New valuemarker (quarter. getmiddlemillisecond (); // timeline
B. Adjustment of data points
Xylineandshaperenderer = (xylineandshaperenderer) xyplot. getrenderer ();
Xylineandshaperenderer. setdefashapshapesvisible (true); // visible to data points
Xylineandshaperenderer. setseriesfillpaint (0, color. Red); // The data point is filled in red.
Xylineandshaperenderer. setseriesfillpaint (1, color. White); // The data point is white.
Xylineandshaperenderer. setusefillpaint (true); // Application
C. Average Value Curve
What is the purpose of this curve? In a simple example, there is a curve drawn based on data units every day for half a year. Let's look at the data in the unit of month.
In this case, you can use it.
Timeseries = createeurtimeseries (); // The data is in the unit of six months per day.
Timeseries timeseries1 = movingaverage. createmovingaverage (Timeseries,
"30 day moving average ",
30, // 30 days is a cycle
30); // skip the first 30 days
Timeseriescollection = new timeseriescollection ();
Timeseriescollection. addseries (Timeseries );
Timeseriescollection. addseries (timeseries1 );
Return timeseriescollection;

6. Summary
Dataset plot Renderer
Pie Chart piedataset (defaultpiedataset) pieplot --
Bar Chart catagorydataset (defaultcategorydataset) categoryplot barrenderer
Line chart catagorydataset (defaultcategorydataset) categoryplot lineandshaperenderer
Xydataset (xyseriescollection) xyplot xylineandshaperenderer
Time Sequence diagram xydataset (timeseriescollection) xyplot xylineandshaperenderer
Here are some common methods. For details, refer to the API
VII. Item lable
The following uses a bar chart as an example to describe how to display its data on each column:
A. Make item lable visible
B. Adjust the color and font of item lable.
C. Adjust the position of item lable
D. Customize the content of item lable
1. assign a lable generator to Renderer
Barrenderer = (barrenderer) categoryplot. getrenderer ();
Gategorylablegenerator generator = new standardgategorylablegenerator (
"{2}", new decimalformat ("0.00") // adjust the displayed number and character format
);
Barrenderer. setlablegenerator (generator );
2. Make item lable visible
Barrenderer. setitemlablevisible (true );
3. Adjust the color and font of item lable.
Barrenderer. setitemlablepaint (color. Red );
Barrenderer. setitemlablefont (new font ("sansserif", Font. Plain, 10 ));
4. Adjust the position of item lable
This involves a new object itemlableposition. The itemlableposition constructor has two or four parameters.
Public itemlabelposition (itemlabelanchor,
Org. jfree. UI. textanchor,
Org. jfree. UI. textanchor rotationanchor,
Double angle)
Location of itemlabelanchor-item lable (most important !!)
Position of the body contained in textanchor-item lable relative to item lable
Rotationanchor-item lable contains the position of the body rotation
Angle-Rotation Angle
Itemlabelposition = new itemlabelposition (itemlabelanchor. inside12,
Textanchor. center_right,
Textanchor. center_right,
-1.57d );
Barrenderer. setpositiveitemlabelposition (itemlabelposition );
In this way, the data of the item lable can be displayed on each column. Of course, the content of the item lable can be customized. For example, if the item lable text exceeds 100
Display, so you need to customize your own class, it needs to implement the gategorylablegenerator interface, implement the generateitemlable () method

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.