23
Jul
07

Formatting multiple data tips in a Slider

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.


2 Responses to “Formatting multiple data tips in a Slider”


  1. 1 P Mar 20th, 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.

  2. 2 Clark Sep 20th, 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.

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".