svg.js 1.7 KB

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