forked from dc-js/dc.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base-mixin.js
1529 lines (1423 loc) · 54.7 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
/**
* `dc.baseMixin` is an abstract functional object representing a basic `dc` chart object
* for all chart and widget implementations. Methods from the {@link #dc.baseMixin dc.baseMixin} are inherited
* and available on all chart implementations in the `dc` library.
* @name baseMixin
* @memberof dc
* @mixin
* @param {Object} _chart
* @returns {dc.baseMixin}
*/
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 _defaultWidthCalc = function (element) {
var width = element && element.getBoundingClientRect && element.getBoundingClientRect().width;
return (width && width > _minWidth) ? width : _minWidth;
};
var _widthCalc = _defaultWidthCalc;
var _minHeight = 200;
var _defaultHeightCalc = function (element) {
var height = element && element.getBoundingClientRect && element.getBoundingClientRect().height;
return (height && height > _minHeight) ? height : _minHeight;
};
var _heightCalc = _defaultHeightCalc;
var _width, _height;
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 _controlsUseVisibility = false;
var _transitionDuration = 750;
var _transitionDelay = 0;
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 _commitHandler;
var _filters = [];
var _filterHandler = function (dimension, filters) {
if (filters.length === 0) {
dimension.filter(null);
} else if (filters.length === 1 && !filters[0].isFiltered) {
// single value and not a function-based filter
dimension.filterExact(filters[0]);
} else if (filters.length === 1 && filters[0].filterType === 'RangedFilter') {
// single range-based filter
dimension.filterRange(filters[0]);
} 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();
};
/**
* Set or get the height attribute of a chart. The height is applied to the SVGElement generated by
* the chart when rendered (or re-rendered). 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 (provided by the
* {@link dc.baseMixin#minHeight minHeight} property). Setting the value falsy will return
* the chart to the default behavior.
* @method height
* @memberof dc.baseMixin
* @instance
* @see {@link dc.baseMixin#minHeight minHeight}
* @example
* // Default height
* chart.height(function (element) {
* var height = element && element.getBoundingClientRect && element.getBoundingClientRect().height;
* return (height && height > chart.minHeight()) ? height : chart.minHeight();
* });
*
* 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
* @param {Number|Function} [height]
* @returns {Number|dc.baseMixin}
*/
_chart.height = function (height) {
if (!arguments.length) {
if (!dc.utils.isNumber(_height)) {
// only calculate once
_height = _heightCalc(_root.node());
}
return _height;
}
_heightCalc = d3.functor(height || _defaultHeightCalc);
_height = undefined;
return _chart;
};
/**
* Set or get the width attribute of a chart.
* @method width
* @memberof dc.baseMixin
* @instance
* @see {@link dc.baseMixin#height height}
* @see {@link dc.baseMixin#minWidth minWidth}
* @example
* // Default width
* chart.width(function (element) {
* var width = element && element.getBoundingClientRect && element.getBoundingClientRect().width;
* return (width && width > chart.minWidth()) ? width : chart.minWidth();
* });
* @param {Number|Function} [width]
* @returns {Number|dc.baseMixin}
*/
_chart.width = function (width) {
if (!arguments.length) {
if (!dc.utils.isNumber(_width)) {
// only calculate once
_width = _widthCalc(_root.node());
}
return _width;
}
_widthCalc = d3.functor(width || _defaultWidthCalc);
_width = undefined;
return _chart;
};
/**
* Set or get the minimum width attribute of a chart. This only has effect when used with the default
* {@link dc.baseMixin#width width} function.
* @method minWidth
* @memberof dc.baseMixin
* @instance
* @see {@link dc.baseMixin#width width}
* @param {Number} [minWidth=200]
* @returns {Number|dc.baseMixin}
*/
_chart.minWidth = function (minWidth) {
if (!arguments.length) {
return _minWidth;
}
_minWidth = minWidth;
return _chart;
};
/**
* Set or get the minimum height attribute of a chart. This only has effect when used with the default
* {@link dc.baseMixin#height height} function.
* @method minHeight
* @memberof dc.baseMixin
* @instance
* @see {@link dc.baseMixin#height height}
* @param {Number} [minHeight=200]
* @returns {Number|dc.baseMixin}
*/
_chart.minHeight = function (minHeight) {
if (!arguments.length) {
return _minHeight;
}
_minHeight = minHeight;
return _chart;
};
/**
* **mandatory**
*
* Set or get the dimension attribute of a chart. In `dc`, a dimension can be any valid
* {@link https://github.com/crossfilter/crossfilter/wiki/API-Reference#dimension crossfilter 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.
* @method dimension
* @memberof dc.baseMixin
* @instance
* @see {@link https://github.com/crossfilter/crossfilter/wiki/API-Reference#dimension crossfilter.dimension}
* @example
* var index = crossfilter([]);
* var dimension = index.dimension(dc.pluck('key'));
* chart.dimension(dimension);
* @param {crossfilter.dimension} [dimension]
* @returns {crossfilter.dimension|dc.baseMixin}
*/
_chart.dimension = function (dimension) {
if (!arguments.length) {
return _dimension;
}
_dimension = dimension;
_chart.expireCache();
return _chart;
};
/**
* 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
* {@link https://github.com/crossfilter/crossfilter/wiki/API-Reference#group_all group.all}.
* This behavior may be modified to, for instance, return only the top 5 groups.
* @method data
* @memberof dc.baseMixin
* @instance
* @example
* // Default data function
* chart.data(function (group) { return group.all(); });
*
* chart.data(function (group) { return group.top(5); });
* @param {Function} [callback]
* @returns {*|dc.baseMixin}
*/
_chart.data = function (callback) {
if (!arguments.length) {
return _data.call(_chart, _group);
}
_data = d3.functor(callback);
_chart.expireCache();
return _chart;
};
/**
* **mandatory**
*
* Set or get the group attribute of a chart. In `dc` a group is a
* {@link https://github.com/crossfilter/crossfilter/wiki/API-Reference#group-map-reduce crossfilter 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.
* @method group
* @memberof dc.baseMixin
* @instance
* @see {@link https://github.com/crossfilter/crossfilter/wiki/API-Reference#group-map-reduce crossfilter.group}
* @example
* var index = crossfilter([]);
* var dimension = index.dimension(dc.pluck('key'));
* chart.dimension(dimension);
* chart.group(dimension.group(crossfilter.reduceSum()));
* @param {crossfilter.group} [group]
* @param {String} [name]
* @returns {crossfilter.group|dc.baseMixin}
*/
_chart.group = function (group, name) {
if (!arguments.length) {
return _group;
}
_group = group;
_chart._groupName = name;
_chart.expireCache();
return _chart;
};
/**
* Get or set an accessor to order ordinal dimensions. The chart uses
* {@link https://github.com/crossfilter/crossfilter/wiki/API-Reference#quicksort_by crossfilter.quicksort.by}
* to sort elements; this accessor returns the value to order on.
* @method ordering
* @memberof dc.baseMixin
* @instance
* @see {@link https://github.com/crossfilter/crossfilter/wiki/API-Reference#quicksort_by crossfilter.quicksort.by}
* @example
* // Default ordering accessor
* _chart.ordering(dc.pluck('key'));
* @param {Function} [orderFunction]
* @returns {Function|dc.baseMixin}
*/
_chart.ordering = function (orderFunction) {
if (!arguments.length) {
return _ordering;
}
_ordering = orderFunction;
_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);
};
/**
* Clear all filters associated with this chart. The same effect can be achieved by calling
* {@link dc.baseMixin#filter chart.filter(null)}.
* @method filterAll
* @memberof dc.baseMixin
* @instance
* @returns {dc.baseMixin}
*/
_chart.filterAll = function () {
return _chart.filter(null);
};
/**
* Execute d3 single selection in the chart's scope using the given selector and return the d3
* selection.
*
* 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.
* @method select
* @memberof dc.baseMixin
* @instance
* @see {@link https://github.com/d3/d3-3.x-api-reference/blob/master/Selections.md#d3_select d3.select}
* @example
* // Has the same effect as d3.select('#chart-id').select(selector)
* chart.select(selector)
* @returns {d3.selection}
*/
_chart.select = function (s) {
return _root.select(s);
};
/**
* Execute in scope d3 selectAll using the given selector and return d3 selection result.
*
* 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.
* @method selectAll
* @memberof dc.baseMixin
* @instance
* @see {@link https://github.com/d3/d3-3.x-api-reference/blob/master/Selections.md#d3_selectAll d3.selectAll}
* @example
* // Has the same effect as d3.select('#chart-id').selectAll(selector)
* chart.selectAll(selector)
* @returns {d3.selection}
*/
_chart.selectAll = function (s) {
return _root ? _root.selectAll(s) : null;
};
/**
* Set the root SVGElement to either be an existing chart's root; or any valid [d3 single
* selector](https://github.com/d3/d3-3.x-api-reference/blob/master/Selections.md#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 SVGElements.
* @method anchor
* @memberof dc.baseMixin
* @instance
* @param {anchorChart|anchorSelector|anchorNode} [parent]
* @param {String} [chartGroup]
* @returns {String|node|d3.selection|dc.baseMixin}
*/
_chart.anchor = function (parent, chartGroup) {
if (!arguments.length) {
return _anchor;
}
if (dc.instanceOfChart(parent)) {
_anchor = parent.anchor();
_root = parent.root();
_isChild = true;
} else if (parent) {
if (parent.select && parent.classed) { // detect d3 selection
_anchor = parent.node();
} else {
_anchor = parent;
}
_root = d3.select(_anchor);
_root.classed(dc.constants.CHART_CLASS, true);
dc.registerChart(_chart, chartGroup);
_isChild = false;
} else {
throw new dc.errors.BadArgumentException('parent must be defined');
}
_chartGroup = chartGroup;
return _chart;
};
/**
* Returns the DOM id for the chart's anchored location.
* @method anchorName
* @memberof dc.baseMixin
* @instance
* @returns {String}
*/
_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();
};
/**
* Returns the root element where a chart resides. Usually it will be the parent div element where
* the SVGElement 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.
* @method root
* @memberof dc.baseMixin
* @instance
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement HTMLElement}
* @param {HTMLElement} [rootElement]
* @returns {HTMLElement|dc.baseMixin}
*/
_chart.root = function (rootElement) {
if (!arguments.length) {
return _root;
}
_root = rootElement;
return _chart;
};
/**
* Returns the top SVGElement for this specific chart. You can also pass in a new SVGElement,
* however this is usually handled by dc internally. Resetting the SVGElement on a chart outside
* of dc internals may have unexpected consequences.
* @method svg
* @memberof dc.baseMixin
* @instance
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/SVGElement SVGElement}
* @param {SVGElement|d3.selection} [svgElement]
* @returns {SVGElement|d3.selection|dc.baseMixin}
*/
_chart.svg = function (svgElement) {
if (!arguments.length) {
return _svg;
}
_svg = svgElement;
return _chart;
};
/**
* Remove the chart's SVGElements from the dom and recreate the container SVGElement.
* @method resetSvg
* @memberof dc.baseMixin
* @instance
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/SVGElement SVGElement}
* @returns {SVGElement}
*/
_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;
}
/**
* 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. The text will get shown
* in the `.filter element; see {@link dc.baseMixin#turnOnControls turnOnControls}.
*
* By default dc charts use a default filter printer {@link dc.printers.filters dc.printers.filters}
* that provides simple printing support for both single value and ranged filters.
* @method filterPrinter
* @memberof dc.baseMixin
* @instance
* @example
* // for a chart with an ordinal brush, print the filters in upper case
* chart.filterPrinter(function(filters) {
* return filters.map(function(f) { return f.toUpperCase(); }).join(', ');
* });
* // for a chart with a range brush, print the filter as start and extent
* chart.filterPrinter(function(filters) {
* return 'start ' + dc.utils.printSingleValue(filters[0][0]) +
* ' extent ' + dc.utils.printSingleValue(filters[0][1] - filters[0][0]);
* });
* @param {Function} [filterPrinterFunction=dc.printers.filters]
* @returns {Function|dc.baseMixin}
*/
_chart.filterPrinter = function (filterPrinterFunction) {
if (!arguments.length) {
return _filterPrinter;
}
_filterPrinter = filterPrinterFunction;
return _chart;
};
/**
* If set, use the `visibility` attribute instead of the `display` attribute for showing/hiding
* chart reset and filter controls, for less disruption to the layout.
* @method controlsUseVisibility
* @memberof dc.baseMixin
* @instance
* @param {Boolean} [controlsUseVisibility=false]
* @returns {Boolean|dc.baseMixin}
**/
_chart.controlsUseVisibility = function (useVisibility) {
if (!arguments.length) {
return _controlsUseVisibility;
}
_controlsUseVisibility = useVisibility;
return _chart;
};
/**
* Turn on 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.
* @method turnOnControls
* @memberof dc.baseMixin
* @instance
* @returns {dc.baseMixin}
*/
_chart.turnOnControls = function () {
if (_root) {
var attribute = _chart.controlsUseVisibility() ? 'visibility' : 'display';
_chart.selectAll('.reset').style(attribute, null);
_chart.selectAll('.filter').text(_filterPrinter(_chart.filters())).style(attribute, null);
}
return _chart;
};
/**
* Turn off optional control elements within the root element.
* @method turnOffControls
* @memberof dc.baseMixin
* @see {@link dc.baseMixin#turnOnControls turnOnControls}
* @instance
* @returns {dc.baseMixin}
*/
_chart.turnOffControls = function () {
if (_root) {
var attribute = _chart.controlsUseVisibility() ? 'visibility' : 'display';
var value = _chart.controlsUseVisibility() ? 'hidden' : 'none';
_chart.selectAll('.reset').style(attribute, value);
_chart.selectAll('.filter').style(attribute, value).text(_chart.filter());
}
return _chart;
};
/**
* Set or get the animation transition duration (in milliseconds) for this chart instance.
* @method transitionDuration
* @memberof dc.baseMixin
* @instance
* @param {Number} [duration=750]
* @returns {Number|dc.baseMixin}
*/
_chart.transitionDuration = function (duration) {
if (!arguments.length) {
return _transitionDuration;
}
_transitionDuration = duration;
return _chart;
};
/**
* Set or get the animation transition delay (in milliseconds) for this chart instance.
* @method transitionDelay
* @memberof dc.baseMixin
* @instance
* @param {Number} [delay=0]
* @returns {Number|dc.baseMixin}
*/
_chart.transitionDelay = function (delay) {
if (!arguments.length) {
return _transitionDelay;
}
_transitionDelay = delay;
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() + ']');
}
}
/**
* 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.
* @method render
* @memberof dc.baseMixin
* @instance
* @returns {dc.baseMixin}
*/
_chart.render = function () {
_height = _width = undefined; // force recalculate
_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()).delay(_chart.transitionDelay())
.each('end', function () {
_listeners.renderlet(_chart);
if (event) {
_listeners[event](_chart);
}
});
} else {
_listeners.renderlet(_chart);
if (event) {
_listeners[event](_chart);
}
}
};
/**
* 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 {@link dc.redrawAll dc.redrawAll}); 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
* {@link https://github.com/crossfilter/crossfilter/wiki/API-Reference#crossfilter_add crossfilter.add}).
* @method redraw
* @memberof dc.baseMixin
* @instance
* @returns {dc.baseMixin}
*/
_chart.redraw = function () {
sizeSvg();
_listeners.preRedraw(_chart);
var result = _chart._doRedraw();
if (_legend) {
_legend.render();
}
_chart._activateRenderlets('postRedraw');
return result;
};
/**
* Gets/sets the commit handler. If the chart has a commit handler, the handler will be called when
* the chart's filters have changed, in order to send the filter data asynchronously to a server.
*
* Unlike other functions in dc.js, the commit handler is asynchronous. It takes two arguments:
* a flag indicating whether this is a render (true) or a redraw (false), and a callback to be
* triggered once the commit is filtered. The callback has the standard node.js continuation signature
* with error first and result second.
* @method commitHandler
* @memberof dc.baseMixin
* @instance
* @returns {dc.baseMixin}
*/
_chart.commitHandler = function (commitHandler) {
if (!arguments.length) {
return _commitHandler;
}
_commitHandler = commitHandler;
return _chart;
};
/**
* Redraws all charts in the same group as this chart, typically in reaction to a filter
* change. If the chart has a {@link dc.baseMixin.commitFilter commitHandler}, it will
* be executed and waited for.
* @method redrawGroup
* @memberof dc.baseMixin
* @instance
* @returns {dc.baseMixin}
*/
_chart.redrawGroup = function () {
if (_commitHandler) {
_commitHandler(false, function (error, result) {
if (error) {
console.log(error);
} else {
dc.redrawAll(_chart.chartGroup());
}
});
} else {
dc.redrawAll(_chart.chartGroup());
}
return _chart;
};
/**
* Renders all charts in the same group as this chart. If the chart has a
* {@link dc.baseMixin.commitFilter commitHandler}, it will be executed and waited for
* @method renderGroup
* @memberof dc.baseMixin
* @instance
* @returns {dc.baseMixin}
*/
_chart.renderGroup = function () {
if (_commitHandler) {
_commitHandler(false, function (error, result) {
if (error) {
console.log(error);
} else {
dc.renderAll(_chart.chartGroup());
}
});
} else {
dc.renderAll(_chart.chartGroup());
}
return _chart;
};
_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;
});
};
/**
* Set or get the has-filter handler. The has-filter handler is a function that checks to see if
* the chart's current filters (first argument) include a specific filter (second argument). Using a custom has-filter handler allows
* you to change the way filters are checked for and replaced.
* @method hasFilterHandler
* @memberof dc.baseMixin
* @instance
* @example
* // default has-filter handler
* chart.hasFilterHandler(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;
* });
* @param {Function} [hasFilterHandler]
* @returns {Function|dc.baseMixin}
*/
_chart.hasFilterHandler = function (hasFilterHandler) {
if (!arguments.length) {
return _hasFilterHandler;
}
_hasFilterHandler = hasFilterHandler;
return _chart;
};
/**
* Check whether any active filter or a specific filter is associated with particular chart instance.
* This function is **not chainable**.
* @method hasFilter
* @memberof dc.baseMixin
* @instance
* @see {@link dc.baseMixin#hasFilterHandler hasFilterHandler}
* @param {*} [filter]
* @returns {Boolean}
*/
_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;
};
/**
* 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.
*
* The handler should return a new or modified array as the result.
* @method removeFilterHandler
* @memberof dc.baseMixin
* @instance
* @example
* // default remove filter handler
* chart.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;
* });
*
* // custom filter handler (no-op)
* chart.removeFilterHandler(function(filters, filter) {
* return filters;
* });
* @param {Function} [removeFilterHandler]
* @returns {Function|dc.baseMixin}
*/
_chart.removeFilterHandler = function (removeFilterHandler) {
if (!arguments.length) {
return _removeFilterHandler;
}
_removeFilterHandler = removeFilterHandler;
return _chart;
};
var _addFilterHandler = function (filters, filter) {
filters.push(filter);
return filters;
};
/**
* 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.
*
* The handler should return a new or modified array as the result.
* @method addFilterHandler
* @memberof dc.baseMixin
* @instance
* @example
* // default add filter handler
* chart.addFilterHandler(function (filters, filter) {
* filters.push(filter);
* return filters;
* });
*
* // custom filter handler (no-op)
* chart.addFilterHandler(function(filters, filter) {
* return filters;
* });
* @param {Function} [addFilterHandler]
* @returns {Function|dc.baseMixin}
*/
_chart.addFilterHandler = function (addFilterHandler) {
if (!arguments.length) {
return _addFilterHandler;
}
_addFilterHandler = addFilterHandler;
return _chart;
};
var _resetFilterHandler = function (filters) {
return [];
};
/**
* 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.
*
* The handler should return a new or modified array as the result.
* @method resetFilterHandler
* @memberof dc.baseMixin
* @instance
* @example
* // default remove filter handler
* function (filters) {
* return [];
* }
*
* // custom filter handler (no-op)
* chart.resetFilterHandler(function(filters) {
* return filters;
* });
* @param {Function} [resetFilterHandler]
* @returns {dc.baseMixin}
*/
_chart.resetFilterHandler = function (resetFilterHandler) {
if (!arguments.length) {
return _resetFilterHandler;
}
_resetFilterHandler = resetFilterHandler;
return _chart;
};
function applyFilters (filters) {
if (_chart.dimension() && _chart.dimension().filter) {
var fs = _filterHandler(_chart.dimension(), filters);
if (fs) {
filters = fs;
}
}
return filters;
}
/**
* Replace the chart filter. This is equivalent to calling `chart.filter(null).filter(filter)`
* but more efficient because the filter is only applied once.
*
* @method replaceFilter
* @memberof dc.baseMixin
* @instance
* @param {*} [filter]
* @returns {dc.baseMixin}
**/
_chart.replaceFilter = function (filter) {
_filters = _resetFilterHandler(_filters);
_chart.filter(filter);
};
/**
* Filter the chart by the given parameter, or return the current filter if no input parameter
* is given.
*
* The filter parameter can take one of these forms:
* * A single value: the value will be toggled (added if it is not present in the current