0
votes

I want to use datatips in my charts in Flex 4. I know how to use them, but the standard DataTip only shows text. I would like to show text + a button. How would I go on about this?

I thought about extending DataTip in a custom DataTip class and add the button there somewhere like this

public class MyDataTip extends DataTip {
    // Override some methods to add the button here?
}

Is this possible / the correct idea? Or do I have to do it differently? Any code examples for this?

1
Why would you want to have a button on DataTip? You will not be able to click the button because the DataTip disappears when you move the cursor position away. - gbdcool
I can move over it and then it stays. The button shall be used to switch to detail views. - user975561

1 Answers

2
votes

You can use a DataTipRenderer to achieve this. Here is a example of it.

<?xml version="1.0"?>
<s:Group  xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        implements="mx.core.IFlexDisplayObject, mx.core.IDataRenderer"
        width="120">
<fx:Metadata>
[Event(name="DataSelect", type="flash.events.Event")]
</fx:Metadata>
<fx:Script>
<![CDATA[
import flashx.textLayout.conversion.TextConverter;
import flashx.textLayout.elements.TextFlow;

import mx.charts.HitData;
import mx.charts.series.items.ColumnSeriesItem;

private var _data:HitData;

[Bindable]
private var _xValue:String;

[Bindable]
private var _yValue:String;

[Bindable]
private var _displayText:TextFlow;

public function get data():Object
{
    // TODO Auto Generated method stub
    return null;
}

public function set data(value:Object):void
{
    // HitData data from chart
    _data = value as HitData;

    // The display text used in datatip which comes in HTML format
    _displayText = TextConverter.importToFlow(_data.displayText, TextConverter.TEXT_FIELD_HTML_FORMAT);

    // HitData contains a reference to the ChartItem
    var item:ColumnSeriesItem = _data.chartItem as ColumnSeriesItem;

    // ChartItem xValue and yValue
    _xValue = String(item.xValue);
    _yValue = String(item.yValue);
}

]]>
</fx:Script>

<fx:Declarations>

</fx:Declarations>

    <s:Rect right="0" left="0" bottom="0" top="0">
        <s:filters>
        <s:DropShadowFilter blurX="20" blurY="20" alpha="0.22" distance="5" angle="90" knockout="false" />
        </s:filters>
        <s:fill>
        <s:SolidColor color="0xFFFFEE"/>
        </s:fill>
        <s:stroke>
            <s:SolidColorStroke color="0x1a1a19"  weight="1" alpha=".2" />
        </s:stroke>
    </s:Rect>

    <s:VGroup width="100%" height="100%" paddingTop="10" paddingRight="10" paddingBottom="10" paddingLeft="10">

        <s:RichEditableText textFlow="{_displayText}" width="100%" textAlign="center" selectable="false" editable="false"/>

        <s:Button label="Click me !"/>


    </s:VGroup>
</s:Group>

And in your Chart mxml tag just add following

 <mx:ColumnChart id="myChart" showDataTips="true" dataTipRenderer="com.CustomDataTipRenderer">