svg.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 resusable polygon graph component
  11. Vue.component('polygraph', {
  12. props: ['stats'],
  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. props: {
  29. stat: Object,
  30. index: Number,
  31. total: Number
  32. },
  33. template: '#axis-label-template',
  34. replace: true,
  35. computed: {
  36. point: function () {
  37. return valueToPoint(
  38. +this.stat.value + 10,
  39. this.index,
  40. this.total
  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. // bootstrap the demo
  62. new Vue({
  63. el: '#demo',
  64. data: {
  65. newLabel: '',
  66. stats: stats
  67. },
  68. methods: {
  69. add: function (e) {
  70. e.preventDefault()
  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)
  81. } else {
  82. alert('Can\'t delete more!')
  83. }
  84. }
  85. }
  86. })