Chart.vue 910 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <template>
  2. <div :id="id"></div>
  3. </template>
  4. <script>
  5. import { Chart } from '@antv/g2';
  6. export default {
  7. name: 'custom-chart',
  8. props: {
  9. value: {
  10. type: Array,
  11. default: () => ([])
  12. },
  13. height: {
  14. type: Number,
  15. default: 300
  16. }
  17. },
  18. data() {
  19. return {
  20. dataModel: this.value,
  21. id: 'chart-' + Math.random().toString(36).slice(-8)
  22. }
  23. },
  24. mounted () {
  25. const chart = new Chart({
  26. container: this.id,
  27. autoFit: true,
  28. height: this.height,
  29. });
  30. chart.data(this.dataModel);
  31. chart.scale('sales', {
  32. nice: true,
  33. });
  34. chart.tooltip({
  35. showMarkers: false
  36. });
  37. chart.interaction('active-region');
  38. chart.interval().position('year*sales');
  39. chart.render();
  40. },
  41. watch: {
  42. value (val) {
  43. this.dataModel = val
  44. },
  45. dataModel (val) {
  46. this.$emit('input', val)
  47. }
  48. }
  49. }
  50. </script>