/**
* ==========初始化LineChart===========
* @param app 是主程序入口:dynamicStackedChart
* @param id 设置id:String
* @param tipStr 用户自定义的笔触提示信息:String
* @param xml 数据集:String
* @param title 竖向标题:String
* @param vtitle 横向标题:String
* @param x x轴显示信息:String
* */
public function createLinkChart(app:dynamicStackedChart,id:String,tipStr:String,xml:String,title:String,vtitle:String,x:String):void{
/**初始化LineChart*/
lineChartTip=tipStr;
var lineChart:LineChart=new LineChart();
lineChart.id=id;
lineChart.percentWidth=100;
lineChart.setStyle("textAlign","center");
lineChart.dataTipFunction=fxLineChartTips;
lineChart.showDataTips=true;
lineChart.height=250;
/**
* 点击Item事件
* */
lineChart.addEventListener(ChartItemEvent.ITEM_CLICK,
function chartClick(evt:ChartItemEvent):void{lineChartItemClick(evt,app,tipStr,lineChart);});
var lineAxis:LinearAxis=new LinearAxis();
lineChart.verticalAxis=lineAxis;
var lineVTitle:CategoryAxis=new CategoryAxis();
lineChart.horizontalAxis=lineVTitle;
/**设置标题和设置Legend显示数据*/
lineAxis.title=title;
lineVTitle.title=vtitle;
lineVTitle.categoryField="@"+x; //Legend
/**LineChart设置背景网格*/
lineChartbackgroundElements(lineChart);
lineChartLoadData(lineChart,xml,title,vtitle,x);
/**LineChart的Legend 数据*/
var lineLegend:Legend=new Legend();
lineLegend.dataProvider=lineChart;
lineLegend.height=28;
app.myBox.addChild(lineChart);
app.myBox.addChild(lineLegend);
}
/**LineChart 设置背景网格*/
private function lineChartbackgroundElements(lineChart:LineChart):void{
// var gl:GridLines=new GridLines();
// gl.setStyle("horizontalTickAligned",true);
// gl.setStyle("verticalTickAligned",true);
//
// var hr:HorizontalList;
// lineChart.backgroundElements=[hr];
}
/**
* LineChart 加载数据
* 2010年6月17日13:37
* 页面加载LineChart数据
* @param app 主程序入口类型stackedChart
* @param xml 存放数据源 String
* @param title 竖向标题 String
* @param vtitle 横向标题 String
* @param x x轴显示数据 String
* */
private function lineChartLoadData(lineChart:LineChart,xml:String,title:String,vtitle:String,x:String):void{
var xmls:XML=new XML(xml);
// 绑定数据
lineChart.dataProvider=xmls.children();
// 动态添加lineSeries
var xmlName:XMLList=xmls.children() as XMLList;
// 获取XML中元素名称
var arr:Array=new Array();
for(var a:int=1;a<xmlName[0].attributes().length();a++)
{
arr.push(xmlName.attributes()[a].name().toString().replace(/^\s*|\s*$/g,"").split(" ").join(""));
}
var arrSeries:Array=new Array();
var colorArr:Array=getRandomColor(arr.length);
for(var i:Number=0;i<arr.length;i++)
{
var lineSeries:LineSeries = new LineSeries();
lineSeries.id = i as String;
lineSeries.yField="@"+arr[i];
lineSeries.displayName=arr[i];
var fa2:Fade=new Fade();
fa2.duration=2000;
fa2.alphaFrom=0.0;
fa2.alphaTo=1.0;
lineSeries.setStyle("showDataEffect",fa2);
lineSeries.filters=[_DropShadowFilter];
lineSeries.setStyle("itemRenderer",new ClassFactory(CircleItemRenderer));
var storkt:SolidColorStroke=new SolidColorStroke();
//随机生成颜色colorArr[i]
storkt.color=colorArr[i];
storkt.weight=1;
lineSeries.setStyle("lineStroke",storkt);
arrSeries.push(lineSeries);
}
lineChart.series = arrSeries;
}
/**
* 2010年6月17日13:37
* LineChart提示信息
* @param hitdata 存放鼠标笔触点显示数据 HitData
* */
private function fxLineChartTips(hitdata:HitData):String{
var show:String=new String("");
var curObj:Object=hitdata.item;
var xml:XML=new XML(curObj);
var curSeries:LineSeries = LineSeries(hitdata.chartItem.element); // 获得当前的BarSeries
/**提示信息*/
var arrs:Array=(lineChartTip.split(";"));
for(var a:int=0;a<arrs.length;a++){
var arr1:Array=arrs[a].toString().split("=");
if(a==0)
{
show=show+arr1[1]+":"+xml.attribute(arr1[0])+"\n";
}else if(a!=arrs.length-1){
if(arr1[0]==curSeries.displayName.toString())
{
show=show+arr1[1]+":"+arr1[0]+"\n";
}
}else{
show=show+arr1[1]+":"+xml.attribute(curSeries.displayName).toString();
}
}
return show;
}
/**
* 2010年6月17日13:37
* 页面加载ColumnChart数据
* @param app 主程序入口类型stackedChart
* @param xml 数据源 String
* @param title 竖向标题 String
* @param vtitle 横向标题 String
* @param x x轴显示数据 String
*
* */
public function fxColumnChartLoader(app:dynamicStackedChart,colChart:ColumnChart,xml:String,title:String,vtitle:String,x:String):void{
/**字符创格式XML转换成XML格式*/
var xmlData:XML=new XML(xml);
/**XML格式转换成XMLList*/
var xmlName:XMLList=xmlData.children() as XMLList;
/**
* 获取XML中每条数据的元素<day dval='12日' val="23" val2="33"/>
* 格式必须是这个格式,第一个元素必须是描述字符串
* 从第二个开始为要显示的数据
* 下面是从第一个元素也就是排除第一个元素
* dval,而是从第二个元素val
* 开始获取要显示的数据必须是数字
* */
var arr:Array=new Array();
for(var a:int=0;a<xmlName[0].attributes().length();a++)
{
arr.push(xmlName.attributes()[a].name().toString().replace(/^\s*|\s*$/g,"").split(" ").join(""));
}
/**
* 动态添加ColumnSeries(柱子)
* */
var arrcSeries:Array=new Array();
for(var f:Number=1;f<arr.length;f++)
{
var columnSeries:ColumnSeries = new ColumnSeries();
/**设定id*/
columnSeries.id = f as String;
/**设定x轴显示数据*/
columnSeries.xField="@"+x;
/**设定y轴显示数据*/
columnSeries.yField="@"+arr[f];
/**设置每个column展现效果*/
columnSeries.setStyle("showDataEffect",app.upColumn2);
/**
* 设置columnSeries的显示名称(displayName)
* */
columnSeries.displayName=arr[f];
arrcSeries.push(columnSeries);
/**设置提示信息显示样式*/
columnSeries.setStyle("labelPosition","outside");
}
/**将ColumnSeries添加到ColumnChart的Series集合中去*/
colChart.series=arrcSeries;
/**
* 渲染加载ColumnChart
* 固定格式的xml格式
**/
colChart.dataProvider=xmlData.children();
}
/**
* 2010年6月17日13:37
* ColumnChart提示信息
* @param hitdata 存放鼠标笔触点显示数据 HitData
* */
public function fxColumnChartShowDataTip(hitdata:HitData):String{
var show:String=new String("");
/**获取提示光点的所有信息*/
var curObj:Object=hitdata.item;
/**将获取的数据转换成xml*/
var xml:XML=new XML(curObj);
/**获取提示列的对象*/
var curSeries:ColumnSeries = ColumnSeries(hitdata.chartItem.element); // 获得当前的ColumnSeries
/**用户自定义格式的提示信息*/
var arrs:Array=(columnChartTip.split(";"));
for(var a:int=0;a<arrs.length;a++){
var arr1:Array=arrs[a].toString().split("=");
if(a==0)
{
/**show存放用户自定义格式的提示信息的最终数据*/
show=show+arr1[1]+":"+xml.attribute(arr1[0])+"\n";
}else if(a!=arrs.length-1){
if(arr1[0]==curSeries.displayName.toString())
{
show=show+arr1[1]+":"+arr1[0]+"\n";
}
}else{
show=show+arr1[1]+":"+xml.attribute(curSeries.displayName).toString();
}
}
return show;
}
/**
* 初始化数据报表
* */
public function createAdvancedDataGrid(app:dynamicStackedChart,id:String,xml:String,headerText:String):void{
var advgrid:AdvancedDataGrid=new AdvancedDataGrid();
advgrid.id=id;
advgrid.designViewDataType="flat";
advgrid.setStyle("textAlign","center");
advgrid.percentWidth=100;
advgrid.height=250;
advgrid.addEventListener(AdvancedDataGridEvent.HEADER_RELEASE,advancedDataGridSort);
advgrid.addEventListener(MouseEvent.CLICK,function clickadvgrideClick():void{advgrideItemClick(advgrid,app);});
/**
* 加载数据
* */
fxAdvancedDataGridLoader(advgrid,xml,headerText);
app.myBox.addChild(advgrid);
}
/**
* ================================AdvancedDataGrid========================================
* 2010年6月17日13:37
* 页面加载AdvancedDataGrid数据
* @param app 主程序入口类型stackedChart
* @param xml 存放数据源 String
* @param headerText AdvancedDataGrid标题 String
* */
public function fxAdvancedDataGridLoader(advgrid:AdvancedDataGrid,xml:String,headerText:String):void{
var xmld:XML=new XML(xml);
var xmlName:XMLList=xmld.children() as XMLList;
// 获取XML中元素名称
var arr:Array=new Array();
for(var a:int=0;a<xmlName[0].attributes().length();a++)
{
arr.push(xmlName.attributes()[a].name().toString().replace(/^\s*|\s*$/g,"").split(" ").join(""));
}
advgrid.dataProvider=xmld.children();
var adcHeaderText:AdvancedDataGridColumnGroup=new AdvancedDataGridColumnGroup();
adcHeaderText.headerText=headerText;
var arrs:Array=new Array();
for(var i:int=0;i<arr.length;i++)
{
var adc:AdvancedDataGridColumn=new AdvancedDataGridColumn();
adc.dataField="@"+arr[i];
adc.headerText=arr[i];
arrs.push(adc);
}
adcHeaderText.children=arrs;
var arrHeader:Array=new Array();
arrHeader.push(adcHeaderText);
advgrid.groupedColumns=arrHeader;
}
/**
* 点击AdvancedDataGrid列头上的排序按钮
*/
public function advancedDataGridSort(evt:AdvancedDataGridEvent):void {
var column:AdvancedDataGridColumn = AdvancedDataGridColumn(evt.currentTarget.columns[evt.columnIndex]);
var columnName:String=column.dataField;
column.sortCompareFunction=function(obj1:Object,obj2:Object):int{
return superSort(obj1,obj2,columnName);
};
}
/**
* 根据不同的数据类型选择不同的排序方式 优先级是数字、日期、字符串
*/
private function superSort(obj1:Object,obj2:Object,columnName:String):int{
//验证数据类型 Numbe Date String
if(!isNaN(obj1[columnName])){
return number_sortCompareFunc(obj1,obj2,columnName);
}
if(strIsDate(obj1[columnName])){
var inputFormat:String=getInputFormat(obj1[columnName]);
return date_sortCompareFunc(obj1,obj2,columnName,inputFormat);
}
return string_sortCompareFunc(obj1,obj2,columnName);
}
/**
* 日期的排序
*/
private function date_sortCompareFunc(obj1:Object, obj2:Object,columnName:String,inputFormat:String):int {
var date1:Date =DateField.stringToDate(obj1[columnName],inputFormat);
var date2:Date =DateField.stringToDate(obj2[columnName],inputFormat);
return ObjectUtil.dateCompare(date1, date2);
}
/**
* 字符串的排序
*/
private function string_sortCompareFunc(obj1:Object, obj2:Object,columnName:String):int{
var str1:String=obj1[columnName];
var str2:String=obj2[columnName];
return ObjectUtil.stringCompare(str1, str2,false);
}
/**
* 数字的排序
*/
private function number_sortCompareFunc(obj1:Object, obj2:Object,columnName:String):int{
var num1:Number=Number(obj1[columnName]);
var num2:Number=Number(obj2[columnName]);
return ObjectUtil.numericCompare(num1,num2);
}
/**
* 验证一个字符串是否是日期格式
*/
private function strIsDate(str:String):Boolean{
var inputFormat:String=getInputFormat(str);
//如果找不到日期表示符号则直接返回false
if(inputFormat==null){
return false;
}
var date:Date=DateField.stringToDate(str,inputFormat);
//如果是日期date不为null
if(date!=null){
return true;
}else{
return false;
}
}
/**
* 得到日期的格式
* 判断的顺序为 2009-11-12 2009/11/12 2009.11.12
*/
private function getInputFormat(str:String):String{
if(str.indexOf("-")!=-1){
return "YYYY-MM-DD";
}
if(str.indexOf("/")!=-1){
return "YYYY/MM/DD";
}
if(str.indexOf(".")!=-1){
return "YYYY.MM.DD";
}
return null;
}
/**
* 随机产生指定数量颜色的方法
* 产生一个集合每个颜色不相同
* @param num 指定产生多少个颜色Number
* @return 指定数量的颜色集合Array(uint)
* */
private function getRandomColor(num:Number):Array{
var arrColor:Array=new Array();
arrColor.push(uint(Math.random()*0xffffff));
for(var i:Number=0;i<10000;i++){
/**标记是否产生元素与arrColor中元素系统,达到每次产生不同的元素*/
var sign:Boolean=false;
var q:uint=uint(Math.random()*0xffffff);
for(var j:Number=0;j<arrColor.length;j++){
if(arrColor[j]==q)
{
sign=true;
}
}
if(sign==false)
{
arrColor.push(q+10);
}
if(arrColor.length==num)
{
break;
}
}
return arrColor;
}
/**
* LineChart点击事件
* */
public function lineChartItemClick(evt:ChartItemEvent,app:dynamicStackedChart,tipStr:String,lineChart:LineChart):void{
var psi:LineSeriesItem= evt.hitData.chartItem as LineSeriesItem;
var tipArr:Array=tipStr.split(";");
var nameArr:Array=new Array();
for(var a:int=0;a<tipArr.length;a++){
nameArr[a]=((tipArr[a]).split("="))[0];
}
var xmlObj:XML=new XML(psi.item);
var str:String=new String("");
for(var b:int=0;b<nameArr.length;b++){
if(xmlObj.attribute(nameArr[b]).toString()!=""&&xmlObj.attribute(nameArr[b])!=null){
str+=nameArr[b]+"="+xmlObj.attribute(nameArr[b])+";";
}
}
app.jsItemClick("id="+lineChart.id+";"+str);
//Alert.show(str,"点击LineChart的Item");
}
/**
* PieChart点击事件
* */
public function pieChartItemClick(evt:ChartItemEvent,app:dynamicStackedChart,tipStr:String,pieChart:PieChart):void{
var psi:PieSeriesItem= evt.hitData.chartItem as PieSeriesItem;
var tipArr:Array=tipStr.split(";");
var nameArr:Array=new Array();
for(var a:int=0;a<tipArr.length;a++){
nameArr[a]=((tipArr[a]).split("="))[0];
}
var xmlObj:XML=new XML(psi.item);
var str:String=new String("");
for(var b:int=0;b<nameArr.length;b++){
if(xmlObj.attribute(nameArr[b]).toString()!=""&&xmlObj.attribute(nameArr[b])!=null){
str+=nameArr[b]+"="+xmlObj.attribute(nameArr[b])+";";
}
}
app.jsItemClick("id="+pieChart.id+";"+str);
// Alert.show(str,"点击LineChart的Item");
}
/**
* AreaChart点击事件
* */
public function areaChartItemClick(evt:ChartItemEvent,app:dynamicStackedChart,tipStr:String,areaChart:AreaChart):void{
var psi:AreaSeriesItem= evt.hitData.chartItem as AreaSeriesItem;
var tipArr:Array=tipStr.split(";");
var nameArr:Array=new Array();
for(var a:int=0;a<tipArr.length;a++){
nameArr[a]=((tipArr[a]).split("="))[0];
}
var xmlObj:XML=new XML(psi.item);
var str:String=new String("");
for(var b:int=0;b<nameArr.length;b++){
if(xmlObj.attribute(nameArr[b]).toString()!=""&&xmlObj.attribute(nameArr[b])!=null){
str+=nameArr[b]+"="+xmlObj.attribute(nameArr[b])+";";
}
}
app.jsItemClick("id="+areaChart.id+";"+str);
// Alert.show(str,"点击LineChart的Item");
}
/**
* ColumnChart点击事件
* */
public function columnChartItemClick(evt:ChartItemEvent,app:dynamicStackedChart,tipStr:String,colChart:ColumnChart):void{
var psi:ColumnSeriesItem= evt.hitData.chartItem as ColumnSeriesItem;
var tipArr:Array=tipStr.split(";");
var nameArr:Array=new Array();
for(var a:int=0;a<tipArr.length;a++){
nameArr[a]=((tipArr[a]).split("="))[0];
}
var xmlObj:XML=new XML(psi.item);
var str:String=new String("");
for(var b:int=0;b<nameArr.length;b++){
if(xmlObj.attribute(nameArr[b]).toString()!=""&&xmlObj.attribute(nameArr[b])!=null){
str+=nameArr[b]+"="+xmlObj.attribute(nameArr[b])+";";
}
}
app.jsItemClick("id="+colChart.id+";"+str);
// Alert.show(str,"点击LineChart的Item");
}
/**
* AdvancedDataGrid点击事件
* */
public function advgrideItemClick(advgride:AdvancedDataGrid,app:dynamicStackedChart):void{
var xmls:XML=advgride.selectedItem as XML;
var dataStr:String=new String("");
for(var a:int=0;a<xmls.attributes().length();a++)
{
dataStr+=xmls.attributes()[a].name()+"="+xmls.attribute(xmls.attributes()[a].name())+";";
}
app.jsItemClick("id="+advgride.id+";"+dataStr);
// Alert.show(dataStr,"advgrid");
}
}
}
转载于:https://blog.51cto.com/piscesonline/521342