-
Notifications
You must be signed in to change notification settings - Fork 4
/
Datasaurus.vue
122 lines (109 loc) · 3.37 KB
/
Datasaurus.vue
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
<script setup lang="ts">
/**
* This component demonstrates basic usage of the composition API and typescript with Pinia store.
* The data from DatasaurusDozen is plotted on the intuitive v-for SVG rendering.
* SVG rendering is suitable for simple static visualizations where complex interactions and animations are unnecessary (e.g., legend).
* For simple animations, visit https://vuejs.org/guide/built-ins/transition-group.html.
*/
import { ref, onMounted } from 'vue';
import { useConfig } from '../stores/vaConfig';
import { useDatasaurusStore } from '../stores/datasaurus';
import { storeToRefs } from 'pinia';
import * as d3 from 'd3';
import DataSaurusLegend from './DatasaurusLegend.vue';
const vaConfig = useConfig(); // define colors
const datasaurusStore = useDatasaurusStore(); // define data source
const { nameList, data } = storeToRefs(datasaurusStore); // obtain reactive variables
/**
* To place the data points around center, we need the center's coordinate.
* Since the svg's width and height are automatically calculated by CSS flex,
* we can get them dynamically after the Datasaurus component is mounted to DOM.
* If you know other workarounds, please let me know.
*/
// create reactive variables to reflect SVG element's size
const height = ref<number>(0);
const width = ref<number>(0);
// scale the original datapoints horizontally and vertically
const h_ratio: number = 4;
const v_ratio: number = 3;
const selectedColor = ref<string>(vaConfig.color.type_one);
onMounted(() => {
// initialize svg
// optain the bounding box's attributes dynamically
const svg: d3.Selection<SVGSVGElement, {}, HTMLElement, {}> =
d3.select('#datasaurus-svg');
const svgNode = svg.node() as SVGSVGElement;
const rect: DOMRect = svgNode.getBoundingClientRect(); // optain the bounding box's attributes dynamically
height.value = rect.height / 2;
width.value = rect.width / 2;
});
</script>
<template>
<div class="datasaurus">
<div class="button-group">
<a-button
class="button"
v-for="name in nameList"
:key="name"
:type="name === datasaurusStore.selectedDataset ? 'primary' : ''"
@click="datasaurusStore.selectDataType(name)"
>
{{ name }}
</a-button>
</div>
<svg id="datasaurus-svg">
<!-- datapoint refers to the "class" in CSS, and g refers to the tag in DOM -->
<TransitionGroup name="datapoint" tag="g">
<circle
v-for="(d, i) in data"
:key="i"
:r="5"
:cx="width + d.x * h_ratio"
:cy="height - d.y * v_ratio"
:fill="selectedColor"
/>
</TransitionGroup>
<!-- legend graphic element -->
<DataSaurusLegend
:height="height"
:width="width"
@select-color="(color) => (selectedColor = color)"
/>
</svg>
</div>
</template>
<style scoped>
.datasaurus {
height: calc(100% - 2px);
width: 100%;
display: flex;
flex-direction: column;
border: 1px solid black;
}
.button-group {
padding: 5px;
display: flex;
flex: 0 1 auto;
flex-flow: row wrap;
}
.button {
flex: auto;
}
#datasaurus-svg {
flex: 1 0 auto;
/* border: 1px solid black; */
}
.datapoint-move,
.datapoint-enter-active,
.datapoint-leave-active {
transition: all 0.5s ease;
}
.datapoint-enter-from,
.datapoint-leave-to {
opacity: 0;
transform: translateX(30px);
}
.noselect {
user-select: none;
}
</style>