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. template: '#polygraph-template',
  13. replace: true,
  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. computed: {
  28. point: function () {
  29. return valueToPoint(
  30. +this.value + 10,
  31. this.$index,
  32. this.$parent.stats.length
  33. )
  34. },
  35. x: function () {
  36. return this.point.x - 4
  37. },
  38. y: function () {
  39. return this.point.y + 4
  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 () {
  68. if (!this.newLabel) return
  69. this.stats.push({
  70. label: this.newLabel,
  71. value: 100
  72. })
  73. this.newLabel = ''
  74. },
  75. remove: function (stat) {
  76. if (this.stats.length > 3) {
  77. this.stats.$remove(stat.$data)
  78. }
  79. }
  80. }
  81. })