Debian Codegraph @

bqplot/js/src/PieModel.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 // var d3 =Object.assign({}, require("d3-scale"), require("d3-scale-chromatic"));
18 import { MarkModel } from './MarkModel';
19 import { getDate } from './utils';
20 import * as serialize from './serialize';
21
22 export interface Slice {
23 size: number;
24 color: number;
25 label: string;
26 index: number;
27 }
28
29 export class PieModel extends MarkModel {
30 defaults() {
31 return {
32 ...MarkModel.prototype.defaults(),
33 _model_name: 'PieModel',
34 _view_name: 'Pie',
35
36 sizes: [],
37 labels: [],
38 color: null,
39 x: 0.5,
40 y: 0.5,
41 scales_metadata: {
42 color: { dimension: 'color' },
43 },
44 sort: false,
45 colors: d3.scaleOrdinal(d3.schemeCategory10).range(),
46 stroke: null,
47 opacities: [],
48 radius: 180,
49 inner_radius: 0.1,
50 start_angle: 0.0,
51 end_angle: 360.0,
52 display_labels: 'inside',
53 display_values: false,
54 values_format: '.1f',
55 label_color: null,
56 font_size: '12px',
57 font_weight: 'normal',
58 };
59 }
60
61 initialize(attributes, options) {
62 super.initialize(attributes, options);
63 this.on('change:sizes', this.update_data, this);
64 this.on(
65 'change:color',
66 function () {
67 this.updateColor();
68 this.trigger('colors_updated');
69 },
70 this
71 );
72 this.on('change:labels', this.updateLabels, this);
73
74 this.on_some_change(['preserve_domain'], this.update_domains, this);
75 this.update_data();
76 this.updateColor();
77 this.updateLabels();
78 this.update_domains();
79 }
80
81 update_data() {
82 const sizes = this.get('sizes') || [];
83 const color = this.get('color') || [];
84 const labels = this.get('labels') || [];
85
86 this.mark_data = Array.prototype.map.call(sizes, (d: number, i: number) => {
87 return {
88 size: d,
89 color: color[i],
90 label: labels[i] == null ? '' : labels[i],
91 index: i,
92 };
93 });
94
95 this.updateColor();
96 this.update_domains();
97 this.trigger('data_updated');
98 }
99
100 private updateLabels() {
101 if (!this.mark_data) {
102 return;
103 }
104
105 const labels = this.get('labels');
106 this.mark_data.forEach((data, index) => {
107 data.label = labels[index] == null ? '' : labels[index];
108 });
109 this.trigger('labels_updated');
110 }
111
112 private updateColor() {
113 if (!this.mark_data) {
114 return;
115 }
116
117 const color = this.get('color');
118 const color_scale = this.get('scales').color;
119
120 if (color_scale) {
121 if (!this.get('preserve_domain').color) {
122 color_scale.compute_and_set_domain(color, this.model_id + '_color');
123 } else {
124 color_scale.del_domain([], this.model_id + '_color');
125 }
126 }
127 }
128
129 update_domains() {
130 if (!this.mark_data) {
131 return;
132 }
133
134 const scales = this.get('scales');
135
136 if (scales.x) {
137 const x =
138 scales.x.type === 'date' ? getDate(this.get('x')) : this.get('x');
139 if (!this.get('preserve_domain').x) {
140 scales.x.compute_and_set_domain([x], this.model_id + '_x');
141 } else {
142 scales.x.del_domain([], this.model_id + '_x');
143 }
144 }
145 if (scales.y) {
146 if (!this.get('preserve_domain').y) {
147 scales.y.compute_and_set_domain([this.get('y')], this.model_id + '_y');
148 } else {
149 scales.y.del_domain([], this.model_id + '_y');
150 }
151 }
152 }
153
154 get_data_dict(data, index) {
155 return data.data;
156 }
157
158 static serializers = {
159 ...MarkModel.serializers,
160 sizes: serialize.array_or_json_serializer,
161 color: serialize.array_or_json_serializer,
162 };
163
164 mark_data: Slice[];
165 }