Spring MVC jfreechar Annotation configuration and database access

Source: Internet
Author: User

1.action class, CONFIGURED with annotations:

Package com.t5.manage.action;
Import Java.awt.Font;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Map;

Import Javax.annotation.Resource;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import org.jfree.chart.ChartFactory;
Import org.jfree.chart.ChartUtilities;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.axis.CategoryAxis;
Import Org.jfree.chart.axis.ValueAxis;
Import Org.jfree.chart.plot.CategoryPlot;
Import org.jfree.chart.plot.PlotOrientation;
Import org.jfree.chart.servlet.ServletUtilities;
Import Org.jfree.chart.title.LegendTitle;
Import Org.jfree.chart.title.TextTitle;
Import Org.jfree.data.category.CategoryDataset;
Import Org.jfree.data.category.DefaultCategoryDataset;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.ModelMap;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.servlet.ModelAndView;

Import Com.t5.entity.Statistics;
Import Com.t5.entity.User;
Import Com.t5.manage.biz.ManagerLoginBiz;
Import Com.t5.manage.biz.ManagerOrderBiz;
@Controller
public class Jfreechart {
@Autowired
Public managerorderbiz managerorderbiz;

@Resource
Public managerloginbiz Manageuserservice;

@RequestMapping (value = "/getmajorchart")
Public Modelandview Getmajorchart (HttpServletRequest request,
HttpServletResponse response, Modelmap modelmap,string time) throws exception{
Categorydataset DataSet = GetDataSet2 (time);//time parameter
Jfreechart chart = Chartfactory.createbarchart3d ("Annual Sales Report",//Chart title
"category",//Catalog Axis display label
"Sales",//display labels for value axes
DataSet,//DataSet
Plotorientation.vertical,//Chart direction: horizontal, Vertical
True,//Whether the legend is displayed (must be false for a simple bar chart)
False,//whether the tool is generated
False//whether to generate a URL link
);
Solve garbled problems
Getchartbyfont (chart);
String fileName = servletutilities.savechartasjpeg (chart, 700,500, NULL, request.getsession ());
String Charturl=request.getcontextpath () + "/chart?filename=" +filename;
Modelmap.put ("Charturl", Charturl);
return new Modelandview ("Test", Modelmap);
}
private void Getchartbyfont (Jfreechart chart) {
Texttitle texttitle = Chart.gettitle ();
Texttitle.setfont (New Font ("Arial", font.bold,20));
Legendtitle legend = Chart.getlegend ();
if (legend!=null) {
Legend.setitemfont (New Font ("Arial", Font.Bold, 20));
}
Categoryplot plot = (Categoryplot) chart.getplot ();
Categoryaxis axis = Plot.getdomainaxis ();
Set the text of the title on x-axis coordinates
Axis.setlabelfont (New Font ("Arial", font.bold,22));
Sets the text on x-axis coordinates,
Axis.setticklabelfont (New Font ("Arial", font.bold,12));

Valueaxis Valueaxis = Plot.getrangeaxis ();
Set the text of the title on the y-axis coordinates
Valueaxis.setlabelfont (New Font ("Arial", font.bold,12));
Set text on y-axis coordinates
Valueaxis.setticklabelfont (New Font ("Sans-serif", font.bold,12));
}

/**
* Get a composite DataSet object for demonstration
*
* @return

* Time is the query parameter
*/
Private Categorydataset GetDataSet2 (String time) {
Defaultcategorydataset DataSet = new Defaultcategorydataset ();
List<map<string, object>> list=managerorderbiz.getorderlist (time);
List<statistics> liststa=new arraylist<statistics> ();
System.out.println ("Number of Sets" +list.size ());
For (map<string, object> map:list)
{
String Typename= (String) map.get ("TypeName"). toString ();//commodity type
String year= (String) Map.get ("year"). ToString (); Years
String Month= (String) map.get ("month"). ToString (); Month
Integer shoppingid=integer.valueof (Map.get ("Shoppingid"). toString ());
Integer userid=integer.valueof (Map.get ("UserId"). toString ());
Integer ordertypeid=integer.valueof (String) map.get ("Ordertypeid"). toString ());
Double moneytotal=double.valueof (Map.get ("Moneytotal"). toString ());
Statistics sta=new Statistics ();
Sta.settypename (TypeName);
Sta.setyear (year);
Sta.setmonth (month);
Sta.setmoneytotal (moneytotal);//Total Amount
Liststa.add (STA);
}
for (Statistics Tics:liststa) {
Dataset.addvalue (Tics.getmoneytotal (), Tics.gettypename (), Tics.getmonth ());
}
return dataset;
}

}

2.jsp page:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base href= "<%=basePath%>" >

<title>jfreechart Demo </title>


<body>
Jfreechart Demo <br>
Please visit <a href= "Getmajorchart.do" >fffff</a>
<br>

</body>

Spring MVC jfreechar Annotation configuration and database access

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.