Struts2+jsp+jfreechart the JSP page to generate a chart __php

Source: Internet
Author: User

1.action:messagelogaction.java

/**
* Generates a diagram from the module name and operation name
* @return
* @throws appexception
*/
@OperateLog (modulename = "error log", content = " Trend analysis ")
Public String Gofindmessagelogtocreatechart () {
Defaultcategorydataset DCD = new Defaultcategorydataset ( );
Messagelogsearchform searchform = Model.getsearchform ();
Searchform.fromjsonstring ();
List<object[]> List = Messagelogbpo.findmessagelogtocreatechart (
Model.getactionname (), Model.getmlfcsearchform ());
if (list!= null && list.size () >) {
list = Messagelogbpo.findmessagelogtocreatechartmonth (
Model.getactionname (), Model.getmlfcsearchform ());
}
if (list!= null && list.size () > 0) {
for int i = 0; i < list.size (); i++) {
Dcd.addvalue (i Nteger.valueof (List.get (i) [0].tostring ()),
Quantity, List.get (i) [1].tostring ()];
}
}
This.chart = Jfreechartutils.makechartbar (Model.getactionname ()
+ "error trend", "date", "Quantity", new string[] {} , DCD);
return SUCCESS;
}


2.struts.xml configuration file

<!--find the error log to generate a chart based on the module name and the operation name-->
<action name= "Findmessagelogtocreatechart" class= ap. Messagelogaction "method=" Gofindmessagelogtocreatechart ">
<result name= "Success" type= "Chart" >
<param name= "width" >850</param>
<param name= "Height" >400</param>
</result>
</action>

Note: you need to

<package name= "Ap.messagelog" extends= "Struts-default.xml" >,ap.messagelog this package's parent configuration file

Add in Struts-default.xml:

<result-types>
<result-type name= "Chart" class= "Com.opensymphony.webwork.dispatcher.ChartResult" >
</result-type>
</result-types>


3.jfreechartutils.java

/**
* Histogram, Divide: General Column chart and multiple groups of columns
*
* @param titlename
* @param xname
* @param yname
* @param subtitles
* @param DataSet
* @return
*/
public static Jfreechart Makechartbar (String titlename,
String XName, String yname, string[] subtitles,
Categorydataset DataSet) {
Jfreechart Jfreechart = Chartfactory.createbarchart3d (TitleName, XName,
Yname, DataSet, Plotorientation.vertical, True, true, false);
Font font = new Font ("Song Body", Font.Bold, 20);
Texttitle title = new Texttitle (titlename);
Title.setfont (font);
Jfreechart.settitle (title);


if (subtitles!= null) {
for (String subtitle:subtitles) {
Jfreechart.addsubtitle (new Texttitle (subtitle));
}
}
Jfreechart.setbackgroundpaint (Color.White);
Categoryplot Categoryplot = (categoryplot) jfreechart.getplot ();
Set display area without black border
Categoryplot.setoutlinevisible (FALSE);


X axis Settings
Categoryaxis Categoryaxis = Categoryplot.getdomainaxis ();
Categoryaxis.setcategorylabelpositions (categorylabelpositions
. Createuprotationlabelpositions (1));
Categoryaxis.setmaximumcategorylabelwidthratio (0.6f);/The lable on the horizontal axis
Categoryaxis.setcategorymargin (0.4);
is full display
Set the distance from the left end of the picture
Categoryaxis.setlowermargin (0.1);
Set the distance from the right end of the picture
Categoryaxis.setuppermargin (0.1);


Valueaxis Rangeaxis = Categoryplot.getrangeaxis ();
Rangeaxis.setuppermargin (0.5);
Categoryplot.setdomainaxis (Categoryaxis); Make the horizontal coordinate set effective

Set the data displayed on the pillar to rotate 90 degrees, and the last parameter is the angle value of rotation/3.14
Itemlabelposition itemlabelposition= New Itemlabelposition (
Itemlabelanchor.inside12, Textanchor.center_right,
Textanchor.center_right, 1.57D);

Sets the display of values that cannot be displayed properly on columns, displaying these values outside the column
Itemlabelposition itemlabelpositionfallback = new Itemlabelposition (
Itemlabelanchor.outside12, Textanchor.baseline_left,
Textanchor.half_ascent_left, -1.57d);


Numberaxis Numberaxis = (numberaxis) categoryplot.getrangeaxis ();
Numberaxis.setautorangeincludeszero (FALSE);
Numberaxis.setstandardtickunits (Numberaxis.createintegertickunits ());


Set display as planar graphic
Intervalbarrenderer Intervalbarrender = new Intervalbarrenderer ();
Categoryplot.setrenderer (0, Intervalbarrender);
Intervalbarrender.setitemlabelgenerator (New Standardcategoryitemlabelgenerator ());
Sets the minimum width of bar to ensure that the value is displayed
Intervalbarrender.setminimumbarlength (0.02);
Max width
Intervalbarrender.setmaximumbarwidth (0.05);
Set the position of column labels that are not displayed correctly
Intervalbarrender.setpositiveitemlabelpositionfallback (Itemlabelpositionfallback);
Intervalbarrender.setnegativeitemlabelpositionfallback (Itemlabelpositionfallback);
Intervalbarrender.setitemlabelsvisible (TRUE);
Intervalbarrender.setbarpainter (New Standardbarpainter ());
Intervalbarrender.setitemmargin (-0.01);

Set the position of the column label that is normally displayed
Intervalbarrender.setpositiveitemlabelposition (itemlabelposition);
Intervalbarrender.setnegativeitemlabelposition (itemlabelposition);

Solve the Chinese garbled problem, the total to deal with these two parts
1, the figure inside the set of Chinese characters, that is, plot settings
Font font2 = new Font ("Song Body", Font.plain, 12); Set font, type, font size
Categoryplot.getdomainaxis (). Setlabelfont (Font2);//equivalent to the horizontal axis or to the X axis
Categoryplot.getrangeaxis (). Setlabelfont (Font2);//equivalent to vertical axis understanding Y axis


2, the following box area is a Legendtitle object
Font font3 = new Font ("Song Body", Font.plain, 12); Set font, type, font size
Jfreechart.getlegend (). Setitemfont (FONT3)/Bottom
Set the following square area without a black border
Jfreechart.getlegend (). Setframe (Blockborder.none);
Set the area of the box below more clearly
Jfreechart.getrenderinghints (). put (renderinghints.key_text_antialiasing,
Renderinghints.value_text_antialias_off);

Set the total background color
Jfreechart.setbackgroundpaint (Chartcolor.white);
Set Title color
Jfreechart.gettitle (). Setpaint (Chartcolor.black);
Get a Chart Object
Categoryplot p = jfreechart.getcategoryplot ();
Display when no data is present
P.setnodatamessage ("No corresponding data, please requery.") ");
Set the background color of a diagram
P.setbackgroundpaint (Chartcolor.white);
Set the table line color
P.setrangegridlinepaint (chartcolor.red);

return jfreechart;
}

/**
* Get pie chart
* @param charttitle Title
* @param DataSet data
* @return Jfreechart
*/
public static Jfreechart Getchartpie (String charttitle, Piedataset DataSet) {
Jfreechart chart = Chartfactory.createpiechart (ChartTitle, DataSet, True, False, false);
Set the title of a chart
Chart.settitle (New Texttitle (ChartTitle, New Font ("Song Body", Font.Bold, 20));
Chart.setbackgroundpaint (Color.White);


Gets the first legend. That's the bottom line of the chart.
Legendtitle legend = chart.getlegend (0);


Set the properties of the first legend
Legend.setitemfont (New Font ("Song Body", Font.plain, 12));
Legend.setframe (Blockborder.none);
Legend.setbackgroundpaint (New Color (245, 245, 245));
Legend.setitempaint (Color.Black);
Get the plot of a pie chart
Pieplot plot = (Pieplot) chart.getplot ();
Plot.setlabelpaint (New Color (41, 41, 41));
Plot.setcircular (TRUE);
Plot.setbackgroundpaint (Color.White);
Plot.setoutlinevisible (FALSE);
Plot.setsectionoutlinesvisible (FALSE);
Plot.setlabeloutlinepaint (Color.White);
Plot.setshadowpaint (New Color (200, 200, 200));
Set what to display when there is no data
Plot.setnodatamessage ("no data");
Set the plot style, which is the display style of the middle part of the entire chart
These operations are only for landscaping purposes, in order to control the style of the chart more carefully
Plot.setlabelfont (New Font ("Song Body", Font.plain, 12));
Show percentages in Picture: custom, {0} for options, {1} for values, {2} for proportions, two decimal places
Plot.setlabelgenerator (New Standardpiesectionlabelgenerator ("{0}={1}" ({2}), Numberformat.getnumberinstance (), New DecimalFormat ("0%"));
Legend Display percentage: Custom way, {0} represents option, {1} represents numeric value, {2} represents%
Plot.setlegendlabelgenerator (New Standardpiesectionlabelgenerator ("{0}"));
Chart.setbackgroundpaint (Color.White);
Chart.setbordervisible (FALSE);
Chart.getrenderinghints (). put (renderinghints.key_text_antialiasing,
Renderinghints.value_text_antialias_off);
return chart;
}

/**
* Common methods for generating dashboards
* @param title
* @param Center Set the information displayed in the center of the Dial
* @param standarddialrangelist Set scale range list
* @param value where the current pointer points
* @return Jfreechart Chart
*/
public static Jfreechart Generatedashboard (string title, String center, list<standarddialrange> Standarddialrangelist, Double value) {
The data collection object is Defaultvaluedataset here
The position the current pointer points to: the data we need to display
Defaultvaluedataset DataSet = new Defaultvaluedataset (value);
Instantiation of Dialplot
Dialplot Dialplot = new Dialplot ();
Dialplot.setview (0.0D, 0.0D, 1.0D, 1.0D);
Set up a data collection
Dialplot.setdataset (DataSet);
Start setting up the display frame structure
Standarddialframe simpledialframe = new Standarddialframe ();
Simpledialframe.setbackgroundpaint (Color.lightgray);
Simpledialframe.setforegroundpaint (Color.darkgray);
Dialplot.setdialframe (Simpledialframe);
End Setting display frame structure
Gradientpaint gradientpaint = new Gradientpaint (new Point (), new color (255, 255, 255), New Point (), new color (170, 170, 22 0));
Dialbackground dialbackground = new Dialbackground (gradientpaint);
Dialbackground.setgradientpainttransformer (New Standardgradientpainttransformer ( gradientpainttransformtype.vertical));
Dialplot.setbackground (Dialbackground);
Set the information displayed in the center of the Dial
Dialtextannotation dialtextannotation = new Dialtextannotation (center);
Dialtextannotation.setfont (New Font ("Dialog", 1, 14));
Dialtextannotation.setradius (0.69999999999999996D);
Dialplot.addlayer (dialtextannotation);
Dialvalueindicator dialvalueindicator = new Dialvalueindicator (0);
Dialplot.addlayer (Dialvalueindicator);

Scope of the instrument panel
Double lowerbound = 0d;//Max scale
Double upperbound = 0d;//Minimum scale
for (Standarddialrange standarddialrange:standarddialrangelist) {
if (Standarddialrange.getlowerbound () < lowerbound) {
Lowerbound = Standarddialrange.getlowerbound ();
}
if (Standarddialrange.getupperbound () > Upperbound) {
Upperbound = Standarddialrange.getupperbound ();
}
Standarddialrange.setinnerradius (0.52000000000000002D);
Standarddialrange.setouterradius (0.55000000000000004D);
Dialplot.addlayer (Standarddialrange);
}
Set the total scale range according to the size of the dial (0.88)
Standarddialscale Standarddialscale = new Standarddialscale (lowerbound, Upperbound, -120d, -300D, 10D, 4);
Standarddialscale.settickradius (0.88D);
Standarddialscale.setticklabeloffset (0.14999999999999999D);
Standarddialscale.setticklabelfont (New Font ("Dialog", 0, 14));
Dialplot.addscale (0, Standarddialscale);

Set pointer
Pointer pointer = new pointer ();
Dialplot.addlayer (pointer);
Instantiation of Dialcap
Dialcap dialcap = new Dialcap ();
Dialcap.setradius (0.10000000000000001D);
Dialplot.setcap (DIALCAP);
Generating Chart Objects
Jfreechart chart = new Jfreechart (dialplot);
Set Title
Chart.settitle (title);
return chart;
}

/**
* Line Chart
*
* @param titlename Main Title
* @param xname x Axis Title
* @param yname y axis Title
* @param categorydataset Chart Data source
* @return
*/
public static Jfreechart Makechartline (String titlename, String xname,
String Yname, Categorydataset categorydataset) {


Jfreechart Jfreechart = Chartfactory.createlinechart (TitleName, XName,
Yname, Categorydataset, Plotorientation.vertical, True, true, false);

Jfreechart.setbackgroundpaint (Color.White);
Categoryplot Categoryplot = (categoryplot) jfreechart.getplot ();
Categoryaxis Categoryaxis = Categoryplot.getdomainaxis ();
Categoryaxis.setcategorylabelpositions (categorylabelpositions
. Createuprotationlabelpositions (1));
Categoryaxis.setmaximumcategorylabelwidthratio (0.6f);/The lable on the horizontal axis
Numberaxis Numberaxis = (numberaxis) categoryplot.getrangeaxis ();

Font xfont = new Font ("Arial", Font.Bold,)//X axis
Font yfont = new Font ("XXFarEastFont-Arial", Font.Bold,)/Y axis
Font kfont = new Font ("Arial", Font.Bold, 12);/Bottom
Font titlefont = new Font ("script", Font.Bold, 18); Picture Title

Picture Title
Jfreechart.settitle (New Texttitle (Jfreechart.gettitle (). GetText (), Titlefont));
Bottom
Jfreechart.getlegend (). Setitemfont (Kfont);
X axis
Categoryaxis.setlabelfont (XFONT);//Axis Title
Categoryaxis.setticklabelfont (XFONT);//axis value
Categoryaxis.setticklabelpaint (Color.Black); Font Color
Categoryaxis.setcategorylabelpositions (CATEGORYLABELPOSITIONS.UP_45); The label on the horizontal axis appears oblique

Y axis
Numberaxis.setlabelfont (Yfont);
Numberaxis.setlabelpaint (Color.Black); Font Color
Numberaxis.setticklabelfont (Yfont);



Categoryplot.setbackgroundpaint (Color.White);
Categoryplot.setaxisoffset (New Rectangleinsets (5D, 5 D, 5 D, 5D));
Categoryplot.setdomaingridlinepaint (Color.gray);
Categoryplot.setrangegridlinepaint (Color.gray);
Lineandshaperenderer lineandshaperenderer = (lineandshaperenderer) categoryplot.getrenderer ();
Lineandshaperenderer.setshapesvisible (TRUE);
Lineandshaperenderer.setshapesfilled (TRUE);
Numberaxis.setstandardtickunits (Numberaxis.createintegertickunits ());

return jfreechart;
}


4.JSP:

&mlfcsearchform.endoperatetime=<ww:property Value= "Mlfcsearchform.endoperatetime"/> '/>

Complete.

Effect preview:







Related Article

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.