在弹性图表中,我想绘制与特定系列相关的“参考线”之类的东西,因此这些线不是独立的系列,不应在图例中显示。是否可以从图表图例中排除某些系列?谢谢!
5 回答
我详细阐述了 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!
可以从图表图例中排除某些系列。
每个图表类(扩展 ChartBase)都有一个 legendData Array 属性。这个 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
好的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 相比,此解决方案的优势在于,您不必每次在应用程序中放置另一个折线图时都重写代码。
另一种选择...
从图表系列类之一派生一个新类,然后覆盖 getterlegendData()
并返回一个空数组。这是 PlotSeries 的示例:
public class PlotSeriesNoLegend extends PlotSeries
{
public function PlotSeriesNoLegend()
{
super();
}
override public function get legendData():Array /* of LegendData */
{
return [ ];
}
}
对先前答案的一些评论。当提到创建一个新数组以将其用作图例时,请小心,因为它与描述的不同。对我来说,它是这样工作的:
当您尝试访问现有 pie.legendData 中的值时,您应该这样做: pie.legendData[0][0] 或 pie.legendData[0][1]
除此之外,为了将这些数据放入一个新数组并将其用于创建新图例,您应该等待已经创建了饼图。
为此,我只需使用饼图的渲染事件。
希望它可以帮助你。