MiniBarChart.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <Bar
  3. :id="props.id"
  4. ref="chart_ref"
  5. :options="chartOptions"
  6. :data="computedChartData"
  7. />
  8. </template>
  9. <script setup>
  10. import {
  11. Chart as ChartJS,
  12. CategoryScale,
  13. LinearScale,
  14. BarElement,
  15. Tooltip,
  16. } from "chart.js";
  17. import { computed, useTemplateRef } from "vue";
  18. import { Bar } from "vue-chartjs";
  19. // Register only necessary components
  20. ChartJS.register(
  21. CategoryScale,
  22. LinearScale,
  23. BarElement,
  24. Tooltip,
  25. );
  26. const chart_ref = useTemplateRef(null);
  27. // Simplified props focusing on essential functionality
  28. const props = defineProps({
  29. // Core data props
  30. data: {
  31. type: Array,
  32. required: true,
  33. },
  34. // Essential styling
  35. barColor: {
  36. type: String,
  37. default: "#1976D2",
  38. },
  39. // Optional configurations for flexibility
  40. horizontal: {
  41. type: Boolean,
  42. default: false,
  43. },
  44. showTooltip: {
  45. type: Boolean,
  46. default: true,
  47. }
  48. });
  49. // Optimized chart options for mini-charts
  50. const chartOptions = computed(() => ({
  51. responsive: true,
  52. maintainAspectRatio: false,
  53. indexAxis: props.horizontal ? "y" : "x",
  54. plugins: {
  55. legend: {
  56. display: false, // Always hide legend in mini charts
  57. },
  58. tooltip: {
  59. enabled: props.showTooltip,
  60. displayColors: false,
  61. callbacks: {
  62. label: (context) => `${context.raw}`
  63. }
  64. },
  65. },
  66. scales: {
  67. x: {
  68. display: false,
  69. grid: {
  70. display: false,
  71. }
  72. },
  73. y: {
  74. display: false,
  75. grid: {
  76. display: false,
  77. },
  78. beginAtZero: true,
  79. },
  80. },
  81. animation: {
  82. duration: 750,
  83. easing: 'easeOutQuad',
  84. },
  85. }));
  86. // Simplified data computation
  87. const computedChartData = computed(() => ({
  88. labels: Array(props.data.length).fill(''),
  89. datasets: [{
  90. data: props.data,
  91. backgroundColor: props.barColor,
  92. borderRadius: 2,
  93. barThickness: 8,
  94. maxBarThickness: 10,
  95. }]
  96. }));
  97. // Expose essential methods
  98. defineExpose({
  99. chart_ref,
  100. });
  101. </script>