需要在 Flex 4 中使用 AS3 创建数据网格和饼图
好吧,我知道这听起来不像是这样,但我曾经非常擅长 Flex 4 之前版本的 AS3。
我曾经能够使用 AS3 和零 MXML 创建任何东西。
使用 MXML 可以很好地工作,但是当我尝试在 AS3 中执行相同的操作时,我遇到了一系列问题。
下面是两者的代码:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="640"
minHeight="480"
width="100%" height="100%"
creationComplete="creationCompleteHandler();">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<mx:RemoteObject id="remotesvc" destination="ColdFusion" source="testCF-debug/cfc/myEmployees" showBusyCursor="true">
<mx:method name="getEmployees" result="getEmployeesResult(event)" fault="getEmployeesFail(event)"/>
</mx:RemoteObject>
<mx:RemoteObject id="remoteDepartment" destination="ColdFusion" source="testCF-debug/cfc/myDepartments" showBusyCursor="true">
<mx:method name="getDepartments" result="getDepartmentsResult(event)" fault="getDepartmentsFail(event)"/>
</mx:RemoteObject>
</fx:Declarations>
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
.headerCustomStyle
{
color: #ffffff;
backgroundColor: #6b6767;
}
</fx:Style>
<fx:Script>
<![CDATA[
import mx.charts.Legend;
import mx.charts.PieChart;
import mx.collections.ArrayCollection;
import mx.containers.HBox;
import mx.controls.Alert;
import mx.controls.DataGrid;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private var dgHeaderStyles:CSSStyleDeclaration;
[Bindable] private var myUsers:ArrayCollection;
public function init():void{
}
private function getEmployeesResult(event:ResultEvent):void {
employeesDg.dataProvider = event.result as ArrayCollection;
}
private function getEmployeesFail(event:FaultEvent):void {
Alert.show(event.toString());
}
private function getDepartmentsResult(event:ResultEvent):void {
departmentChart.dataProvider = event.result as ArrayCollection;
}
private function getDepartmentsFail(event:FaultEvent):void {
Alert.show(event.toString());
}
private function creationCompleteHandler():void{
remotesvc.getEmployees();
remoteDepartment.getDepartments();
}
]]>
</fx:Script>
<s:Panel title="Employees and Departments" id="empDept">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<mx:DataGrid id="employeesDg" x="10" y="88" headerStyleName="headerCustomStyle" chromeColor="{0x6b6767}">
<mx:columns>
<mx:DataGridColumn dataField="id" />
<mx:DataGridColumn dataField="firstname" />
<mx:DataGridColumn dataField="lastname" />
<mx:DataGridColumn dataField="title" />
<mx:DataGridColumn dataField="departmentid" />
<mx:DataGridColumn dataField="officephone" />
<mx:DataGridColumn dataField="cellphone" />
<mx:DataGridColumn dataField="email" />
<mx:DataGridColumn dataField="street" />
<mx:DataGridColumn dataField="city" />
<mx:DataGridColumn dataField="state" />
<mx:DataGridColumn dataField="zipcode" />
<mx:DataGridColumn dataField="office" />
<mx:DataGridColumn dataField="photofile">
<mx:itemRenderer>
<fx:Component>
<mx:Image height="20" />
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
<mx:HBox>
<mx:PieChart id="departmentChart" height="100%" width="100%">
<mx:series>
<mx:PieSeries field="budget" nameField="name" labelPosition="callout">
</mx:PieSeries>
</mx:series>
</mx:PieChart>
<mx:Legend dataProvider="{departmentChart}" />
</mx:HBox>
</s:Panel>
</s:Application>
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="640"
minHeight="480"
width="100%" height="100%"
creationComplete="creationCompleteHandler();">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<mx:RemoteObject id="remotesvc" destination="ColdFusion" source="testCF-debug/cfc/myEmployees" showBusyCursor="true">
<mx:method name="getEmployees" result="getEmployeesResult(event)" fault="getEmployeesFail(event)"/>
</mx:RemoteObject>
<mx:RemoteObject id="remoteDepartment" destination="ColdFusion" source="testCF-debug/cfc/myDepartments" showBusyCursor="true">
<mx:method name="getDepartments" result="getDepartmentsResult(event)" fault="getDepartmentsFail(event)"/>
</mx:RemoteObject>
</fx:Declarations>
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
.headerCustomStyle
{
color: #ffffff;
backgroundColor: #6b6767;
}
</fx:Style>
<fx:Script>
<![CDATA[
import flash.utils.flash_proxy;
import mx.charts.Legend;
import mx.charts.PieChart;
import mx.charts.chartClasses.Series;
import mx.charts.series.ColumnSeries;
import mx.charts.series.PieSeries;
import mx.collections.ArrayCollection;
import mx.containers.HBox;
import mx.controls.Alert;
import mx.controls.DataGrid;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private var employeesDg:DataGrid;
private var employeesCol1:DataGridColumn;
private var employeesCol2:DataGridColumn;
private var employeesCol3:DataGridColumn;
private var employeesCol4:DataGridColumn;
private var employeesCol5:DataGridColumn;
private var employeesCol6:DataGridColumn;
private var employeesCol7:DataGridColumn;
private var employeesCol8:DataGridColumn;
private var employeesCol9:DataGridColumn;
private var employeesCol10:DataGridColumn;
private var employeesCol11:DataGridColumn;
private var employeesCol12:DataGridColumn;
private var employeesCol13:DataGridColumn;
private var employeesCol14:DataGridColumn;
private var departmentChart:PieChart;
private var departmentChartPieSeries:PieSeries;
private var departmentChartSeries:Series;
private var departmentLegend:Legend;
private var departmentChartHbox:HBox;
private var dgHeaderStyles:CSSStyleDeclaration;
[Bindable] private var myUsers:ArrayCollection;
public function init():void{
departmentChartHbox = new HBox();
departmentChartHbox.id = "departmentChartHbox";
employeesDg = new DataGrid();
employeesDg.id = "employeesDg";
employeesDg.x = 10;
employeesDg.y = 88;
employeesDg.styleName = "";
// employeesDg.headerStyleName="headerCustomStyle";
employeesDg.chromeColor="{0x6b6767}";
employeesCol1 = new DataGridColumn;
employeesCol2 = new DataGridColumn;
employeesCol3 = new DataGridColumn;
employeesCol4 = new DataGridColumn;
employeesCol5 = new DataGridColumn;
employeesCol6 = new DataGridColumn;
employeesCol7 = new DataGridColumn;
employeesCol8 = new DataGridColumn;
employeesCol9 = new DataGridColumn;
employeesCol10 = new DataGridColumn;
employeesCol11 = new DataGridColumn;
employeesCol12 = new DataGridColumn;
employeesCol13 = new DataGridColumn;
employeesCol14 = new DataGridColumn;
employeesCol1.dataField = "id";
employeesCol2.dataField = "firstname";
employeesCol3.dataField = "lastname";
employeesCol4.dataField = "title";
employeesCol5.dataField = "departmentid";
employeesCol6.dataField = "officephone";
employeesCol7.dataField = "cellphone";
employeesCol8.dataField = "email";
employeesCol9.dataField = "street";
employeesCol10.dataField = "city";
employeesCol11.dataField = "state";
employeesCol12.dataField = "zipcode";
employeesCol13.dataField = "office";
employeesCol14.dataField = "photofile";
employeesCol14.itemRenderer;
employeesDg.addChild(employeesCol1);
employeesDg.addChild(employeesCol2);
employeesDg.addChild(employeesCol3);
employeesDg.addChild(employeesCol4);
employeesDg.addChild(employeesCol5);
employeesDg.addChild(employeesCol6);
employeesDg.addChild(employeesCol7);
employeesDg.addChild(employeesCol8);
employeesDg.addChild(employeesCol9);
employeesDg.addChild(employeesCol10);
employeesDg.addChild(employeesCol11);
employeesDg.addChild(employeesCol12);
employeesDg.addChild(employeesCol13);
employeesDg.addChild(employeesCol14);
departmentChartHbox.addChild(employeesDg);
departmentChart = new PieChart();
departmentChart.id = "departmentChart";
departmentChart.height = 320;
departmentChart.width = 240;
departmentChartSeries = new Series();
departmentChart.addChild(departmentChartSeries);
departmentChartPieSeries = new PieSeries();
departmentChartPieSeries.field = "budget";
departmentChartPieSeries.nameField = "name";
// departmentChartPieSeries.labelPosition = "callout";
departmentChartSeries.addChild(departmentChartPieSeries);
departmentLegend = new Legend();
departmentLegend.dataProvider = departmentChart;
departmentLegend.id = "departmentLegend";
departmentChartHbox.addChild(departmentChart);
departmentChartHbox.addChild(departmentLegend);
empDept.addChild(departmentChartHbox);
}
private function getEmployeesResult(event:ResultEvent):void {
employeesDg.dataProvider = event.result as ArrayCollection;
}
private function getEmployeesFail(event:FaultEvent):void {
Alert.show(event.toString());
}
private function getDepartmentsResult(event:ResultEvent):void {
departmentChart.dataProvider = event.result as ArrayCollection;
}
private function getDepartmentsFail(event:FaultEvent):void {
Alert.show(event.toString());
}
private function creationCompleteHandler():void{
remotesvc.getEmployees();
remoteDepartment.getDepartments();
}
]]>
</fx:Script>
<s:Panel title="Employees and Departments" id="empDept">
<s:layout>
<s:VerticalLayout/>
</s:layout>
</s:Panel>
</s:Application>
我不知道如何添加 itemrenderer 或如何导入 fx:Component 等...
我如何在 AS3 中做同样的事情?
如果您应该问“为什么?”,那么这是因为我想创建按钮,通过单击调用函数执行类似操作的按钮来更改 PieSeries field="budget" nameField="name" 属性的值:
myPieSeries.field="expectedCosts";
随后新数据会反映在饼图中。
提前致谢
OK, I know that this will not sound like it is so, but I used to be really good at AS3 in versions prior to Flex 4.
I used to be able to create anything with AS3 and zero MXML.
This works fine using MXML, but I have a series of issues when I try to do the same thing in AS3.
Below is the code for both:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="640"
minHeight="480"
width="100%" height="100%"
creationComplete="creationCompleteHandler();">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<mx:RemoteObject id="remotesvc" destination="ColdFusion" source="testCF-debug/cfc/myEmployees" showBusyCursor="true">
<mx:method name="getEmployees" result="getEmployeesResult(event)" fault="getEmployeesFail(event)"/>
</mx:RemoteObject>
<mx:RemoteObject id="remoteDepartment" destination="ColdFusion" source="testCF-debug/cfc/myDepartments" showBusyCursor="true">
<mx:method name="getDepartments" result="getDepartmentsResult(event)" fault="getDepartmentsFail(event)"/>
</mx:RemoteObject>
</fx:Declarations>
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
.headerCustomStyle
{
color: #ffffff;
backgroundColor: #6b6767;
}
</fx:Style>
<fx:Script>
<![CDATA[
import mx.charts.Legend;
import mx.charts.PieChart;
import mx.collections.ArrayCollection;
import mx.containers.HBox;
import mx.controls.Alert;
import mx.controls.DataGrid;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private var dgHeaderStyles:CSSStyleDeclaration;
[Bindable] private var myUsers:ArrayCollection;
public function init():void{
}
private function getEmployeesResult(event:ResultEvent):void {
employeesDg.dataProvider = event.result as ArrayCollection;
}
private function getEmployeesFail(event:FaultEvent):void {
Alert.show(event.toString());
}
private function getDepartmentsResult(event:ResultEvent):void {
departmentChart.dataProvider = event.result as ArrayCollection;
}
private function getDepartmentsFail(event:FaultEvent):void {
Alert.show(event.toString());
}
private function creationCompleteHandler():void{
remotesvc.getEmployees();
remoteDepartment.getDepartments();
}
]]>
</fx:Script>
<s:Panel title="Employees and Departments" id="empDept">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<mx:DataGrid id="employeesDg" x="10" y="88" headerStyleName="headerCustomStyle" chromeColor="{0x6b6767}">
<mx:columns>
<mx:DataGridColumn dataField="id" />
<mx:DataGridColumn dataField="firstname" />
<mx:DataGridColumn dataField="lastname" />
<mx:DataGridColumn dataField="title" />
<mx:DataGridColumn dataField="departmentid" />
<mx:DataGridColumn dataField="officephone" />
<mx:DataGridColumn dataField="cellphone" />
<mx:DataGridColumn dataField="email" />
<mx:DataGridColumn dataField="street" />
<mx:DataGridColumn dataField="city" />
<mx:DataGridColumn dataField="state" />
<mx:DataGridColumn dataField="zipcode" />
<mx:DataGridColumn dataField="office" />
<mx:DataGridColumn dataField="photofile">
<mx:itemRenderer>
<fx:Component>
<mx:Image height="20" />
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
<mx:HBox>
<mx:PieChart id="departmentChart" height="100%" width="100%">
<mx:series>
<mx:PieSeries field="budget" nameField="name" labelPosition="callout">
</mx:PieSeries>
</mx:series>
</mx:PieChart>
<mx:Legend dataProvider="{departmentChart}" />
</mx:HBox>
</s:Panel>
</s:Application>
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="640"
minHeight="480"
width="100%" height="100%"
creationComplete="creationCompleteHandler();">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<mx:RemoteObject id="remotesvc" destination="ColdFusion" source="testCF-debug/cfc/myEmployees" showBusyCursor="true">
<mx:method name="getEmployees" result="getEmployeesResult(event)" fault="getEmployeesFail(event)"/>
</mx:RemoteObject>
<mx:RemoteObject id="remoteDepartment" destination="ColdFusion" source="testCF-debug/cfc/myDepartments" showBusyCursor="true">
<mx:method name="getDepartments" result="getDepartmentsResult(event)" fault="getDepartmentsFail(event)"/>
</mx:RemoteObject>
</fx:Declarations>
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
.headerCustomStyle
{
color: #ffffff;
backgroundColor: #6b6767;
}
</fx:Style>
<fx:Script>
<![CDATA[
import flash.utils.flash_proxy;
import mx.charts.Legend;
import mx.charts.PieChart;
import mx.charts.chartClasses.Series;
import mx.charts.series.ColumnSeries;
import mx.charts.series.PieSeries;
import mx.collections.ArrayCollection;
import mx.containers.HBox;
import mx.controls.Alert;
import mx.controls.DataGrid;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private var employeesDg:DataGrid;
private var employeesCol1:DataGridColumn;
private var employeesCol2:DataGridColumn;
private var employeesCol3:DataGridColumn;
private var employeesCol4:DataGridColumn;
private var employeesCol5:DataGridColumn;
private var employeesCol6:DataGridColumn;
private var employeesCol7:DataGridColumn;
private var employeesCol8:DataGridColumn;
private var employeesCol9:DataGridColumn;
private var employeesCol10:DataGridColumn;
private var employeesCol11:DataGridColumn;
private var employeesCol12:DataGridColumn;
private var employeesCol13:DataGridColumn;
private var employeesCol14:DataGridColumn;
private var departmentChart:PieChart;
private var departmentChartPieSeries:PieSeries;
private var departmentChartSeries:Series;
private var departmentLegend:Legend;
private var departmentChartHbox:HBox;
private var dgHeaderStyles:CSSStyleDeclaration;
[Bindable] private var myUsers:ArrayCollection;
public function init():void{
departmentChartHbox = new HBox();
departmentChartHbox.id = "departmentChartHbox";
employeesDg = new DataGrid();
employeesDg.id = "employeesDg";
employeesDg.x = 10;
employeesDg.y = 88;
employeesDg.styleName = "";
// employeesDg.headerStyleName="headerCustomStyle";
employeesDg.chromeColor="{0x6b6767}";
employeesCol1 = new DataGridColumn;
employeesCol2 = new DataGridColumn;
employeesCol3 = new DataGridColumn;
employeesCol4 = new DataGridColumn;
employeesCol5 = new DataGridColumn;
employeesCol6 = new DataGridColumn;
employeesCol7 = new DataGridColumn;
employeesCol8 = new DataGridColumn;
employeesCol9 = new DataGridColumn;
employeesCol10 = new DataGridColumn;
employeesCol11 = new DataGridColumn;
employeesCol12 = new DataGridColumn;
employeesCol13 = new DataGridColumn;
employeesCol14 = new DataGridColumn;
employeesCol1.dataField = "id";
employeesCol2.dataField = "firstname";
employeesCol3.dataField = "lastname";
employeesCol4.dataField = "title";
employeesCol5.dataField = "departmentid";
employeesCol6.dataField = "officephone";
employeesCol7.dataField = "cellphone";
employeesCol8.dataField = "email";
employeesCol9.dataField = "street";
employeesCol10.dataField = "city";
employeesCol11.dataField = "state";
employeesCol12.dataField = "zipcode";
employeesCol13.dataField = "office";
employeesCol14.dataField = "photofile";
employeesCol14.itemRenderer;
employeesDg.addChild(employeesCol1);
employeesDg.addChild(employeesCol2);
employeesDg.addChild(employeesCol3);
employeesDg.addChild(employeesCol4);
employeesDg.addChild(employeesCol5);
employeesDg.addChild(employeesCol6);
employeesDg.addChild(employeesCol7);
employeesDg.addChild(employeesCol8);
employeesDg.addChild(employeesCol9);
employeesDg.addChild(employeesCol10);
employeesDg.addChild(employeesCol11);
employeesDg.addChild(employeesCol12);
employeesDg.addChild(employeesCol13);
employeesDg.addChild(employeesCol14);
departmentChartHbox.addChild(employeesDg);
departmentChart = new PieChart();
departmentChart.id = "departmentChart";
departmentChart.height = 320;
departmentChart.width = 240;
departmentChartSeries = new Series();
departmentChart.addChild(departmentChartSeries);
departmentChartPieSeries = new PieSeries();
departmentChartPieSeries.field = "budget";
departmentChartPieSeries.nameField = "name";
// departmentChartPieSeries.labelPosition = "callout";
departmentChartSeries.addChild(departmentChartPieSeries);
departmentLegend = new Legend();
departmentLegend.dataProvider = departmentChart;
departmentLegend.id = "departmentLegend";
departmentChartHbox.addChild(departmentChart);
departmentChartHbox.addChild(departmentLegend);
empDept.addChild(departmentChartHbox);
}
private function getEmployeesResult(event:ResultEvent):void {
employeesDg.dataProvider = event.result as ArrayCollection;
}
private function getEmployeesFail(event:FaultEvent):void {
Alert.show(event.toString());
}
private function getDepartmentsResult(event:ResultEvent):void {
departmentChart.dataProvider = event.result as ArrayCollection;
}
private function getDepartmentsFail(event:FaultEvent):void {
Alert.show(event.toString());
}
private function creationCompleteHandler():void{
remotesvc.getEmployees();
remoteDepartment.getDepartments();
}
]]>
</fx:Script>
<s:Panel title="Employees and Departments" id="empDept">
<s:layout>
<s:VerticalLayout/>
</s:layout>
</s:Panel>
</s:Application>
I can not figure out how to add an itemrenderer or how to import an fx:Component, etc...
How do I do the same thing in AS3?
In case you should ask "why?", then it is because I would like to create buttons that change the PieSeries field="budget" nameField="name" attribute's values by clicking on a button which calls a function to do something like this:
myPieSeries.field="expectedCosts";
Followed by the new data being reflected in the PieChart.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,一旦我弄清楚如何进行正确的导入,就没有错误了。
这是一个可以查看的链接:
在此处输入链接说明
OK, no errors once I figured out how to do the proper imports.
Here's a link to view:
enter link description here