Debian Codegraph @

bqplot/js/src/Label.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-selection"));
18 import { ScatterBase } from './ScatterBase';
19
20 export class Label extends ScatterBase {
21 create_listeners() {
22 super.create_listeners();
23 this.model.on_some_change(
24 ['font_weight', 'default_size', 'colors', 'align', 'font_unit'],
25 this.update_style,
26 this
27 );
28 this.model.on_some_change(
29 ['x', 'y', 'x_offset', 'y_offset', 'rotate_angle'],
30 this.update_position,
31 this
32 );
33 }
34
35 update_opacities(animate) {
36 if (!this.model.dirty) {
37 const animation_duration =
38 animate === true ? this.parent.model.get('animation_duration') : 0;
39
40 // update opacity scale range?
41 const that = this;
42 this.d3el
43 .selectAll('.label')
44 .transition('update_opacities')
45 .duration(animation_duration)
46 .style('opacity', (d, i) => {
47 return that.get_element_opacity(d, i);
48 });
49 }
50 }
51
52 update_default_size(animate) {
53 this.compute_view_padding();
54 // update size scale range?
55 if (!this.model.dirty) {
56 const animation_duration =
57 animate === true ? this.parent.model.get('animation_duration') : 0;
58 const that = this;
59 this.d3el
60 .selectAll('.label')
61 .transition('update_default_size')
62 .duration(animation_duration)
63 .style('font-size', (d, i) => {
64 return that.get_element_size(d);
65 });
66 }
67 }
68
69 relayout() {
70 this.set_ranges();
71 this.update_position();
72 }
73
74 draw_elements(animate, elements_added) {
75 elements_added.append('text').classed('label element', true);
76
77 this.update_text();
78 this.update_style();
79 this.update_opacities(true);
80 }
81
82 update_text() {
83 this.d3el
84 .selectAll('.object_grp')
85 .select('.label')
86 .text((d: any) => {
87 return d.text;
88 });
89 }
90
91 get_element_size(data) {
92 const size_scale = this.scales.size;
93 const unit = this.model.get('font_unit');
94 if (size_scale && data.size !== undefined) {
95 return size_scale.scale(data.size) + unit;
96 }
97 return this.model.get('default_size') + unit;
98 }
99
100 get_element_rotation(data) {
101 const rotation_scale = this.scales.rotation;
102 return !rotation_scale || !data.rotation
103 ? 'rotate(' + this.model.get('rotate_angle') + ')'
104 : 'rotate(' + rotation_scale.scale(data.rotation) + ')';
105 }
106
107 update_position() {
108 const that = this;
109 const x_scale = this.x_scale;
110 const y_scale = this.y_scale;
111 const x_offset = this.model.get('x_offset'),
112 y_offset = this.model.get('y_offset');
113 this.d3el.selectAll('.object_grp').attr('transform', (d: any) => {
114 return (
115 'translate(' +
116 (x_scale.scale(d.x) + x_scale.offset + x_offset) +
117 ',' +
118 (y_scale.scale(d.y) + y_scale.offset + y_offset) +
119 ')' +
120 that.get_element_rotation(d)
121 );
122 });
123 }
124
125 update_style() {
126 const that = this;
127 this.d3el
128 .selectAll('.object_grp')
129 .select('text')
130 .attr('dominant-baseline', 'central')
131 .style('font-size', (d, i) => {
132 return that.get_element_size(d);
133 })
134 .style('font-weight', this.model.get('font_weight'))
135 .style('text-anchor', this.model.get('align'));
136
137 this.d3el.selectAll('.label').style('fill', this.get_mark_color.bind(this));
138 }
139
140 color_scale_updated(animate) {
141 const animation_duration =
142 animate === true ? this.parent.model.get('animation_duration') : 0;
143
144 this.d3el
145 .selectAll('.object_grp')
146 .select('text')
147 .transition('color_scale_updated')
148 .duration(animation_duration)
149 .style('fill', this.get_mark_color.bind(this));
150 }
151
152 set_default_style(indices) {
153 // For all the elements with index in the list indices, the default
154 // style is applied.
155 if (!indices || indices.length === 0) {
156 return;
157 }
158 const elements = this.d3el.selectAll('.element').filter((data, index) => {
159 return indices.indexOf(index) !== -1;
160 });
161 const that = this;
162 elements
163 .style('font-size', (d, i) => {
164 return that.get_element_size(d);
165 })
166 .style('font-weight', this.model.get('font_weight'))
167 .style('text-anchor', this.model.get('align'))
168 .style('fill', this.get_mark_color.bind(this));
169 }
170
171 set_drag_style(d, i, dragged_node) {
172 const dragged_size =
173 this.model.get('drag_size') * this.model.get('default_size') +
174 this.model.get('font_unit');
175 d3.select(dragged_node)
176 .select('text')
177 .classed('drag_label', true)
178 .transition('set_drag_style')
179 .style('font-size', dragged_size);
180 }
181
182 reset_drag_style(d, i, dragged_node) {
183 d3.select(dragged_node)
184 .select('text')
185 .classed('drag_label', false)
186 .transition('reset_drag_style')
187 .style('font-size', this.get_element_size(d));
188 }
189
190 // TODO: put this here just because it is an abstract method on the base
191 // class.
192 update_default_skew() {}
193 }