Formatting multiple data tips in a Slider

by Peter deHaan on July 23, 2007

Some imaginary blog reader asks, “How do i change a Slider’s dataTip based on which Slider thumb is being dragged?”
I crafted up this sneaky code to listen for the thumbPress and thumbRelease events to log which slider is being… slid?

Full code after the jump.

The following example formats the Slider control’s tool tip based on whether the first or second thumb is being dragged:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/07/23/formatting-multiple-data-tips-in-a-slider/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            layout="vertical"
            verticalAlign="middle"
            backgroundColor="white"
            applicationComplete="init();" >

    <mx:Script>
        <![CDATA[
            import mx.events.SliderEvent;

            private var thumbPrefixArray:Array;
            private var thumbIdx:int;

            /**
             * Initialize thumbPrefixArray with the two prefixes. There
             * needs to be enough items in the thumbPrefix array to
             * match the Slider's thumbCount property.
             */
            private function init():void {
                thumbPrefixArray = ["Minimum:", "Maximum:"];
            }

            /**
             * Formatter function for the dataTip.
             */
            private function formatDataTip(item:Number):String {
                // Set the data tip prefix based on the currently
                // selected thumb and get the prefix string from the
                // thumbPrefixArray array.
                var prefix:String = thumbPrefixArray[thumbIdx]
                return String(prefix + " $" + item.toFixed(2));
            }

            /**
             * Set/clear the thumbIdx variable depending on the
             * event that called this function. The thumbIdx variable
             * will be used to control the text that gets displayed in
             * the data tip.
             */
            private function setThumbIdx(evt:SliderEvent):void {
                switch (evt.type) {
                    case SliderEvent.THUMB_PRESS:
                        thumbIdx = evt.thumbIndex;
                        break;
                    case SliderEvent.THUMB_RELEASE:
                        thumbIdx = undefined;
                        break;
                }
            }
        ]]>
    </mx:Script>

    <mx:HSlider id="slider"
            thumbCount="2"
            values="[10, 90]"
            minimum="0"
            maximum="100"
            liveDragging="true"
            thumbPress="setThumbIdx(event);"
            thumbRelease="setThumbIdx(event);"
            dataTipFormatFunction="formatDataTip" />

    <mx:Label id="minimumLabel"
            text="Minimum: ${Number(slider.values[0]).toFixed(2)}" />

    <mx:Label id="maximumLabel"
            text="Maximum: ${Number(slider.values[1]).toFixed(2)}" />

</mx:Application>

View source is enabled in the following example.

Not sure that it is the most elegant solution (I sincerely doubt it), but it works, and hopefully helps somebody out there.

{ 3 comments… read them below or add one }

P March 20, 2008 at 6:27 am

Nice solution which almost works. Try pressing and dragging either thumb. Now press down on the other thumb (but don’t move the slider right away). You’ll see that the datatip will still display the text from the first thumb you moved. This is because the datatip function is called to display before the thumb index is updated, thus displaying old data.

Reply

Clark September 20, 2008 at 11:02 am

I see what your saying P.

Is there a way to fix this. I have tried a few things but havin no luck.

Reply

PR April 28, 2009 at 9:58 am

I ran into this same issue, and as mentioned, the above solution doesn’t really cut it.

I solved it by using the following dateTipFormatFunction:

private var currThumbIndex:int=0;
private function formatDataTip(value:Number):String{
//need to determine which thumb we’re on
//when FIRST pressed on the thumb, the current value matches one of the two
// thumb values. Store the thumb index based on that.
if(dateSlider.values[0]==value){
currThumbIndex = 0;
}else if(dateSlider.values[1]==value){
currThumbIndex = 1;
}

if(currThumbIndex==0){
return “minimum”;
}else{
return “maximum”;
}

}

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: