-
Notifications
You must be signed in to change notification settings - Fork 0
/
c3.js
5610 lines (5190 loc) · 243 KB
/
c3.js
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
(function (window) {
'use strict';
/*global define, module, exports, require */
var c3 = {
version: "0.2.4"
};
var CLASS = {
target: 'c3-target',
chart : 'c3-chart',
chartLine: 'c3-chart-line',
chartLines: 'c3-chart-lines',
chartBar: 'c3-chart-bar',
chartBars: 'c3-chart-bars',
chartText: 'c3-chart-text',
chartTexts: 'c3-chart-texts',
chartArc: 'c3-chart-arc',
chartArcs: 'c3-chart-arcs',
chartArcsTitle: 'c3-chart-arcs-title',
chartArcsBackground: 'c3-chart-arcs-background',
chartArcsGaugeUnit: 'c3-chart-arcs-gauge-unit',
chartArcsGaugeMax: 'c3-chart-arcs-gauge-max',
chartArcsGaugeMin: 'c3-chart-arcs-gauge-min',
selectedCircle: 'c3-selected-circle',
selectedCircles: 'c3-selected-circles',
eventRect: 'c3-event-rect',
eventRects: 'c3-event-rects',
eventRectsSingle: 'c3-event-rects-single',
eventRectsMultiple: 'c3-event-rects-multiple',
zoomRect: 'c3-zoom-rect',
brush: 'c3-brush',
focused: 'c3-focused',
region: 'c3-region',
regions: 'c3-regions',
tooltip: 'c3-tooltip',
tooltipName: 'c3-tooltip-name',
shape: 'c3-shape',
shapes: 'c3-shapes',
line: 'c3-line',
lines: 'c3-lines',
bar: 'c3-bar',
bars: 'c3-bars',
circle: 'c3-circle',
circles: 'c3-circles',
arc: 'c3-arc',
arcs: 'c3-arcs',
area: 'c3-area',
areas: 'c3-areas',
empty: 'c3-empty',
text: 'c3-text',
texts: 'c3-texts',
gaugeValue: 'c3-gauge-value',
grid: 'c3-grid',
xgrid: 'c3-xgrid',
xgrids: 'c3-xgrids',
xgridLine: 'c3-xgrid-line',
xgridLines: 'c3-xgrid-lines',
xgridFocus: 'c3-xgrid-focus',
ygrid: 'c3-ygrid',
ygrids: 'c3-ygrids',
ygridLine: 'c3-ygrid-line',
ygridLines: 'c3-ygrid-lines',
axis: 'c3-axis',
axisX: 'c3-axis-x',
axisXLabel: 'c3-axis-x-label',
axisY: 'c3-axis-y',
axisYLabel: 'c3-axis-y-label',
axisY2: 'c3-axis-y2',
axisY2Label: 'c3-axis-y2-label',
legendBackground: 'c3-legend-background',
legendItem: 'c3-legend-item',
legendItemEvent: 'c3-legend-item-event',
legendItemTile: 'c3-legend-item-tile',
legendItemHidden: 'c3-legend-item-hidden',
legendItemFocused: 'c3-legend-item-focused',
dragarea: 'c3-dragarea',
EXPANDED: '_expanded_',
SELECTED: '_selected_',
INCLUDED: '_included_',
};
/*
* Generate chart according to config
*/
c3.generate = function (config) {
var d3 = window.d3 ? window.d3 : 'undefined' !== typeof require ? require("d3") : undefined;
var c3 = { data : {}, axis: {}, legend: {} },
cache = {};
/*-- Handle Config --*/
function checkConfig(key, message) {
if (! (key in config)) { throw Error(message); }
}
function getConfig(keys, defaultValue) {
var target = config, i, isLast, nextTarget;
for (i = 0; i < keys.length; i++) {
// return default if key not found
if (typeof target === 'object' && !(keys[i] in target)) { return defaultValue; }
// Check next key's value
isLast = (i === keys.length - 1);
nextTarget = target[keys[i]];
if (!isLast && typeof nextTarget !== 'object') {
return defaultValue;
}
target = nextTarget;
}
return target;
}
// bindto - id to bind the chart
var __bindto = getConfig(['bindto'], '#chart');
var __size_width = getConfig(['size', 'width']),
__size_height = getConfig(['size', 'height']);
var __padding_left = getConfig(['padding', 'left']),
__padding_right = getConfig(['padding', 'right']),
__padding_top = getConfig(['padding', 'top']),
__padding_bottom = getConfig(['padding', 'bottom']);
var __zoom_enabled = getConfig(['zoom', 'enabled'], false),
__zoom_extent = getConfig(['zoom', 'extent']),
__zoom_privileged = getConfig(['zoom', 'privileged'], false),
__zoom_onzoom = getConfig(['zoom', 'onzoom'], function () {});
var __interaction_enabled = getConfig(['interaction', 'enabled'], true);
var __onmouseover = getConfig(['onmouseover'], function () {}),
__onmouseout = getConfig(['onmouseout'], function () {}),
__onresize = getConfig(['onresize'], function () {}),
__onresized = getConfig(['onresized'], function () {});
var __transition_duration = getConfig(['transition', 'duration'], 350);
// data - data configuration
checkConfig('data', 'data is required in config');
var __data_x = getConfig(['data', 'x']),
__data_xs = getConfig(['data', 'xs'], {}),
__data_x_format = getConfig(['data', 'x_format'], '%Y-%m-%d'),
__data_x_localtime = getConfig(['data', 'x_localtime'], true),
__data_id_converter = getConfig(['data', 'id_converter'], function (id) { return id; }),
__data_names = getConfig(['data', 'names'], {}),
__data_classes = getConfig(['data', 'classes'], {}),
__data_groups = getConfig(['data', 'groups'], []),
__data_axes = getConfig(['data', 'axes'], {}),
__data_type = getConfig(['data', 'type']),
__data_types = getConfig(['data', 'types'], {}),
__data_labels = getConfig(['data', 'labels'], {}),
__data_order = getConfig(['data', 'order'], null),
__data_regions = getConfig(['data', 'regions'], {}),
__data_color = getConfig(['data', 'color']),
__data_colors = getConfig(['data', 'colors'], {}),
__data_hide = getConfig(['data', 'hide'], false),
__data_filter = getConfig(['data', 'filter']),
__data_selection_enabled = getConfig(['data', 'selection', 'enabled'], false),
__data_selection_grouped = getConfig(['data', 'selection', 'grouped'], false),
__data_selection_isselectable = getConfig(['data', 'selection', 'isselectable'], function () { return true; }),
__data_selection_multiple = getConfig(['data', 'selection', 'multiple'], true),
__data_onclick = getConfig(['data', 'onclick'], function () {}),
__data_onmouseover = getConfig(['data', 'onmouseover'], function () {}),
__data_onmouseout = getConfig(['data', 'onmouseout'], function () {}),
__data_onselected = getConfig(['data', 'onselected'], function () {}),
__data_onunselected = getConfig(['data', 'onunselected'], function () {}),
__data_ondragstart = getConfig(['data', 'ondragstart'], function () {}),
__data_ondragend = getConfig(['data', 'ondragend'], function () {});
// configuration for no plot-able data supplied.
var __data_empty_label_text = getConfig(['data', 'empty', 'label', 'text'], "");
// subchart
var __subchart_show = getConfig(['subchart', 'show'], false),
__subchart_size_height = getConfig(['subchart', 'size', 'height'], 60),
__subchart_onbrush = getConfig(['subchart', 'onbrush'], function () {});
// color
var __color_pattern = getConfig(['color', 'pattern'], []),
__color_threshold = getConfig(['color', 'threshold'], {});
// legend
var __legend_show = getConfig(['legend', 'show'], true),
__legend_position = getConfig(['legend', 'position'], 'bottom'),
__legend_inset_anchor = getConfig(['legend', 'inset', 'anchor'], 'top-left'),
__legend_inset_x = getConfig(['legend', 'inset', 'x'], 10),
__legend_inset_y = getConfig(['legend', 'inset', 'y'], 0),
__legend_inset_step = getConfig(['legend', 'inset', 'step']),
__legend_item_onclick = getConfig(['legend', 'item', 'onclick']),
__legend_item_onmouseover = getConfig(['legend', 'item', 'onmouseover']),
__legend_item_onmouseout = getConfig(['legend', 'item', 'onmouseout']),
__legend_equally = getConfig(['legend', 'equally'], false);
// axis
var __axis_rotated = getConfig(['axis', 'rotated'], false),
__axis_x_show = getConfig(['axis', 'x', 'show'], true),
__axis_x_type = getConfig(['axis', 'x', 'type'], 'indexed'),
__axis_x_localtime = getConfig(['axis', 'x', 'localtime'], true),
__axis_x_categories = getConfig(['axis', 'x', 'categories'], []),
__axis_x_tick_centered = getConfig(['axis', 'x', 'tick', 'centered'], false),
__axis_x_tick_format = getConfig(['axis', 'x', 'tick', 'format']),
__axis_x_tick_culling = getConfig(['axis', 'x', 'tick', 'culling'], {}),
__axis_x_tick_culling_max = getConfig(['axis', 'x', 'tick', 'culling', 'max'], 10),
__axis_x_tick_count = getConfig(['axis', 'x', 'tick', 'count']),
__axis_x_tick_fit = getConfig(['axis', 'x', 'tick', 'fit'], true),
__axis_x_tick_values = getConfig(['axis', 'x', 'tick', 'values'], null),
__axis_x_tick_rotate = getConfig(['axis', 'x', 'tick', 'rotate']),
__axis_x_tick_outer = getConfig(['axis', 'x', 'tick', 'outer'], true),
__axis_x_max = getConfig(['axis', 'x', 'max'], null),
__axis_x_min = getConfig(['axis', 'x', 'min'], null),
__axis_x_padding = getConfig(['axis', 'x', 'padding'], {}),
__axis_x_height = getConfig(['axis', 'x', 'height']),
__axis_x_default = getConfig(['axis', 'x', 'default']),
__axis_x_label = getConfig(['axis', 'x', 'label'], {}),
__axis_y_show = getConfig(['axis', 'y', 'show'], true),
__axis_y_max = getConfig(['axis', 'y', 'max']),
__axis_y_min = getConfig(['axis', 'y', 'min']),
__axis_y_center = getConfig(['axis', 'y', 'center']),
__axis_y_label = getConfig(['axis', 'y', 'label'], {}),
__axis_y_inner = getConfig(['axis', 'y', 'inner'], false),
__axis_y_tick_format = getConfig(['axis', 'y', 'tick', 'format']),
__axis_y_tick_outer = getConfig(['axis', 'y', 'tick', 'outer'], true),
__axis_y_padding = getConfig(['axis', 'y', 'padding']),
__axis_y_ticks = getConfig(['axis', 'y', 'ticks'], 10),
__axis_y2_show = getConfig(['axis', 'y2', 'show'], false),
__axis_y2_max = getConfig(['axis', 'y2', 'max']),
__axis_y2_min = getConfig(['axis', 'y2', 'min']),
__axis_y2_center = getConfig(['axis', 'y2', 'center']),
__axis_y2_label = getConfig(['axis', 'y2', 'label'], {}),
__axis_y2_inner = getConfig(['axis', 'y2', 'inner'], false),
__axis_y2_tick_format = getConfig(['axis', 'y2', 'tick', 'format']),
__axis_y2_tick_outer = getConfig(['axis', 'y2', 'tick', 'outer'], true),
__axis_y2_padding = getConfig(['axis', 'y2', 'padding']),
__axis_y2_ticks = getConfig(['axis', 'y2', 'ticks'], 10);
// grid
var __grid_x_show = getConfig(['grid', 'x', 'show'], false),
__grid_x_type = getConfig(['grid', 'x', 'type'], 'tick'),
__grid_x_lines = getConfig(['grid', 'x', 'lines'], []),
__grid_y_show = getConfig(['grid', 'y', 'show'], false),
// not used
// __grid_y_type = getConfig(['grid', 'y', 'type'], 'tick'),
__grid_y_lines = getConfig(['grid', 'y', 'lines'], []),
__grid_y_ticks = getConfig(['grid', 'y', 'ticks'], 10),
__grid_focus_show = getConfig(['grid', 'focus', 'show'], true);
// point - point of each data
var __point_show = getConfig(['point', 'show'], true),
__point_r = getConfig(['point', 'r'], 2.5),
__point_focus_expand_enabled = getConfig(['point', 'focus', 'expand', 'enabled'], true),
__point_focus_expand_r = getConfig(['point', 'focus', 'expand', 'r']),
__point_select_r = getConfig(['point', 'focus', 'select', 'r']),
__point_paint_prop = getConfig(['point', 'paintProperty'], 'fill');
var __line_connect_null = getConfig(['line', 'connect_null'], false);
// bar
var __bar_width = getConfig(['bar', 'width']),
__bar_width_ratio = getConfig(['bar', 'width', 'ratio'], 0.6),
__bar_width_max = getConfig(['bar', 'width', 'max']),
__bar_zerobased = getConfig(['bar', 'zerobased'], true);
// area
var __area_zerobased = getConfig(['area', 'zerobased'], true);
// pie
var __pie_label_show = getConfig(['pie', 'label', 'show'], true),
__pie_label_format = getConfig(['pie', 'label', 'format']),
__pie_label_threshold = getConfig(['pie', 'label', 'threshold'], 0.05),
__pie_sort = getConfig(['pie', 'sort'], false),
__pie_expand = getConfig(['pie', 'expand'], true);
// gauge
var __gauge_label_show = getConfig(['gauge', 'label', 'show'], true),
__gauge_label_format = getConfig(['gauge', 'label', 'format']),
__gauge_expand = getConfig(['gauge', 'expand'], true),
__gauge_min = getConfig(['gauge', 'min'], 0),
__gauge_max = getConfig(['gauge', 'max'], 100),
__gauge_units = getConfig(['gauge', 'units']),
__gauge_width = getConfig(['gauge', 'width']),
__gauge_format = getConfig(['gauge', 'format'], function(d){ return d; });
// donut
var __donut_label_show = getConfig(['donut', 'label', 'show'], true),
__donut_label_format = getConfig(['donut', 'label', 'format']),
__donut_label_threshold = getConfig(['donut', 'label', 'threshold'], 0.05),
__donut_width = getConfig(['donut', 'width']),
__donut_sort = getConfig(['donut', 'sort'], true),
__donut_expand = getConfig(['donut', 'expand'], true),
__donut_title = getConfig(['donut', 'title'], "");
// region - region to change style
var __regions = getConfig(['regions'], []);
// tooltip - show when mouseover on each data
var __tooltip_show = getConfig(['tooltip', 'show'], true),
__tooltip_grouped = getConfig(['tooltip', 'grouped'], true),
__tooltip_format_title = getConfig(['tooltip', 'format', 'title']),
__tooltip_format_name = getConfig(['tooltip', 'format', 'name']),
__tooltip_format_value = getConfig(['tooltip', 'format', 'value']),
__tooltip_contents = getConfig(['tooltip', 'contents'], function (d, defaultTitleFormat, defaultValueFormat, color) {
var titleFormat = __tooltip_format_title ? __tooltip_format_title : defaultTitleFormat,
nameFormat = __tooltip_format_name ? __tooltip_format_name : function (name) { return name; },
valueFormat = __tooltip_format_value ? __tooltip_format_value : defaultValueFormat,
text, i, title, value, name, bgcolor;
for (i = 0; i < d.length; i++) {
if (! (d[i] && (d[i].value || d[i].value === 0))) { continue; }
if (! text) {
title = titleFormat ? titleFormat(d[i].x) : d[i].x;
text = "<table class='" + CLASS.tooltip + "'>" + (title || title === 0 ? "<tr><th colspan='2'>" + title + "</th></tr>" : "");
}
name = nameFormat(d[i].name);
value = valueFormat(d[i].value, d[i].ratio, d[i].id, d[i].index);
bgcolor = levelColor ? levelColor(d[i].value) : color(d[i].id);
text += "<tr class='" + CLASS.tooltipName + "-" + d[i].id + "'>";
text += "<td class='name'><span style='background-color:" + bgcolor + "'></span>" + name + "</td>";
text += "<td class='value'>" + value + "</td>";
text += "</tr>";
}
return text + "</table>";
}),
__tooltip_init_show = getConfig(['tooltip', 'init', 'show'], false),
__tooltip_init_x = getConfig(['tooltip', 'init', 'x'], 0),
__tooltip_init_position = getConfig(['tooltip', 'init', 'position'], {top: '0px', left: '50px'});
/*-- Set Variables --*/
// MEMO: clipId needs to be unique because it conflicts when multiple charts exist
var clipId = "c3-" + (+new Date()) + '-clip',
clipIdForXAxis = clipId + '-xaxis',
clipIdForYAxis = clipId + '-yaxis',
clipPath = getClipPath(clipId),
clipPathForXAxis = getClipPath(clipIdForXAxis),
clipPathForYAxis = getClipPath(clipIdForYAxis);
var isTimeSeries = (__axis_x_type === 'timeseries'),
isCategorized = (__axis_x_type.indexOf('categor') >= 0),
isCustomX = function () { return !isTimeSeries && (__data_x || notEmpty(__data_xs)); };
var dragStart = null, dragging = false, cancelClick = false, mouseover = false, transiting = false;
var defaultColorPattern = d3.scale.category10().range(),
color = generateColor(__data_colors, notEmpty(__color_pattern) ? __color_pattern : defaultColorPattern, __data_color),
levelColor = notEmpty(__color_threshold) ? generateLevelColor(__color_pattern, __color_threshold) : null;
var dataTimeFormat = __data_x_localtime ? d3.time.format : d3.time.format.utc,
axisTimeFormat = __axis_x_localtime ? d3.time.format : d3.time.format.utc,
defaultAxisTimeFormat = axisTimeFormat.multi([
[".%L", function (d) { return d.getMilliseconds(); }],
[":%S", function (d) { return d.getSeconds(); }],
["%I:%M", function (d) { return d.getMinutes(); }],
["%I %p", function (d) { return d.getHours(); }],
["%-m/%-d", function (d) { return d.getDay() && d.getDate() !== 1; }],
["%-m/%-d", function (d) { return d.getDate() !== 1; }],
["%-m/%-d", function (d) { return d.getMonth(); }],
["%Y/%-m/%-d", function () { return true; }]
]);
var hiddenTargetIds = [], hiddenLegendIds = [];
/*-- Set Chart Params --*/
var margin, margin2, margin3, width, width2, height, height2, currentWidth, currentHeight;
var radius, radiusExpanded, innerRadius, arcWidth, arcHeight, svgArc, svgArcExpanded, svgArcExpandedSub, pie;
var xMin, xMax, yMin, yMax, subXMin, subXMax, subYMin, subYMax;
var x, y, y2, subX, subY, subY2, xAxis, yAxis, y2Axis, subXAxis;
var axes = {};
var xOrient = __axis_rotated ? "left" : "bottom",
yOrient = __axis_rotated ? (__axis_y_inner ? "top" : "bottom") : (__axis_y_inner ? "right" : "left"),
y2Orient = __axis_rotated ? (__axis_y2_inner ? "bottom" : "top") : (__axis_y2_inner ? "left" : "right"),
subXOrient = __axis_rotated ? "left" : "bottom";
var translate = {
main : function () { return "translate(" + asHalfPixel(margin.left) + "," + asHalfPixel(margin.top) + ")"; },
context : function () { return "translate(" + asHalfPixel(margin2.left) + "," + asHalfPixel(margin2.top) + ")"; },
legend : function () { return "translate(" + margin3.left + "," + margin3.top + ")"; },
x : function () { return "translate(0," + (__axis_rotated ? 0 : height) + ")"; },
y : function () { return "translate(0," + (__axis_rotated ? height : 0) + ")"; },
y2 : function () { return "translate(" + (__axis_rotated ? 0 : width) + "," + (__axis_rotated ? 1 : 0) + ")"; },
subx : function () { return "translate(0," + (__axis_rotated ? 0 : height2) + ")"; },
arc: function () { return "translate(" + (arcWidth / 2) + "," + (arcHeight / 2) + ")"; }
};
var isLegendRight = __legend_position === 'right';
var isLegendInset = __legend_position === 'inset';
var isLegendTop = __legend_inset_anchor === 'top-left' || __legend_inset_anchor === 'top-right';
var isLegendLeft = __legend_inset_anchor === 'top-left' || __legend_inset_anchor === 'bottom-left';
var legendStep = 0, legendItemWidth = 0, legendItemHeight = 0, legendOpacityForHidden = 0.15;
var currentMaxTickWidth = 0;
/*-- Define Functions --*/
function getClipPath(id) {
var isIE9 = window.navigator.appVersion.toLowerCase().indexOf("msie 9.") >= 0;
return "url(" + (isIE9 ? "" : document.URL.split('#')[0]) + "#" + id + ")";
}
function asHalfPixel(n) {
return Math.ceil(n) + 0.5;
}
function transformMain(withTransition, transitions) {
var xAxis, yAxis, y2Axis;
if (transitions && transitions.axisX) {
xAxis = transitions.axisX;
} else {
xAxis = main.select('.' + CLASS.axisX);
if (withTransition) { xAxis = xAxis.transition(); }
}
if (transitions && transitions.axisY) {
yAxis = transitions.axisY;
} else {
yAxis = main.select('.' + CLASS.axisY);
if (withTransition) { yAxis = yAxis.transition(); }
}
if (transitions && transitions.axisY2) {
y2Axis = transitions.axisY2;
} else {
y2Axis = main.select('.' + CLASS.axisY2);
if (withTransition) { y2Axis = y2Axis.transition(); }
}
(withTransition ? main.transition() : main).attr("transform", translate.main);
xAxis.attr("transform", translate.x);
yAxis.attr("transform", translate.y);
y2Axis.attr("transform", translate.y2);
main.select('.' + CLASS.chartArcs).attr("transform", translate.arc);
}
function transformContext(withTransition, transitions) {
var subXAxis;
if (transitions && transitions.axisSubX) {
subXAxis = transitions.axisSubX;
} else {
subXAxis = context.select('.' + CLASS.axisX);
if (withTransition) { subXAxis = subXAxis.transition(); }
}
context.attr("transform", translate.context);
subXAxis.attr("transform", translate.subx);
}
function transformLegend(withTransition) {
(withTransition ? legend.transition() : legend).attr("transform", translate.legend);
}
function transformAll(withTransition, transitions) {
transformMain(withTransition, transitions);
if (__subchart_show) { transformContext(withTransition, transitions); }
transformLegend(withTransition);
}
//-- Sizes --//
// TODO: configurabale
var rotated_padding_left = 30, rotated_padding_right = __axis_rotated && !__axis_x_show ? 0 : 30, rotated_padding_top = 5;
// MEMO: each value should be int to avoid disabling antialiasing
function updateSizes() {
var legendHeight = getLegendHeight(), legendWidth = getLegendWidth(),
legendHeightForBottom = isLegendRight || isLegendInset ? 0 : legendHeight,
hasArc = hasArcType(c3.data.targets),
xAxisHeight = __axis_rotated || hasArc ? 0 : getHorizontalAxisHeight('x'),
subchartHeight = __subchart_show && !hasArc ? (__subchart_size_height + xAxisHeight) : 0;
currentWidth = getCurrentWidth();
currentHeight = getCurrentHeight();
// for main, context
if (__axis_rotated) {
margin = {
top: getHorizontalAxisHeight('y2') + getCurrentPaddingTop(),
right: hasArc ? 0 : getCurrentPaddingRight(),
bottom: getHorizontalAxisHeight('y') + legendHeightForBottom + getCurrentPaddingBottom(),
left: subchartHeight + (hasArc ? 0 : getCurrentPaddingLeft())
};
margin2 = {
top: margin.top,
right: NaN,
bottom: 20 + legendHeightForBottom,
left: rotated_padding_left
};
} else {
margin = {
top: 4 + getCurrentPaddingTop(), // for top tick text
right: hasArc ? 0 : getCurrentPaddingRight(),
bottom: xAxisHeight + subchartHeight + legendHeightForBottom + getCurrentPaddingBottom(),
left: hasArc ? 0 : getCurrentPaddingLeft()
};
margin2 = {
top: currentHeight - subchartHeight - legendHeightForBottom,
right: NaN,
bottom: xAxisHeight + legendHeightForBottom,
left: margin.left
};
}
// for legend
var insetLegendPosition = {
top: isLegendTop ? getCurrentPaddingTop() + __legend_inset_y + 5.5 : currentHeight - legendHeight - getCurrentPaddingBottom() - __legend_inset_y,
left: isLegendLeft ? getCurrentPaddingLeft() + __legend_inset_x + 0.5 : currentWidth - legendWidth - getCurrentPaddingRight() - __legend_inset_x + 0.5
};
margin3 = {
top: isLegendRight ? 0 : isLegendInset ? insetLegendPosition.top : currentHeight - legendHeight,
right: NaN,
bottom: 0,
left: isLegendRight ? currentWidth - legendWidth : isLegendInset ? insetLegendPosition.left : 0
};
width = currentWidth - margin.left - margin.right;
height = currentHeight - margin.top - margin.bottom;
if (width < 0) { width = 0; }
if (height < 0) { height = 0; }
width2 = __axis_rotated ? margin.left - rotated_padding_left - rotated_padding_right : width;
height2 = __axis_rotated ? height : currentHeight - margin2.top - margin2.bottom;
if (width2 < 0) { width2 = 0; }
if (height2 < 0) { height2 = 0; }
// for arc
arcWidth = width - (isLegendRight ? legendWidth + 10 : 0);
arcHeight = height - (isLegendRight ? 0 : 10);
updateRadius();
if (isLegendRight && hasArc) {
margin3.left = arcWidth / 2 + radiusExpanded * 1.1;
}
}
function updateXgridFocus() {
main.select('line.' + CLASS.xgridFocus)
.attr("x1", __axis_rotated ? 0 : -10)
.attr("x2", __axis_rotated ? width : -10)
.attr("y1", __axis_rotated ? -10 : 0)
.attr("y2", __axis_rotated ? -10 : height);
}
function updateRadius() {
var innerRadiusRatio, w = __gauge_width || __donut_width;
radiusExpanded = Math.min(arcWidth, arcHeight) / 2;
radius = radiusExpanded * 0.95;
innerRadiusRatio = w ? (radius - w) / radius : 0.6;
innerRadius = hasDonutType(c3.data.targets) || hasGaugeType(c3.data.targets) ? radius * innerRadiusRatio : 0;
}
function getSvgLeft() {
var leftAxisClass = __axis_rotated ? CLASS.axisX : CLASS.axisY,
leftAxis = main.select('.' + leftAxisClass).node(),
svgRect = leftAxis ? leftAxis.getBoundingClientRect() : {right: 0},
chartRect = selectChart.node().getBoundingClientRect(),
hasArc = hasArcType(c3.data.targets),
svgLeft = svgRect.right - chartRect.left - (hasArc ? 0 : getCurrentPaddingLeft());
return svgLeft > 0 ? svgLeft : 0;
}
function getCurrentWidth() {
return __size_width ? __size_width : getParentWidth();
}
function getCurrentHeight() {
var h = __size_height ? __size_height : getParentHeight();
return h > 0 ? h : 320;
}
function getCurrentPaddingTop() {
return isValue(__padding_top) ? __padding_top : 0;
}
function getCurrentPaddingBottom() {
return isValue(__padding_bottom) ? __padding_bottom : 0;
}
function getCurrentPaddingLeft() {
if (isValue(__padding_left)) {
return __padding_left;
} else if (__axis_rotated) {
return !__axis_x_show ? 1 : Math.max(ceil10(getAxisWidthByAxisId('x')), 40);
} else {
return !__axis_y_show || __axis_y_inner ? 1 : ceil10(getAxisWidthByAxisId('y'));
}
}
function getCurrentPaddingRight() {
var defaultPadding = 10, legendWidthOnRight = isLegendRight ? getLegendWidth() + 20 : 0;
if (isValue(__padding_right)) {
return __padding_right + 1; // 1 is needed not to hide tick line
} else if (__axis_rotated) {
return defaultPadding + legendWidthOnRight;
} else {
return (!__axis_y2_show || __axis_y2_inner ? defaultPadding : ceil10(getAxisWidthByAxisId('y2'))) + legendWidthOnRight;
}
}
function getAxisWidthByAxisId(id) {
var position = getAxisLabelPositionById(id);
return position.isInner ? 20 + getMaxTickWidth(id) : 40 + getMaxTickWidth(id);
}
function getHorizontalAxisHeight(axisId) {
if (axisId === 'x' && !__axis_x_show) { return 0; }
if (axisId === 'x' && __axis_x_height) { return __axis_x_height; }
if (axisId === 'y' && !__axis_y_show) { return __legend_show && !isLegendRight && !isLegendInset ? 10 : 1; }
if (axisId === 'y2' && !__axis_y2_show) { return rotated_padding_top; }
return (getAxisLabelPositionById(axisId).isInner ? 30 : 40) + (axisId === 'y2' ? -10 : 0);
}
function getParentRectValue(key) {
var parent = selectChart.node(), v;
while (parent && parent.tagName !== 'BODY') {
v = parent.getBoundingClientRect()[key];
if (v) {
break;
}
parent = parent.parentNode;
}
return v;
}
function getParentWidth() {
return getParentRectValue('width');
}
function getParentHeight() {
var h = selectChart.style('height');
return h.indexOf('px') > 0 ? +h.replace('px', '') : 0;
}
function getAxisClipX(forHorizontal) {
// axis line width + padding for left
return forHorizontal ? -(1 + 30) : -(margin.left - 1);
}
function getAxisClipY(forHorizontal) {
return forHorizontal ? -20 : -4;
}
function getXAxisClipX() {
return getAxisClipX(!__axis_rotated);
}
function getXAxisClipY() {
return getAxisClipY(!__axis_rotated);
}
function getYAxisClipX() {
return getAxisClipX(__axis_rotated);
}
function getYAxisClipY() {
return getAxisClipY(__axis_rotated);
}
function getAxisClipWidth(forHorizontal) {
// width + axis line width + padding for left/right
return forHorizontal ? width + 2 + 30 + 30 : margin.left + 20;
}
function getAxisClipHeight(forHorizontal) {
return forHorizontal ? (__axis_x_height ? __axis_x_height : 0) + 80 : height + 8;
}
function getXAxisClipWidth() {
return getAxisClipWidth(!__axis_rotated);
}
function getXAxisClipHeight() {
return getAxisClipHeight(!__axis_rotated);
}
function getYAxisClipWidth() {
return getAxisClipWidth(__axis_rotated);
}
function getYAxisClipHeight() {
return getAxisClipHeight(__axis_rotated);
}
function getEventRectWidth() {
var target = getMaxDataCountTarget(c3.data.targets),
firstData, lastData, base, maxDataCount, ratio, w;
if (!target) {
return 0;
}
firstData = target.values[0], lastData = target.values[target.values.length - 1];
base = x(lastData.x) - x(firstData.x);
if (base === 0) {
return __axis_rotated ? height : width;
}
maxDataCount = getMaxDataCount();
ratio = (hasBarType(c3.data.targets) ? (maxDataCount - (isCategorized ? 0.25 : 1)) / maxDataCount : 1);
w = maxDataCount > 1 ? (base * ratio) / (maxDataCount - 1) : base;
return w < 1 ? 1 : w;
}
function updateLegendStep(step) {
legendStep = step;
}
function updateLegendItemWidth(w) {
legendItemWidth = w;
}
function updateLegendItemHeight(h) {
legendItemHeight = h;
}
function getLegendWidth() {
return __legend_show ? isLegendRight || isLegendInset ? legendItemWidth * (legendStep + 1) : currentWidth : 0;
}
function getLegendHeight() {
var h = 0;
if (__legend_show) {
if (isLegendRight) {
h = currentHeight;
} else if (isLegendInset) {
h = __legend_inset_step ? Math.max(20, legendItemHeight) * (__legend_inset_step + 1) : height;
} else {
h = Math.max(20, legendItemHeight) * (legendStep + 1);
}
}
return h;
}
//-- Scales --//
function updateScales() {
var xAxisTickFormat, xAxisTickValues, forInit = !x;
// update edges
xMin = __axis_rotated ? 1 : 0;
xMax = __axis_rotated ? height : width;
yMin = __axis_rotated ? 0 : height;
yMax = __axis_rotated ? width : 1;
subXMin = xMin;
subXMax = xMax;
subYMin = __axis_rotated ? 0 : height2;
subYMax = __axis_rotated ? width2 : 1;
// update scales
x = getX(xMin, xMax, forInit ? undefined : x.orgDomain(), function () { return xAxis.tickOffset(); });
y = getY(yMin, yMax, forInit ? undefined : y.domain());
y2 = getY(yMin, yMax, forInit ? undefined : y2.domain());
subX = getX(xMin, xMax, orgXDomain, function (d) { return d % 1 ? 0 : subXAxis.tickOffset(); });
subY = getY(subYMin, subYMax, forInit ? undefined : subY.domain());
subY2 = getY(subYMin, subYMax, forInit ? undefined : subY2.domain());
// update axes
xAxisTickFormat = getXAxisTickFormat();
xAxisTickValues = __axis_x_tick_values ? __axis_x_tick_values : (forInit ? undefined : xAxis.tickValues());
xAxis = getXAxis(x, xOrient, xAxisTickFormat, xAxisTickValues);
subXAxis = getXAxis(subX, subXOrient, xAxisTickFormat, xAxisTickValues);
yAxis = getYAxis(y, yOrient, __axis_y_tick_format, __axis_y_ticks, __axis_y_tick_outer);
y2Axis = getYAxis(y2, y2Orient, __axis_y2_tick_format, __axis_y2_ticks, __axis_y2_tick_outer);
// Set initialized scales to brush and zoom
if (!forInit) {
brush.scale(subX);
if (__zoom_enabled) { zoom.scale(x); }
}
// update for arc
updateArc();
}
function updateArc() {
svgArc = getSvgArc();
svgArcExpanded = getSvgArcExpanded();
svgArcExpandedSub = getSvgArcExpanded(0.98);
}
function getScale(min, max, forTimeseries) {
return (forTimeseries ? d3.time.scale() : d3.scale.linear()).range([min, max]);
}
function getX(min, max, domain, offset) {
var scale = getScale(min, max, isTimeSeries),
_scale = domain ? scale.domain(domain) : scale, key;
// Define customized scale if categorized axis
if (isCategorized) {
offset = offset || function () { return 0; };
scale = function (d, raw) {
var v = _scale(d) + offset(d);
return raw ? v : Math.ceil(v);
};
} else {
scale = function (d, raw) {
var v = _scale(d);
return raw ? v : Math.ceil(v);
};
}
// define functions
for (key in _scale) {
scale[key] = _scale[key];
}
scale.orgDomain = function () {
return _scale.domain();
};
// define custom domain() for categorized axis
if (isCategorized) {
scale.domain = function (domain) {
if (!arguments.length) {
domain = this.orgDomain();
return [domain[0], domain[1] + 1];
}
_scale.domain(domain);
return scale;
};
}
return scale;
}
function getY(min, max, domain) {
var scale = getScale(min, max);
if (domain) { scale.domain(domain); }
return scale;
}
function getYScale(id) {
return getAxisId(id) === 'y2' ? y2 : y;
}
function getSubYScale(id) {
return getAxisId(id) === 'y2' ? subY2 : subY;
}
//-- Axes --//
function getXAxis(scale, orient, tickFormat, tickValues) {
var axisParams = {isCategory: isCategorized, withOuterTick: __axis_x_tick_outer},
axis = c3_axis(d3, axisParams).scale(scale).orient(orient);
// Set tick
axis.tickFormat(tickFormat).tickValues(tickValues);
if (isCategorized) {
axis.tickCentered(__axis_x_tick_centered);
if (isEmpty(__axis_x_tick_culling)) {
__axis_x_tick_culling = false;
}
} else {
// TODO: move this to c3_axis
axis.tickOffset = function () {
var edgeX = getEdgeX(c3.data.targets), diff = x(edgeX[1]) - x(edgeX[0]),
base = diff ? diff : (__axis_rotated ? height : width);
return (base / getMaxDataCount()) / 2;
};
}
return axis;
}
function getYAxis(scale, orient, tickFormat, ticks, withOuterTick) {
var axisParams = {withOuterTick: withOuterTick};
return c3_axis(d3, axisParams).scale(scale).orient(orient).tickFormat(tickFormat).ticks(ticks);
}
function getAxisId(id) {
return id in __data_axes ? __data_axes[id] : 'y';
}
function getXAxisTickFormat() {
var format = isTimeSeries ? defaultAxisTimeFormat : isCategorized ? categoryName : function (v) { return v < 0 ? v.toFixed(0) : v; };
if (__axis_x_tick_format) {
if (typeof __axis_x_tick_format === 'function') {
format = __axis_x_tick_format;
} else if (isTimeSeries) {
format = function (date) {
return date ? axisTimeFormat(__axis_x_tick_format)(date) : "";
};
}
}
return format;
}
function getAxisLabelOptionByAxisId(axisId) {
var option;
if (axisId === 'y') {
option = __axis_y_label;
} else if (axisId === 'y2') {
option = __axis_y2_label;
} else if (axisId === 'x') {
option = __axis_x_label;
}
return option;
}
function getAxisLabelText(axisId) {
var option = getAxisLabelOptionByAxisId(axisId);
return typeof option === 'string' ? option : option ? option.text : null;
}
function setAxisLabelText(axisId, text) {
var option = getAxisLabelOptionByAxisId(axisId);
if (typeof option === 'string') {
if (axisId === 'y') {
__axis_y_label = text;
} else if (axisId === 'y2') {
__axis_y2_label = text;
} else if (axisId === 'x') {
__axis_x_label = text;
}
} else if (option) {
option.text = text;
}
}
function xForRotatedTickText(r) {
return 10 * Math.sin(Math.PI * (r / 180));
}
function yForRotatedTickText(r) {
return 11.5 - 2.5 * (r / 15);
}
function rotateTickText(axis, transition, rotate) {
axis.selectAll('.tick text')
.style("text-anchor", "start");
transition.selectAll('.tick text')
.attr("y", yForRotatedTickText(rotate))
.attr("x", xForRotatedTickText(rotate))
.attr("transform", "rotate(" + rotate + ")");
}
function getAxisLabelPosition(axisId, defaultPosition) {
var option = getAxisLabelOptionByAxisId(axisId),
position = (option && typeof option === 'object' && option.position) ? option.position : defaultPosition;
return {
isInner: position.indexOf('inner') >= 0,
isOuter: position.indexOf('outer') >= 0,
isLeft: position.indexOf('left') >= 0,
isCenter: position.indexOf('center') >= 0,
isRight: position.indexOf('right') >= 0,
isTop: position.indexOf('top') >= 0,
isMiddle: position.indexOf('middle') >= 0,
isBottom: position.indexOf('bottom') >= 0
};
}
function getXAxisLabelPosition() {
return getAxisLabelPosition('x', __axis_rotated ? 'inner-top' : 'inner-right');
}
function getYAxisLabelPosition() {
return getAxisLabelPosition('y', __axis_rotated ? 'inner-right' : 'inner-top');
}
function getY2AxisLabelPosition() {
return getAxisLabelPosition('y2', __axis_rotated ? 'inner-right' : 'inner-top');
}
function getAxisLabelPositionById(id) {
return id === 'y2' ? getY2AxisLabelPosition() : id === 'y' ? getYAxisLabelPosition() : getXAxisLabelPosition();
}
function textForXAxisLabel() {
return getAxisLabelText('x');
}
function textForYAxisLabel() {
return getAxisLabelText('y');
}
function textForY2AxisLabel() {
return getAxisLabelText('y2');
}
function xForAxisLabel(forHorizontal, position) {
if (forHorizontal) {
return position.isLeft ? 0 : position.isCenter ? width / 2 : width;
} else {
return position.isBottom ? -height : position.isMiddle ? -height / 2 : 0;
}
}
function dxForAxisLabel(forHorizontal, position) {
if (forHorizontal) {
return position.isLeft ? "0.5em" : position.isRight ? "-0.5em" : "0";
} else {
return position.isTop ? "-0.5em" : position.isBottom ? "0.5em" : "0";
}
}
function textAnchorForAxisLabel(forHorizontal, position) {
if (forHorizontal) {
return position.isLeft ? 'start' : position.isCenter ? 'middle' : 'end';
} else {
return position.isBottom ? 'start' : position.isMiddle ? 'middle' : 'end';
}
}
function xForXAxisLabel() {
return xForAxisLabel(!__axis_rotated, getXAxisLabelPosition());
}
function xForYAxisLabel() {
return xForAxisLabel(__axis_rotated, getYAxisLabelPosition());
}
function xForY2AxisLabel() {
return xForAxisLabel(__axis_rotated, getY2AxisLabelPosition());
}
function dxForXAxisLabel() {
return dxForAxisLabel(!__axis_rotated, getXAxisLabelPosition());
}
function dxForYAxisLabel() {
return dxForAxisLabel(__axis_rotated, getYAxisLabelPosition());
}
function dxForY2AxisLabel() {
return dxForAxisLabel(__axis_rotated, getY2AxisLabelPosition());
}
function dyForXAxisLabel() {
var position = getXAxisLabelPosition();
if (__axis_rotated) {
return position.isInner ? "1.2em" : -25 - getMaxTickWidth('x');
} else {
return position.isInner ? "-0.5em" : __axis_x_height ? __axis_x_height - 10 : "3em";
}
}
function dyForYAxisLabel() {
var position = getYAxisLabelPosition();
if (__axis_rotated) {
return position.isInner ? "-0.5em" : "3em";
} else {
return position.isInner ? "1.2em" : -20 - getMaxTickWidth('y');
}
}
function dyForY2AxisLabel() {
var position = getY2AxisLabelPosition();
if (__axis_rotated) {
return position.isInner ? "1.2em" : "-2.2em";
} else {
return position.isInner ? "-0.5em" : 30 + getMaxTickWidth('y2');
}
}
function textAnchorForXAxisLabel() {
return textAnchorForAxisLabel(!__axis_rotated, getXAxisLabelPosition());
}
function textAnchorForYAxisLabel() {
return textAnchorForAxisLabel(__axis_rotated, getYAxisLabelPosition());
}
function textAnchorForY2AxisLabel() {
return textAnchorForAxisLabel(__axis_rotated, getY2AxisLabelPosition());
}
function getMaxTickWidth(id) {
var maxWidth = 0, targetsToShow, scale, axis;
if (svg) {
targetsToShow = filterTargetsToShow(c3.data.targets);
if (id === 'y') {
scale = y.copy().domain(getYDomain(targetsToShow, 'y'));
axis = getYAxis(scale, yOrient, __axis_y_tick_format, __axis_y_ticks, __axis_y_tick_outer);
} else if (id === 'y2') {
scale = y2.copy().domain(getYDomain(targetsToShow, 'y2'));
axis = getYAxis(scale, y2Orient, __axis_y2_tick_format, __axis_y2_ticks, __axis_y2_tick_outer);
} else {
scale = x.copy().domain(getXDomain(targetsToShow));
axis = getXAxis(scale, xOrient, getXAxisTickFormat(), __axis_x_tick_values ? __axis_x_tick_values : xAxis.tickValues());
}
main.append("g").call(axis).each(function () {
d3.select(this).selectAll('text').each(function () {
var box = this.getBoundingClientRect();
if (maxWidth < box.width) { maxWidth = box.width; }
});
}).remove();