-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathbase-mixin.js
1148 lines (985 loc) · 35.5 KB
/
base-mixin.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
/**
## Base Mixin
Base Mixin is an abstract functional object representing a basic dc chart object
for all chart and widget implementations. Methods from the Base Mixin are inherited
and available on all chart implementation in the DC library.
**/
dc.baseMixin = function (_chart) {
_chart.__dcFlag__ = dc.utils.uniqueId();
var _dimension;
var _group;
var _anchor;
var _root;
var _svg;
var _isChild;
var _minWidth = 200;
var _defaultWidth = function (element) {
var width = element && element.getBoundingClientRect && element.getBoundingClientRect().width;
return (width && width > _minWidth) ? width : _minWidth;
};
var _width = _defaultWidth;
var _minHeight = 200;
var _defaultHeight = function (element) {
var height = element && element.getBoundingClientRect && element.getBoundingClientRect().height;
return (height && height > _minHeight) ? height : _minHeight;
};
var _height = _defaultHeight;
var _keyAccessor = dc.pluck('key');
var _valueAccessor = dc.pluck('value');
var _label = dc.pluck('key');
var _ordering = dc.pluck('key');
var _orderSort;
var _renderLabel = false;
var _title = function (d) {
return _chart.keyAccessor()(d) + ': ' + _chart.valueAccessor()(d);
};
var _renderTitle = true;
var _transitionDuration = 750;
var _filterPrinter = dc.printers.filters;
var _mandatoryAttributes = ['dimension', 'group'];
var _chartGroup = dc.constants.DEFAULT_CHART_GROUP;
var _listeners = d3.dispatch(
'preRender',
'postRender',
'preRedraw',
'postRedraw',
'filtered',
'zoomed',
'renderlet',
'pretransition');
var _legend;
var _filters = [];
var _filterHandler = function (dimension, filters) {
dimension.filter(null);
if (filters.length === 0) {
dimension.filter(null);
} else {
dimension.filterFunction(function (d) {
for (var i = 0; i < filters.length; i++) {
var filter = filters[i];
if (filter.isFiltered && filter.isFiltered(d)) {
return true;
} else if (filter <= d && filter >= d) {
return true;
}
}
return false;
});
}
return filters;
};
var _data = function (group) {
return group.all();
};
/**
#### .width([value])
Set or get the width attribute of a chart. See `.height` below for further description of the
behavior.
**/
_chart.width = function (w) {
if (!arguments.length) {
return _width(_root.node());
}
_width = d3.functor(w || _defaultWidth);
return _chart;
};
/**
#### .height([value])
Set or get the height attribute of a chart. The height is applied to the SVG element generated by
the chart when rendered (or rerendered). If a value is given, then it will be used to calculate
the new height and the chart returned for method chaining. The value can either be a numeric, a
function, or falsy. If no value is specified then the value of the current height attribute will
be returned.
By default, without an explicit height being given, the chart will select the width of its
anchor element. If that isn't possible it defaults to 200. Setting the value falsy will return
the chart to the default behavior
Examples:
```js
chart.height(250); // Set the chart's height to 250px;
chart.height(function(anchor) { return doSomethingWith(anchor); }); // set the chart's height with a function
chart.height(null); // reset the height to the default auto calculation
```
**/
_chart.height = function (h) {
if (!arguments.length) {
return _height(_root.node());
}
_height = d3.functor(h || _defaultHeight);
return _chart;
};
/**
#### .minWidth([value])
Set or get the minimum width attribute of a chart. This only applicable if the width is
calculated by dc.
**/
_chart.minWidth = function (w) {
if (!arguments.length) {
return _minWidth;
}
_minWidth = w;
return _chart;
};
/**
#### .minHeight([value])
Set or get the minimum height attribute of a chart. This only applicable if the height is
calculated by dc.
**/
_chart.minHeight = function (w) {
if (!arguments.length) {
return _minHeight;
}
_minHeight = w;
return _chart;
};
/**
#### .dimension([value]) - **mandatory**
Set or get the dimension attribute of a chart. In dc a dimension can be any valid [crossfilter
dimension](https://github.com/square/crossfilter/wiki/API-Reference#wiki-dimension).
If a value is given, then it will be used as the new dimension. If no value is specified then
the current dimension will be returned.
**/
_chart.dimension = function (d) {
if (!arguments.length) {
return _dimension;
}
_dimension = d;
_chart.expireCache();
return _chart;
};
/**
#### .data([callback])
Set the data callback or retrieve the chart's data set. The data callback is passed the chart's
group and by default will return `group.all()`. This behavior may be modified to, for instance,
return only the top 5 groups:
```
chart.data(function(group) {
return group.top(5);
});
```
**/
_chart.data = function (d) {
if (!arguments.length) {
return _data.call(_chart, _group);
}
_data = d3.functor(d);
_chart.expireCache();
return _chart;
};
/**
#### .group([value, [name]]) - **mandatory**
Set or get the group attribute of a chart. In dc a group is a [crossfilter
group](https://github.com/square/crossfilter/wiki/API-Reference#wiki-group). Usually the group
should be created from the particular dimension associated with the same chart. If a value is
given, then it will be used as the new group.
If no value specified then the current group will be returned.
If `name` is specified then it will be used to generate legend label.
**/
_chart.group = function (g, name) {
if (!arguments.length) {
return _group;
}
_group = g;
_chart._groupName = name;
_chart.expireCache();
return _chart;
};
/**
#### .ordering([orderFunction])
Get or set an accessor to order ordinal charts
**/
_chart.ordering = function (o) {
if (!arguments.length) {
return _ordering;
}
_ordering = o;
_orderSort = crossfilter.quicksort.by(_ordering);
_chart.expireCache();
return _chart;
};
_chart._computeOrderedGroups = function (data) {
var dataCopy = data.slice(0);
if (dataCopy.length <= 1) {
return dataCopy;
}
if (!_orderSort) {
_orderSort = crossfilter.quicksort.by(_ordering);
}
return _orderSort(dataCopy, 0, dataCopy.length);
};
/**
#### .filterAll()
Clear all filters associated with this chart.
**/
_chart.filterAll = function () {
return _chart.filter(null);
};
/**
#### .select(selector)
Execute d3 single selection in the chart's scope using the given selector and return the d3
selection. Roughly the same as:
```js
d3.select('#chart-id').select(selector);
```
This function is **not chainable** since it does not return a chart instance; however the d3
selection result can be chained to d3 function calls.
**/
_chart.select = function (s) {
return _root.select(s);
};
/**
#### .selectAll(selector)
Execute in scope d3 selectAll using the given selector and return d3 selection result. Roughly
the same as:
```js
d3.select('#chart-id').selectAll(selector);
```
This function is **not chainable** since it does not return a chart instance; however the d3
selection result can be chained to d3 function calls.
**/
_chart.selectAll = function (s) {
return _root ? _root.selectAll(s) : null;
};
/**
#### .anchor([anchorChart|anchorSelector|anchorNode], [chartGroup])
Set the svg root to either be an existing chart's root; or any valid [d3 single
selector](https://github.com/mbostock/d3/wiki/Selections#selecting-elements) specifying a dom
block element such as a div; or a dom element or d3 selection. Optionally registers the chart
within the chartGroup. This class is called internally on chart initialization, but be called
again to relocate the chart. However, it will orphan any previously created SVG elements.
**/
_chart.anchor = function (a, chartGroup) {
if (!arguments.length) {
return _anchor;
}
if (dc.instanceOfChart(a)) {
_anchor = a.anchor();
_root = a.root();
_isChild = true;
} else {
_anchor = a;
_root = d3.select(_anchor);
_root.classed(dc.constants.CHART_CLASS, true);
dc.registerChart(_chart, chartGroup);
_isChild = false;
}
_chartGroup = chartGroup;
return _chart;
};
/**
#### .anchorName()
Returns the dom id for the chart's anchored location.
**/
_chart.anchorName = function () {
var a = _chart.anchor();
if (a && a.id) {
return a.id;
}
if (a && a.replace) {
return a.replace('#', '');
}
return 'dc-chart' + _chart.chartID();
};
/**
#### .root([rootElement])
Returns the root element where a chart resides. Usually it will be the parent div element where
the svg was created. You can also pass in a new root element however this is usually handled by
dc internally. Resetting the root element on a chart outside of dc internals may have
unexpected consequences.
**/
_chart.root = function (r) {
if (!arguments.length) {
return _root;
}
_root = r;
return _chart;
};
/**
#### .svg([svgElement])
Returns the top svg element for this specific chart. You can also pass in a new svg element,
however this is usually handled by dc internally. Resetting the svg element on a chart outside
of dc internals may have unexpected consequences.
**/
_chart.svg = function (_) {
if (!arguments.length) {
return _svg;
}
_svg = _;
return _chart;
};
/**
#### .resetSvg()
Remove the chart's SVG elements from the dom and recreate the container SVG element.
**/
_chart.resetSvg = function () {
_chart.select('svg').remove();
return generateSvg();
};
function sizeSvg() {
if (_svg) {
_svg
.attr('width', _chart.width())
.attr('height', _chart.height());
}
}
function generateSvg() {
_svg = _chart.root().append('svg');
sizeSvg();
return _svg;
}
/**
#### .filterPrinter([filterPrinterFunction])
Set or get the filter printer function. The filter printer function is used to generate human
friendly text for filter value(s) associated with the chart instance. By default dc charts use a
default filter printer `dc.printers.filter` that provides simple printing support for both
single value and ranged filters.
**/
_chart.filterPrinter = function (_) {
if (!arguments.length) {
return _filterPrinter;
}
_filterPrinter = _;
return _chart;
};
/**
#### .turnOnControls() & .turnOffControls()
Turn on/off optional control elements within the root element. dc currently supports the
following html control elements.
* root.selectAll('.reset') - elements are turned on if the chart has an active filter. This type
of control element is usually used to store a reset link to allow user to reset filter on a
certain chart. This element will be turned off automatically if the filter is cleared.
* root.selectAll('.filter') elements are turned on if the chart has an active filter. The text
content of this element is then replaced with the current filter value using the filter printer
function. This type of element will be turned off automatically if the filter is cleared.
**/
_chart.turnOnControls = function () {
if (_root) {
_chart.selectAll('.reset').style('display', null);
_chart.selectAll('.filter').text(_filterPrinter(_chart.filters())).style('display', null);
}
return _chart;
};
_chart.turnOffControls = function () {
if (_root) {
_chart.selectAll('.reset').style('display', 'none');
_chart.selectAll('.filter').style('display', 'none').text(_chart.filter());
}
return _chart;
};
/**
#### .transitionDuration([duration])
Set or get the animation transition duration(in milliseconds) for this chart instance. Default
duration is 750ms.
**/
_chart.transitionDuration = function (d) {
if (!arguments.length) {
return _transitionDuration;
}
_transitionDuration = d;
return _chart;
};
_chart._mandatoryAttributes = function (_) {
if (!arguments.length) {
return _mandatoryAttributes;
}
_mandatoryAttributes = _;
return _chart;
};
function checkForMandatoryAttributes(a) {
if (!_chart[a] || !_chart[a]()) {
throw new dc.errors.InvalidStateException('Mandatory attribute chart.' + a +
' is missing on chart[#' + _chart.anchorName() + ']');
}
}
/**
#### .render()
Invoking this method will force the chart to re-render everything from scratch. Generally it
should only be used to render the chart for the first time on the page or if you want to make
sure everything is redrawn from scratch instead of relying on the default incremental redrawing
behaviour.
**/
_chart.render = function () {
_listeners.preRender(_chart);
if (_mandatoryAttributes) {
_mandatoryAttributes.forEach(checkForMandatoryAttributes);
}
var result = _chart._doRender();
if (_legend) {
_legend.render();
}
_chart._activateRenderlets('postRender');
return result;
};
_chart._activateRenderlets = function (event) {
_listeners.pretransition(_chart);
if (_chart.transitionDuration() > 0 && _svg) {
_svg.transition().duration(_chart.transitionDuration())
.each('end', function () {
_listeners['renderlet'](_chart);
if (event) {
_listeners[event](_chart);
}
});
} else {
_listeners['renderlet'](_chart);
if (event) {
_listeners[event](_chart);
}
}
};
/**
#### .redraw()
Calling redraw will cause the chart to re-render data changes incrementally. If there is no
change in the underlying data dimension then calling this method will have no effect on the
chart. Most chart interaction in dc will automatically trigger this method through internal
events (in particular [dc.redrawAll](#dcredrawallchartgroup)); therefore, you only need to
manually invoke this function if data is manipulated outside of dc's control (for example if
data is loaded in the background using `crossfilter.add()`).
**/
_chart.redraw = function () {
sizeSvg();
_listeners.preRedraw(_chart);
var result = _chart._doRedraw();
if (_legend) {
_legend.render();
}
_chart._activateRenderlets('postRedraw');
return result;
};
_chart.redrawGroup = function () {
dc.redrawAll(_chart.chartGroup());
};
_chart.renderGroup = function () {
dc.renderAll(_chart.chartGroup());
};
_chart._invokeFilteredListener = function (f) {
if (f !== undefined) {
_listeners.filtered(_chart, f);
}
};
_chart._invokeZoomedListener = function () {
_listeners.zoomed(_chart);
};
var _hasFilterHandler = function (filters, filter) {
if (filter === null || typeof(filter) === 'undefined') {
return filters.length > 0;
}
return filters.some(function (f) {
return filter <= f && filter >= f;
});
};
/**
#### .hasFilterHandler([function])
Set or get the has filter handler. The has filter handler is a function that checks to see if
the chart's current filters include a specific filter. Using a custom has filter handler allows
you to change the way filters are checked for and replaced.
```js
// default has filter handler
function (filters, filter) {
if (filter === null || typeof(filter) === 'undefined') {
return filters.length > 0;
}
return filters.some(function (f) {
return filter <= f && filter >= f;
});
}
// custom filter handler (no-op)
chart.hasFilterHandler(function(filters, filter) {
return false;
});
```
**/
_chart.hasFilterHandler = function (_) {
if (!arguments.length) {
return _hasFilterHandler;
}
_hasFilterHandler = _;
return _chart;
};
/**
#### .hasFilter([filter])
Check whether any active filter or a specific filter is associated with particular chart instance.
This function is **not chainable**.
**/
_chart.hasFilter = function (filter) {
return _hasFilterHandler(_filters, filter);
};
var _removeFilterHandler = function (filters, filter) {
for (var i = 0; i < filters.length; i++) {
if (filters[i] <= filter && filters[i] >= filter) {
filters.splice(i, 1);
break;
}
}
return filters;
};
/**
#### .removeFilterHandler([function])
Set or get the remove filter handler. The remove filter handler is a function that removes a
filter from the chart's current filters. Using a custom remove filter handler allows you to
change how filters are removed or perform additional work when removing a filter, e.g. when
using a filter server other than crossfilter.
Any changes should modify the `filters` array argument and return that array.
```js
// default remove filter handler
function (filters, filter) {
for (var i = 0; i < filters.length; i++) {
if (filters[i] <= filter && filters[i] >= filter) {
filters.splice(i, 1);
break;
}
}
return filters;
}
// custom filter handler (no-op)
chart.removeFilterHandler(function(filters, filter) {
return filters;
});
```
**/
_chart.removeFilterHandler = function (_) {
if (!arguments.length) {
return _removeFilterHandler;
}
_removeFilterHandler = _;
return _chart;
};
var _addFilterHandler = function (filters, filter) {
filters.push(filter);
return filters;
};
/**
#### .addFilterHandler([function])
Set or get the add filter handler. The add filter handler is a function that adds a filter to
the chart's filter list. Using a custom add filter handler allows you to change the way filters
are added or perform additional work when adding a filter, e.g. when using a filter server other
than crossfilter.
Any changes should modify the `filters` array argument and return that array.
```js
// default add filter handler
function (filters, filter) {
filters.push(filter);
return filters;
}
// custom filter handler (no-op)
chart.addFilterHandler(function(filters, filter) {
return filters;
});
```
**/
_chart.addFilterHandler = function (_) {
if (!arguments.length) {
return _addFilterHandler;
}
_addFilterHandler = _;
return _chart;
};
var _resetFilterHandler = function (filters) {
return [];
};
/**
#### .resetFilterHandler([function])
Set or get the reset filter handler. The reset filter handler is a function that resets the
chart's filter list by returning a new list. Using a custom reset filter handler allows you to
change the way filters are reset, or perform additional work when resetting the filters,
e.g. when using a filter server other than crossfilter.
This function should return an array.
```js
// default remove filter handler
function (filters) {
return [];
}
// custom filter handler (no-op)
chart.resetFilterHandler(function(filters) {
return filters;
});
```
**/
_chart.resetFilterHandler = function (_) {
if (!arguments.length) {
return _resetFilterHandler;
}
_resetFilterHandler = _;
return _chart;
};
function applyFilters() {
if (_chart.dimension() && _chart.dimension().filter) {
var fs = _filterHandler(_chart.dimension(), _filters);
_filters = fs ? fs : _filters;
}
}
_chart.replaceFilter = function (_) {
_filters = [];
_chart.filter(_);
};
/**
#### .filter([filterValue])
Filter the chart by the given value or return the current filter if the input parameter is missing.
```js
// filter by a single string
chart.filter('Sunday');
// filter by a single age
chart.filter(18);
```
**/
_chart.filter = function (_) {
if (!arguments.length) {
return _filters.length > 0 ? _filters[0] : null;
}
if (_ instanceof Array && _[0] instanceof Array && !_.isFiltered) {
_[0].forEach(function (d) {
if (_chart.hasFilter(d)) {
_removeFilterHandler(_filters, d);
} else {
_addFilterHandler(_filters, d);
}
});
} else if (_ === null) {
_filters = _resetFilterHandler(_filters);
} else {
if (_chart.hasFilter(_)) {
_removeFilterHandler(_filters, _);
} else {
_addFilterHandler(_filters, _);
}
}
applyFilters();
_chart._invokeFilteredListener(_);
if (_root !== null && _chart.hasFilter()) {
_chart.turnOnControls();
} else {
_chart.turnOffControls();
}
return _chart;
};
/**
#### .filters()
Returns all current filters. This method does not perform defensive cloning of the internal
filter array before returning, therefore any modification of the returned array will effect the
chart's internal filter storage.
**/
_chart.filters = function () {
return _filters;
};
_chart.highlightSelected = function (e) {
d3.select(e).classed(dc.constants.SELECTED_CLASS, true);
d3.select(e).classed(dc.constants.DESELECTED_CLASS, false);
};
_chart.fadeDeselected = function (e) {
d3.select(e).classed(dc.constants.SELECTED_CLASS, false);
d3.select(e).classed(dc.constants.DESELECTED_CLASS, true);
};
_chart.resetHighlight = function (e) {
d3.select(e).classed(dc.constants.SELECTED_CLASS, false);
d3.select(e).classed(dc.constants.DESELECTED_CLASS, false);
};
/**
#### .onClick(datum)
This function is passed to d3 as the onClick handler for each chart. The default behavior is to
filter on the clicked datum (passed to the callback) and redraw the chart group.
**/
_chart.onClick = function (d) {
var filter = _chart.keyAccessor()(d);
dc.events.trigger(function () {
_chart.filter(filter);
_chart.redrawGroup();
});
};
/**
#### .filterHandler([function])
Set or get the filter handler. The filter handler is a function that performs the filter action
on a specific dimension. Using a custom filter handler allows you to perform additional logic
before or after filtering.
```js
// default filter handler
function(dimension, filter){
dimension.filter(filter); // perform filtering
return filter; // return the actual filter value
}
// custom filter handler
chart.filterHandler(function(dimension, filter){
var newFilter = filter + 10;
dimension.filter(newFilter);
return newFilter; // set the actual filter value to the new value
});
```
**/
_chart.filterHandler = function (_) {
if (!arguments.length) {
return _filterHandler;
}
_filterHandler = _;
return _chart;
};
// abstract function stub
_chart._doRender = function () {
// do nothing in base, should be overridden by sub-function
return _chart;
};
_chart._doRedraw = function () {
// do nothing in base, should be overridden by sub-function
return _chart;
};
_chart.legendables = function () {
// do nothing in base, should be overridden by sub-function
return [];
};
_chart.legendHighlight = function () {
// do nothing in base, should be overridden by sub-function
};
_chart.legendReset = function () {
// do nothing in base, should be overridden by sub-function
};
_chart.legendToggle = function () {
// do nothing in base, should be overriden by sub-function
};
_chart.isLegendableHidden = function () {
// do nothing in base, should be overridden by sub-function
return false;
};
/**
#### .keyAccessor([keyAccessorFunction])
Set or get the key accessor function. The key accessor function is used to retrieve the key
value from the crossfilter group. Key values are used differently in different charts, for
example keys correspond to slices in a pie chart and x axis positions in a grid coordinate chart.
```js
// default key accessor
chart.keyAccessor(function(d) { return d.key; });
// custom key accessor for a multi-value crossfilter reduction
chart.keyAccessor(function(p) { return p.value.absGain; });
```
**/
_chart.keyAccessor = function (_) {
if (!arguments.length) {
return _keyAccessor;
}
_keyAccessor = _;
return _chart;
};
/**
#### .valueAccessor([valueAccessorFunction])
Set or get the value accessor function. The value accessor function is used to retrieve the
value from the crossfilter group. Group values are used differently in different charts, for
example values correspond to slice sizes in a pie chart and y axis positions in a grid
coordinate chart.
```js
// default value accessor
chart.valueAccessor(function(d) { return d.value; });
// custom value accessor for a multi-value crossfilter reduction
chart.valueAccessor(function(p) { return p.value.percentageGain; });
```
**/
_chart.valueAccessor = function (_) {
if (!arguments.length) {
return _valueAccessor;
}
_valueAccessor = _;
return _chart;
};
/**
#### .label([labelFunction])
Set or get the label function. The chart class will use this function to render labels for each
child element in the chart, e.g. slices in a pie chart or bubbles in a bubble chart. Not every
chart supports the label function for example bar chart and line chart do not use this function
at all.
```js
// default label function just return the key
chart.label(function(d) { return d.key; });
// label function has access to the standard d3 data binding and can get quite complicated
chart.label(function(d) { return d.data.key + '(' + Math.floor(d.data.value / all.value() * 100) + '%)'; });
```
**/
_chart.label = function (_) {
if (!arguments.length) {
return _label;
}
_label = _;
_renderLabel = true;
return _chart;
};
/**
#### .renderLabel(boolean)
Turn on/off label rendering
**/
_chart.renderLabel = function (_) {
if (!arguments.length) {
return _renderLabel;
}
_renderLabel = _;
return _chart;
};
/**
#### .title([titleFunction])
Set or get the title function. The chart class will use this function to render the svg title
(usually interpreted by browser as tooltips) for each child element in the chart, e.g. a slice
in a pie chart or a bubble in a bubble chart. Almost every chart supports the title function;
however in grid coordinate charts you need to turn off the brush in order to see titles, because
otherwise the brush layer will block tooltip triggering.
```js
// default title function just return the key
chart.title(function(d) { return d.key + ': ' + d.value; });
// title function has access to the standard d3 data binding and can get quite complicated
chart.title(function(p) {
return p.key.getFullYear()
+ '\n'
+ 'Index Gain: ' + numberFormat(p.value.absGain) + '\n'
+ 'Index Gain in Percentage: ' + numberFormat(p.value.percentageGain) + '%\n'
+ 'Fluctuation / Index Ratio: ' + numberFormat(p.value.fluctuationPercentage) + '%';
});
```
**/
_chart.title = function (_) {
if (!arguments.length) {
return _title;
}
_title = _;
return _chart;
};
/**
#### .renderTitle(boolean)
Turn on/off title rendering, or return the state of the render title flag if no arguments are
given.
**/
_chart.renderTitle = function (_) {
if (!arguments.length) {
return _renderTitle;
}
_renderTitle = _;
return _chart;
};
/**
#### .renderlet(renderletFunction)
A renderlet is similar to an event listener on rendering event. Multiple renderlets can be added
to an individual chart. Each time a chart is rerendered or redrawn the renderlets are invoked
right after the chart finishes its transitions, giving you a way to modify the svg
elements. Renderlet functions take the chart instance as the only input parameter and you can
use the dc API or use raw d3 to achieve pretty much any effect.
@Deprecated - Use [Listeners](#Listeners) with a 'renderlet' prefix
Generates a random key for the renderlet, which makes it hard to remove.
```js
// do this instead of .renderlet(function(chart) { ... })