svg.js 1.8 KB

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