1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<script>
import * as d3 from 'd3'
import { onMount } from 'svelte'
export let selected = {}
export let datapoints = []
export let x = "x";
export let y = "y";
export let svg_id;
const brush = d3.brush()
.on("start brush end", brushed);
onMount(() => {
const g = document.createElement('g');
let svg = d3.select('#' + svg_id)
svg.insert("g",":first-child")
.attr("class", "brush")
.call(brush)
.call(g => g.select(".overlay").style("cursor", "default"));
})
function brushed({selection}) {
if (selection === null) {
selected = {}
} else {
let [[x0, y0], [x1, y1]] = selection;
console.log(x0)
datapoints.forEach((d) => {
( inBrush(x0,y0,x1,y1,d) )
? selected[d.id] = 1
: selected[d.id] = 0
})
}
}
const inBrush = (brush_x0, brush_y0, brush_x1, brush_y1, datapoint) => {
if ( brush_x0 > datapoint[x] ) { return false }
if ( brush_x1 < datapoint[x] ) { return false }
if ( brush_y0 > datapoint[y] ) { return false }
if ( brush_y1 < datapoint[y] ) { return false }
return true
}
</script>
<slot />
|