2
0

svg.html 3.4 KB

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