Debian Codegraph @

bqplot/js/src/BoxplotModel.ts

raw
1 /* Copyright 2015 Bloomberg Finance L.P.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 import * as d3 from 'd3';
17 import * as _ from 'underscore';
18 import { MarkModel } from './MarkModel';
19 import * as serialize from './serialize';
20
21 export class BoxplotModel extends MarkModel {
22 defaults() {
23 return {
24 ...MarkModel.prototype.defaults(),
25 _model_name: 'BoxplotModel',
26 _view_name: 'Boxplot',
27 x: [],
28 y: [],
29 scales_metadata: {
30 x: { orientation: 'horizontal', dimension: 'x' },
31 y: { orientation: 'vertical', dimension: 'y' },
32 },
33 stroke: null,
34 box_fill_color: 'dodgerblue',
35 outlier_fill_color: 'gray',
36 opacities: [],
37 box_width: null, // auto calculate box width
38 auto_detect_outliers: true,
39 };
40 }
41
42 initialize(): void {
43 super.initialize.apply(this, arguments);
44
45 this.on_some_change(['x', 'y'], this.update_data, this);
46 this.on_some_change(['preserve_domain'], this.update_domains, this);
47 this.update_data();
48 this.update_domains();
49 }
50
51 get_data_dict(data, index) {
52 return data.dataDict;
53 }
54
55 update_data() {
56 let xData = this.get('x');
57 let yData = this.get('y');
58
59 yData.forEach((elm) => {
60 elm.sort((a, b) => {
61 return a - b;
62 });
63 });
64
65 if (xData.length > yData.length) {
66 xData = xData.slice(0, yData.length);
67 } else if (xData.length < yData.length) {
68 yData = yData.slice(0, xData.length);
69 }
70
71 this.mark_data = _.zip(xData, yData);
72
73 this.update_domains();
74 this.trigger('data_updated');
75 }
76
77 update_domains() {
78 // color scale needs an issue in DateScaleModel to be fixed. It
79 // should be moved here as soon as that is fixed.
80 const scales = this.get('scales');
81
82 if (!this.get('preserve_domain').x && this.mark_data) {
83 scales.x.compute_and_set_domain(
84 this.mark_data.map((elem) => {
85 return elem[0];
86 }),
87 this.model_id + '_x'
88 );
89 } else {
90 scales.x.del_domain([], this.model_id + '_x');
91 }
92
93 if (!this.get('preserve_domain').y && this.mark_data) {
94 //The values are sorted, so we are using that to calculate the min/max
95
96 const min = d3.min(
97 this.mark_data.map((d) => {
98 return d[1][0];
99 })
100 );
101 const max = d3.max(
102 this.mark_data.map((d) => {
103 const values = d[1];
104 return values[values.length - 1];
105 })
106 );
107
108 scales.y.set_domain([min, max], this.model_id + '_y');
109 } else {
110 scales.y.del_domain([], this.model_id + '_y');
111 }
112 }
113
114 static serializers = {
115 ...MarkModel.serializers,
116 x: serialize.array_or_json_serializer,
117 y: serialize.array_or_json_serializer,
118 };
119 }