BarMultid.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <div :style="{ padding: '0 0 32px 32px' }">
  3. <h4 :style="{ marginBottom: '20px' }">{{ title }}</h4>
  4. <v-chart :data="data" :height="height" :force-fit="true" :onClick="handleClick">
  5. <v-tooltip />
  6. <v-axis />
  7. <v-legend />
  8. <v-bar position="x*y" color="type" :adjust="adjust" />
  9. </v-chart>
  10. </div>
  11. </template>
  12. <script>
  13. import { DataSet } from '@antv/data-set'
  14. import { ChartEventMixins } from './mixins/ChartMixins'
  15. export default {
  16. name: 'BarMultid',
  17. mixins: [ChartEventMixins],
  18. props: {
  19. title: {
  20. type: String,
  21. default: '',
  22. },
  23. dataSource: {
  24. type: Array,
  25. default: () => [
  26. {
  27. type: 'Jeecg',
  28. 'Jan.': 18.9,
  29. 'Feb.': 28.8,
  30. 'Mar.': 39.3,
  31. 'Apr.': 81.4,
  32. May: 47,
  33. 'Jun.': 20.3,
  34. 'Jul.': 24,
  35. 'Aug.': 35.6,
  36. },
  37. {
  38. type: 'Jeebt',
  39. 'Jan.': 12.4,
  40. 'Feb.': 23.2,
  41. 'Mar.': 34.5,
  42. 'Apr.': 99.7,
  43. May: 52.6,
  44. 'Jun.': 35.5,
  45. 'Jul.': 37.4,
  46. 'Aug.': 42.4,
  47. },
  48. ],
  49. },
  50. fields: {
  51. type: Array,
  52. default: () => ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.'],
  53. },
  54. // 别名,需要的格式:[{field:'name',alias:'姓名'}, {field:'sex',alias:'性别'}]
  55. aliases: {
  56. type: Array,
  57. default: () => [],
  58. },
  59. height: {
  60. type: Number,
  61. default: 254,
  62. },
  63. },
  64. data() {
  65. return {
  66. adjust: [
  67. {
  68. type: 'dodge',
  69. marginRatio: 1 / 32,
  70. },
  71. ],
  72. }
  73. },
  74. computed: {
  75. data() {
  76. const dv = new DataSet.View().source(this.dataSource)
  77. dv.transform({
  78. type: 'fold',
  79. fields: this.fields,
  80. key: 'x',
  81. value: 'y',
  82. })
  83. // bar 使用不了 - 和 / 所以替换下
  84. let rows = dv.rows.map((row) => {
  85. if (typeof row.x === 'string') {
  86. row.x = row.x.replace(/[-/]/g, '_')
  87. }
  88. return row
  89. })
  90. // 替换别名
  91. rows.forEach((row) => {
  92. for (let item of this.aliases) {
  93. if (item.field === row.type) {
  94. row.type = item.alias
  95. break
  96. }
  97. }
  98. })
  99. return rows
  100. },
  101. },
  102. }
  103. </script>
  104. <style scoped></style>