Debian Codegraph @

bqplot/js/src/FlexLine.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-array"), require("d3-selection"));
18 import { Lines } from './Lines';
19
20 export class FlexLine extends Lines {
21 render() {
22 const base_render_promise = super.render.apply(this);
23 const that = this;
24
25 return base_render_promise.then(() => {
26 that.create_listeners();
27 that.draw();
28 });
29 }
30
31 set_ranges() {
32 super.set_ranges();
33 const width_scale = this.scales.width;
34 if (width_scale) {
35 width_scale.set_range([0.5, this.model.get('stroke_width')]);
36 }
37 }
38
39 create_listeners() {
40 super.create_listeners();
41 this.listenTo(
42 this.model,
43 'change:labels_visibility',
44 this.update_legend_labels
45 );
46 }
47
48 draw_legend(elem, x_disp, y_disp, inter_x_disp, inter_y_disp) {
49 const g_elements = elem
50 .selectAll('.legend' + this.uuid)
51 .data(this.model.mark_data, (d, i) => {
52 return d.name;
53 });
54
55 const that = this;
56 const rect_dim = inter_y_disp * 0.8;
57 g_elements
58 .enter()
59 .append('g')
60 .attr('class', 'legend' + this.uuid)
61 .attr('transform', (d, i) => {
62 return 'translate(0, ' + (i * inter_y_disp + y_disp) + ')';
63 })
64 .append('line')
65 .style('stroke', (d, i) => {
66 return that.get_colors(i);
67 })
68 .attr('x1', 0)
69 .attr('x2', rect_dim)
70 .attr('y1', rect_dim / 2)
71 .attr('y2', rect_dim / 2);
72
73 g_elements
74 .append('text')
75 .attr('class', 'legendtext')
76 .attr('x', rect_dim * 1.2)
77 .attr('y', rect_dim / 2)
78 .attr('dy', '0.35em')
79 .text((d, i) => {
80 return that.model.get('labels')[i];
81 })
82 .style('fill', (d, i) => {
83 return that.get_colors(i);
84 });
85 const max_length = d3.max(this.model.get('labels'), (d: any[]) => {
86 return d.length;
87 });
88
89 g_elements.exit().remove();
90 return [this.model.mark_data.length, max_length];
91 }
92
93 set_positional_scales() {
94 const x_scale = this.scales.x,
95 y_scale = this.scales.y;
96 this.listenTo(x_scale, 'domain_changed', function () {
97 if (!this.model.dirty) {
98 this.draw();
99 }
100 });
101 this.listenTo(y_scale, 'domain_changed', function () {
102 if (!this.model.dirty) {
103 this.draw();
104 }
105 });
106 }
107
108 initialize_additional_scales() {
109 const color_scale = this.scales.color;
110 if (color_scale) {
111 this.listenTo(color_scale, 'domain_changed', function () {
112 this.draw();
113 });
114 color_scale.on('color_scale_range_changed', this.draw, this);
115 }
116 }
117
118 draw() {
119 this.set_ranges();
120 let curves_sel: d3.Selection<any, any, any, any> = this.d3el
121 .selectAll('.curve')
122 .data(this.model.mark_data, (d: any, i) => {
123 return d.name;
124 });
125
126 curves_sel
127 .exit()
128 .transition('draw')
129 .duration(this.parent.model.get('animation_duration'))
130 .remove();
131
132 curves_sel = curves_sel
133 .enter()
134 .append('g')
135 .attr('class', 'curve')
136 .merge(curves_sel);
137
138 const x_scale = this.scales.x,
139 y_scale = this.scales.y;
140
141 const that = this;
142 curves_sel.nodes().forEach((elem, index) => {
143 let lines = d3
144 .select(elem)
145 .selectAll<SVGLineElement, undefined>('line')
146 .data(that.model.mark_data[index].values);
147 lines = lines.enter().append('line').merge(lines);
148 lines
149 .attr('class', 'line-elem')
150 .attr('x1', (d: any) => {
151 return x_scale.scale(d.x1);
152 })
153 .attr('x2', (d: any) => {
154 return x_scale.scale(d.x2);
155 })
156 .attr('y1', (d: any) => {
157 return y_scale.scale(d.y1);
158 })
159 .attr('y2', (d: any) => {
160 return y_scale.scale(d.y2);
161 })
162 .attr('stroke', that.get_mark_color.bind(that))
163 .attr('stroke-width', (d) => {
164 return that.get_element_width(d);
165 });
166 });
167 }
168
169 get_element_width(d) {
170 const width_scale = this.scales.width;
171 if (width_scale !== undefined && d.size !== undefined) {
172 return width_scale.scale(d.size);
173 }
174 return this.model.get('stroke_width');
175 }
176
177 relayout() {
178 this.set_ranges();
179
180 const x_scale = this.scales.x,
181 y_scale = this.scales.y;
182
183 this.d3el
184 .selectAll('.curve')
185 .selectAll('.line-elem')
186 .transition('relayout')
187 .duration(this.parent.model.get('animation_duration'))
188 .attr('x1', (d: any) => {
189 return x_scale.scale(d.x1);
190 })
191 .attr('x2', (d: any) => {
192 return x_scale.scale(d.x2);
193 })
194 .attr('y1', (d: any) => {
195 return y_scale.scale(d.y1);
196 })
197 .attr('y2', (d: any) => {
198 return y_scale.scale(d.y2);
199 });
200 }
201 }