フレックス チャートでは、特定の系列に関連する「参照線」のようなものを描画したいため、これらの線は独立した系列ではなく、凡例に表示されるべきではありません。チャートの凡例から一部の系列を除外することはできますか? ありがとう!
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 のみを使用する場合。次に、その配列を凡例の dataProvider として設定できます。
また、ゼロから作成した LegendItem に基づいて、LegendItem の独自の配列を作成することもできます。そして、その配列を Legend の dataProvider として使用します。
たとえば、ここでは凡例の 1 番目と 3 番目のシリーズのみを表示します。
<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
OK 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 にバインドします。
Devtron のソリューションに対するこのソリューションの利点は、アプリに別の折れ線グラフを配置するたびにコードを書き直す必要がないことです。
別の選択肢...
チャートシリーズクラスの1つから新しいクラスを派生させてから、のゲッターをオーバーライドしlegendData()
て空の配列を返します。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]
それに加えて、これらのデータを新しい配列に取得し、それを使用して新しい凡例を作成するには、パイが既に作成されるまで待つ必要があります。
そのために、単純にパイのレンダリング イベントを使用します。
お役に立てば幸いです。