MiniBar.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <div :style="{ width: width == null ? 'auto' : width + 'px' }">
  3. <v-chart :forceFit="width == null" :height="height" :data="data" padding="0">
  4. <v-tooltip />
  5. <v-bar position="x*y" />
  6. </v-chart>
  7. </div>
  8. </template>
  9. <script>
  10. import moment from 'dayjs'
  11. const sourceData = []
  12. const beginDay = new Date().getTime()
  13. for (let i = 0; i < 10; i++) {
  14. sourceData.push({
  15. x: moment(new Date(beginDay + 1000 * 60 * 60 * 24 * i)).format('YYYY-MM-DD'),
  16. y: Math.round(Math.random() * 10),
  17. })
  18. }
  19. const tooltip = [
  20. 'x*y',
  21. (x, y) => ({
  22. name: x,
  23. value: y,
  24. }),
  25. ]
  26. const scale = [
  27. {
  28. dataKey: 'x',
  29. min: 2,
  30. },
  31. {
  32. dataKey: 'y',
  33. title: '时间',
  34. min: 1,
  35. max: 30,
  36. },
  37. ]
  38. export default {
  39. name: 'MiniBar',
  40. props: {
  41. dataSource: {
  42. type: Array,
  43. default: () => [],
  44. },
  45. width: {
  46. type: Number,
  47. default: null,
  48. },
  49. height: {
  50. type: Number,
  51. default: 200,
  52. },
  53. },
  54. created() {
  55. if (this.dataSource.length === 0) {
  56. this.data = sourceData
  57. } else {
  58. this.data = this.dataSource
  59. }
  60. },
  61. data() {
  62. return {
  63. tooltip,
  64. data: [],
  65. scale,
  66. }
  67. },
  68. }
  69. </script>
  70. <style lang="less" scoped>
  71. @import 'chart';
  72. </style>