svg.js 1.7 KB

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