20
20
'left': (this.pMarginLeft) + 'px'
21
21
}"
22
22
></canvas >
23
- <div v-show =" this.highlightXY !== null"
23
+ <div v-show =" this.highlightY !== null"
24
24
:style =" {
25
25
'height': (this.highlightHeight - 0.5) + 'px',
26
26
'width': (this.highlightWidth - 0.5) + 'px',
27
- 'top': (this.pMarginTop - 0.5) + 'px',
27
+ 'top': (this.pMarginTop + this.highlightY - 0.5) + 'px',
28
28
'left': (this.pMarginLeft - 0.5) + 'px'
29
29
}"
30
30
class =" vdp-plot-highlight-rect"
46
46
47
47
<script >
48
48
import Two from ' ../../two.js' ;
49
+ import { scaleBand as d3_scaleBand } from ' d3-scale' ;
49
50
import { select as d3_select , create as d3_create } from ' d3-selection' ;
50
51
import { mouse as d3_mouse , event as d3_event } from ' d3' ;
51
52
import debounce from ' lodash/debounce' ;
53
+ import range from ' lodash/range' ;
52
54
import { TOOLTIP_DEBOUNCE , BAR_WIDTH_MIN } from ' ./../../constants.js' ;
53
55
import { getRetinaRatio } from ' ./../../helpers.js' ;
54
56
@@ -58,19 +60,14 @@ import DataContainer from './../../data/DataContainer.js';
58
60
import mixin from ' ./mixin.js' ;
59
61
import CategoricalScale from ' ./../../scales/CategoricalScale.js' ;
60
62
61
- const SIDES = Object .freeze ({ " TOP" : 1 , " LEFT" : 2 , " RIGHT" : 3 , " BOTTOM" : 4 });
62
- const ORIENTATIONS = Object .freeze ({ " VERTICAL" : 1 , " HORIZONTAL" : 2 }); // vertical = left/right, horizontal = top/bottom
63
-
64
63
let uuid = 0 ;
65
64
/**
66
65
* @prop {string} dataArray An array of data keys.
67
66
* @prop {string} cArray An array of color-scale variable keys.
68
67
* @prop {string} z The observation-scale variable key.
69
68
* @prop {string} o The observation (observation-scale domain element of interest).
70
- * @prop {string} orientation One of {'vertical', 'horizontal'}. TODO: implement horizontal.
71
69
* @prop {number} rectMargin The margin between each rect.
72
70
* @prop {number} rectSize The size of rects (only used if text values are enabled).
73
- * @prop {string} textSide One of {'top', 'left', 'right', 'bottom'} (only used if text values are enabled). TODO: implement top, left, bottom.
74
71
* @prop {boolean} disableText Whether to disable the rendering of text for each value.
75
72
* @prop {boolean} disableTooltip Whether to disable tooltips. Default: false
76
73
* @extends mixin
@@ -81,7 +78,6 @@ let uuid = 0;
81
78
* : cArray= " ['mut_class', 'mut_class', 'mut_class']"
82
79
* z= " sample_id"
83
80
* o= " SA12345"
84
- * orientation= " vertical"
85
81
* : pWidth= " 500"
86
82
* : pHeight= " 300"
87
83
* : pMarginTop= " 10"
@@ -124,10 +120,6 @@ export default {
124
120
type: String ,
125
121
default: " #000"
126
122
},
127
- ' textSide' : {
128
- type: String ,
129
- default: ' right'
130
- },
131
123
' disableText' : {
132
124
type: Boolean ,
133
125
default: true
@@ -144,7 +136,8 @@ export default {
144
136
c: ' ' ,
145
137
i: 0 ,
146
138
},
147
- highlightXY: null ,
139
+ highlightYScale: null ,
140
+ highlightY: null ,
148
141
highlightHeight: null ,
149
142
highlightWidth: null ,
150
143
}
@@ -154,14 +147,6 @@ export default {
154
147
uuid += 1 ;
155
148
},
156
149
created () {
157
- const orientationString = this .orientation .toUpperCase ();
158
- console .assert (Object .keys (ORIENTATIONS ).includes (orientationString));
159
- this ._orientation = ORIENTATIONS [orientationString];
160
-
161
- const textSideString = this .textSide .toUpperCase ();
162
- console .assert (Object .keys (SIDES ).includes (textSideString));
163
- this ._textSide = SIDES [textSideString];
164
-
165
150
console .assert (this .dataArray .length === this .cArray .length );
166
151
167
152
// Set data
@@ -232,7 +217,7 @@ export default {
232
217
this .tooltipPosition .left = mouseX;
233
218
this .tooltipPosition .top = mouseY;
234
219
}
235
-
220
+
236
221
// Dispatch highlights
237
222
this ._zScale .emitHighlight (z);
238
223
this ._cScales [i].emitHighlight (c);
@@ -259,12 +244,9 @@ export default {
259
244
return ;
260
245
}
261
246
262
-
263
247
const cScales = vm ._cScales ;
264
248
265
249
const point = vm ._dataContainers .map ((dc ) => dc .dataCopy .find ((el ) => el[vm .z ] === vm .o )); // the single data point
266
-
267
- console .log (point);
268
250
269
251
/*
270
252
* Scale up the canvas
@@ -335,61 +317,38 @@ export default {
335
317
*/
336
318
337
319
// Compute sizes.
338
- const numRects = point .length ;
339
- let rectHeight, rectWidth, rectMargin, textWidth, textHeight;
340
320
341
- rectMargin = vm .rectMargin ;
342
-
343
- if (vm ._orientation === ORIENTATIONS .HORIZONTAL ) {
344
- rectHeight = vm .pHeight ;
345
- rectWidth = vm .pWidth / numRects;
321
+ const numRects = point .length ;
346
322
347
- if (rectWidth - rectMargin <= BAR_WIDTH_MIN ) {
348
- rectMargin = 0 ;
349
- }
323
+ const y = d3_scaleBand ()
324
+ . domain ( range (numRects))
325
+ . range ([ 0 , vm . pHeight ]);
350
326
351
- if (! vm .disableText && vm .rectSize > 0 ) {
352
- rectHeight = vm .rectSize ;
353
- }
354
- textWidth = rectWidth;
355
- textHeight = vm .pHeight - rectHeight;
327
+ let rectHeight, rectWidth, rectMargin, textWidth;
356
328
357
- vm .highlightHeight = vm .pHeight ;
358
- vm .highlightWidth = rectWidth;
359
- } else if (vm ._orientation === ORIENTATIONS .VERTICAL ) {
360
- rectHeight = vm .pHeight / numRects;
361
- rectWidth = vm .pWidth ;
329
+ rectMargin = vm .rectMargin ;
330
+
362
331
363
- if (rectHeight - rectMargin <= BAR_WIDTH_MIN ) {
364
- rectMargin = 0 ;
365
- }
332
+ rectHeight = vm .pHeight / numRects;
333
+ rectWidth = vm .pWidth ;
366
334
367
- if (! vm .disableText && vm .rectSize > 0 ) {
368
- rectWidth = vm .rectSize ;
369
- }
370
- textHeight = rectHeight;
371
- textWidth = vm .pWidth - rectWidth;
335
+ if (rectHeight - rectMargin <= BAR_WIDTH_MIN ) {
336
+ rectMargin = 0 ;
337
+ }
372
338
373
- vm .highlightHeight = rectHeight;
374
- vm . highlightWidth = vm .pWidth ;
339
+ if ( ! vm . disableText && vm .rectSize > 0 ) {
340
+ rectWidth = vm .rectSize ;
375
341
}
342
+ textWidth = vm .pWidth - rectWidth;
343
+
344
+ vm .highlightHeight = rectHeight;
345
+ vm .highlightWidth = vm .pWidth ;
376
346
377
347
// Compute offsets if text will be on one side.
378
- let rectOffsetX = 0 ;
379
- let rectOffsetY = 0 ;
380
348
let textOffsetX = 0 ;
381
- let textOffsetY = 0 ;
382
349
383
350
if (! vm .disableText && vm .rectSize > 0 ) {
384
- if (vm ._orientation === ORIENTATIONS .HORIZONTAL && vm ._textSide === SIDES .TOP ) {
385
- rectOffsetY = textHeight;
386
- } else if (vm ._orientation === ORIENTATIONS .HORIZONTAL && vm ._textSide === SIDES .BOTTOM ) {
387
- textOffsetY = rectHeight;
388
- } else if (vm ._orientation === ORIENTATIONS .VERTICAL && vm ._textSide === SIDES .LEFT ) {
389
- rectOffsetX = textWidth;
390
- } else if (vm ._orientation === ORIENTATIONS .VERTICAL && vm ._textSide === SIDES .RIGHT ) {
391
- textOffsetX = rectWidth;
392
- }
351
+ textOffsetX = rectWidth;
393
352
}
394
353
395
354
point .forEach ((pointValue , i ) => {
@@ -399,31 +358,21 @@ export default {
399
358
400
359
const textValue = this ._cScales [i].toHuman (point[i][vm .cArray [i]]);
401
360
402
- let rect, text;
403
- if (vm ._orientation === ORIENTATIONS .HORIZONTAL ) {
404
- rect = two .makeRectangle (
405
- i* rectWidth + (rectWidth/ 2 ) + (rectMargin/ 2 ), rectOffsetY + (rectHeight/ 2 ),
406
- rectWidth - rectMargin, rectHeight
407
- );
408
- contextHidden .fillRect (i* rectWidth, 0 , rectWidth, rectHeight);
409
-
410
- text = two .makeText (
411
- i* textWidth + (textWidth/ 2 ), textOffsetY + (textHeight/ 2 ),
412
- textWidth, textHeight, textValue
413
- );
414
- } else if (vm ._orientation === ORIENTATIONS .VERTICAL ) {
415
- rect = two .makeRectangle (
416
- rectOffsetX + (rectWidth/ 2 ), i* rectHeight + (rectHeight/ 2 ) + (rectMargin/ 2 ),
417
- rectWidth, rectHeight - rectMargin
418
- );
419
- contextHidden .fillRect (0 , i* rectHeight, rectWidth, rectHeight);
420
-
421
- text = two .makeText (
422
- textOffsetX + 4 , i* textHeight + (textHeight/ 2 ) + 7 ,
423
- textWidth, textHeight, textValue
424
- );
425
- text .textalign = " left" ;
426
- }
361
+ const rect = two .makeRectangle (
362
+ 0.5 + rectWidth/ 2 , y (i) + rectHeight/ 2 ,
363
+ rectWidth, rectHeight - rectMargin
364
+ );
365
+ contextHidden .fillRect (
366
+ 0.5 , y (i),
367
+ rectWidth, rectHeight
368
+ );
369
+
370
+ const text = two .makeText (
371
+ textOffsetX + 4 , y (i) + (rectHeight/ 2 ) + 7 ,
372
+ textWidth, rectHeight, textValue
373
+ );
374
+
375
+ text .textalign = " left" ;
427
376
rect .fill = cScales[i].color (point[i][vm .cArray [i]]);
428
377
rect .noStroke ();
429
378
0 commit comments