forked from AliBassam/css-pie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSSpie.js
More file actions
1201 lines (1180 loc) · 49.9 KB
/
CSSpie.js
File metadata and controls
1201 lines (1180 loc) · 49.9 KB
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
-------------------------------------------------
CSS3 Pie Chart Generator
------------------------------------------------
A JavaScript Library that allows you to create a dynamic CSS3 Pie Chart anywhere on your webpage. Purely CSS.
How to Pie:
mistercss.blogspot.com/2013/04/pie-chart-javascript-library.html
code.google.com/p/css-pie/wiki/HowToPie
Download from:
code.google.com/p/css-pie/downloads/list
github.com/AliBassam/css-pie
by : Ali Bassam
alibassam.27@gmail.com
edited by: Jonas Krispin - Object Orientation
github.com/Colorfulstan/css-pie
jonas.krispin@fh-duesseldorf.de
Known Issues:
- visual glitch between slices possible but rare
- % values as size not supported
*/
/**
* Pie Object contains of a background-circle and its slices. At Object-creation, a
* background with given color and of given size and unit is created. The pie
* has the given id which equals the html-tag "id" in the produced html code.
* The Object offers methods for creating, deleting and manipulating Slices
* and delivering the pie as pure HTML/CSS Element.
*
* The Pie Object is designed as Linked-List of Slices so it allows to navigate
* through the Slices via the traditional Linked-List Methods. It also allows to
* create an Iterator through .iterator() to navigate over an independent set of
* cursors.
*
* See <TODO: insert URL> for details on the methods.
* @param {type} _idStr
* String which will be used as "id" tag for the HTML-wrapper-div of the pie
* @param {type} _sizeStr
* Size of the Pie as String with unit.
* % as unit is currently not supported.
* TODO: integrate % support
* @param {type} _basecolorStr
* Any legal CSS Color as String for the background-color of the pie-circle
* @returns {Pie}
*/
function Pie(_idStr, _sizeStr, _basecolorStr) { // Pie START ///////////////
/////////////////////////////////////////////////////////////////
////////////////////// Constructor //////////////////////////////
/////////////////////////////////////////////////////////////////
// ID //////////////////////////////////
var _id = _idStr;
this.id = function() { return _id; };
// size ////////////////////////////////
// var _size = (function(sizeStr) { return parseFloat(sizeStr);})(_sizeStr); // return of anonymous function as value
var _size = parseFloat(_sizeStr);
this.setSize = function(_sizeValue){
_size = _sizeValue;
pieBackground.style.width = this.sizeString();
pieBackground.style.height = this.sizeString();
this.update();
};
var _sizeUnit = (function(sizeString) { // function START //////////////////
// trim spaces and remove the _size chars from it = unit
var unit = sizeString.replace(("" + _size), "");
unit = unit.trim();
// DEBUGG: not supporting % usage atm
// pie wont show on site but appears in DOM so it seems to need a
// set size for the container in addition or at least a parent element on
// the site with a set width/height to scale off of
if (unit === '%') {
console.log("Pie:'" + _id +
"' uses % values\n" +
"These are not supported by CSSPie.js yet!\n" +
"We'll use px instead for now\n" +
"Please Inform your Webmaster.");
return "px";
}
return unit;
})(_sizeStr); // return of anonymous function as value
// function END //////////////////////////////////////////////
this.size = function() { return _size; };
this.sizeUnit = function() { return _sizeUnit; };
this.sizeString = function() { return ( "" + _size + _sizeUnit); };
// pieContainer - background of the pie and container for the slices ///////
var pieBackground = this._createBackground(_basecolorStr);
// TODO: image compatibility for piebackground
this.setBackground= function(_colorStr){
pieBackground.style.backgroundColor = _colorStr;
this.update();
};
this.container = function() { return pieBackground; };
//Pie HTML - the wrapping div of the resulting pie therefore the HTML of the pie
var pieHTML = document.createElement("div");
pieHTML.id = _id;
pieHTML.style.display = "inline-block";
pieHTML.appendChild(pieBackground);
/**
* Method returns the HTML-code to be appended to the body or other elements on the site
* @returns {HTML}
* HTML-Element of the pie
*/
this.html = function() { return pieHTML; };
/////////////////////////////////////////////////////////////////
////////////////////// End Constructor //////////////////////////
/////////////////////////////////////////////////////////////////
//-------------------------------------------------------------//
/////////////////////////////////////////////////////////////////
////////////////////// Slice Management //////////////////////////
/////////////////////////////////////////////////////////////////
var _startingPercentage = 0;
this.firstPercentage = function(){ return _startingPercentage; };
this.setNewStart = function(_newStartingPercentage){
_startingPercentage = _newStartingPercentage;
this.update();
};
var _firstSlice = null;
this.first = function() { return _firstSlice; };
var _lastSlice = null;
this.last = function() { return _lastSlice; };
var _currentSlice = null;
this.current = function() { return _currentSlice; };
// Navigation //////////////////////////////////////////////////
/**
* Method returns the next Slice to the current one or it returns the
* current Slice if its the last Slice.
* Sets the cursor of the Pie for currentSlice on the Slice returned.
* @returns {Slice}
* next Slice or currentSlice if thats the last one
*/
this.next = function() {
if (!_currentSlice.equals(_lastSlice)) {
_currentSlice = _currentSlice.next;
return _currentSlice;
}
else {return _currentSlice;}
};
/**
* Method returns the previous Slice to the current one or it returns the
* current Slice if its the first Slice.
* Sets the cursor of the Pie for currentSlice on the Slice returned.
* @returns {Slice}
* next Slice or currentSlice if thats the last one
*/
this.previous = function() {
if (!_currentSlice.equals(_firstSlice)) {
_currentSlice = _currentSlice.previous;
return _currentSlice;
}
else
return _currentSlice;
};
this.hasNext = function() {
if (!_currentSlice.equals(this.next())) {return true; }
else {return false;}
};
this.hasPrevious = function() {
if (!_currentSlice.equals(this.previous())){ return true; }
else { return false; }
};
/////////////////////////////////////////////////////////////////
/**
* Adds a Slice after possibly existing Slices and moves Cursor to the new slice
* @param {Int} _percentageInt
* value how big the slice will be in percent of the pie
* @param {Int} _percentageStartInt
* value at which the slice starts (0=top mid of circle)
* Only relevant if you dont create Pie.update()
* Or a single Slice
* @param {String} _backgroundStr String with background (any legal css colorvalue or url(...) to image)
* @returns {Slice} the added, new currentSlice
*/
this.addSlice = function(_percentageInt, _percentageStartInt, _backgroundStr) {
var newSlice = new Slice(this, _percentageInt, _percentageStartInt, _backgroundStr);
// already existing slices
if (_firstSlice !== null) {
_currentSlice.next = newSlice;
newSlice.previous = _currentSlice;
_lastSlice = newSlice;
_currentSlice = newSlice;
}
// newSLice is first Slice
else {
_startingPercentage = newSlice.percentageStart();
_firstSlice = newSlice;
_currentSlice = newSlice;
_lastSlice = newSlice;
}
// this.container().appendChild(newSlice.html());
this.update();
return _currentSlice;
};
/**
* Adds a Slice with an onclick-Handler after possibly existing Slices and moves Cursor to the new slice
* @param {Int} _percentageInt
* value how big the slice will be in percent of the pie
* @param {Int} _percentageStartInt
* value at which the slice starts (0=top mid of circle)
* Only relevant if you dont create Pie.update()
* Or a single Slice
* @param {type} _onclickString
* String to be placed within the "onclick" attribute of the Slice
* @param {String} _backgroundStr String with background (any legal css colorvalue or url(...) to image)
* @returns {Slice} the added, new currentSlice
*/
this.addClickableSlice = function(_percentageInt, _percentageStartInt, _onclickString, _backgroundStr) {
var newSlice = this.addSlice(_percentageInt, _percentageStartInt, _backgroundStr);
newSlice.onclick(_onclickString);
this.update();
return newSlice;
};
/**
* Removes a slice from the pie and updates its html.
* Remaining Slices will adapt there position on the pie to fill the gap.
* if the removed Slice is the currentSlice of the pie, that Cursor will
* be set onto the firstSlice
*
* @param {type} _sliceIdToRemove
* Id of the Slice to remove
* @return {Boolean} true if a Slice got removed
*/
this.removeSlice = function(_sliceIdToRemove){
// find Slice
var needle = this.getSliceById(_sliceIdToRemove);
// If Slice was found
if (needle !== null){
needle._removeFromLinks();
if (needle.equals(_firstSlice)){
_firstSlice = needle.next;
}
if (needle.equals(_lastSlice)){
_lastSlice = needle.previous;
}
if (needle.equals(_currentSlice)){
_currentSlice = _firstSlice;
}
this.update();
return true;
}
return false;
};
/**
* searches for the slice with given id and returns it if found.
* @param {type} _indexOfSliceInt
* @returns {Slice} - Slice with the given id or null if not found
*/
this.getSliceById = function(_indexOfSliceInt) {
return this.first()._findSlice(_indexOfSliceInt);
};
/**
* Method moves the slice by given values for top/left
* @param {Int} _sliceId - Id of the Slice to be moved
* @param {Float} _topNum - Value to move the Slice by
* @param {Float} _leftNum - Value to move the Slice by
* @returns {undefined}
*/
this.moveSlice = function(_sliceId,_topNum,_leftNum){
var slice = this.getSliceById(_sliceId);
slice._moveSlice(_topNum,_leftNum);
this.update();
};
/**
* Method moves the slice away from the middle by the given value
* movingVector = anglehalfing of startdegree + enddegree
* @param {type} _sliceIdInt - Id of the slice to move
* @param {type} _valueInt - value to move it away from the middle
* @returns {undefined}
*/
this.offsetSlice= function(_sliceIdInt,_valueInt){
var slice = this.getSliceById(_sliceIdInt);
slice._offset(_valueInt);
this.update();
};
/**
* Appends the HTML off all the Slices to this.container()
*/
this._drawSlices = function(){
var it = this.iterator();
var current = it.first();
while(current !== null){
this.container().appendChild(current.html());
current = it.next();
}
};
/**
* Removes all Slices from container
*/
this.dropSlices= function(){
while (this.container().hasChildNodes()){
this.container().removeChild(this.container().firstChild);
}
};
/////////////////////////////////////////////////////////////////
////////////////////// Slicegroups //////////////////////////////
/////////////////////////////////////////////////////////////////
// TODO: Slices should contain their group? REVISION
this.slicegroups = [];
/**
* Checks if the given number of a slicegroup is valid.
*
* Conditions:
* - Number < 0
* - Number > number of slicegroups
*
* @param {type} _slicegroupInt
* number of the slice group to check
* @returns {Boolean}
* true if the slicegroup exists
*/
this.isSlicegroupIdValid = function(_slicegroupInt){
if (_slicegroupInt <= 0){
console.log("isSlicegroupIdValid(): slicegroupId is <= 0 and invalid"); // DEBUGG Info
return false;
}
if (_slicegroupInt > this.slicegroups.length){
console.log("isSlicegroupIdValid(): slicegroups.length <= _slicegroupIdInt - slicegroup Number " + _slicegroupInt + "doesnt exist"); // DEBUGG Info
return false;
}
else {
return true;
}
};
/**
* Groups Slices together and returns an Slicegroup Object.
* @param {type} _fromSliceIdInt
* first Slice (lowest id) to include in the group
* @param {type} _toSliceIdInt
* last Slice (highest id) to include in the group
* @returns {Slicegroup}
* the created Slicegroup Object
*/
this.groupSlices = function(_fromSliceIdInt, _toSliceIdInt){
var sliceArray = [];
// loop
for (var i = _fromSliceIdInt; i <= _toSliceIdInt; i++){
// findSlice
sliceArray.push(this.getSliceById(i));// put in array
} // end loop
var slicegroup = new Slicegroup(this,sliceArray); // create SLciegroup from array
this.slicegroups.push(slicegroup);// push into slicegroups Array
// return id of Slicegroup
console.log("Slicegroup " + slicegroup.id() + " created"); // DEBUG INFO
return slicegroup;
};
/**
* Finds the Slicegroup that contains the Slice with the given id
* and ungroups it elements by removing the group-reference from
* the Slicegroups Array
*
* @param {type} _sliceIdInGroupInt
* Id of a Slice in the group that should be deleted
*/
this.ungroupSlicegroupContaining = function(_sliceIdInGroupInt){
// find group and its index
for (var i=0;i<this.slicegroups.length;i++){
var currentSG = this.slicegroups[i];
if (currentSG.isSliceInGroup(_sliceIdInGroupInt)){
// remove it from _slicegroups
this.slicegroups.splice(i,1);
console.log("Slicegroup " + i + " containing Slice " +_sliceIdInGroupInt+" removed"); // DEBUG INFO
}
}
};
/**
*
* Ungroups the elements of the n-th Slicegroup, where n = the given value.
* It does that by removing the group-reference from the Slicegroups Array
* @param {type} _slicegroupInt
* Slicegroup to delete (n-th)
*/
this.ungroupSlicegroup = function(_slicegroupInt){
// TODO id handling! <= arrayindex
if (this.isSlicegroupIdValid(_slicegroupInt)){
var index = _slicegroupInt - 1;
// remove it from _slicegroups
this.slicegroups.splice(index,1);
console.log("Slicegroup " + _slicegroupInt + " removed"); // DEBUG INFO
}
};
/**
* Moves the n-th Slicegroup by the given values and updates the pie.
* @param {type} _slicegroupInt
* Slicegroup to move (n-th)
* @param {Float} _offsetX
* "left"-value to add
* @param {Float} _offsetY
* "top"-value to add
*/
this.moveSlicegroup = function(_slicegroupInt, _offsetX, _offsetY){
if (this.isSlicegroupIdValid(_slicegroupInt)){
var slicegroup = this.slicegroups[_slicegroupInt-1];
slicegroup.moveSlicegroup(_offsetX, _offsetY);
this.update();
}
};
/**
* Moves the n-th Slicegroup by the given value away from the center of the pie.
* Moving Vector is half angle between starting Angle of the first and
* Ending Angle of the last Slice within the group.
* Updates the pie.
* @param {type} _slicegroupInt
* Slicegroup to move (n-th)
* @param {Float} _offsetX
* "left"-value to add
* @param {Float} _offsetY
* "top"-value to add
*/
this.offsetSlicegroup = function(_slicegroupInt,_offsetValue){
var slicegroup = this.slicegroups[_slicegroupInt-1];
slicegroup.offsetSlicegroup(_offsetValue);
this.update();
};
/**
* Searches the Slicegroup with the given groupId and returns it if found.
* @param {type} _slicegroupIdInt
* groupId of the slicegroup to get
* @returns {Slicegroup}
*/
// TODO: fix and test (compare ids)
// this.getSlicegroupById = function(_slicegroupIdInt){
// if (this.slicegroups.length !== 0){
// return this.slicegroups[_slicegroupIdInt - 1];
// } else {
// return null;
// }
// };
/**
* Finds the slicegroup that contains the Slice with the given Id and returns it.
* Returns null if that group or slice doesnt exist
* @param {type} _sliceIdInGroupInt
* Id of a Slice within the group to find
* @returns {Slicegroup}
*/
this.findSlicegroupContaining = function(_sliceIdInGroupInt){
for (var i=0;i<this.slicegroups.length;i++){
var currentSG = this.slicegroups[i];
if (currentSG.isSliceInGroup(_sliceIdInGroupInt)){
return currentSG;
}
}
return null;
};
/////////////////////////////////////////////////////////////////
////////////////////// END Slicegroups //////////////////////////
/////////////////////////////////////////////////////////////////
/**
* Updates the HTML of the Pie using properties of the slices.
* Checks that key-properties are in place
*
* 1) Removes all HTML-slices
* 2) Checks that ids of the slices start with 1 and increase by 1 each
* 3) updates the following slices (id + percentages) if the ids dont match the pattern
* 4) adds HTML-slices with updated properties
*
*/
this.update = function() {
var it = this.iterator();
var current = it.first();
// removing HTML of al slices
this.dropSlices();
while (current !== null) {
// update following ids if the current and following id are not correct
// (counting up 1 per slice)
// if current Slice is first set its id to 1
// and make sure its percentages are set right
if (current.equals(this.first())) {
current.newId(1);
// _startingPercentage = current.percentageStart();
current.setPercentages(this.firstPercentage(),current.percentageSize());
}
// if ids dont match the order
if (current.hasNext() && current.id() !== (current.next.id() - 1)) {
// start from that with updating the following ids
current._updateFollowingIds();
}
if (current.hasNext() && current.percentageEnd() !== current.next.percentageStart()){
current._updateFollowingPercentages();
}
current = it.next();
}
// and update following percentages
// it.first()._updateFollowingPercentages();
// adding html of Slices
this._drawSlices();
};
/**
* The Number of Slices within the pie
* @returns {Integer}
*/
this.count = function() {
if (_lastSlice !== null) {
return _lastSlice.id();
} else
return 0;
};
/**
* Number of slicegroups in this pie
* @returns {Integer}
*/
this.groupCount = function() {
return slicegroups.length;
};
//-----------------------------------------------------------------------//
// inner-Object Slice //////////
/** Object representation of a slice of the pie.
* Background will be set to a color if a css color is given or to an image
* if an url(..) is given in _backgroundStr.
*
* @param {type} _pie - the pie this slice belongs to
* @param {Int} _percentageInt
* value how big the slice will be in percent of the pie
* @param {Int} _percentageStartInt - value at which the slice starts (0=top mid of circle)
* @param {String} _backgroundStr - String with either a legal CSS Color or an url(...) to an image
* @returns {Pie.Slice}
*/
function Slice(_pie, _percentageInt, _percentageStartInt, _backgroundStr) {
// TODO: adding possibility to insertSlice between some others
var pie = _pie;
// ID //////////////////////////
var _id = 1;
if (pie.first() !== null) {
// use id of currentSlice because insertion will always be behind the currentSlice
_id = (pie.current().id()) + 1;
}
this.id = function() {
return _id;
};
this.newId = function(idInt) {
_id = idInt;
};
var onclickString = "";
this.onclick = function(_onclickString){
onclickString = _onclickString;
};
// BACKGROUND //////////////////
var _background = _backgroundStr;
this.setBackground = function(_backgroundStr){
if (_backgroundStr !== null){
_background = _backgroundStr;
}
};
this.background = function (){
return _background;
};
//-------------------------------------------------------------------//
var _offsetX = 0;
var _offsetY = 0;
var _percentageCovered = _percentageInt;
this.percentageSize = function(){
return _percentageCovered;
};
this.setPercentageSize = function(_covered){
_percentageCovered = _covered;
_percentageEndingAt = _percentageStartingAt + _covered;
};
var _percentageStartingAt = _percentageStartInt;
this.percentageStart = function(){
return _percentageStartingAt;
};
var _percentageEndingAt = _percentageStartingAt + _percentageCovered;
this.percentageEnd = function(){
return _percentageEndingAt;
};
this.setPercentages = function(_start, _covered){
_percentageCovered = _covered;
_percentageStartingAt = _start;
_percentageEndingAt = _start + _covered;
};
// TODO: make next/previous private!
this.next = null;
this.hasNext = function() {
return (this.next !== null);
};
this.previous = null;
this.hasPrevious = function() {
return (this.previous !== null);
};
/**
* Methods returns HTML-Code of the Slice by creating it with attributes
* saved in calling Slice
* @returns {HTML} - HTML-Element - HTML-Code of the slice
*/
this.html = function() {
if (onclickString !== ""){
return pie.createClickableSlice(_percentageCovered, _percentageStartingAt, onclickString, _background, _offsetX, _offsetY);
} else {
return pie.createSlice(_percentageCovered, _percentageStartingAt, _background, _offsetX, _offsetY);
}
};
/**
* Method offsets the slice by given values for top/right
* @param {Float} _topNum - Value to move the Slice by
* @param {Float} _leftNum - Value to move the Slice by
*/
this._moveSlice = function(_topNum,_leftNum){
_offsetX += _leftNum;
_offsetY += _topNum;
};
/**
* Moves a Slice away from the center of the circle by the given value.
* Moving Vector is the half angle between starting and ending angle/percentage
* of the Slice.
*
* @param {type} _offsetToMiddleInt
* Value to move the Slice by
*/
this._offset = function(_offsetToMiddleInt){
var percentageMid = (this.percentageStart() + this.percentageEnd()) / 2;
// calculating vector through triangle
var degree = pie.percentageToDegree(percentageMid);
var rad = pie.percentageToRadiant(percentageMid);
// var hypo = (pie.size()/2); // Point on circle / mid if degrees from slice / searched vector
// var cos = Math.abs(Math.cos(degree));
// var sin = Math.abs(Math.sin(degree));
var cos = Math.cos(rad);
var sin = Math.sin(rad);
////// Probe /////////////
// sin = gegenkathete / Hypotenuse
// gegenkathete = Hypo * sin
// vectorX = hypo * sin;
// alert("vectorX= " + vectorX);
// cos = ankathete / Hypothenuse
// ankathete = Hypo * cos
// vectorY = hypo * cos;
// alert("vectorY= " + vectorY);
var vectorMoveX = 0.0;
var vectorMoveY = 0.0;
vectorMoveX = (_offsetToMiddleInt * sin)/2;
vectorMoveY = -(_offsetToMiddleInt * cos)/2;
this._moveSlice(vectorMoveY, vectorMoveX);
};
};
Slice.prototype = {
/**
* Checks if the calling Slice equals the passed Slice
* @param {Slice} _sliceToCompareTo
* @returns {Boolean}
*/
equals: function(_sliceToCompareTo) {
// TODO: more parameters for comparisson and perhaps compare() method
var equalId = this.id() === _sliceToCompareTo.id();
if (equalId) {
return true;
}
},
/** updates all ids to be increasing by 1 each slice */
_updateFollowingIds: function() {
var next = this.next;
if (next !== null) {
next.newId((this.id()) + 1);
next._updateFollowingIds();
}
return;
},
/**
* updates all following slices to have their starting percentage
* set to the previous ending percentage
*/
_updateFollowingPercentages: function(){
var next = this.next;
if(next !== null){
// alert(this.percentageEnd() + " " + next.percentageStart());
next.setPercentages(this.percentageEnd(),next.percentageSize());
// alert(this.percentageEnd() + " " + next.percentageStart());
next._updateFollowingPercentages();
}
else {
return;
}
},
/**
* Cycles through all Slices to find the passed Slice
* @param {type} _sliceToFind
* @returns {Boolean} - returns true if Slice is found
*/
_isSliceInPie: function(_sliceToFind) {
if (this.equals(_sliceToFind)) {
return true;
}
var next = this.next;
if (next !== null) {
return next.isSliceInPie(_sliceToFind);
}
// alert("currently processing: " + this.id()); // DEBUGG INFO
return false;
},
/**
* Cycles through all Slices to find the Slice with passed id
* @param {type} _idToFindInt
* @returns {Pie.Slice.prototype} - returns Slice if found or null if not in Pie
*/
_findSlice: function(_idToFindInt) {
if (this.id() === _idToFindInt) {
// alert("found " + this.id());
return this;
} else if (this.next !== null) {
return this.next._findSlice(_idToFindInt);
}
return null;
},
/**
* Redirects the references from surrounding Slices to remove
* the calling Slice from the links
* @returns {Pie.Slice.prototype} the removed Slice
*/
_removeFromLinks: function(){
var previous = this.previous;
var next = this.next;
if (previous!== null){
previous.next = next;
}
if (next !== null){
next.previous = previous;
}
return this;
}
};
//------------------------------------------------------------------------//
// how to define Slicegroup as a Slice? (extends, vererbung)
/**
* Slicegroup Objects are used to group up slices and use the same
* manipulation on all of them equally.
* Slicegroups are not organized as linked Elements,
* so they have to be managed from outside.
*
* @param {type} _pie
* Pie Object that the slicegroup belongs to
* @param {type} _sliceArray
* Array with all Slices to group together
* !!has to be ordered from lowest id to highest!!
* @returns {Pie.Slicegroup}
*/
function Slicegroup(_pie,_sliceArray){
// TODO: fix the order-dependency of _sliceArray
var pie = _pie;
var _groupId;
_groupId = pie.slicegroups.length + 1;
this.id=function(){
return _groupId;
};
// Slices ///////////////////////////////////////////
this.slices = [];
for (var i = 0; i < _sliceArray.length; i++){
// console.log("slice pushed in group: " + _sliceArray[i].id()); // DEBUGG INFO
this.slices.push(_sliceArray[i]);
};
/**
* Returns the n-th slice within the group.
* @param {type} _sliceInt
* Number of the slice within the group (n-th)
* @returns {Slice}
*/
this.slice = function(_sliceInt){
var sliceIndexInt = _sliceInt -1;
// TODO automatic ordering of the given slices by id
if (sliceIndexInt < _sliceArray.length){
return slices[sliceIndexInt];
}
else return null;
};
/**
* Moves all Slices of the group.
* @param {type} _offsetX
* "left" value to add
* @param {type} _offsetY
* "top" value to add
*/
this.moveSlicegroup = function(_offsetX, _offsetY){
for (var i = 0; i< this.slices.length; i++){
this.slices[i]._moveSlice(_offsetX, _offsetY);
console.log("moved slices: "+ this.slices[i].id()); // debugg
};
};
/**
* Moves a Slicegroup away from the center of the circle by the given value.
* Moving Vector is the half angle between starting and ending angle/percentage
* of the Slice.
* @param {Float} _offsetValueNum
* Value to move the group away from the center
*/
this.offsetSlicegroup = function(_offsetValueNum){
var percentageMid = (this.slices[0].percentageStart() + this.slices[(this.slices.length-1)].percentageEnd()) / 2;
// calculating vector through triangle
var degree = pie.percentageToDegree(percentageMid);
var rad = pie.percentageToRadiant(percentageMid);
var cos = Math.cos(rad);
var sin = Math.sin(rad);
var vectorMoveX = 0.0;
var vectorMoveY = 0.0;
vectorMoveX = (_offsetValueNum * sin)/2;
vectorMoveY = -(_offsetValueNum * cos)/2;
this.moveSlicegroup(vectorMoveY, vectorMoveX);
};
/**
* Checks if the Slice with the given id is in this Slicegroup.
* @param {type} _sliceIdInt
* @returns {Boolean}
*/
this.isSliceInGroup = function(_sliceIdInt){
var found = false;
for (var i = 0; i< this.slices.length; i++){
if (this.slices[i].id() === _sliceIdInt){
found = true;
}
}
return found;
};
};
/////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------//
/**
* Use iterator() to get your Iterator
* @param {type} _pie
* @returns {Pie.Iterator}
*/
function Iterator(_pie) {
var _currentSlice = _pie.first();
this.first = function() {
_currentSlice = _pie.first();
return _pie.first();
};
this.last = function() {
_currentSlice = _pie.last();
return _pie.last();
};
this.current = function(){
return _currentSlice;
};
this.next = function() {
_currentSlice = _currentSlice.next;
return _currentSlice;
};
this.previous = function() {
_currentSlice = _currentSlice.previous;
return _currentSlice;
};
/**
* Checks if there is a next slice and if so changes the cursor to it!
* @returns {Boolean} true if nextSlice equals currentslice
*/
this.hasNext = function() {
if (_currentSlice.equals(this.next())){
return false;
}
else return true;
};
/**
* Checks if there is a previous slice and if so changes the cursor to it!
* @returns {Boolean} true if previousSlice equals currentslice
*/
this.hasPrevious = function() {
if (_currentSlice.equals(this.previous())){
return false;
}
else return true;
};
};
/**
* The reversed Iterator starts at the last Slice and has its operations reversed:
* it.next returns currentSlice.previous so that the Iterator may
* cycle from last to first Slice of the pie (it.first() and it.last() respectively) by only using .next()
*
* Use reversedIterator() to get your reversed Iterator.
* @param {type} _pie
* @returns {Pie.ReversedIterator}
*/
function ReversedIterator(_pie) {
var _currentSlice = _pie.last();
this.first = function() {
_currentSlice = _pie.last();
return _pie.last();
};
this.last = function() {
_currentSlice = _pie.first();
return _pie.first();
};
this.current = function(){
return _currentSlice;
};
this.next = function() {
_currentSlice = _currentSlice.previous;
return _currentSlice;
};
this.previous = function() {
_currentSlice = _currentSlice.next;
return _currentSlice;
};
/**
* Checks if there is a next slice and if so changes the cursor to it!
* @returns {Boolean} true if nextSlice equals currentslice
*/
this.hasNext = function() {
if (_currentSlice.equals(this.next())){
return false;
}
else return true;
};
/**
* Checks if there is a previous slice and if so changes the cursor to it!
* @returns {Boolean} true if previousSlice equals currentslice
*/
this.hasPrevious = function() {
if (_currentSlice.equals(this.previous())){
return false;
}
else return true;
};
};
/**
* Returns an Iterator for the pie
* @returns {Pie.Iterator}
*/
this.iterator = function() {
return new Iterator(this);
};
/**
* Returns an reversed Iterator for the pie
* @returns {Pie.Iterator}
*/
this.reversedIterator = function() {
return new ReversedIterator(this);
};
}; // Pie END //////////////////////////////////////////////////////////////////
/** Contains basic Methods neccessary to create a pie and its slices */
Pie.prototype = { // Pie.prototype Start ///////////////////////////////////////
_createBackground: function(_basecolorStr) {
var sizeString = this.sizeString();
var pieBackground = document.createElement("div");
pieBackground.style.width = sizeString;
pieBackground.style.height = sizeString;
pieBackground.style.position = "relative";
pieBackground.style.webkitBorderRadius = "100%";
pieBackground.style.mozBorderRadius = "100%";
pieBackground.style.borderRadius = "100%";
pieBackground.style.backgroundColor = _basecolorStr;
return pieBackground;
},
/**
* takes degree-values and color to create a slice of the calling pieObject
*
* @param {Int} _percentageInt
* value how big the slice will be in percent of the pie
* @param {Int} _percentageStartInt value at which the slice starts (0=top mid of circle)
* @param {String} _colorString color of the slice as String (any legal css colorvalue)
* @returns {"div"} Slice of the pie
*/
createSlice: function(_percentageInt, _percentageStartInt, _backgroundStr, _offsetX, _offsetY) {
var sliceHtml = this._createSlice(_percentageInt, _percentageStartInt, _backgroundStr);
sliceHtml.style.top = _offsetY + "" + this.sizeUnit();
sliceHtml.style.left = _offsetX + "" + this.sizeUnit();
if (parseInt(_offsetX) !== 0 || parseInt(_offsetY) !== 0){
sliceHtml.style.zIndex += 1;
}
return sliceHtml;
},
createClickableSlice: function(_percentageInt, _percentageStartInt, _onclickString, _basecolorStr, _offsetX, _offsetY){
var sliceHtml = this._createSlice(_percentageInt, _percentageStartInt, _basecolorStr);
sliceHtml.setAttribute("onclick",_onclickString);
sliceHtml.style.cursor = "pointer";
return sliceHtml;
},
_createSlice: function(_percentageInt, _percentageStartInt, _backgroundStr) {
if (_percentageInt <= 50) {
var sliceHtml = this._createSlimSlice(_percentageInt, _percentageStartInt, _backgroundStr);
}
else {
var sliceHtml = this._createBigSlice(_percentageInt, _percentageStartInt, _backgroundStr);
}
// sliceHtml.style.top = _offsetY + "" + this.sizeUnit();
// sliceHtml.style.left = _offsetX + "" + this.sizeUnit();
// if (parseInt(_offsetX) !== 0 || parseInt(_offsetY) !== 0){
// sliceHtml.style.zIndex += 1;
// }
return sliceHtml;
},
/**
* creates Slice of up to 50% value
* @param {Int} _percentageInt - percentage value the slice contains
* @param {Int} _percentageStartInt - percentage value to start the slice at
* @param {String} _colorString color of the slice as String (any legal css colorvalue)
* @returns {"div"} Container with the big Slice
*/
_createSlimSlice: function(_percentageInt, _percentageStartInt, _backgroundStr) {
var degree = this.percentageToDegree(_percentageInt);
var startDegree = this.percentageToDegree(_percentageStartInt);
//New Slice
var sliceHtml = this._createSliceMask();