e30cb06874a70756d3dfb8f694ec0e476f7bd372.svn-base 2.5 KB

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