Flex3 DataGrid拖拽到ClumnChart动态显示图表实现代码 ClumnChart 拖拽 DataGrid Flex  第1张 Flex3 DataGrid拖拽到ClumnChart动态显示图表实现代码 ClumnChart 拖拽 DataGrid Flex  第2张

适用几行另外拖动,反复数据信息太重得加上,加上了数据图表鼠标右键菜单.
复制代码 编码以下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.controls.DataGrid;
import mx.managers.DragManager;
import mx.core.UIComponent;
import mx.collections.ArrayCollection;
import mx.events.DragEvent;
//DataGrid的数据库
[Bindable]
private var medalsAC:ArrayCollection = new ArrayCollection( [
{ Country: "英国", Gold: 35, Silver:39, Bronze: 29 },
{ Country: "我国", Gold: 99, Silver:17, Bronze: 14 },
{ Country: "日本国", Gold: 32, Silver:27, Bronze: 38 },
{ Country: "日本", Gold: 27, Silver:27, Bronze: 2 },
{ Country: "马来西亚", Gold: 55, Silver:27, Bronze: 63 },
{ Country: "北朝鲜", Gold: 11, Silver:21, Bronze: 16 },
{ Country: "新加坡", Gold: 7, Silver:14, Bronze: 77 },
{ Country: "澳大利亚", Gold: 0, Silver:12, Bronze: 11 }
]);
//ColumnChart的数据库, 默认设置为空
[Bindable]
private var chartData:ArrayCollection = new ArrayCollection();
[Bindable]
private var menu:ContextMenu = new ContextMenu();
//让columnChart监视拖动恶性事件
private function init():void{
column.addEventListener(DragEvent.DRAG_ENTER,dragEnterHandle);
column.addEventListener(DragEvent.DRAG_DROP,dragdropHandle);
//复位鼠标右键菜单
initMenu();
}
//复位chart鼠标右键菜单
private function initMenu():void
{
var clear:ContextMenuItem = new ContextMenuItem("清除数据图表");
menu.customItems.push(clear);
clear.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,clearAction);
}
//解决鼠标点击恶性事件
private function clearAction(event:ContextMenuEvent):void
{
this.chartData.removeAll();
}
//由于仅有DataGrid推拽,因此立即容许,假如页面上面有好几个控制监视拖动恶性事件,必须分辨后容许恰当的数据信息进到
private function dragEnterHandle(e:DragEvent):void{
DragManager.acceptDragDrop(e.currentTarget as UIComponent)
}
//拖动放宽后处理工艺
private function dragdropHandle(e:DragEvent):void{
//往column chart的dataprovider中加上拖动数据信息。
//假如只必须特殊的数据信息进到column chart,能够 先做数据筛选。
var datas: Array = (e.dragInitiator as DataGrid).selectedItems;
for(var i:int = 0; i < datas.length; i )
{
//不包含早已存有的数据信息再加上
if(!chartData.contains(datas[i]))
{
chartData.addItem(datas[i]);
}
}
}
]]>
</mx:Script>
<mx:DataGrid dragEnabled="true" dataProvider="{medalsAC}" x="192" y="52" allowMultipleSelection="true">
<mx:columns>
<mx:DataGridColumn dataField="Country" headerText="我国" />
<mx:DataGridColumn dataField="Gold" headerText="冠军"/>
<mx:DataGridColumn dataField="Silver" headerText="金牌"/>
<mx:DataGridColumn dataField="Bronze" headerText="奖牌"/>
</mx:columns>
</mx:DataGrid>
<!-- 界定色调 -->
<mx:SolidColor id="sc1" color="yellow" alpha=".8"/>
<mx:SolidColor id="sc2" color="0xCCCCCC" alpha=".6"/>
<mx:SolidColor id="sc3" color="0xFFCC66" alpha=".6"/>
<!-- 界定色调 -->
<mx:Stroke id="s1" color="yellow" weight="2"/>
<mx:Stroke id="s2" color="0xCCCCCC" weight="2"/>
<mx:Stroke id="s3" color="0xFFCC66" weight="2"/>
<!--Column chart设成能分析Country: "Russia", Gold: 27, Silver:27, Bronze: 38那样的数值数据-->
<mx:ColumnChart id="column" contextMenu="{menu}"
height="202"
width="402"
paddingLeft="5"
paddingRight="5"
showDataTips="true"
dataProvider="{chartData}"
x="192" y="215">
<!--设定水准轴-->
<mx:horizontalAxis>
<!--水准轴拖拽数据信息到chart后的文本显示信息-->
<mx:CategoryAxis categoryField="Country" />
</mx:horizontalAxis>
<!--设定柱头-->
<!--fill填充颜色,stroke外框色调-->
<mx:series>
<mx:ColumnSeries
xField="Country"
yField="Gold"
displayName="冠军"
fill="{sc1}"
stroke="{s1}"
/>
<mx:ColumnSeries
xField="Country"
yField="Silver"
displayName="金牌"
fill="{sc2}"
stroke="{s2}"
/>
<mx:ColumnSeries
xField="Country"
yField="Bronze"
displayName="奖牌"
fill="{sc3}"
stroke="{s3}"
/>
</mx:series>
</mx:ColumnChart>
</mx:Application>