svg.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // math helper...
  67. function valueToPoint (value, index, total) {
  68. var x = 0
  69. var y = -value * 0.8
  70. var angle = Math.PI * 2 / total * index
  71. var cos = Math.cos(angle)
  72. var sin = Math.sin(angle)
  73. var tx = x * cos - y * sin + 100
  74. var ty = x * sin + y * cos + 100
  75. return {
  76. x: tx,
  77. y: ty
  78. }
  79. }
  80. </script>
  81. <!-- demo root element -->
  82. <div id="demo">
  83. <!-- Use the polygraph component -->
  84. <svg width="200" height="200">
  85. <polygraph :stats="stats"></polygraph>
  86. </svg>
  87. <!-- controls -->
  88. <div v-for="stat in stats">
  89. <label>{{stat.label}}</label>
  90. <input type="range" v-model="stat.value" min="0" max="100">
  91. <span>{{stat.value}}</span>
  92. <button @click="remove(stat)" class="remove">X</button>
  93. </div>
  94. <form id="add">
  95. <input name="newlabel" v-model="newLabel">
  96. <button @click="add">Add a Stat</button>
  97. </form>
  98. <pre id="raw">{{ stats }}</pre>
  99. </div>
  100. <script>
  101. const App = {
  102. components: {
  103. Polygraph
  104. },
  105. data: {
  106. newLabel: '',
  107. stats: [
  108. { label: 'A', value: 100 },
  109. { label: 'B', value: 100 },
  110. { label: 'C', value: 100 },
  111. { label: 'D', value: 100 },
  112. { label: 'E', value: 100 },
  113. { label: 'F', value: 100 }
  114. ]
  115. },
  116. methods: {
  117. add(e) {
  118. e.preventDefault()
  119. if (!this.newLabel) return
  120. this.stats.push({
  121. label: this.newLabel,
  122. value: 100
  123. })
  124. this.newLabel = ''
  125. },
  126. remove(stat) {
  127. if (this.stats.length > 3) {
  128. this.stats.splice(this.stats.indexOf(stat), 1)
  129. } else {
  130. alert('Can\'t delete more!')
  131. }
  132. }
  133. }
  134. }
  135. Vue.createApp().mount(App, '#demo')
  136. </script>
  137. <style>
  138. body {
  139. font-family: Helvetica Neue, Arial, sans-serif;
  140. }
  141. polygon {
  142. fill: #42b983;
  143. opacity: .75;
  144. }
  145. circle {
  146. fill: transparent;
  147. stroke: #999;
  148. }
  149. text {
  150. font-family: Helvetica Neue, Arial, sans-serif;
  151. font-size: 10px;
  152. fill: #666;
  153. }
  154. label {
  155. display: inline-block;
  156. margin-left: 10px;
  157. width: 20px;
  158. }
  159. #raw {
  160. position: absolute;
  161. top: 0;
  162. left: 300px;
  163. }
  164. </style>