【问题标题】:How to exclude series in legend (Flex)如何排除图例中的系列(Flex)
【发布时间】:2011-01-30 06:47:33
【问题描述】:

在弹性图表中, 我想绘制与特定系列相关的“参考线”之类的东西, 因此这些行不是独立的系列,不应在图例中显示。 是否可以从图表图例中排除某些系列? 谢谢!

【问题讨论】:

    标签: apache-flex legend


    【解决方案1】:

    可以从图表图例中排除某些系列。

    每个图表类(扩展 ChartBase)都有一个 legendData 数组属性。这个 legendData 有一个 LegendItem 的列表。如果您基于 legendData 创建一个 newArray,但只包含您想要的 LegendItem;然后您可以将该数组设置为您的图例的数据提供者。

    此外,您可以根据从头创建的 LegendItem 创建自己的 LegendItem 数组。并使用该数组作为图例的数据提供者。

    例如,这里我只显示我的图例中的第一个和第三个系列:

    <mx:Script>
        <![CDATA[
            private function cc(event:Event):void
            {
                var newArray:Array = new Array();
                newArray.push(myChart.legendData[0]);
                newArray.push(myChart.legendData[2]);
    
                myActionScriptLegend.dataProvider = newArray;
            }
        ]]>
    </mx:Script>
    
    <mx:ColumnChart id="myChart">
        <mx:series>
            <mx:ColumnSeries id="series0"/>
            <mx:ColumnSeries id="series1"/>
            <mx:ColumnSeries id="series2"/>
        </mx:series>
    </mx:ColumnChart>
    <mx:Legend dataProvider="{[myChart.legendData[0],myChart.legendData[2]]}" />
    <mx:Legend id="myActionScriptLegend" creationComplete="cc(event)" />
    

    http://livedocs.adobe.com/flex/3/langref/mx/charts/chartClasses/ChartBase.html#legendData
    http://livedocs.adobe.com/flex/3/langref/mx/charts/LegendItem.html
    http://livedocs.adobe.com/flex/3/html/charts_displayingdata_12.html#330954

    【讨论】:

    • 谢谢!它也可以在 actionscript 中使用吗?
    • 我提供了第二个图例 (id="myActionScriptLegend"),它在 ActionScript 中做同样的事情。希望对您有所帮助。
    • 感谢 Luis,它帮助我解决了耗费我大量时间的问题
    • 你如何动态地做到这一点?如何检查“myChart.legendData[2]”的空值?我需要排除空的legendData。我该如何检查呢? myChart.legendData[2].element.items.length 不起作用。有什么想法吗?
    【解决方案2】:

    我详细阐述了 Luis B 的答案,以便动态反映折线图的数据提供者。这样,图例仅显示图表中可用的字段。有点妙。

    这是我想出的,效果很好:

            protected function onUpdateLinechartComplete(e:FlexEvent):void 
            {
    
                //empty legend for fresh display
                var legendArray:Array = new Array();
                legend1.dataProvider = legendArray;
    
                //filter Legend data so that only LineSeries with data can be shown
                for(var i:int=0; i< linechart1.legendData.length; i++) {
    
                    //if data is found in the line series, let's add it to the chart legend data provider, so it can be displayed in the legend
                    if (linechart1.legendData[i].element.items.length != 0) {
                        legendArray.push(linechart1.legendData[i]); 
                    }
    
                }
                legend1.dataProvider = legendArray;
                legend1.direction = "vertical";
            }
    
    
    
    //in the page Initialize function, I add a listener event to the linechart component for when the legend update completes so it can filter lineseries on the legend's dataprovider in [onUpdateLegendComplete]
    linechart1.addEventListener(FlexEvent.UPDATE_COMPLETE, onUpdateLinechartComplete);
    

    我最终不得不使用 EventHandler 并将事件侦听器附加到折线图本身。这是因为我正在与传奇的数据提供者一起经历“竞争条件”。有时它会起作用,有时它不会。使用事件监听器消除了这个问题,并且只在折线图完成加载数据时过滤图例。

    请随意支持这个答案,FLEX FOLKS!

    【讨论】:

    • 我喜欢它。可能其他答案对于 OP 来说确实是正确的,但这是我需要的。
    【解决方案3】:

    确定另一个版本的 devtron 答案,如果您已经像我一样拥有自定义线图类,那么将其放入:

    [Bindable] public var activeLegendData:Array;
    
    // this goes in an initialize handler
    addEventListener(FlexEvent.UPDATE_COMPLETE, onUpdateChartComplete);
    
    protected function onUpdateChartComplete(e:FlexEvent):void {
        activeLegendData = new Array();
        for(var i:int=0; i < legendData.length; i++) {
            if (legendData[i].element.items.length != 0) {
                activeLegendData.push(legendData[i]); 
            }
        }
    }
    

    然后你将你的图例数据提供者绑定到 linechart.activeLegendData 而不是 linechart。

    与 Devtron 相比,此解决方案的优势在于,您不必每次在应用中添加另一个折线图时都重新编写代码。

    【讨论】:

    • 我看不出这与我的回答有何不同,除了您没有明确绑定图例?
    【解决方案4】:

    另一种选择...

    从图表系列类之一派生一个新类,然后覆盖 legendData() 的 getter 并返回一个空数组。以下是 PlotSeries 的示例:

    public class PlotSeriesNoLegend extends PlotSeries
    {
        public function PlotSeriesNoLegend()
        {
            super();
        }
    
        override public function get legendData():Array /* of LegendData */
        {
            return [ ];
        }
    }
    

    【讨论】:

      【解决方案5】:

      一些关于以前答案的问题。 当提到创建一个新数组以将其用作图例时,请小心,因为它与描述的不同。对我来说,它是这样工作的:

      当您尝试访问现有 pie.legendData 中的值时,您应该这样做: pie.legendData[0][0] 或 pie.legendData[0][1]

      除此之外,为了将这些数据放入一个新数组并使用它来创建新图例,您应该等待饼图已经创建。

      为此,我只是使用饼图的渲染事件。

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2011-09-13
        • 2020-08-02
        • 1970-01-01
        • 2015-10-07
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        • 2017-03-29
        • 2012-11-04
        相关资源
        最近更新 更多