flex编程动态生成图像

要动态生成图像必需先把记录转为图像处理所需的格式,以下是处理类
复制代码 代码如下:

package emtit.utils
{
import mx.charts.CategoryAxis;
import mx.charts.series.ColumnSeries;
import mx.charts.series.PieSeries;
import mx.collections.ArrayCollection;
/**
* author:黄建文
* data:2008-12-03
* 图形数据转换类
*/
public class ChartDataTransform
{
public function ChartDataTransform()
{
}
/**
*高级图表分析生成,把datagride的数据转换为适合图像显示的数据
* 返回数据格式如下
* [{keyword1:'张三',keyword1:'10',keyword3:11},{keyword1:'李四',keyword1:'10',keyword3:11}]
*
*/
public static function advanced(sourceData:Array,xaxis:String,yfield:Array):ArrayCollection{
var i:int,j:int;
var obj:Object;
var name:String,field:String;
var returnObj:Object = new Object();
var returnArrC:ArrayCollection=new ArrayCollection;
var nowname:*=null;
var isChange:Boolean=false;
sourceData.sortOn(xaxis);
for(i=0;ifor(field in sourceData[i]){
name=sourceData[i][xaxis];
if(name!=nowname){
returnObj[name]=new Object;
returnObj[name][xaxis]=sourceData[i][xaxis];
nowname=name;
}
for(j=0;jif(field==yfield[j]['data']){
if(returnObj[name][field]==undefined){
returnObj[name][field]=0;
}
returnObj[name][field]=parseInt(returnObj[name][field])+parseInt(sourceData[i][field]);
// trace('把这个字段'+yfield[j]['data']+'的值'+sourceData[i][field]+'放到Y轴的一个数组中');
}
}
}
}
for each(var num:* in returnObj){
returnArrC.addItem(num);
for(var field2:String in num){
trace(num[xaxis]+'要输出图的字段:'+field2);
}
}
return returnArrC;
}
/**
*设置柱图形categoryField
*/
public static function setCategoryAxis(categoryField:String):CategoryAxis{
var ca:CategoryAxis=new CategoryAxis;
ca.categoryField=categoryField;
return ca;
}
/**
* 设置柱形X轴对应柱状数据
* xy数组必需为xy=array({lable:'分数',data:'keyword2'},{lable:'只数',data:'keyword3'})
* displayName要与setCategoryAxis的值对应
*/
public static function setColumnSeries(yFieldArr:Array,xField:String):Array{
var cs:ColumnSeries;
var rsArr:Array=new Array;
for(var i:int=0;ics=new ColumnSeries;
cs.displayName=yFieldArr[i]['lable'];
cs.xField=xField;
cs.yField=yFieldArr[i]['data'];
rsArr.push(cs);
trace('第'+i+'条柱的属性,displayName:'+cs.displayName+',xField:'+cs.xField+',yField:'+cs.yField);
}
return rsArr;
}
}
}

在视图中代码如下
复制代码 代码如下:




import mx.automation.delegates.charts.ColumnSeriesAutomationImpl;
import mx.controls.Label;
import mx.charts.series.ColumnSeries;
import mx.charts.chartClasses.Series;
import mx.collections.ArrayCollection;
import yes3d.model.MainModel;
import mx.managers.PopUpManager;

private function init():void{
//下面这三句是数据转换
ch_column.dataProvider=MainModel.chartData;
ch_column.horizontalAxis=ChartDataTransform.setCategoryAxis('xaxis');
ch_column.series=ChartDataTransform.setColumnSeries([{lable:orderby,data:'value'}],'xaxis');
for(var i:int=0;itrace('id:'+ch_column.series[i]['id']+',displayName:'+ch_column.series[i]['displayName']+',yfield:'+ch_column.series[i]['yField']+',xfield:'+ch_column.series[i]['xField']);
}
}
]]>

















你可能感兴趣的:(flex编程动态生成图像)