Copyright 2007 FlexLib Contributors See http code google com flexlib w

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
Copyright (c) 2007 FlexLib Contributors. See:
http://code.google.com/p/flexlib/wiki/ProjectContributors
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package flexlib.charts
{
import flash.display.Graphics;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import mx.charts.chartClasses.CartesianChart;
import mx.charts.chartClasses.ChartElement;
import mx.charts.chartClasses.Series;
import mx.core.EventPriority;
import mx.styles.CSSStyleDeclaration;
import mx.styles.StyleManager;
// =================================================
// E V E N T S
// =================================================
/**
* Dispatched when the selected data on the horizontal axis changes.
*
* @eventType flash.events.Event.CHANGE
*/
[Event( name="change", type="flash.events.Event" )]
// =================================================
// S T Y L E S
// =================================================
/**
* Color of the vertical line that appears for the selected x value
* The default value is <code>0xFF0000</code>.
*
* @default 0xFF0000
*/
[Style( name="selectorColor", type="uint", format="Color", inherit="no" )]
/**
* Allows for the selection of a specific data value on the horizontal
* axis.
*/
public class HorizontalAxisDataSelector extends ChartElement
{
/** Flag for initializing the styles */
private static var classConstructed:Boolean = classConstruct();
/** Initialize styles to default values */
private static function classConstruct():Boolean
{
if ( !StyleManager.getStyleDeclaration( "HorizontalAxisDataSelector" ) )
{
// If HorizontalAxisDataSelector has no CSS definition,
// create one and set the default value.
var newStyleDeclaration:CSSStyleDeclaration = new CSSStyleDeclaration();
newStyleDeclaration.setStyle( "selectorColor", 0xFF0000 );
StyleManager.setStyleDeclaration( "HorizontalAxisDataSelector", newStyleDeclaration, true );
}
return true;
}
/**
* Flag to determine if the mouse is pressed and we should update selected
* data index on mouse move.
*/
private var mouseIsDown:Boolean = false;
// /**
// * When <code>true</code>, instructs the selector to snap to the nearest data point.
// * When <code>false</code>, the selector will interpolate values based on mouse positions.
// *
// * @default true
// */
// public var snap:Boolean = true;
/**
* The last x location of the mouse. Used when snapping is disabled to determine at which
* point values should be interpolated.
*/
private var lastX:Number;
/** The selected index in the data provider for the chart. Used when snapping is enabled. */
private var selectedDataIndex:int = -1;
/**
* Constructor.
*/
public function HorizontalAxisDataSelector()
{
super();
// Register for mouse events to handle data selection
addEventListener( MouseEvent.MOUSE_DOWN, handleMouseDown, false, EventPriority.DEFAULT, false );
addEventListener( MouseEvent.MOUSE_MOVE, handleMouseMove, false, EventPriority.DEFAULT, true );
// Wait for us to be added to the stage before adding the mouse up handler
// otherwise we get null reference errors when accessing stage
addEventListener( Event.ADDED_TO_STAGE, onAddedToStage, false, EventPriority.DEFAULT, true );
}
/**
* Event handler; Invoked when we're added to the stage.
*/
private function onAddedToStage( event:Event ):void
{
// Listen on the stage for mouse up so we detect mouse up anywhere
stage.addEventListener( MouseEvent.MOUSE_UP, handleMouseUp, false, EventPriority.DEFAULT, true );
removeEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
}
/**
* Draws the vertical selection line overlay.
*/
override protected function updateDisplayList( unscaledWidth:Number,
unscaledHeight:Number):void
{
super.updateDisplayList( unscaledWidth, unscaledHeight );
var g:Graphics = graphics;
g.clear();
// Draw a big transparent square over the entire chart area
// so Flash Player sees us for mouse events
g.moveTo( 0, 0 );
g.lineStyle( 0, 0, 0 );
g.beginFill( 0, 0 );
g.drawRect( 0, 0, unscaledWidth, unscaledHeight );
g.endFill();
// Only draw the overlay if there is a selection
if ( selectedDataIndex >= 0 )
{
var values:Array = dataValues;
// Get the location of where the selection line should be drawn
var location:Point = chart.dataToLocal( values[ 0 ], values[ 1 ] );
// KLUDGE: We seem to off here by the width of the vertical axis. Strange, not
// quite what I expected. Fix it by subtracting the different coordinate systems
// from the x location.
var diff:Number = chart.mouseX - this.mouseX;
location.x -= diff;
// TODO: Add style for selectorThickness
// TODO: Add style for selectorThickness alpha
g.lineStyle( 1.5, getStyle( "selectorColor" ), .8 );
// Draw a vertical line at the point's x location across the entire height of the area
g.moveTo( location.x, 0 );
g.lineTo( location.x, unscaledHeight );
}
}
/**
* Gets a list of data values for the currently selected data item on the horizontal
* axis. The x field value is always the first element in the array. Then, for each
* series, the y value is added to a values array in the same order that the series
* are defined.
*/
public function get dataValues():Array
{
// Create a new array time so that someone gettnig the data values cannot
// change them by hanging on to the reference
var values:Array = new Array();
var xField:String = chart.series[ 0 ].xField;
// Save the x field value first
values.push( chart.dataProvider.getItemAt( selectedDataIndex )[ xField ] );
// Loop over all the series and get the y information from each series
for ( var i:int = 0; i < chart.series.length; i++ )
{
var currentSeries:* = chart.series[ i ];
// Add the y field values for the values array
values.push( chart.dataProvider.getItemAt( selectedDataIndex )[ currentSeries.yField ] );
}
return values;
}
/**
* Event handler; invoked when the mouse is pressed.
*/
private function handleMouseDown( event:MouseEvent ):void
{
mouseIsDown = true;
setSelectedDataIndex( event );
}
/**
* Event handler; invoked when the mouse is moved.
*/
private function handleMouseMove( event:MouseEvent ):void
{
// Change the value as the user drags the mouse
if ( mouseIsDown )
{
setSelectedDataIndex( event );
}
}
/**
* Event handler; invoked when the mouse is released.
*/
private function handleMouseUp( event:MouseEvent ):void
{
mouseIsDown = false;
}
/**
* Helper function to set the selected data index based on mouse coordinates.
*/
private function setSelectedDataIndex( event:MouseEvent ):void
{
// Keep track of the old value to determine if it actually changes
var oldSelectedDataIndex:int = selectedDataIndex;
// Get the coordinates of the mouse relative to the chart
var point:Point = new Point( chart.mouseX, chart.mouseY );
// Get the data values that the point represents
var dataValues:Array = chart.localToData( point );
// The first value in the dataValues in the target x field value
var targetXFieldValue:Number = dataValues[ 0 ];
// Get the xField value to know what to look for in the
// chart data provider
var series:* = chart.series[ 0 ];
var xField:String = series.xField;
// Covert the point's x value to the nearest x value
// from the horizontal axis. Loop over the data provider
// and find the xField value closest to the target value.
// TODO: This would be better as a binary search!
for ( var i:int = 0; i < chart.dataProvider.length; i++ )
{
var item:* = chart.dataProvider.getItemAt( i );
try
{
var nextItem:* = chart.dataProvider.getItemAt( i + 1 );
if ( item[ xField ] <= targetXFieldValue
&& nextItem[ xField ] > targetXFieldValue )
{
// Find out which of the two valus is closer
var diff1:Number = Math.abs( targetXFieldValue - item[ xField ] );
var diff2:Number = Math.abs( targetXFieldValue - nextItem[ xField ] );
if ( diff1 < diff2 ) // closer to item
{
selectedDataIndex = i;
}
else // closer to nextItem
{
selectedDataIndex = i + 1;
}
break;
}
}
catch ( re:RangeError )
{
// Reached the last item, which means the value to be used is
// chart.dataProvider.length - 1
selectedDataIndex = chart.dataProvider.length - 1;
}
}
// Special case, check for click before the first item
if ( chart.dataProvider.getItemAt( 0 )[ xField ] > targetXFieldValue )
{
selectedDataIndex = 0;
}
// Determine if the selected data index changed or not
if ( selectedDataIndex != oldSelectedDataIndex )
{
// Since the value changed, the display list needs updating
invalidateDisplayList();
// Send out the event letting the everyone know the value changed. When
// the change event is picked up, examine the dataValues property of the
// selector to get a list of the selected values.
var changeEvent:Event = new Event( Event.CHANGE, true, false );
dispatchEvent( changeEvent );
}
}
} // end class
} // end package