-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathChart.svelte
More file actions
102 lines (83 loc) · 1.91 KB
/
Copy pathChart.svelte
File metadata and controls
102 lines (83 loc) · 1.91 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<script context="module">
import { getContext } from 'svelte';
const key = {};
export function getChartContext() {
return getContext(key);
}
</script>
<script>
import { setContext } from 'svelte';
import { writable, derived } from 'svelte/store';
import * as yootils from 'yootils';
export let x1 = 0;
export let y1 = 0;
export let x2 = 1;
export let y2 = 1;
export let clip = false;
let chart;
const _x1 = writable();
const _y1 = writable();
const _x2 = writable();
const _y2 = writable();
const width = writable();
const height = writable();
const pointer = writable(null);
const handle_mousemove = e => {
const bcr = chart.getBoundingClientRect();
const left = e.clientX - bcr.left;
const top = e.clientY - bcr.top;
const x = $x_scale_inverse(100 * left / (bcr.right - bcr.left));
const y = $y_scale_inverse(100 * top / (bcr.bottom - bcr.top));
pointer.set({ x, y, left, top });
};
const handle_mouseleave = () => {
pointer.set(null);
};
$: _x1.set(x1);
$: _y1.set(y1);
$: _x2.set(x2);
$: _y2.set(y2);
const x_scale = derived([_x1, _x2], ([$x1, $x2]) => {
return yootils.linearScale([$x1, $x2], [0, 100]);
});
const y_scale = derived([_y1, _y2], ([$y1, $y2]) => {
return yootils.linearScale([$y1, $y2], [100, 0]);
});
const x_scale_inverse = derived(x_scale, $x_scale => $x_scale.inverse());
const y_scale_inverse = derived(y_scale, $y_scale => $y_scale.inverse());
setContext(key, {
x1: _x1,
y1: _y1,
x2: _x2,
y2: _y2,
x_scale,
y_scale,
x_scale_inverse,
y_scale_inverse,
pointer,
width,
height
});
</script>
<div
class="pancake-chart"
bind:this={chart}
bind:clientWidth={$width}
bind:clientHeight={$height}
on:mousemove={handle_mousemove}
on:mouseleave={handle_mouseleave}
class:clip
>
<slot></slot>
</div>
<style>
.pancake-chart {
position: relative;
display: block;
width: 100%;
height: 100%;
}
.clip {
overflow: hidden;
}
</style>