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.
Use Open Flash Chart (OFC) production chart (Struts2 processing)

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.
Use Open Flash Chart (OFC) production chart (Struts2 processing)

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.
Use Open Flash Chart (OFC) production chart (Struts2 processing)

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:
Use Open Flash Chart (OFC) production chart (Struts2 processing)

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.
Use Open Flash Chart (OFC) production chart (Struts2 processing)

Hope be useful to users, welcome to explore more in-depth exchange of content.

分类:Java 时间:2010-06-06 人气:432
分享到:
blog comments powered by Disqus

相关文章

  • Create a pie chart using JFreeChart 2010-07-02

    package com.cs.jfreechart; import java.awt.Color; import java.awt.Font; import java.io.FileOutputStream; import java.io.IOException; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import

  • Create 3D pie chart using JFreeChart 2010-07-02

    package com.cs.jfreechart; import java.awt.Color; import java.awt.Font; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; imp

  • 3D pie chart to see written by someone else, to imitate a 2011-01-01

    Mouse click pie when clicked will move out of the part. package test; import java.awt.Color; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.Toolkit; import java.awt.e

  • sitemap generation tool 2010-08-01

    sitemap for your site better in search engine rankings, how it generates sitemap? In wordpress and some good open source web applications have corresponding plug-ins, but most other sites do not specific to this feature! So as a webmaster or site adm

  • Share down my code generation tool (uploaded document) 2010-03-29

    Begun to taste the ROR, impressive is that he who created automatically control and the CRUD files, you can think of so doing JAVA With this in mind on the practice, and an interface with the swing, combined with the effect of eclipse, based on sprin

  • CodeSmith code generation tool based on template 2010-07-29

    CodeSmith CodeSmith is a template-based code generation tool that uses syntax similar to ASP.NET to generate any type of code or text. And many other different code generation tools, CodeSmith does not require you to subscribe to a particular applica

  • kaptcha simple validation code generation tool 2010-09-11

    kaptcha is a very practical verification code generation tool, with it, you can generate a variety of styles verification code as it is configurable. kaptcha work principle is called com.google.code.kaptcha.servlet.KaptchaServlet, generate an image.

  • Code generation tool lombok 2010-03-29

    Accidentally discovered a code generation tool to lombok, try a small feeling pretty good! lombok The main features are: automatic generation of default getter / setter methods, automated resource management (through @ Cleanup annotation), and annota

  • Automatic code generation tool that steps 2010-03-02

    E-mail to colleagues back, note it here as a data. We have done before a code generation, then the absence of such use hibernate Mapping tool is their own writing, and thinking is this: 1, with powerdesigner generate construction schedules, and so th

  • Use of mobile software. Net code generation tool automatically steps 2010-05-13

    Using dynamic soft. Net code generation tools (V2.4.1), after the establishment of a good database hygiene quick way their code works. First of all, create their own database connection to connect the database with mobile software. According to need

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 1.015 (s). 12 q(s)