Debian Codegraph @

bqplot/js/src/HandDraw.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 utils from './utils';
18 import { Interaction } from './Interaction';
19 import { Lines } from './Lines';
20 import { LinesModel } from './LinesModel';
21
22 export class HandDraw extends Interaction {
23 render() {
24 super.render();
25 this.d3el.style('cursor', 'crosshair');
26 this.active = false;
27
28 // Register the mouse callback when the mark view promises are
29 // resolved.
30 this.setLinesView().then(() => {
31 this.d3el.on('mousedown', () => {
32 return this.mousedown();
33 });
34 this.setLimits();
35 });
36
37 this.model.on_some_change(['min_x', 'max_x'], this.setLimits, this);
38 }
39
40 private async setLinesView(): Promise<void> {
41 const fig = this.parent;
42
43 const views = await Promise.all(fig.mark_views.views);
44
45 const fig_mark_ids = fig.mark_views._models.map((markModel) => {
46 return markModel.model_id; // Model ids of the marks in the figure
47 });
48 const mark_index = fig_mark_ids.indexOf(this.lines.model_id);
49 this.linesView = views[mark_index] as Lines;
50 }
51
52 private mousedown(): void {
53 this.active = true;
54 this.mouseEntry(false);
55
56 this.d3el.on('mousemove', this.mousemove.bind(this));
57 this.d3el.on('mouseleave', this.mouseup.bind(this));
58 this.d3el.on('mouseup', this.mouseup.bind(this));
59 }
60
61 private mouseup(): void {
62 if (this.active) {
63 this.mouseEntry(true);
64
65 this.lines.set(
66 'y',
67 utils.convert_dates(utils.deepCopy(this.lines.y_data))
68 );
69 this.linesView.touch();
70 this.active = false;
71 this.d3el.on('mousemove', null);
72 this.d3el.on('mouseleave', null);
73 this.d3el.on('mouseup', null);
74 }
75 }
76
77 private mousemove(): void {
78 this.mouseEntry(true);
79 }
80
81 private mouseEntry(memory: boolean) {
82 // If memory is set to true, itermediate positions between the last
83 // position of the mouse and the current one will be interpolated.
84 if (this.active) {
85 const xindex = Math.min(this.lineIndex, this.lines.x_data.length - 1);
86 const mousePos = d3.mouse(this.el);
87 if (!memory || !('previousPos' in this)) {
88 this.previousPos = mousePos;
89 }
90 const scaleX = this.linesView.scales.x.scale;
91 const scaleY = this.linesView.scales.y.scale;
92
93 const newx = scaleX.invert(mousePos[0]);
94 const newy = scaleY.invert(mousePos[1]);
95 const oldx = scaleX.invert(this.previousPos[0]);
96 scaleY.invert(this.previousPos[1]);
97 const old_index = this.nearestNeighbourSearch(
98 this.lines.x_data[xindex],
99 oldx
100 );
101 const new_index = this.nearestNeighbourSearch(
102 this.lines.x_data[xindex],
103 newx
104 );
105 const min = Math.min(old_index, new_index);
106 const max = Math.max(old_index, new_index);
107 for (let i = min; i <= max; ++i) {
108 if (
109 (!this.validMin || this.lines.x_data[xindex][i] >= this.minX) &&
110 (!this.validMax || this.lines.x_data[xindex][i] <= this.maxX)
111 ) {
112 this.lines.y_data[this.lineIndex][i] = newy;
113 }
114 }
115 // since x_data may be a TypedArray, explicitly use Array.map
116 const xy_data = Array.prototype.map.call(
117 this.lines.x_data[xindex],
118 (d, i) => {
119 return {
120 x: d,
121 y: this.lines.y_data[this.lineIndex][i],
122 };
123 }
124 );
125 this.linesView.d3el
126 .select('#curve' + (this.lineIndex + 1))
127 .attr('d', (d) => {
128 return this.linesView.line(xy_data);
129 });
130 this.previousPos = mousePos;
131 }
132 }
133
134 private setLimits(): void {
135 const is_date = this.linesView.scales.x.model.type == 'date';
136 if (is_date) {
137 this.minX = utils.getDate(this.model.get('min_x'));
138 this.validMin = !(
139 this.minX === null ||
140 this.minX === undefined ||
141 isNaN(this.minX.getTime())
142 );
143 this.maxX = utils.getDate(this.model.get('max_x'));
144 this.validMax = !(
145 this.maxX === null ||
146 this.maxX === undefined ||
147 isNaN(this.maxX.getTime())
148 );
149 } else {
150 this.minX = this.model.get('min_x');
151 this.maxX = this.model.get('max_x');
152 this.validMin = !(this.minX === null || this.minX === undefined);
153 this.validMax = !(this.maxX === null || this.maxX === undefined);
154 }
155 }
156
157 private nearestNeighbourSearch(x_data: number[], x: number): number {
158 const idx = this.linesView.bisect(x_data, x);
159 if (x - x_data[idx - 1] > x_data[idx] - x) {
160 return idx;
161 } else {
162 return idx - 1;
163 }
164 }
165
166 private get lineIndex(): number {
167 return this.model.get('line_index');
168 }
169
170 private get lines(): LinesModel {
171 return this.model.get('lines');
172 }
173
174 private active: boolean;
175 private linesView: Lines;
176 private minX: number | Date;
177 private maxX: number | Date;
178 private validMin: boolean;
179 private validMax: boolean;
180 private previousPos: [number, number];
181 }