c0d1b6bff452b19dd638f262c707317be05f308e.svn-base 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <div :style="{ padding: '0 0 32px 32px' }">
  3. <h4 :style="{ marginBottom: '20px' }">{{ title }}</h4>
  4. <v-chart :forceFit="true" :height="height" :data="data" :onClick="handleClick">
  5. <v-coord type="rect" direction="LB" />
  6. <v-tooltip />
  7. <v-legend />
  8. <v-axis dataKey="y" position="right" />
  9. <v-axis :dataKey="xField" :label="label" />
  10. <v-bar :position="position" :color="this.yField?fields[0]:'x'" :adjust="adjust" />
  11. </v-chart>
  12. </div>
  13. </template>
  14. <script>
  15. import { DataSet } from '@antv/data-set'
  16. import { ChartEventMixins } from './mixins/ChartMixins'
  17. export default {
  18. name: 'TransverseBarMuiltid',
  19. mixins: [ChartEventMixins],
  20. props: {
  21. title: {
  22. type: String,
  23. default: ''
  24. },
  25. dataSource: {
  26. type: Array,
  27. default: () => [
  28. { label: 'Mon.', series1: 2800, series2: 2260 },
  29. { label: 'Tues.', series1: 1800, series2: 1300 },
  30. { label: 'Wed.', series1: 950, series2: 900 },
  31. { label: 'Thur.', series1: 500, series2: 390 },
  32. { label: 'Fri.', series1: 170, series2: 100 }
  33. ]
  34. },
  35. fields: {
  36. type: Array,
  37. default: () => ['Mon.', 'Tues.', 'Wed.', 'Thur.', 'Fri.']
  38. },
  39. // 别名,需要的格式:[{field:'name',alias:'姓名'}, {field:'sex',alias:'性别'}]
  40. aliases: {
  41. type: Array,
  42. default: () => []
  43. },
  44. height: {
  45. type: Number,
  46. default: 254
  47. },
  48. xField:{
  49. type: String,
  50. default: 'type'
  51. },
  52. yField:{
  53. type: String,
  54. default: ''
  55. }
  56. },
  57. data() {
  58. return {
  59. label: { offset: 12 },
  60. adjust: [{ type: 'dodge', marginRatio: 1 / 32 }],
  61. position: this.xField+'*'+(this.yField?this.yField:'y')
  62. }
  63. },
  64. computed: {
  65. data() {
  66. if(this.yField){
  67. return this.dataSource
  68. }
  69. const dv = new DataSet.View().source(this.dataSource)
  70. dv.transform({
  71. type: 'fold',
  72. fields: this.fields,
  73. key: 'x',
  74. value: 'y'
  75. })
  76. // bar 使用不了 - 和 / 所以替换下
  77. const rows = dv.rows.map(row => {
  78. if (typeof row.x === 'string') {
  79. row.x = row.x.replace(/[-/]/g, '_')
  80. }
  81. return row
  82. })
  83. // 替换别名
  84. rows.forEach(row => {
  85. for (const item of this.aliases) {
  86. if (item.field === row.x) {
  87. row.x = item.alias
  88. break
  89. }
  90. }
  91. })
  92. return rows
  93. }
  94. }
  95. }
  96. </script>