Posted on 2011-04-08 12:02
云云 閱讀(8095)
評論(5) 編輯 收藏
最近在做的這個項目需要用圖形報表展示數據給用戶
團隊使用amcharts來做圖形報表 界面很酷 操作簡單
需要寫的代碼也不多 只是一般在項目中用到的圖形報表都是動態的
所以需要動態生成data.xml 來變換數據
amcharts提供的圖形很豐富 去官網下載demo很有多simple
圖形都很炫的 好了廢話不多說
先從頁面入手 下面以餅圖為例
新建目錄images 將amcharts需要的圖片放進來
再建一目錄ampie將swf放進來ampie.swf 并將setting文件也放在此處 ampie_settings.xml
ampie_settings.xml的內容是設置圖形的效果
<?xml version="1.0" encoding="UTF-8" ?>
<settings>
<data_type>xml</data_type>
<pie>
<x>320</x>
<y>100</y>
<radius>100</radius>
<inner_radius>30</inner_radius>
<height>10</height>
<angle>35</angle>
<alpha>90</alpha>
</pie>
<animation>
<start_time>1</start_time>
<start_radius>100%</start_radius>
<start_effect>strong</start_effect>
<pull_out_time>1</pull_out_time>
</animation>
<data_labels>
<radius>20</radius>
<text_size>12</text_size>
<text_color>#000000</text_color>
<line_color>#000000</line_color>
<show><![CDATA[]]></show>
<hide_labels_percent>10</hide_labels_percent>
</data_labels>
<strings>
<no_data>沒有數據</no_data>
</strings>
<decimals_separator>.</decimals_separator>

<legend>
<enabled>true</enabled>
<x>20</x>
<y>20</y>
<width>350</width>
<max_columns>1</max_columns>
<color>#FFFFFF</color>
<text_color>#000000</text_color>
<text_size>12</text_size>
<spacing>2</spacing>
<margins></margins>
<align>left</align>
<key>
<size>16</size>
<border_color></border_color>
</key>
<values>
<enabled>true</enabled>
<width></width>

<text><![CDATA[
{percents}% (
{value})]]></text>
</values>
</legend>
</settings>

然后去頁面寫一個js腳本

function loadChart(chart)
{
var container = chart.one(".chart-control");
var chartType = container.getAttribute("chart-type") || 'amline';
var chartName = container.getAttribute("chart-name");
var chartHeight = container.getAttribute("chart-height");
var graphsAxis = container.getAttribute("graphs-axis");
var percentUnit = container.getAttribute("percent-unit"); // 坐標軸單位

var dataURI = Y.Reporting.dataURI;//action路徑 這個action稍后會寫出來
loadSWF(container.getAttribute("id"), chartType, chartName, chartHeight, dataURI,
chart.one("h3 span").get("innerHTML"), graphsAxis, percentUnit);
chart.addClass("loaded");
}

function loadSWF(containerID, chartType,chartName, chartHeight, dataURI, title, graphsAxis, percentUnit)
{
chartType = chartType || "amline";
var so;
var baseURI = Y.Reporting.baseURI;
var num=0;
var transids = document.getElementsByName("transId");

var s = "";

for (i = 0; i < transids.length; i++)
{

if (transids[i].checked)
{
num++;
s = s + transids[i].value + "!";
}
}

if (s.length > 0)
{
s = s.substring(0, s.length - 1);
}
var chartURI = baseURI + "/amchart/" + chartType + "/";
var mybaseUrl = "";

if (!isNaN(chartName))
{
var destType = Y.one("#J_consumeid");

if (destType && destType != null)
{
destType = destType.get("value");
}
mybaseUrl = baseURI
+ '/nolayout/report/amlineSettingsTotalRpt.htm?type='
+ chartName + '&graphsAxis=' + graphsAxis
+ '&percentUnit=' + percentUnit+'&transIds=' + s+'&destType='+destType;

} else
{

if (num > 1)
{
mybaseUrl = baseURI
+ '/nolayout/report/amlineSettingsMultiChart.htm?type='
+ chartName + '&num=' + num + '&transIds=' + s
+ '&graphsAxis=' + graphsAxis + '&percentUnit='
+ percentUnit

} else
{
mybaseUrl = baseURI
+ '/nolayout/report/amlineSettingsSingleChart.htm?type='
+ chartName + '&num=' + num + '&graphsAxis='
+ graphsAxis + '&percentUnit=' + percentUnit

}
}

var settingFileMap =
{
amline : mybaseUrl,
ampie: chartURI + 'ampie_settings.xml' //引入setting.xml文件 chartURI是一個變量
}
var settingFile = settingFileMap[chartType];
var swfURI = chartURI + chartType + '.swf';

if (!chartHeight)
{
chartHeight = 233;
}
so = new SWFObject(swfURI, chartType, "100%", chartHeight, "7", "");
so.addVariable("path", chartURI);
so.addVariable("settings_file", escape(settingFile));
so.addVariable("data_file", escape(dataURI));
so.addVariable("preloader_color", "#000000");
so.addParam("wmode", "transparent");
so.write(containerID); //containerID是頁面上的div id屬性
}
下面是action

protected void execute() throws WebxException
{
List<Members> list = reportAO.doFindAreaPvList(areaDO);
dataString = generatePieXmlString(list,queryDO);
PrintWriter writer = response.getWriter();
writer.write(dataString);//writeResult是一個流

}

/** *//**
* 返回地域餅圖xml數據
* @param list
* @param queryDO
* @return
*/

private String generatePieXmlString(List<Members> list, Members queryDO)
{

if (list == null || list.size() == 0)
{
return "<pie><slice>0</slice></pie>";
}
StringBuffer sb = new StringBuffer("<pie>");
long sumpv = 0;

for (int i = 0; i < list.size(); i++)
{
Members rDO = list.get(i);

if (i<7)
{
sb.append("<slice title='").append(rDO.getProvincename()!=null?rDO.getProvincename():"")
.append("'>").append(rDO.getSumpv()!=null?rDO.getSumpv():0).append("</slice>");

} else
{
sumpv += rDO.getSumpv()!=null?rDO.getSumpv():0;
}
}

if (list.size()>ConstantResource.DEFAULT_PIE_COUNT)
{
sb.append("<slice title='其它'>").append(sumpv).append("</slice>");
}
sb.append("</pie>");
return sb.toString();
}
下面是一些常用參數設置 :
XML中的標簽和屬性有:
<graph> 所具有的屬性
flash背景參數:
* bgColor=”HexColorCode” : 設置flash的背景顏色
* bgAlpha=”NumericalValue(0-100)” : 設置背景的透明度
* bgSWF=”Path of SWF File” : 設置一個外部的Flash 為flash的背景
圖表背景參數:
* canvasBgColor=”HexColorCode” : 設置圖表背景的顏色
* canvasBaseColor=”HexColorCode” : 設置圖表基部的顏色
* canvasBaseDepth=”Numerical Value” : 設置圖表基部的高度
* canvasBgDepth=”Numerical Value” : 設置圖表背景的深度
* showCanvasBg=”1/0″ : 設置是否顯示圖表背景
* showCanvasBase=”1/0″ : 設置是否顯示圖表基部
圖表和軸的標題
* caption=”String” : 圖表上方的標題
* subCaption=”String” : 圖表上方的副標題
* xAxisName= “String” : X軸的名字
* yAxisName= “String” : y軸的名字
圖表數量值的限制
* yAxisMinValue=”value”: y軸最小值
* yAxisMaxValue=”value”: y舟最大值
通用參數
* shownames=”1/0″ : 設置是否在x軸下顯示<set>里指定的name
* showValues=”1/0″ : 設置是否在柱型圖或餅型圖上顯示數據的值
* showLimits=”1/0″ : 設置是否在圖表的y軸坐標上顯示最大最小的數據值
* rotateNames=”1/0″ : 設置x軸下的name 是水平顯示還是垂直顯示
* animation=”1/0″ : 設置柱型圖的顯示是否是動畫顯示
字體屬性
* baseFont=”FontName” : 設置字體樣式
* baseFontSize=”FontSize” : 設置字體大小
* baseFontColor=”HexColorCode” : 設置字體顏色
* outCnvBaseFont = “FontName” : 設置圖表外側的字體樣式
* outCnvBaseFontSze=”FontSize” : 設置圖表外側的字體大小
* outCnvBaseFontColor=”HexColorCode”: 設置圖表外側的字體顏色
數字格式選項
* numberPrefix=”$” : 設置數據值的前綴
* numberSuffix=”p.a” : 設置數據值的后綴(如果是特殊字符,需要使用URL Encode重編碼)
* formatNumber=”1/0″ : 設置是否格式化數據
* formatNumberScale=”1/0″ : 設置是否用“K”來代表千,“M”來代表百萬
* decimalSeparator=”.” : 用指定的字符來代替小數點
* thousandSeparator=”,” : 用指定的字符來代替千位分隔符
* decimalPrecision=”2″ : 設置十進制的精度
* divLineDecimalPrecision=”2″: 設置y軸數值的小數位數
* limitsDecimalPrecision=”2″ : 設置y軸的最大最小值的小數位數
水平分隔線
* numdivlines=”NumericalValue” : 設置水平分隔線的數量
* divlinecolor=”HexColorCode” : 設置水平分隔線的顏色
* divLineThickness=”NumericalValue” : 設置水平分隔線的寬度
* divLineAlpha=”NumericalValue0-100″ : 設置水平分隔線的透明度
* showDivLineValue=”1/0″ : 設置是否顯示水平分隔線的數值
鼠標旋停參數
* showhovercap=”1/0″ : 顯示是否激活鼠標旋停效果
* hoverCapBgColor=”HexColorCode” : 設置鼠標旋停效果的背景顏色
* hoverCapBorderColor=”HexColorCode” : 設置鼠標旋停效果的邊框顏色
* hoverCapSepChar=”Char” : 設置鼠標旋停后顯示的文本中的分隔符號
圖表邊距的設置
* chartLeftMargin=”Numerical Value (in pixels)” : 設置圖表左邊距
* chartRightMargin=”Numerical Value (in pixels)” : 設置圖表右邊距
* chartTopMargin=”Numerical Value (in pixels)” : 設置圖表上邊距
* chartBottomMargin=”Numerical Value (in pixels)” : 設置圖表下邊距
Zero Plane
The zero plane is a 3D plane that signifies the 0 position on the chart. If there are no negative numbers on the chart, you won’t see a visible zero plane.
* zeroPlaneShowBorder=”1/0″ : Whether the border of a 3D zero plane would be plotted or not.
* zeroPlaneBorderColor=”Hex Code” : If the border is to be plotted, this attribute sets the border color for the plane.
* zeroPlaneColor=”Hex Code” : The intended color for the zero plane.
* zeroPlaneAlpha=”Numerical Value 0-100″ : The intended transparency for the zero plane.
<set> 所具有的屬性
* name=”string” : 設置在圖表中體現出來的名字
Example: <set name=’Jan’ …>
* value=”NumericalValue” : 設置在圖表中各個名字想對應的值
Example: <set name=’Jan’ value=’12345′ …>
* color=”HexCode” : 設置在圖表中相對應的柱行圖的顏色
Example: <set name=’Jan’ value=’12345′ color=’636363′ …>
* hoverText=”String value” : 設置鼠標旋停在相對應的柱行圖 上出現的文本內容
Example: <set name=’Jan’ value=’12345′ color=’636363′ hoverText=’January’…>
* link=”URL” : 設置該柱行圖的鏈接地址(需要URL Encode重編碼)
Example: <set … link=’ShowDetails.asp%3FMonth=Jan’ …>
* alpha=”Numerical Value 0-100″ : 設置在圖表中相對應的柱行圖的透明度
Example: <set … alpha=’100′ …>
* showName=”1″ : 設置在是否顯示圖表中相對應的柱行圖的name
Example : <set … showName=”1″ …>
第一次用amcharts 共同學習 共同進步
最近在淘寶上開了服裝店鋪 以批發價銷售 為了沖冠
朋友們有時間去逛逛啊 http://shop57153607.taobao.com/