FLEX如何将combobox下拉列表所选的值给columnchart 50
<?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="955" minHeight="600"
creationComplete="init()">
<fx: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]);
}
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 定义颜色 -->
<mx:SolidColor id="sc1" color="yellow" alpha=".8"/>
<mx:SolidColor id="sc2" color="0xCCCCCC" alpha=".6"/>
<mx:SolidColor id="sc3" color="0xFFCC66" alpha=".6"/>
<!-- 定义颜色 -->
<s:SolidColorStroke id="s1" color="yellow" weight="2"/>
<s:SolidColorStroke id="s2" color="0xCCCCCC" weight="2"/>
<s:SolidColorStroke id="s3" color="0xFFCC66" weight="2"/>
</fx:Declarations>
<s:VGroup width="100%" height="100%" gap="100" top="20" y="20">
<mx:DataGrid dragEnabled="true" dataProvider="{medalsAC}" 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>
<!--Column chart设置成能解析Country: "Russia", Gold: 27, Silver:27, Bronze: 38这样的数据项-->
<mx:ColumnChart id="column" contextMenu="{menu}" height="202" width="402" showDataTips="true" dataProvider="{chartData}">
<!--设置水平轴-->
<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>
</s:VGroup>
</s:Application>
一个可以从datagrid拖拽到chart的例子。用combox也只是切换事件而已,希望对你有启发
要是能给我个combobox就好了,而且,这还是静态的
动态的只是你DATAGRID读数据是动态的。你选中的selectItem还是和这个原理一样,现成的都给弄好,不利于学习