2
0

svg.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <script src="../../dist/vue.global.js"></script>
  2. <script>
  3. // math helper...
  4. function valueToPoint (value, index, total) {
  5. var x = 0
  6. var y = -value * 0.8
  7. var angle = Math.PI * 2 / total * index
  8. var cos = Math.cos(angle)
  9. var sin = Math.sin(angle)
  10. var tx = x * cos - y * sin + 100
  11. var ty = x * sin + y * cos + 100
  12. return {
  13. x: tx,
  14. y: ty
  15. }
  16. }
  17. const AxisLabel = {
  18. template: '<text :x="point.x" :y="point.y">{{stat.label}}</text>',
  19. props: {
  20. stat: Object,
  21. index: Number,
  22. total: Number
  23. },
  24. computed: {
  25. point: function () {
  26. return valueToPoint(
  27. +this.stat.value + 10,
  28. this.index,
  29. this.total
  30. )
  31. }
  32. }
  33. }
  34. </script>
  35. <!-- template for the polygraph component. -->
  36. <script type="text/x-template" id="polygraph-template">
  37. <g>
  38. <polygon :points="points"></polygon>
  39. <circle cx="100" cy="100" r="80"></circle>
  40. <axis-label
  41. v-for="(stat, index) in stats"
  42. :stat="stat"
  43. :index="index"
  44. :total="stats.length">
  45. </axis-label>
  46. </g>
  47. </script>
  48. <script>
  49. const Polygraph = {
  50. props: ['stats'],
  51. template: '#polygraph-template',
  52. computed: {
  53. // a computed property for the polygon's points
  54. points() {
  55. const total = this.stats.length
  56. return this.stats.map((stat, i) => {
  57. const point = valueToPoint(stat.value, i, total)
  58. return point.x + ',' + point.y
  59. }).join(' ')
  60. }
  61. },
  62. components: {
  63. AxisLabel
  64. }
  65. }
  66. </script>
  67. <!-- demo root element -->
  68. <div id="demo">
  69. <!-- Use the polygraph component -->
  70. <svg width="200" height="200">
  71. <polygraph :stats="stats"></polygraph>
  72. </svg>
  73. <!-- controls -->
  74. <div v-for="stat in stats">
  75. <label>{{stat.label}}</label>
  76. <input type="range" v-model="stat.value" min="0" max="100">
  77. <span>{{stat.value}}</span>
  78. <button @click="remove(stat)" class="remove">X</button>
  79. </div>
  80. <form id="add">
  81. <input name="newlabel" v-model="newLabel">
  82. <button @click="add">Add a Stat</button>
  83. </form>
  84. <pre id="raw">{{ stats }}</pre>
  85. </div>
  86. <script>
  87. const globalStats = [
  88. { label: 'A', value: 100 },
  89. { label: 'B', value: 100 },
  90. { label: 'C', value: 100 },
  91. { label: 'D', value: 100 },
  92. { label: 'E', value: 100 },
  93. { label: 'F', value: 100 }
  94. ]
  95. const App = {
  96. components: {
  97. Polygraph
  98. },
  99. data: {
  100. newLabel: '',
  101. stats: globalStats
  102. },
  103. methods: {
  104. add(e) {
  105. e.preventDefault()
  106. if (!this.newLabel) return
  107. this.stats.push({
  108. label: this.newLabel,
  109. value: 100
  110. })
  111. this.newLabel = ''
  112. },
  113. remove(stat) {
  114. if (this.stats.length > 3) {
  115. this.stats.splice(this.stats.indexOf(stat), 1)
  116. } else {
  117. alert('Can\'t delete more!')
  118. }
  119. }
  120. }
  121. }
  122. Vue.createApp().mount(App, '#demo')
  123. </script>
  124. <style>
  125. body {
  126. font-family: Helvetica Neue, Arial, sans-serif;
  127. }
  128. polygon {
  129. fill: #42b983;
  130. opacity: .75;
  131. }
  132. circle {
  133. fill: transparent;
  134. stroke: #999;
  135. }
  136. text {
  137. font-family: Helvetica Neue, Arial, sans-serif;
  138. font-size: 10px;
  139. fill: #666;
  140. }
  141. label {
  142. display: inline-block;
  143. margin-left: 10px;
  144. width: 20px;
  145. }
  146. #raw {
  147. position: absolute;
  148. top: 0;
  149. left: 300px;
  150. }
  151. </style>