通过Adobe Flex图表库中的ActionScript动态创建轴; Adobe Bug?

时间:2011-03-21 17:11:24

标签: flex actionscript dynamic axis charts

通过MXML创建多轴工作正常: http://livedocs.adobe.com/flex/3/html/help.html?content=charts_types_12.html

但是当我尝试动态创建水平和垂直轴时,我会得到额外的轴。我相信这是Adobe的bug。我怎么能解决这个问题?感谢。

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<s:Application 
    minHeight="600"
    minWidth="955"
    creationComplete="application1_creationCompleteHandler(event)"
    xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:mx="library://ns.adobe.com/flex/mx">
    <fx:Script>
        <![CDATA[
            import mx.charts.AxisRenderer;
            import mx.charts.LinearAxis;
            import mx.charts.series.ColumnSeries;
            import mx.charts.series.LineSeries;
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;

            [Bindable] public var SMITH:ArrayCollection = new ArrayCollection([{date:"22-Aug-05", close:41.87},
                                                                               {date:"23-Aug-05", close:45.74},
                                                                               {date:"24-Aug-05", close:42.77},
                                                                               {date:"25-Aug-05", close:48.06},]);

            [Bindable] public var DECKER:ArrayCollection = new ArrayCollection([{date:"22-Aug-05", close:157.59},
                                                                                {date:"23-Aug-05", close:160.3},
                                                                                {date:"24-Aug-05", close:150.71},
                                                                                {date:"25-Aug-05", close:156.88},]);

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                // VERTICAL AXIS
                var verticalAxis1:LinearAxis = new LinearAxis();
                var verticalAxis2:LinearAxis = new LinearAxis();

                var verticalAxisRenderer1:AxisRenderer = new AxisRenderer();
                var verticalAxisRenderer2:AxisRenderer = new AxisRenderer();

                verticalAxisRenderer1.axis = verticalAxis1;
                verticalAxisRenderer2.axis = verticalAxis2;

                // HORIZONTAL AXIS

                var horizontalAxis:LinearAxis = new LinearAxis();
                var horizontalAxisRenderer:AxisRenderer = new AxisRenderer();
                horizontalAxisRenderer.axis = horizontalAxis;
                horizontalAxisRenderer.placement = "bottom";

                // SERIES

                var newSeries:Array = new Array();

                var columnSeries:ColumnSeries = new ColumnSeries();
                columnSeries.dataProvider = SMITH;
                columnSeries.yField = "close";
                columnSeries.verticalAxis = verticalAxis1;
                columnSeries.displayName = "SMITH";
                newSeries.push(columnSeries);

                var lineSeries:LineSeries = new LineSeries();
                lineSeries.dataProvider = DECKER;
                lineSeries.yField = "close";
                lineSeries.verticalAxis = verticalAxis2;
                lineSeries.displayName = "DECKER";
                newSeries.push(lineSeries);

                // CHART

                myChart.verticalAxisRenderers = [verticalAxisRenderer1, verticalAxisRenderer2];
                myChart.horizontalAxisRenderers = [horizontalAxisRenderer];
                myChart.series = newSeries;
            }
        ]]>
    </fx:Script>

    <mx:Panel title="Column Chart With Multiple Axes">
        <mx:ColumnChart id="myChart"
            showDataTips="true"/>
        <mx:Legend dataProvider="{myChart}"/>
    </mx:Panel>
</s:Application>

4 个答案:

答案 0 :(得分:3)

好的,这就是诀窍。不要在MXML中创建图表。像在渲染器中一样在AS代码中创建它。该图表在其commitProperties()方法中添加了一个默认的AxisRenderer。因此,如果您在mxml中创建图表,它将在您在AS代码中添加您的轴渲染器之前执行提交(以及添加轴渲染器)。只需将图表创建移动到AS代码中,然后移出MXML:

var columnChart:ColumnChart = new ColumnChart(); 的addElement(的ColumnChart)

祝你好运!

答案 1 :(得分:0)

如果您仔细查看代码,您会注意到您正在添加两次轴渲染器。一旦进入数据系列,另一个进入图表本身。

答案 2 :(得分:0)

horizo​​ntalAxisRenderer.axis = horizo​​ntalAxis;

myChart.horizo​​ntalAxisRenderers = [horizo​​ntalAxisRenderer];

正如Jax所说,你要两次添加渲染器。尝试删除一个,并且verticalaxisrenderers也需要放置。

答案 3 :(得分:0)

我的2美分......

我尝试通过 nulling verticalAxisRenderer 并在 verticalAxisRenderers 中设置axisrenderers来解决此问题。这导致了一个问题,因为 verticalAxisRenderer setter在从其接受 axis obejct之前不检查 null 的值。 我安顿下来的代码。不优雅,但工作..

if(myChart.verticalAxisRenderer)
{
    myChart.verticalAxisRenderer  = verticalAxisRenderer2
    myChart.verticalAxisRenderers = [verticalAxisRenderer1];
}
else
{
    myChart.verticalAxisRenderers = [verticalAxisRenderer2,verticalAxisRenderer1];
}