svg.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. template: '#axis-label-template',
  29. replace: true,
  30. computed: {
  31. point: function () {
  32. return valueToPoint(
  33. +this.value + 10,
  34. this.$index,
  35. this.$parent.stats.length
  36. )
  37. }
  38. }
  39. }
  40. }
  41. })
  42. // math helper...
  43. function valueToPoint (value, index, total) {
  44. var x = 0
  45. var y = -value * 0.8
  46. var angle = Math.PI * 2 / total * index
  47. var cos = Math.cos(angle)
  48. var sin = Math.sin(angle)
  49. var tx = x * cos - y * sin + 100
  50. var ty = x * sin + y * cos + 100
  51. return {
  52. x: tx,
  53. y: ty
  54. }
  55. }
  56. // bootstrap the demo
  57. new Vue({
  58. el: '#demo',
  59. data: {
  60. newLabel: '',
  61. stats: stats
  62. },
  63. methods: {
  64. add: function (e) {
  65. e.preventDefault()
  66. if (!this.newLabel) return
  67. this.stats.push({
  68. label: this.newLabel,
  69. value: 100
  70. })
  71. this.newLabel = ''
  72. },
  73. remove: function (stat) {
  74. if (this.stats.length > 3) {
  75. this.stats.$remove(stat.$data)
  76. } else {
  77. alert('Can\'t delete more!')
  78. }
  79. }
  80. }
  81. })