-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.svelte
172 lines (143 loc) · 3.57 KB
/
Graph.svelte
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<script>
import { scaleLinear } from "d3-scale";
import * as d3 from "d3";
import InformationWidget from "./InformationWidget.svelte";
// Exports
export let data;
export let width;
export let height;
// Variables
let value = "aantal_zitplaatsen";
let label = "kenteken";
let widgetActive = false;
let widgetData = {};
const padding = { top: 20, right: 20, bottom: 30, left: 40 };
// Reactive variables
$: bars = data.map((d) => {
// Reformat the data into something more easily useable:
// [
// {
// label: "value from set label variable",
// value: "value from set value variable"
// }
// ...etc
// ]
const dValue = d[value];
// If value is undefined return 0
const format = {
label: d[label],
value: dValue == undefined ? 0 : +dValue,
};
return format;
});
$: correctedWidth = width - 60;
$: xScale = scaleLinear()
.domain([0, bars.length])
.range([padding.left, width - padding.right]);
$: yScale = scaleLinear()
.domain([0, d3.max(bars, (d) => d.value)])
.range([height - padding.bottom, padding.top]);
$: yTicks = function () {
const [startPoint, highestPoint] = yScale.domain();
const ticks = [];
for (let i = startPoint; i <= highestPoint; i++) {
ticks.push(i);
}
return ticks;
};
$: innerWidth = width - (padding.left + padding.right);
$: barWidth = innerWidth / bars.length;
// Methods
function showInformation(event) {
const index = this.getAttribute("data-index");
const thisItemsData = data[index];
const [x, y] = d3.pointer(event);
// Set data.
widgetData = { thisItemsData, x, y };
widgetActive = true;
}
</script>
<style>
.tick {
font-family: Helvetica, Arial;
font-size: 0.725em;
font-weight: 200;
}
.tick line {
stroke: #e2e2e2;
stroke-dasharray: 2;
}
.tick text {
fill: #aaa;
text-anchor: start;
}
.tick.tick-0 line {
stroke-dasharray: 0;
}
.x-axis .tick text {
text-anchor: middle;
}
.bar-data {
fill: tomato;
stroke: none;
opacity: 0.75;
transition: all 200ms;
}
.bar-container {
opacity: 0;
fill: black;
cursor: pointer;
transition: all 200ms;
}
.bar-container:hover {
opacity: 0.1;
}
</style>
<h3>Selected value: {value}</h3>
<p>Click a bar to see more information</p>
<svg
width={correctedWidth + padding.left + padding.right}
height={height + padding.top + padding.bottom}>
<!-- y axis -->
<g class="axis y-axis">
{#each yTicks() as tick}
<g class="tick tick-{tick}" transform="translate(0, {yScale(tick)})">
<line x2="100%" />
<text y="-4">{tick}</text>
</g>
{/each}
</g>
<!-- x axis -->
<g class="axis x-axis">
{#each bars as { label }, i}
<g class="tick" transform="translate({xScale(i)},{height})">
<text x={barWidth / 2} y="-4">{label}</text>
</g>
{/each}
</g>
<g class="bars">
{#each bars as { value }, i}
<!-- Bars for data -->
<rect
class="bar-data"
x={xScale(i) + 2}
y={yScale(value)}
width={barWidth - 4}
height={height - padding.bottom - yScale(value)} />
<!-- Bars for clickable -->
<rect
class="bar-container"
width={barWidth - 4}
x={xScale(i) + 2}
height={height - padding.bottom}
data-index={i}
on:click={showInformation} />
{/each}
</g>
{#if widgetActive}
<InformationWidget
data={widgetData.thisItemsData}
x={widgetData.x}
y={widgetData.y} />
{/if}
</svg>