Use Open Flash Chart (OFC) production chart (Struts2 processing)

Java open source projects produce more excellent is the JFreeChart chart, and I believe we have heard, it can not only make a very nice bar graph, pie chart, line chart basic graphics in addition, can produce Gantt charts, instrument panel and other charts. In the Web applications can add much color for the project. JFreeChart technology is mature, completely control chart generated by Java code, not difficult to master. But its drawback is that all resources of a server-side generation, need to take up a lot of server resources, but also charts the form of stream delivery to the client also takes up a lot of network resources.
Server resources and network resources for tight project also had to think of a way, although the JFreeChart very gorgeous, but at least with Open Flash Chart, create a simple bar chart, pie charts and line charts, sufficient, and the display effect of no more than Flash JFreeChart poor. Project address is: http://teethgrinder.co.uk/open-flash-chart/
First talk about the Open Flash Chart works, do not see Flash on the fear, OFC development package is a flash file, in fact, we do not need to change the original Flash, Flash just to meet the specifications of the data can be provided. OFC required data format is JSON, JSON does not rely on any technology (Java,. NET and PHP has JSON code generation tool), which makes the OFC more Open. From the Java perspective, the use Struts2 framework to analyze the use of OFC.
GoogleCode community an open source plug-ins for the Java developers using JSON generated OFC provides specific tools that jofc2, I can use JFreeChart jofc2 similar way to organize code, greatly improve development efficiency. The project's address is: http://jofc2.googlecode.com
The development of the project in accordance with the standard build Struts2 and then develop the necessary OFC flash file, the page shows the support Flash file swfobject.js into the appropriate directory location of release, then jofc2 and its dependence xstream the jar package into the WEB- INF / lib and add the next compilation path.
Such a demand: recording system used to access the user's browser and displayed graphically. Then need to record such information in the database, as shown:
Use Open Flash Chart (OFC) production chart (Struts2 processing)
This is recorded inside the system 9 browser traffic, and now we want the page to display it using OFC, first, do Struts2 configuration. Code:
package xxx.app.action.chart;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import jofc2.model.Chart;
import jofc2.model.axis.Label;
import jofc2.model.axis.XAxis;
import jofc2.model.axis.YAxis;
import jofc2.model.elements.LineChart;
import neo.app.action.BaseAction;
public class OfcChartAction extends BaseAction {
        private Chart ofcChart;
        public Chart getOfcChart() {
                return ofcChart;
        }
        public String showChart() throws Exception{
                //y Axis data collection-use quantity  
                List<Number> dataSet = new ArrayList<Number>();
                //x Axis data collection-browser type  
                List<Label> xLabel = new ArrayList<Label>();
                // Gets the dataset needs to be displayed  
                List browserList = getServMgr().getVisitStatService().getBrowserStat();
                for (int i = 0; i < browserList.size(); i++) {
                        Map map = (Map) browserList.get(i);
                        // Fill the x axis  
                        dataSet.add((Integer) map.get("statCount"));
                        // Fill the y axis  
                        xLabel.add(new Label((String) map.get("statVar")));
                }
                // Set the contents of the x axis  
                XAxis labels = new XAxis();
                labels.addLabels(xLabel);
                // Set the Y axis shows the ranges  :Range The meaning of the third argument to  : Coordinate minimum value, maximum value and the step value  
                YAxis range = new YAxis();
                range.setRange(0, 200, 10);
                //OFC A line chart settings  
                LineChart lineChart = new LineChart(LineChart.Style.NORMAL);
                lineChart.addValues(dataSet);
                lineChart.setColour("#6666FF");
                lineChart.setText(" The number of users  ");
                // Chart settings  
                Chart chart = new Chart(" The user's browser usage distribution  ");
                chart.setXAxis(labels);
                chart.setYAxis(range);
                chart.addElements(lineChart);
                // Print the text of the JSON format  
                System.out.print(chart.toString());
                HttpServletResponse response = ServletActionContext.getResponse();
                response.setContentType("application/json-rpc;charset=utf-8");
                response.setHeader("Cache-Control", "no-cache");
                response.setHeader("Expires", "0");
                response.setHeader("Pragma", "No-cache");
                response.getWriter().write(chart.toString());
                return null;
        }
}

Service to obtain the data we use Spring's JdbcTemplate for.
              // Getting a browser information  
        private static final String SQL_GET_BROWER_STAT = "select statVar,statCount from nd_stats where statType='browser'";
        /**
         *  Getting a browser information  
         */
        public List getBrowserStat() {
                return jt.queryForList(SQL_GET_BROWER_STAT);
        }

Written Action, we simply configure it because we only get JSON text, so do not configure the jump, you can only configure the request.
          <action name="ofcChart">
                </action>

Page of the JS code is simple. As you can:
<script type="text/javascript" src="${base}/js/swfobject.js"></script>
<script type="text/javascript">
        var flashvars = {"data-file":"${base}/manage/stat/ofcChart!showChart.action"};
        var params = {menu: "false",scale: "noScale",wmode:"opaque"};
        swfobject.embedSWF("${base}/swf/open-flash-chart.swf", "chart", "600px", "400px", "9.0.0",
        "expressInstall.swf",flashvars,params);
</script>
<div>
</div>

Here need to say a few words, first the path can not be wrong, wrong, not on the request data. Swfobject.js configure the page using JS Flash display, and more convenient for all to use it. Set up a div for the Flash display, the id is embedSWF the second parameter, can not forget to write. There is the request address parameters, here we are not passing parameters to facilitate testing, but the actual development of the inevitable passing parameters. Parameters in the request when the address back in the normal form of writing can be, but & the connectors need to escape, written% 26, the other on nothing.
The result is that we get this, it is very common line chart. But oh no difference JFreeChart.

At the same time we get jofc2 the console for our generated JSON text.
{"is_thousand_separator_disabled":0,"is_decimal_separator_comma":0,"title":{"tex
t":" The user's browser usage distribution  "},"y_axis":{"min":0,"steps":10,"max":200},"x_axis":{"la
bels":{"labels":[{"text":"Safari"},{"text":"MSIE6X"},{"text":"MSIE7X"},{"text":"
MSIE8X"},{"text":"Firefox"},{"text":"Chrome"},{"text":"Opera"},{"text":"Maxthon"
},{"text":"Other"}]}},"num_decimals":2,"is_fixed_num_decimals_forced":0,"element
s":[{"font-size":10,"text":" The number of users  ","type":"line","values":[6,2,63,168,1,10,
1,0,0],"colour":"#6666FF"}]}

Now we do histogram, to be a personal point, with sketch type.
//Sketch Set the type shape.  
SketchBarChart sketchChart = new SketchBarChart();
sketchChart.setColour("#6666FF");
sketchChart.addValues(dataSet);
sketchChart.setText(" The number of users  ");
// Chart settings  
Chart chart = new Chart(" The user's browser usage distribution  ");
chart.setXAxis(labels);
chart.setYAxis(range);
chart.addElements(sketchChart);

Thus we get the following display is not very cartoon, haha.

Note that the sketch-style histogram jofc2 written a separate class, but from time to time using the general style of the histogram so conducted.
// A collection of the bar chart Bar  
List<Bar> barList=new ArrayList<Bar>();
for (int i = 0; i < browserList.size(); i++) {
        Map map = (Map) browserList.get(i);
        // Fill the Bar, and give tips  
        barList.add(new Bar((Integer) map.get("statCount")).setTooltip("#val# Time  "));
        // Fill the y axis  
        xLabel.add(new Label((String) map.get("statVar")));
}
// The bar chart settings  
BarChart barChart=new BarChart(BarChart.Style.GLASS);
barChart.addBars(barList);
barChart.setColour("#6666FF");
barChart.setText(" The number of users  ");
// Chart settings  
Chart chart = new Chart(" The user's browser usage distribution  ");
chart.setXAxis(labels);
chart.setYAxis(range);
chart.addElements(barChart);

We get the following histogram.

The last is the pie chart, let's Huabing.
// A pie chart  
PieChart pieChart = new PieChart();
for (int i = 0; i < browserList.size(); i++) {
        Map map = (Map) browserList.get(i);
        // Fill the x axis  
        dataSet.add((Integer) map.get("statCount"));
        // Add a piece of pie  
        pieChart.addSlice((Integer) map.get("statCount"), (String) map.get("statVar"));
}
// A pie chart settings  
pieChart.setColours("#d01f3c", "#356aa0", "#C79810");
pieChart.setText(" The number of users  ");
pieChart.setRadius(180); // The radius of the pie chart  
pieChart.setTooltip("#val# / #total#<br>#percent#");
// Chart settings  
Chart chart = new Chart(" The user's browser usage distribution  ");
chart.setXAxis(labels);
chart.setYAxis(range);
chart.addElements(pieChart);

Generated pie chart as follows:

OFC receives a JSON formatted text to fill Flash, Flash parsing this text to generate a pretty chart. Of course, there are many OFC set, you can make a very stunning effect, where no in-depth study, of course, set some properties Bale. Finally we look at the JFreeChart of it.

Hope be useful to users, welcome to explore more in-depth exchange of content.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Use Open Flash Chart (OFC) production chart (Struts2 processing)

  • Eclipse to run using the specified JVM m2eclipse plugin can not find tools.jar

    Used the m2eclipse plug-ins using struts2 when com.sun necessary to rely on the default-tools.jar, specifically because at the struts-annotations bag designated default-tools.jar <profile> <id> default-tools.jar </ id> <activatio ...

  • J2EEer Learn Ruby

    Ruby has long heard of the development of efficient and accurate is to use Ruby on Rails to develop WEB applications very convenient. While J2EE has a lot of very good Opensource framework, automatic code generation tools, can greatly enhance the developm

  • Struts + Spring + Hibernate practice

    Tools: Eclipse3.1, MyEclipse4.03, Tomcat5.5.9, Properties Editor plug-ins, MySql4.1.13 New construction: the name for the login Create Struts framework Create index.jsp, add a link to login.jsp Press Ctrl + N, to create login.jsp, LoginAction, the use of

  • Pring Beanfactory at the jsp, servlet, web.xml and other configuration

    Keywords: spring beanfactory Spring in the web application called the Beanfactory 1) Configure web.xml Java code <? xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE web-app PUBLIC "- / / Sun Microsystems, Inc. / / ...

  • jBPM Development Getting Started Guide

    Although the workflow is still immature stage of development, not even a recognized standard. But its application has already been launched in the Express, indicating the market's demand for job-flow framework are urgent and enormous. Backgrounds of o

  • Openfire Plugin Developer's Guide

    Introduction Openfire features plug-ins are enhanced. This document is a guide to developers to create plug-ins. The structure of a plug-in Plug-ins Plug-ins openfireHome stored in the directory. When deploying a plug-in jar or war file, it will automatic

  • JS charts extjs implementation tools can be combined

    OpenFlashChart brief introduction by valensoft 2009-1-10 8:29:00 OpenFlashChart is an open source Flash and Javascript to the technical foundation for the free chart, using it to create some very useful analysis of the effect of the statements of the char

  • Hibernate Inteceptor

    The end of the project stage, the client suddenly put forward a very troublesome but normal demand, the system records all changes must be carried out. Formats such as: 2004.1.1 12:30 Ikuya wind orders Sales Order Date 2004.1.2-> 2004.1.3 The firs ...

  • FLEX: integrating Spring + Hibernate

    Before a friend also wanted to study development of FLEX. Asked me to help him to be a small sample. Spent a weekend time, to integrate a sampleproject. Client: FLEX Server: Spring2.5 + Hibernate3.2 + Hibernate-annotations3.3.1 + MySQL5 FDS: BlazeDS3 IDE:

  • The level Hibernate cache

    Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries