To dynamically generate an image, you must first convert the record to the format required for image processing.
CopyCode The Code is as follows: Package emtit. utils
{
Import MX. charts. categoryaxis;
Import MX. charts. Series. columnseries;
Import MX. charts. Series. pieseries;
Import MX. Collections. arraycollection;
/**
* Author: Huang Jianwen
* Data: 2008-12-03
* Graphic Data Conversion
*/
Public class chartdatatransform
{
Public Function chartdatatransform ()
{
}
/**
* Advanced chart analysis generation: converts the data in the datagride to the data suitable for image display.
* The returned data format is as follows:
* [{Keyword1: 'zhang san', keyword1: '10', keyword3: 11}, {keyword1: 'Li si', 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; I <sourcedata. length; I ++ ){
For (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; j <yfield. length; j ++ ){
If (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 ('put this field '+ yfield [J] ['data'] + 'value' + sourcedata [I] [field] +' into an array on the Y axis ');
}
}
}
}
For each (VAR num: * In returnobj ){
Returnarrc. additem (Num );
For (VAR field2: String in num ){
Trace (Num [xaxis] + 'field of the graph to be output:' + field2 );
}
}
Return returnarrc;
}
/**
* Set the column chart categoryfield
*/
Public static function setcategoryaxis (categoryfield: string): categoryaxis {
VaR CA: categoryaxis = new categoryaxis;
CA. categoryfield = categoryfield;
Return CA;
}
/**
* Set the column data corresponding to the X axis of the column.
* The XY array must be xy = array ({lable: 'fractional ', data: 'keyword2'}, {lable: 'count only ', data: 'keyword3 '})
* Displayname must correspond to the value of setcategoryaxis.
*/
Public static function setcolumnseries (yfieldarr: array, xfield: string): Array {
VaR Cs: columnseries;
VaR rsarr: array = new array;
For (var I: Int = 0; I <yfieldarr. length; I ++ ){
Cs = new columnseries;
CS. displayname = yfieldarr [I] ['lable'];
CS. xfield = xfield;
CS. yfield = yfieldarr [I] ['data'];
Rsarr. Push (CS );
Trace ('column '+ I +' attributes, displayname: '+ CS. displayname + ', xfield:' + CS. xfield + ', yfield:' + CS. yfield );
}
Return rsarr;
}
}
}
The code in the view is as follows:Copy codeThe Code is as follows: <? XML version = "1.0" encoding = "UTF-8"?>
<Mx: titlewindow xmlns: MX = "http://www.adobe.com/2006/mxml" creationcomplete = "Init () "layout =" absolute "width =" 468 "Height =" 514 "showclosebutton =" true "Close =" popupmanager. removepopup (this) ">
<Mx: SCRIPT>
<! [CDATA [
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 {
// The following three statements are data conversion.
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; I <ch_column.series.length; I ++ ){
Trace ('Id: '+ ch_column.series [I] ['id'] +', displayname: '+ ch_column.series [I] ['displayname'] +', yfield: '+ ch_column.series [I] ['yfield'] + ', xfield:' + ch_column.series [I] ['xfield ']);
}
}
]>
</MX: SCRIPT>
<Mx: viewstack x = "0" Y = "0" id = "vs_chart" width = "100%" Height = "100%">
<Mx: canvas id = "cv_column" label = "Bar Chart" width = "100%" Height = "100%">
<Mx: columnchart id = "ch_column" showdatatips = "true" x = "24" Y = "61" width = "399" Height = "336">
</MX: columnchart>
<Mx: Legend dataprovider = "{ch_column}"/>
</MX: canvas>
<Mx: canvas id = "cv_pie" label = "Pie Chart" width = "100%" Height = "100%">
<Mx: piechart id = "ch_pie" showdatatips = "true" dataprovider = "{mainmodel. chartdata} "x =" 38 "Y =" 48 "Height =" 394 "width =" 389 ">
<Mx: Series>
<Mx: pieseries namefield = "xaxis" field = "value"/>
</MX: Series>
</MX: piechart>
<Mx: Legend dataprovider = "{ch_pie}"/>
</MX: canvas>
</MX: viewstack>
</MX: titlewindow>