Skip to content

Commit be95135

Browse files
committed
More two.js adoption
1 parent af07d94 commit be95135

File tree

5 files changed

+762
-461
lines changed

5 files changed

+762
-461
lines changed

examples-src/App.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
:pMarginLeft="120"
8080
:pMarginRight="10"
8181
:pMarginBottom="150"
82+
:showDownloadButton="true"
8283
>
8384
<Axis
8485
slot="axisLeft"
@@ -705,6 +706,7 @@
705706
:pMarginLeft="120"
706707
:pMarginRight="10"
707708
:pMarginBottom="180"
709+
:showDownloadButton="true"
708710
>
709711
<Axis
710712
slot="axisLeft"

src/components/plots/ScatterPlot.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export default {
301301
/*
302302
* Scale up the canvas
303303
*/
304-
var canvas;
304+
let canvas;
305305
if(d3Node) {
306306
canvas = d3Node;
307307
} else {

src/components/plots/StackedBarPlot.vue

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
</template>
5959

6060
<script>
61+
import Two from 'two.js';
6162
import { scaleBand as d3_scaleBand, scaleLinear as d3_scaleLinear } from 'd3-scale';
6263
import { select as d3_select } from 'd3-selection';
6364
import { stack as d3_stack, stackOrderNone as d3_stackOrderNone, stackOffsetNone as d3_stackOffsetNone } from 'd3-shape';
@@ -221,7 +222,7 @@ export default {
221222
this.highlightX1 = null;
222223
this.highlightX2 = null;
223224
},
224-
drawPlot() {
225+
drawPlot(d3Node) {
225226
const vm = this;
226227
227228
if(vm._dataContainer.isLoading || vm._xScale.isLoading || vm._yScale.isLoading || vm._cScale.isLoading) {
@@ -277,21 +278,29 @@ export default {
277278
/*
278279
* Scale up the canvas
279280
*/
280-
const canvas = d3_select(this.plotSelector);
281-
const context = canvas.node().getContext('2d');
281+
282+
let canvas;
283+
if(d3Node) {
284+
canvas = d3Node;
285+
} else {
286+
canvas = d3_select(this.plotSelector);
287+
}
288+
289+
const canvasNode = canvas.node();
290+
291+
const two = new Two({
292+
width: vm.pWidth,
293+
height: vm.pHeight,
294+
domElement: canvasNode
295+
});
282296
283297
const canvasHidden = d3_select(this.hiddenPlotSelector);
284298
const contextHidden = canvasHidden.node().getContext('2d');
285299
286-
const ratio = getRetinaRatio(context);
300+
const ratio = getRetinaRatio(contextHidden);
287301
const scaledWidth = vm.pWidth * ratio;
288302
const scaledHeight = vm.pHeight * ratio;
289303
290-
canvas
291-
.attr("width", scaledWidth)
292-
.attr("height", scaledHeight);
293-
context.scale(ratio, ratio);
294-
295304
canvasHidden
296305
.attr("width", scaledWidth)
297306
.attr("height", scaledHeight);
@@ -329,8 +338,9 @@ export default {
329338
barMarginX = 0;
330339
}
331340
341+
const twoRectArray = [];
342+
332343
series.forEach((layer) => {
333-
context.fillStyle = cScale.color(layer["key"]);
334344
layer.forEach((d) => {
335345
const col = genColor();
336346
colToNode[col] = { "x": d.data[vm.x], "y": d.data[layer["key"]], "c": layer["key"] };
@@ -339,15 +349,31 @@ export default {
339349
if(height + y(d[1]) > vm.pHeight) {
340350
height = vm.pHeight - y(d[1]);
341351
}
342-
context.fillRect(x(d.data[vm.x]) + (barMarginX/2), y(d[1]), barWidth - barMarginX, height);
352+
353+
const rect = two.makeRectangle(x(d.data[vm.x]) + (barMarginX/2) + ((barWidth - barMarginX)/2), y(d[1]) + (height/2), barWidth - barMarginX, height);
354+
//console.log(x(d.data[vm.x]) + (barMarginX/2), y(d[1]), barWidth - barMarginX, height);
355+
rect.fill = cScale.color(layer["key"]);
356+
twoRectArray.push(rect);
357+
343358
contextHidden.fillRect(x(d.data[vm.x]), y(d[1]), barWidth, height);
344359
})
345360
});
361+
362+
const twoRectGroup = two.makeGroup(twoRectArray);
363+
twoRectGroup.linewidth = 0;
364+
twoRectGroup.opacity = 1;
365+
twoRectGroup.noStroke();
366+
367+
two.update();
368+
369+
if(!canvas) {
370+
/* Ignore interactivity if no canvas. In this case an SVG was probably passed in */
371+
return;
372+
}
346373
347374
/*
348375
* Listen for mouse events
349376
*/
350-
const canvasNode = canvas.node();
351377
352378
const getDataFromMouse = (mouseX, mouseY) => {
353379
// Get the corresponding pixel color on the hidden canvas

src/components/plots/StratifiedSinaPlot.vue

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,15 @@
8989
</template>
9090

9191
<script>
92+
import Two from 'two.js';
9293
import { scaleLinear as d3_scaleLinear, scaleQuantile as d3_scaleQuantile, scaleBand as d3_scaleBand } from 'd3-scale';
9394
import { select as d3_select } from 'd3-selection';
9495
import { mouse as d3_mouse, event as d3_event } from 'd3';
9596
import debounce from 'lodash/debounce';
9697
import { min as d3_min, max as d3_max, mean as d3_mean, histogram as d3_histogram } from 'd3-array';
9798
9899
import { TOOLTIP_DEBOUNCE } from './../../constants.js';
99-
import { getRetinaRatio, seededRandom, getDelaunay } from './../../helpers.js';
100+
import { seededRandom, getDelaunay } from './../../helpers.js';
100101
101102
102103
import AbstractScale from './../../scales/AbstractScale.js';
@@ -317,7 +318,7 @@ export default {
317318
this.highlightX2 = null;
318319
this.highlightY1 = null;
319320
},
320-
drawPlot() {
321+
drawPlot(d3Node) {
321322
const vm = this;
322323
323324
if(vm._dataContainer.isLoading || vm._stratificationDataContainer.isLoading || vm._xScale.isLoading || vm._yScale.isLoading || vm._oScale.isLoading) {
@@ -352,17 +353,20 @@ export default {
352353
/*
353354
* Scale up the canvas
354355
*/
355-
const canvas = d3_select(this.plotSelector);
356-
const context = canvas.node().getContext('2d');
356+
let canvas;
357+
if(d3Node) {
358+
canvas = d3Node;
359+
} else {
360+
canvas = d3_select(this.plotSelector);
361+
}
357362
358-
const ratio = getRetinaRatio(context);
359-
const scaledWidth = vm.pWidth * ratio;
360-
const scaledHeight = vm.pHeight * ratio;
363+
const canvasNode = canvas.node();
361364
362-
canvas
363-
.attr("width", scaledWidth)
364-
.attr("height", scaledHeight);
365-
context.scale(ratio, ratio);
365+
const two = new Two({
366+
width: vm.pWidth,
367+
height: vm.pHeight,
368+
domElement: canvasNode
369+
});
366370
367371
/*
368372
* Get the random number generator.
@@ -379,18 +383,12 @@ export default {
379383
* Draw the boxes
380384
*/
381385
386+
const twoCircleArray = [];
387+
382388
const boxWidth = (barWidth / 2);
383389
const boxMargin = barWidth / 4;
384390
385-
if(vm.strokeColor !== undefined) {
386-
context.strokeStyle = vm.strokeColor;
387-
}
388391
xScale.domainFiltered.forEach((boxVar) => {
389-
if(vm.strokeColor === undefined) {
390-
context.strokeStyle = xScale.color(boxVar);
391-
}
392-
context.fillStyle = xScale.color(boxVar);
393-
394392
let boxData = data.filter((dEl) => {
395393
let sEl = stratificationData.find((sEl) => sEl[vm.o] === dEl[vm.o]);
396394
return (sEl !== undefined && sEl[vm.x] === boxVar);
@@ -433,14 +431,14 @@ export default {
433431
434432
bins.forEach((binData) => {
435433
binData.forEach((d) => {
436-
context.beginPath();
437-
let xVal = innerX(-binData.length)+random()*2*(innerX(binData.length)-innerXZero);
438-
let yVal = y(d[vm.variable]);
439-
context.arc(xVal, yVal, vm.pointSize, 0, 2*Math.PI);
440-
context.stroke();
441-
if(vm.fillPoints) {
442-
context.fill();
443-
}
434+
const xVal = innerX(-binData.length)+random()*2*(innerX(binData.length)-innerXZero);
435+
const yVal = y(d[vm.variable]);
436+
437+
const circle = two.makeCircle(xVal, yVal, vm.pointSize);
438+
circle.stroke = (vm.strokeColor ? vm.strokeColor : xScale.color(boxVar));
439+
circle.fill = xScale.color(boxVar);
440+
441+
twoCircleArray.push(circle);
444442
445443
points.push([xVal, yVal]); // For Delaunay
446444
pointsData.push({
@@ -451,14 +449,27 @@ export default {
451449
});
452450
});
453451
});
452+
453+
const twoCircleGroup = two.makeGroup(twoCircleArray);
454+
twoCircleGroup.linewidth = 1;
455+
twoCircleGroup.opacity = 1;
456+
457+
if(!vm.fillPoints) {
458+
twoCircleGroup.noFill();
459+
}
460+
461+
two.update();
462+
463+
if(!canvas) {
464+
/* Ignore interactivity if no canvas. In this case an SVG was probably passed in */
465+
return;
466+
}
454467
455468
const delaunay = getDelaunay(points, false);
456469
457470
/*
458471
* Listen for mouse events
459472
*/
460-
const canvasNode = canvas.node();
461-
462473
const getDataFromMouse = (mouseX, mouseY) => {
463474
const i = delaunay.find(mouseX, mouseY);
464475
return pointsData[i];

0 commit comments

Comments
 (0)