histogram.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <div>
  3. <base-chart chartType="ve-bar"
  4. :chartData="chartData"
  5. :chartConfig="chartConfig"
  6. :chartSettings="chartSettings"
  7. :chartExtend="chartExtend"
  8. :loading="loading"
  9. :chartEvents="chartEvents" />
  10. </div>
  11. </template>
  12. <script>
  13. import commonChartProps from './common'
  14. import numerify from './formatters'
  15. import { sizestr } from '@/utils/utils'
  16. export default {
  17. name: 'OverviewHistogram',
  18. props: commonChartProps(),
  19. computed: {
  20. chartExtend () {
  21. /* 数据展示样式 */
  22. const commonSeries = {
  23. barWidth: '12px',
  24. barMaxWidth: '24px',
  25. barCategoryGap: '80%',
  26. label: {
  27. show: true,
  28. normal: {
  29. position: 'insideTopLeft',
  30. offset: [15, 0],
  31. distance: -15,
  32. show: true,
  33. color: '#939EAB',
  34. formatter: this.labelFormatter,
  35. rich: {
  36. left: {
  37. color: '#000000',
  38. align: 'left',
  39. },
  40. right: {
  41. color: '#4DA1FF',
  42. width: 45,
  43. verticalAlign: 'top',
  44. padding: [0, 10, 0, 10],
  45. align: 'left',
  46. },
  47. },
  48. }, // 顶部数值标签显示
  49. emphasis: { color: '#4DA1FF' },
  50. },
  51. }
  52. return {
  53. series (v) {
  54. return v ? v.map(i => ({ ...i, ...commonSeries })) : []
  55. },
  56. tooltip: {
  57. trigger: 'axis',
  58. axisPointer: {
  59. type: 'shadow',
  60. shadowStyle: { color: 'rgb(77, 161, 255)', opacity: 0.1 },
  61. },
  62. formatter: this.tooltipFormatter,
  63. },
  64. xAxis: {
  65. boundaryGap: [0, 0.01],
  66. splitLine: { show: false },
  67. axisLabel: { show: false },
  68. max: function (value) { return value.max * 1.08 }, // 设置X轴的最大刻度,避免 顶部标签被遮挡
  69. },
  70. yAxis: {
  71. nameGap: 30,
  72. splitLine: { show: false },
  73. axisLabel: {
  74. formatter: (value, index) => {
  75. return ''
  76. // if (typeof value === 'string' && value.length > 12) {
  77. // return `${value.slice(0, 6)}...${value.slice(value.length - 6)}`
  78. // }
  79. // return value
  80. },
  81. },
  82. },
  83. }
  84. },
  85. chartConfig () {
  86. const height = this.chartData.rows && this.chartData.rows.length > 0 ? this.chartData.rows.length * 45 + 100 : 200
  87. return {
  88. height: `${height}px`,
  89. width: '95%',
  90. legend: { show: this.showLegend },
  91. }
  92. },
  93. chartSettings () {
  94. const cs = {}
  95. if (this.chartData && this.chartData.columns && this.chartData.columns.length > 0) {
  96. cs.yAxisType = [this.yAxisFormat]
  97. }
  98. return Object.assign(cs, this.chartSetting)
  99. },
  100. },
  101. methods: {
  102. valueFormatter (value) {
  103. if (this.yAxisFormat === '0.00 b') {
  104. return sizestr(value, 'B', 1024)
  105. }
  106. return numerify(value, this.yAxisFormat)
  107. },
  108. labelFormatter (params) {
  109. return `{left|${params.name}}{right|${this.valueFormatter(params.value)}}`
  110. },
  111. dataFormatter (params) {
  112. return this.valueFormatter(params.value)
  113. },
  114. tooltipFormatter (params) {
  115. if (!params) {
  116. return ''
  117. }
  118. let format = ''
  119. for (const item of params) {
  120. format += item.marker + item.name + ': ' + this.valueFormatter(item.value) + '<br />'
  121. }
  122. return format
  123. },
  124. },
  125. }
  126. </script>
  127. <style lang="less" scoped>
  128. ::v-deep {
  129. .ve-bar {
  130. max-height: 1400px; // 30条数据的高度,超过30条数据滚动
  131. overflow: auto;
  132. }
  133. }
  134. </style>