2
0

svg.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. // A reusable polygon graph component
  11. Vue.component('polygraph', {
  12. props: ['stats'],
  13. template: '#polygraph-template',
  14. computed: {
  15. // a computed property for the polygon's points
  16. points: function () {
  17. var total = this.stats.length
  18. return this.stats.map(function (stat, i) {
  19. var point = valueToPoint(stat.value, i, total)
  20. return point.x + ',' + point.y
  21. }).join(' ')
  22. }
  23. },
  24. components: {
  25. // a sub component for the labels
  26. 'axis-label': {
  27. props: {
  28. stat: Object,
  29. index: Number,
  30. total: Number
  31. },
  32. template: '#axis-label-template',
  33. computed: {
  34. point: function () {
  35. return valueToPoint(
  36. +this.stat.value + 10,
  37. this.index,
  38. this.total
  39. )
  40. }
  41. }
  42. }
  43. }
  44. })
  45. // math helper...
  46. function valueToPoint (value, index, total) {
  47. var x = 0
  48. var y = -value * 0.8
  49. var angle = Math.PI * 2 / total * index
  50. var cos = Math.cos(angle)
  51. var sin = Math.sin(angle)
  52. var tx = x * cos - y * sin + 100
  53. var ty = x * sin + y * cos + 100
  54. return {
  55. x: tx,
  56. y: ty
  57. }
  58. }
  59. // bootstrap the demo
  60. new Vue({
  61. el: '#demo',
  62. data: {
  63. newLabel: '',
  64. stats: stats
  65. },
  66. methods: {
  67. add: function (e) {
  68. e.preventDefault()
  69. if (!this.newLabel) return
  70. this.stats.push({
  71. label: this.newLabel,
  72. value: 100
  73. })
  74. this.newLabel = ''
  75. },
  76. remove: function (stat) {
  77. if (this.stats.length > 3) {
  78. this.stats.splice(this.stats.indexOf(stat), 1)
  79. } else {
  80. alert('Can\'t delete more!')
  81. }
  82. }
  83. }
  84. })