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:
<?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.
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
0 Responses to Formatting multiple data tips in a Slider
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


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.
I see what your saying P.
Is there a way to fix this. I have tried a few things but havin no luck.
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”;
}
}