-
Notifications
You must be signed in to change notification settings - Fork 20
/
WidgetChartSVG.vue
135 lines (123 loc) · 2.84 KB
/
WidgetChartSVG.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
123
124
125
126
127
128
129
130
131
132
133
134
135
<template>
<div class="widget widget-chart-svg">
<h3 class="title">{{title}}</h3>
<svg
xmlns="http://www.w3.org/2000/svg"
preserveAspectRatio="none"
viewBox="0 0 300 100" >
<text
class="label"
v-for="(value, index) of values"
:key="'label' + index"
x="60"
:y="1 + labelOffsetY(index)"
text-anchor="end"
>{{labels[index]}}</text>
<line
v-for="(value, index) in values.length + 1"
:key="'line-' + index"
x1="55" :y1="labelLineOffsetY(index)"
x2="65" :y2="labelLineOffsetY(index)"
></line>
<line
x1="65" y1="0"
x2="65" y2="100"
></line>
<rect
v-for="(value, index) of values"
:key="'rect-' + index"
x="70"
:y="2 + labelLineOffsetY(index)"
:width="barWith(value, 230)"
:height="(100 / values.length) - 4"
/>
<text
class="value"
v-for="(value, index) of values"
:key="index"
x="75"
:y="1 + labelOffsetY(index)"
text-anchor="start"
>{{formatValue(values[index])}}</text>
</svg>
</div>
</template>
<script>
export default {
name: 'WidgetChartSVG',
props: {
title: {
type: String,
default: "title"
},
range: {
type: Array,
required: true
},
values: {
type: Array,
required: true
},
labels: {
type: Array,
required: true
}
},
methods: {
labelOffsetY (index) {
// chart is 100 unit height
// we divide 100 by the number of items and multiply by index to get offset
const offset = Math.round(100 / this.values.length)
return offset / 2 + offset * index
},
labelLineOffsetY (index) {
// chart is 100 unit height
// we divide 100 by the number of items and multiply by index to get offset
const offset = Math.round(100 / this.values.length)
return offset * index
},
formatValue (value) {
return value.toLocaleString('en')
},
barWith (value, fullWidth) {
const ratio = (value - this.range[0]) / (this.range[1] - this.range[0])
return ratio * fullWidth
}
}
}
</script>
<style lang="scss">
@import "./../../styles/variables.scss";
div.widget.widget-chart-svg {
display: block;
svg {
margin: 2PX 5px 5px;
width: calc(100% - 10px);
height: auto;
font-size: 0.5rem;
overflow: hidden;
text {
dominant-baseline: middle;
fill: $accent-color;
&.value {
fill: white;
}
}
line {
stroke: rgba($accent-color, 0.5);
stroke-width: 1;
}
rect {
stroke-width: 0;
fill: $primary-color;
}
}
.title {
color: $accent-color;
margin: 2px 5px 0;
text-align: left;
font-variant: small-caps;
font-size: 1rem;
}
}
</style>