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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
|
// rectangle with rounded corners
//#include "rrectangle.as"
#include "prototype.drawCircle.as"
#include "prototype.fillCircle.as"
#include "String.prototype.replace.as"
MovieClip.prototype.rect = function( x:Number, y:Number, width:Number, height:Number, colour:Number, alpha:Number )
{
this.beginFill( colour, 100 );
this.moveTo( x, y );
this.lineTo( x+width, y );
this.lineTo( x+width, y+height );
this.lineTo( x, y+height );
this.lineTo( x, y );
this.endFill();
this._alpha = alpha;
this._alpha_original = alpha; // <-- remember our original alpha while tweening
}
function get_colour( col:String )
{
if( col.substr(0,2) == '0x' )
return Number(col);
if( col.substr(0,1) == '#' )
return Number( '0x'+col.substr(1,col.length) );
if( col.length=6 )
return Number( '0x'+col );
// not recognised as a valid colour, so?
return Number( col );
}
// why isn't this built into flash?
// make a number 1000 = 1,000
function format( i:Number )
{
var s:String = '';
if( i<0 )
var num:Array = String(-i).split('.');
else
var num:Array = String(i).split('.');
var x:String = num[0];
var pos:Number=0;
for(c=x.length-1;c>-1;c--)
{
if( pos%3==0 && s.length>0 )
{
s=','+s;
pos=0;
}
pos++;
s=x.substr(c,1)+s;
}
if( num[1] != undefined )
s += '.'+ num[1].substr(0,2);
if( i<0 )
s = '-'+s;
return s;
}
// added by NetVicious June 2007
function setContextualMenu()
{
var contextual_menu:ContextMenu = new ContextMenu();
var About:ContextMenuItem = new ContextMenuItem("About Open Flash Chart...");
About.onSelect = function(obj, item) {
// Go to project url
getURL("javascript:popup=window.open('http://teethgrinder.co.uk/open-flash-chart/','ofc', 'toolbar=Yes,location=Yes,scrollbars=Yes,menubar=Yes,status=Yes,resizable=Yes,fullscreen=No'); popup.focus()");
};
/*
If you want to remove default items of Flash (except conf and about) uncomment this line
contextual_menu.hideBuiltInItems();
*/
contextual_menu.customItems.push(About);
createClassObject
_root.menu = contextual_menu;
}
function TxtFormat(size:Number,colour:Number)
{
var fmt:TextFormat = new TextFormat();
fmt.color = colour;
fmt.font = "Verdana";
fmt.size = size;
fmt.align = "center";
return fmt;
}
function FadeIn()
{
this.onEnterFrame = function ()
{
_root.show_tip(
this,
this._x,
this._y-20,
this.tooltip
);
if( this._alpha < 100 )
{
this._alpha += 10;
}
else
{
this._alpha = 100;
delete this.onEnterFrame;
}
}
}
function FadeOut()
{
this.onEnterFrame = function ()
{
if( (this._alpha-5) > this._alpha_original )
{
this._alpha -= 5;
}
else
{
this._alpha = this._alpha_original;
_root.hide_tip( this );
delete this.onEnterFrame;
}
}
}
function hide_tip( owner:Object )
{
if( _root.tooltip._owner == owner )
removeMovieClip("tooltip");
}
function show_tip( owner:Object, x:Number, y:Number, tip_obj:Object )
{
if( ( _root.tooltip != undefined ) )
{
if(_root.tooltip._owner==owner)
return; // <-- it's our tooltip and it is showing
else
removeMovieClip("tooltip"); // <-- it is someone elses tootlip - remove it
}
var tmp:String;
var lines:Array = [];
//
// Dirty hack. Takes a tool_tip_wrapper, and replaces the #val# with the
// tool_tip text, so noew you can do: "My Val = $#val#%", which turns into:
// "My Val = $12.00%"
//
if( _root.tool_tip_wrapper != undefined )
{
tmp = _root.tool_tip_wrapper.replace('#val#',tip_obj.value);
tmp = tmp.replace('#key#',tip_obj.key);
tmp = tmp.replace('#x_label#',tip_obj.x_label);
if( _root._x_legend != undefined )
tmp = tmp.replace('#x_legend#',_root._x_legend.get_legend());
}
else
{
if( tip_obj.x_label == undefined )
tmp = tip_obj.value;
else
tmp = tip_obj.x_label+'<br>'+tip_obj.value;
}
lines = tmp.split( '<br>' );
var tooltip = _root.createEmptyMovieClip( "tooltip", this.getNextHighestDepth() );
// let the tooltip know who owns it, else we get weird race conditions where one
// bar has onRollOver fired, then another has onRollOut and deletes the tooltip
tooltip._owner = owner;
tooltip.createTextField( "txt_title", tooltip.getNextHighestDepth(), 5, 5, 100, 100);
if( lines.length > 1 )
tooltip.txt_title.text = lines.shift();
var fmt:TextFormat = new TextFormat();
fmt.color = 0x0000F0;
fmt.font = "Verdana";
// this needs to be an option:
fmt.bold = true;
fmt.size = 12;
fmt.align = "right";
tooltip.txt_title.setTextFormat(fmt);
tooltip.txt_title.autoSize="left";
tooltip.createTextField( "txt", tooltip.getNextHighestDepth(), 5, tooltip.txt_title._height, 100, 100);
tooltip.txt.text = lines.join( '\n' );
var fmt2:TextFormat = new TextFormat();
fmt2.color = 0x000000;
fmt2.font = "Verdana";
fmt2.size = 12;
fmt2.align = "left";
tooltip.txt.setTextFormat(fmt2);
tooltip.txt.autoSize="left";
var max_width:Number = Math.max( tooltip.txt_title._width, tooltip.txt._width );
var y_pos:Number = y - tooltip.txt_title._height - tooltip.txt._height;
if( y_pos < 0 )
{
// the tooltip has drifted off the top of the screen, move it down:
y_pos = y + tooltip.txt_title._height + tooltip.txt._height;
}
var cstroke = {width:2, color:0x808080, alpha:100};
var ccolor = {color:0xf0f0f0, alpha:100};
ChartUtil.rrectangle(
tooltip,
max_width+10,
tooltip.txt_title._height + tooltip.txt._height + 5,
6,
((x+max_width+16) > Stage.width ) ? (Stage.width-max_width-16) : x,
y_pos,
cstroke,
ccolor);
// NetVicious, June, 2007
// create shadow filter
var dropShadow = new flash.filters.DropShadowFilter();
dropShadow.blurX = 4;
dropShadow.blurY = 4;
dropShadow.distance = 4;
dropShadow.angle = 45;
dropShadow.quality = 2;
dropShadow.alpha = 0.5;
// apply shadow filter
tooltip.filters = [dropShadow];
}
function hide_oops()
{
removeMovieClip("oops");
}
function oops( text:String )
{
if( _root.oops != undefined )
{
hide_oops();
}
var mc:MovieClip = _root.createEmptyMovieClip( "oops", this.getNextHighestDepth() );
mc.createTextField("txt", this.getNextHighestDepth(), 5, 5, 100, 100 );
mc.txt.text = text;
var fmt:TextFormat = new TextFormat();
fmt.color = 0x000000;
fmt.font = "Verdana";
fmt.size = 12;
fmt.align = "center";
mc.txt.setTextFormat(fmt);
mc.txt.autoSize="left";
mc.txt.setTextFormat(fmt);
var cstroke = {width:2, color:0x808080, alpha:100};
var ccolor = {color:0xf0f0f0, alpha:100};
ChartUtil.rrectangle(
mc,
mc.txt._width+10,
mc.txt._height+10,
6,
(Stage.width/2)-((mc.txt._width+10)/2),
(Stage.height/2)-((mc.txt._height+10)/2),
cstroke,
ccolor);
var dropShadow = new flash.filters.DropShadowFilter();
dropShadow.blurX = 4;
dropShadow.blurY = 4;
dropShadow.distance = 4;
dropShadow.angle = 45;
dropShadow.quality = 2;
dropShadow.alpha = 0.5;
// apply shadow filter
//mc.filters = [dropShadow];
}
function make_pie()
{
_root._pie = new PieStyle( this, 'pie' );//!=undefined ? lv['values'] : "", lv['links']);
_root._title = new Title( this );
}
function make_chart()
{
//
// the order that these are built determines their Z order:
//
_root._inner_background = new InnerBackground( this );
_root._min_max = new MinMax( this );
// we build the graph from top to bottom
_root._title = new Title( this );
_root._x_legend = new XLegend( this );
_root._y_legend = new YLegend( this , 1);
if(_root.lv.show_y2)
_root._y2_legend = new YLegend( this , 2);
var xTicks = 5;
if( this.x_ticks != undefined )
xTicks = Number( this.x_ticks );
// size, colour
var x_label_style:XLabelStyle = new XLabelStyle( this );
var y_label_style:YLabelStyle = new YLabelStyle( this, 1 );
if(_root.lv.show_y2)
var y_label_style2:YLabelStyle = new YLabelStyle( this, 2 );
// create X labels and measure the height:
_root._x_axis_labels = new XAxisLabels( this, x_label_style );
var xSteps = 1;
if( this.x_axis_steps != undefined )
xStep = Number( this.x_axis_steps );
_root._x_axis = new XAxis(
xTicks, // <-- tick size
this,
_root._x_axis_labels.count(),
xStep
);
_root._y_ticks = new YTicks( this );
_root._y_axis_labels = new YAxisLabels(
y_label_style,
_root._min_max.y_min,
_root._min_max.y_max,
_root._y_ticks.steps,
1,
this
);
if(_root.lv.show_y2)
{
_root._y_axis_labels2 = new YAxisLabels(
y_label_style2,
_root._min_max.y2_min,
_root._min_max.y2_max,
_root._y_ticks.steps,
2,
this
);
}
_root._y_axis = new YAxis(
_root._y_ticks,
this,
_root._min_max.y_min,
_root._min_max.y_max,
_root._y_ticks.steps,
1
);
if(_root.lv.show_y2)
{
_root._y_axis2 = new YAxis(
_root._y_ticks,
this,
_root._min_max.y_min,
_root._min_max.y_max,
_root._y_ticks.steps,
2
);
}
// The chart values are defined last and are on TOP of every thing else
_root.chartValues = new Values( this, _root._background.colour, _root._x_axis_labels.labels );
// tell the x axis where the grid lines are:
_root._x_axis.set_grid_count( _root.chartValues.length() );
if( _root._keys != undefined )
{
//_root._keys.del();
//_root.oops('deleted');
}
_root._keys = new Keys(
(_root._y_legend.width()+_root._y_axis_labels.width()+_root._y_axis.width()), // <-- from left
_root._title.height(), // <-- from top
_root.chartValues.styles );
}
var lv:LoadVars = new LoadVars();
//lv.onLoad = function( success )
lv.onLoad = LoadVarsOnLoad;
lv.make_chart = make_chart;
lv.make_pie = make_pie;
function LoadVarsOnLoad( success )
{
if( !success )
{
_root.loading.done();
//_root.oops('Open Flash Chart: Error opening data file URL\n'+_root.data);
_root.oops(_root.data);
return;
}
// remove loading data... message
if( _root.oops != undefined )
removeMovieClip("oops");
_root.css = new Css('margin-top: 30;margin-right: 40;');
//
// Now we build the objects, the order in which we
// build them determins their Z position.
//
_root._background = new Background( this );
//
// if we have a pie chart, we don't build the
// axis, grid and stuff..
//
if( this.pie != undefined )
this.make_pie();
else
this.make_chart();
if( this.tool_tip != undefined )
{
_root.tool_tip_wrapper = this.tool_tip;
}
_root.loading.done();
_root.move();
}
function move()
{
if( _root._pie != undefined )
{
_root._background.move();
_root._title.move();
_root._pie.draw();
return;
}
//
// move items that may resize themselves:
//
_root._keys.move();
//
// measure the box:
//
var top:Number = _root._title.height()+_root._keys.height();
var left:Number = _root._y_legend.width()+_root._y_axis_labels.width()+_root._y_axis.width();
var right:Number = Stage.width;
// do we jiggle the box smaller because the last X Axis label
// is hanging off the end of the screen?
var jiggle:Boolean = true;
if(_root.lv.show_y2)
{
right -= _root._y2_legend.width()+_root._y_axis_labels2.width()+_root._y_axis2.width();
// no need to shrink the box:
jiggle = false;
}
var bottom:Number = Stage.height-(_root._x_axis_labels.height()+_root._x_legend.height()+_root._x_axis.height())
var b:Box = new Box(
top, left, right, bottom,
_root._min_max, // <-- scale everything between min/max
_root._x_axis_labels.first_label_width(),
_root._x_axis_labels.last_label_width(),
_root.chartValues.length(),
jiggle,
_root._x_axis.three_d
);
//
// tell everything else to move, the order in
// which we .move() things doesn't matter because
// they allready have their z-order assigned.
//
_root._background.move();
_root._inner_background.move( b );
_root._title.move();
_root._x_legend.move();
_root._y_legend.move(1);
if(_root.lv.show_y2)
_root._y2_legend.move(2);
_root._y_axis_labels.move( _root._y_legend.width(), b );
// position of second y axel labels..
if(_root.lv.show_y2)
_root._y_axis_labels2.move( Stage.width-(_root._y2_legend.width()+_root._y_axis_labels2.width()), b );
_root._x_axis.move( b );
// move x labels
_root._x_axis_labels.move(
Stage.height-(_root._x_legend.height()+_root._x_axis_labels.height()), // <-- up from the bottom
_root.chartValues.styles[0].values.length,
b );
_root._y_axis.move( b , 1);
if(_root.lv.show_y2)
_root._y_axis2.move( b , 2 );
_root.chartValues.move(
b,
_root._min_max.y_min,
_root._min_max.y_max, // <-- scale everything between min/max
_root._min_max.y2_min,
_root._min_max.y2_max
);
}
//
// test JS to flash coms
//
import flash.external.*;
ExternalInterface.addCallback("set_title", null, setTitle);
function setTitle(str:String):Void
{
if( _root._title != undefined )
{
_root._title.build( str );
_root.move();
}
// for debuggig:
//_root.oops(str);
}
ExternalInterface.addCallback("push_value", null, pushValue);
function pushValue( val:String, label:String ):Void
{
_root.chartValues.styles[0].add( Number( val ), label );
_root._x_axis_labels.add(label);
// tell the x axis where the grid lines are:
_root._x_axis.set_grid_count( _root.chartValues.length() );
_root.move();
}
ExternalInterface.addCallback("delete_value", null, deleteValue);
function deleteValue():Void
{
_root.chartValues.styles[0].del();
_root._x_axis_labels.del();
// tell the x axis where the grid lines are:
_root._x_axis.set_grid_count( _root.chartValues.length() );
_root.move();
}
ExternalInterface.addCallback("show_message", null, show_message);
function show_message( msg:String ):Void
{
_root.oops(msg);
}
ExternalInterface.addCallback("hide_message", null, hide_message);
function hide_message():Void
{
hide_oops();
}
ExternalInterface.addCallback("reload", null, reload);
function reload( u:String ):Void
{
// inform the user we are reloading data:
_root.loading = new Loading('Loading data...');
var lv:LoadVars = new LoadVars();
// ugh!:
lv.onLoad = LoadVarsOnLoad;
lv.make_chart = make_chart;
lv.make_pie = make_pie;
//
var url:String = '';
if( _root.data != undefined )
url = _root.data;
if( u != undefined )
{
if( u.length > 0 )
{
url = u;
}
}
//setTitle( u );
lv.load(url);
}
//
//
//
// ********************************************************************************
//_root.loading = new Loading('Loading data...');
//
// variables from the url:
if( _root.width == undefined )
_root.width = 400; // <-- default width
if( _root.height == undefined )
_root.height = 300; // <-- default width
_root.loading = new Loading('Loading data...');
// so we can rotate text:
this.embedFonts = true;
_root.chartValues = new Array();
// -------------------------------------------------------------+
//
// tell flash to align top left, and not to scale
// anything (we do that in the code)
//
Stage.align = "LT";
//
// ----- RESIZE ----
//
// noScale: now we can pick up resize events
Stage.scaleMode = "noScale";
//
var stageListener:Object = new Object();
stageListener.onResize = function()
{
//trace("w:"+Stage.width+", h:"+Stage.height);
_root.move();
};
Stage.addListener(stageListener);
//
// ------ END RESIZE ----
//
//
// NetVicious, June 2007
// Right click menu:
setContextualMenu();
// from URL
if( _root.data == undefined )
_root.data="C:\\Users\\John\\Documents\\flash\\svn\\data-files\\data-23.txt";
//_root.data="http://www.stelteronline.de/index.php?option=com_joomleague&func=showStats_GetChartData&p=1";
lv.load(_root.data);
stop();
|