Skip to content

Commit baffce4

Browse files
committed
add svg example
1 parent 74675a1 commit baffce4

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

examples/svg/index.html

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Vue.js SVG example</title>
6+
<link rel="stylesheet" href="style.css">
7+
<script src="../../dist/vue.js"></script>
8+
</head>
9+
<body>
10+
11+
<!-- template for the polygraph component. -->
12+
<script type="text/x-template" id="polygraph-template">
13+
<svg width="200" height="200">
14+
<polygon v-attr="points: points"></polygon>
15+
<circle cx="100" cy="100" r="80"></circle>
16+
<text v-component="axis-label" v-repeat="stats" v-attr="x:x, y:y">
17+
{{label}}
18+
</text>
19+
</svg>
20+
</script>
21+
22+
<!-- demo root element -->
23+
<div id="demo">
24+
<!-- Use the component -->
25+
<svg v-component="polygraph" v-with="stats:stats"></svg>
26+
<!-- controls -->
27+
<div v-repeat="stats">
28+
<label>{{label}}</label>
29+
<input type="range" v-model="value" min="0" max="100">
30+
<span>{{value}}</span>
31+
<button v-on="click:remove(this)">X</button>
32+
</div>
33+
<input v-model="newLabel"> <button v-on="click:add">Add a Stat</button>
34+
<pre id="raw">{{stats | json}}</pre>
35+
</div>
36+
37+
<p style="font-size:12px">* input[type="range"] requires IE10 or above.</p>
38+
39+
<script src="svg.js"></script>
40+
41+
</body>
42+
</html>

examples/svg/style.css

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
body {
2+
font-family: Helvetica Neue, Arial, sans-serif;
3+
}
4+
5+
polygon {
6+
fill: #42b983;
7+
opacity: .75;
8+
}
9+
10+
circle {
11+
fill: transparent;
12+
stroke: #999;
13+
}
14+
15+
text {
16+
font-family: Helvetica Neue, Arial, sans-serif;
17+
font-size: 10px;
18+
fill: #666;
19+
}
20+
21+
label {
22+
display: inline-block;
23+
margin-left: 10px;
24+
width: 20px;
25+
}
26+
27+
#raw {
28+
position: absolute;
29+
top: 0;
30+
left: 300px;
31+
}

examples/svg/svg.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// The raw data to observe
2+
var stats = [
3+
{ label: 'A', value: 100 },
4+
{ label: 'B', value: 100 },
5+
{ label: 'C', value: 100 },
6+
{ label: 'D', value: 100 },
7+
{ label: 'E', value: 100 },
8+
{ label: 'F', value: 100 }
9+
]
10+
11+
// A resusable polygon graph component
12+
Vue.component('polygraph', {
13+
template: '#polygraph-template',
14+
replace: true,
15+
computed: {
16+
// a computed property for the polygon's points
17+
points: function () {
18+
var total = this.stats.length
19+
return this.stats.map(function (stat, i) {
20+
var point = valueToPoint(stat.value, i, total)
21+
return point.x + ',' + point.y
22+
}).join(' ')
23+
}
24+
},
25+
components: {
26+
// a sub component for the labels
27+
'axis-label': {
28+
computed: {
29+
point: function () {
30+
return valueToPoint(
31+
+this.value + 10,
32+
this.$index,
33+
this.$parent.stats.length
34+
)
35+
},
36+
x: function () {
37+
return this.point.x - 4
38+
},
39+
y: function () {
40+
return this.point.y + 4
41+
}
42+
}
43+
}
44+
}
45+
})
46+
47+
// math helper...
48+
function valueToPoint (value, index, total) {
49+
var x = 0
50+
var y = -value * 0.8
51+
var angle = Math.PI * 2 / total * index
52+
var cos = Math.cos(angle)
53+
var sin = Math.sin(angle)
54+
var tx = x * cos - y * sin + 100
55+
var ty = x * sin + y * cos + 100
56+
return {
57+
x: tx,
58+
y: ty
59+
}
60+
}
61+
62+
// bootstrap the demo
63+
new Vue({
64+
el: '#demo',
65+
data: {
66+
newLabel: '',
67+
stats: stats
68+
},
69+
methods: {
70+
add: function () {
71+
if (!this.newLabel) return
72+
this.stats.push({
73+
label: this.newLabel,
74+
value: 100
75+
})
76+
this.newLabel = ''
77+
},
78+
remove: function (stat) {
79+
if (this.stats.length > 3) {
80+
this.stats.$remove(stat.$data)
81+
}
82+
}
83+
}
84+
})

0 commit comments

Comments
 (0)