怎样在java web中使用fusioncharts

 我来答
pieryon
2017-09-16 · 知道合伙人数码行家
pieryon
知道合伙人数码行家
采纳数:14410 获赞数:166868
获取软件设计师高级职称 万达金融最佳创新奖

向TA提问 私信TA
展开全部
一般是跟jsp配合使用的,这里有个完整的例子供参考:
<%@page import="javax.swing.JOptionPane"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@page import="java.util.*" %>
<%@page import="com.google.gson.*" %>

<%
String hostdb = "localhost:3306"; // MySQl 主机名
String userdb = "root"; // MySQL 用户名
String passdb = ""; // MySQL 密码
String namedb = "fusioncharts_jspsample"; // MySQL 数据库名
// 建立数据库连接
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://" + hostdb + "/" + namedb , userdb , passdb);
st = con.createStatement();
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Creating Charts with Data from a Database - fusioncharts.com</title>
<!-- Step 1: Include the `fusioncharts.js` file. This file is needed to
render the chart. Ensure that the path to this JS file is correct.
Otherwise, it may lead to JavaScript errors.
-->
<script src="fusioncharts.js"></script>
</head>
<body>
<div id="chart"></div>
<!-- Step 2: Include the `FusionCharts.java` file as a package in your
project.
-->
<%@page import="fusioncharts.FusionCharts" %>

<!-- Step 3:Include the package in the file where you want to show
FusionCharts as follows.

Step 4: Create a chart object using the FusionCharts JAVA class
constructor. Syntax for the constructor:
`FusionCharts("type of chart", "unique chart id", "width of chart",
"height of chart", "div id to render the chart",
"data format", "data source")`
-->
<%
/*
google-gson

Gson is a Java library that can be used to convert Java Objects into
their JSON representation. It can also be used to convert a JSON string to
an equivalent Java object. Gson can work with arbitrary Java objects including
pre-existing objects that you do not have source-code of.
link : https://github.com/google/gson
*/

Gson gson = new Gson();

// Form the SQL query that returns the top 10 most populous countries
// String sql="SELECT * FROM stackedbarchart";
rs = st.executeQuery("SELECT * FROM stackedbarchart");
// Execute the query.
// PreparedStatement pt=con.prepareStatement(sql);
// ResultSet result=pt.executeQuery();
System.out.println("Result of sql one"+rs);
// The 'chartobj' map object holds the chart attributes and data.
Map<String, String> chartobj = new HashMap<String, String>();//for getting key value pair

chartobj.put("caption", "Product-wise quarterly revenue in current year");
chartobj.put("subCaption", "Harrys SuperMart");
chartobj.put("captionFontSize", "14");
chartobj.put("subcaptionFontSize", "14");
chartobj.put("subcaptionFontBold", "0");
chartobj.put("paletteColors", "#0075c2,#1aaf5d");
chartobj.put("bgcolor", "#ffffff");
chartobj.put("showBorder", "0");
chartobj.put("showShadow", "0");
chartobj.put("showCanvasBorder", "0");
chartobj.put("valueFontColor","#ffffff");
chartobj.put("usePlotGradientColor", "0");
chartobj.put("legendBorderAlpha", "0");
chartobj.put("legendShadow", "0");
chartobj.put("showAxisLines", "0");
chartobj.put("showAlternateHGridColor", "0");
chartobj.put("divlineThickness", "1");
chartobj.put("divLineDashed", "1");
chartobj.put("divLineDashLen", "1");
chartobj.put("divLineGapLen", "1");
chartobj.put("xAxisName", "Quarter");
chartobj.put("yAxisName","Revenue(In USD)");
chartobj.put("showValues", "1");
chartobj.put("showHoverEffect","1");

//prepare categories
ArrayList categories = new ArrayList();
ArrayList dataset = new ArrayList();
int id1,id2,id3;
String a ="";
while (rs.next())
{
id1=rs.getInt(3);
id2=rs.getInt(4);
//id3=rs.getInt(2);
a=rs.getString(2);
System.out.println("Result of sql two1"+id1);
System.out.println("Result of sql two2"+id2);
System.out.println("Result of sql two3"+a);
//System.out.println("Result of sql two3"+id3);
}

categories.add(buildCategories("label",rs,gson));
System.out.println("Result of sql buildCategories"+buildCategories("label",rs,gson));

dataset.add(buildDataset("RamcoCount","ramco_imp", rs, gson));
dataset.add(buildDataset("PartnerCount", "partner_imp", rs, gson));
System.out.println("Result of sql buildDataset"+buildDataset("PartnerCount", "partner_imp", rs, gson));
// }System.out.println("Result of sql two"+categories);
//prepare dataset

System.out.println("Result of sql three"+dataset);
//close the connection.
rs.close();

//create 'dataMap' map object to make a complete FusionCharts datasource.
Map<String, String> dataMap = new LinkedHashMap<String, String>();
/*
gson.toJson() the data to retrieve the string containing the
JSON representation of the data in the array.
*/
dataMap.put("chart", gson.toJson(chartobj));
dataMap.put("categories", gson.toJson(categories));
dataMap.put("dataset", gson.toJson(dataset));

FusionCharts mslineChart= new FusionCharts(
"stackedbar2d",// chartType
"chart1",// chartId
"600","400",// chartWidth, chartHeight
"chart",// chartContainer
"json",// dataFormat
gson.toJson(dataMap) //dataSource
);
System.out.println(dataMap);

%>

<%!
/**
* @description - Build the Json for the categories
* @param {String} data_item - Name of the column from table
* @param {ResultSet} rs - The object of ResultSet maintains a
* cursor pointing to a particular row of data.

* @param {Gson} gson - Gson is a Java library that can be used
* to convert Java Objects into their JSON representation.
* @return {Map Object}
*/
public Map buildCategories(String data_item, ResultSet rs,Gson gson) {
//creation of the inner category
Map<String, String> categoryinner = new HashMap<String, String>();
ArrayList category = new ArrayList();
int counter = -1;
try {
//to restore the position of the result set.
rs.beforeFirst();
while(rs.next()) {
//for creating the key value for the category label from database.
Map<String, String> lv = new HashMap<String, String>();
lv.put("label", rs.getString(data_item));
category.add(lv);

counter ++;

}
categoryinner.put("category", gson.toJson(category));
System.out.println(categoryinner);
}catch(Exception ex) {/* if any error occurs */}

return categoryinner;

}

/**
* @description - Build the Json for datasets
* @param {String} seriesname - Lets you specify the series
* name for a particular dataset.
* @param {String} seriescolumnname - Name of the column from table
* @param {ResultSet} - The object of ResultSet maintains a
* cursor pointing to a particular row of data.
* @param {Gson} gson - Gson is a Java library that can be used
* to convert Java Objects into their JSON representation.
* @return {Map Object}
- */
public Map buildDataset(String seriesname, String seriescolumnname, ResultSet rs, Gson gson ) {

Map<String, String> datasetinner = new HashMap<String, String>();
datasetinner.put("seriesname", seriesname);

ArrayList makedata = new ArrayList();
try {
//is used to move the cursor to the first row in result set object.
rs.beforeFirst();
while(rs.next()) {
Map<String, String> preparedata = new HashMap<String, String>();
preparedata.put("value", rs.getString(seriescolumnname));
makedata.add(preparedata);
}
datasetinner.put("data", gson.toJson(makedata));

} catch(Exception err) {/* if any error occurs */}

return datasetinner;
}
%>

<!-- Step 5: 渲染chart-->

<%= mslineChart.render() %>

</body>
</html>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式