如何动态更改Flex中PlotSeries中的yField

时间:2013-03-04 21:26:04

标签: flex

我在Flex中有一个带有PlotSeries的PlotChart。我的数据有不同的列作为参数。用户应该能够从Combobox中选择一个参数,然后PlotChart必须显示所选参数的图表。我开发的代码可以在第一次运行应用程序时创建图表。它显示了Combobox中第一个参数的图表。但是当用户更改参数时,图表不会显示数据。它仅显示带有X和Y轴的干净图表。以下是演示我如何开发代码的示例代码。

<mx:Canvas id="cp" width="100%" height="100%" alpha="1" creationComplete="initChart()">
<mx:PlotChart id="pChart" showDataTips="true" width="100%" height="85%">
    <mx:verticalAxis>
        <mx:LinearAxis id="pChartlinearAxis" baseAtZero="false" title="{parameterLabel}" minorInterval="0.5" interval="1.0" />
    </mx:verticalAxis>
    <mx:verticalAxisRenderers>
        <mx:AxisRenderer axis="{pChartlinearAxis}" verticalAxisTitleAlignment="vertical">
            <mx:axisStroke>
                <mx:SolidColorStroke weight="6" color="#BBCCDD" alpha="1" caps="square"/>
            </mx:axisStroke>                    
        </mx:AxisRenderer>
    </mx:verticalAxisRenderers>
    <mx:horizontalAxis>
        <mx:DateTimeAxis id="pChartca"  title="Date" dataUnits="days" dataInterval="1" labelUnits="days"/> 
    </mx:horizontalAxis>
    <mx:horizontalAxisRenderers>
        <mx:AxisRenderer axis="{pChartca}" canDropLabels="true" labelRotation="45">         
            <mx:axisStroke>
                <mx:SolidColorStroke weight="6" color="#BBCCDD" alpha="1" caps="square"/>
            </mx:axisStroke>                    
        </mx:AxisRenderer>              
    </mx:horizontalAxisRenderers>
    <mx:series>
        <mx:PlotSeries id="pSeries" />
    </mx:series>
</mx:PlotChart>
<mx:Legend id="mylgn" horizontalCenter="0" bottom="32"/>
<s:Label id="lblChart1" text="{parameterLabel}" horizontalCenter="0" bottom="20"/>

// The function for the first time that the Canvas is created.
protected function initChart():void{
    cmbParmNameList.selectedIndex = 0;
    function setPlotChart();
}

// The function for setting the chart for the first time when the Canvas is created or whenever cmbWQChartParmNameList is changed
protected function setPlotChart():void{
    var ac:ArrayCollection = new ArrayCollection();
    var chart:ChartBase;
    var seriesArray:Array;
    var obj:Object;

    pChart.visible = true;
    // Name of the chart is selected from the selected item in the combobox in which the parameters are listed
    parameterLabel = cmbParmNameList.selectedItem.Parameter; 
    // data is an ArryCollection is assigned as pChart's dataprovider
    for each(obj in data){ 
        ac.addItem(obj);
        pSeries.visible= true;
        // The selected parameter is considered as the yField. There is a column in "data" with the exact same name
        pSeries.yField = cmbParmNameList.selectedItem.Parameter; 
        // "Date" is a field name in "data"
        pSeries.xField = "Date"; 
        // StationID is the name of the station in which the "data" is collected
        pSeries.displayName = StationID.toString();
    }
    pSeries.dataProvider = null;
    pChart.dataProvider = null;
    pSeries.dataProvider = ac;
    pChart.dataProvider = pSeries.dataProvider;
    chart = (pChart as ChartBase);
    seriesArray = chart.series;
    chart.series = seriesArray;
    mylgn.dataProvider= chart;
    mylgn.validateNow();
    mylgn.direction= "horizontal";
    pChart.series = seriesArray;
    pChart.validateNow();
}

protected function cmbParmNameList_changeHandler(event:ListEvent):void
{
    // TODO Auto-generated method stub
    setPlotChart();
}

0 个答案:

没有答案