index.vue 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <div class="component">
  3. <h6 class="title">{{data.title}}</h6>
  4. <div id="ringChart" ref="box"></div>
  5. </div>
  6. </template>
  7. <script>
  8. import G2 from '@antv/g2'
  9. export default {
  10. name: '',
  11. props: ['data'],
  12. data () {
  13. return {
  14. }
  15. },
  16. components: {
  17. G2,
  18. },
  19. mounted () {
  20. this.$nextTick(function () {
  21. this.init()
  22. })
  23. },
  24. methods: {
  25. init () {
  26. var _that = this
  27. var chart = new G2.Chart({
  28. container: 'ringChart',
  29. forceFit: true,
  30. width: _that.$refs.box.clientWidth,
  31. height: 400
  32. })
  33. chart.source(_that.data.list, {
  34. percent: {
  35. formatter: function formatter (val) {
  36. val = val * 100 + '%'
  37. return val
  38. }
  39. }
  40. })
  41. chart.coord('theta', {
  42. radius: 0.75,
  43. innerRadius: 0.6
  44. })
  45. chart.tooltip({
  46. showTitle: false,
  47. itemTpl: '<li><span style="background-color:{color};" class="g2-tooltip-marker"></span>{name}: {value}</li>'
  48. })
  49. var num = 0
  50. for (var n = 0; n < _that.data.list.length; n++) {
  51. num += _that.data.list[n].count
  52. }
  53. // 辅助文本
  54. chart.guide().html({
  55. position: ['50%', '50%'],
  56. html: '<div style="color:#8c8c8c;font-size: 14px;text-align: center;width: 10em;">' + _that.data.title + '<br><span style="color:#8c8c8c;font-size:20px">' + num + '</span></div>',
  57. alignX: 'middle',
  58. alignY: 'middle'
  59. })
  60. var interval = chart.intervalStack().position('percent').color('item').label('percent', {
  61. formatter: function formatter (val, item) {
  62. return item.point.item + ': ' + val
  63. }
  64. }).tooltip('item*percent', function (item, percent) {
  65. percent = percent * 100 + '%'
  66. return {
  67. name: item,
  68. value: percent
  69. }
  70. }).style({
  71. lineWidth: 1,
  72. stroke: '#fff'
  73. })
  74. chart.render()
  75. interval.setSelected(_that.data.list[0])
  76. },
  77. }
  78. }
  79. </script>
  80. <!-- Add "scoped" attribute to limit CSS to this component only -->
  81. <style lang="scss" scoped>
  82. @import "page.scss";
  83. </style>