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 人气:428
分享到:
blog comments powered by Disqus

相关文章

  • Decompile Java under the Eclipse plug-ins 2010-10-02

    [Size = x-small] [/ size] Eclipse plug-ins under the Java decompiler: Jode Decompiler Name: Jode Decompiler Summary: An Elipse the Java decompilation tools. Used to view the Jar in the Eclipse class files in the package source code. Installation: Ecl

  • How to achieve without plug-ins to WordPress code highlighting (plus color) 2010-12-15

    Station for a technology blog, where the code to show a lot. To beautify the code display, highlighting the WordPress code is the best way. Currently the network has highlighted many WordPress plugin code, but very bloated. For example, code highligh

  • 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

  • eclipse plug-in development 2008-12-31

    eclipse plug-in development process 1. First, you should download eclipse2.1.2 (not recommended eclipse3.0 learning) and its counterpart in the Chinese language pack 2. Should have to eclipse their own use and have a good understanding of the structu

  • Eclipse plug-in development of the Getting Started 2010-03-29

    Now the Internet has been a lot of Eclipse plug-in development see the introductory article, I wrote here, the main purpose of this paper is to my own experience and the beginning of learning to tell to everyone. Meanwhile, we wish to use the most si

  • Eclipse plug-ins download Address 2009-05-07

    1 EMF, GEF - Graphical Editor Framework, UML2, VE - Visual Editor download here http://www.eclipse.org/downloads/index.php 2 lomboz J2EE plug-ins, development JSP, EJB http://forge.objectweb.org/projects/lomboz 3 MyEclipse J2EE development of plug-in

  • Eclipse plug-ins download the relevant address 2009-05-15

    Eclipse Download EMF, GEF - Graphical Editor Framework, UML2, VE - Visual Editor download here http://www.eclipse.org/downloads/index.php 2 lomboz J2EE plug-ins, development JSP, EJB http://forge.objectweb.org/projects/lomboz 3 MyEclipse J2EE develop

  • Download eclipse plug-ins 2009-07-11

    velocity plug: You can download, http://sourceforge.net/projects/veloedit/ There are a: http://www.cnblogs.com/hw/articles/251148.html A powerful package jar download address, click on the registration can be downloaded quickly, address the www.jarba

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

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