MiniBarChart.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <Bar
  3. :id="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. import { getCssVar } from "quasar";
  20. ChartJS.register(CategoryScale, LinearScale, BarElement, Tooltip);
  21. const chart_ref = useTemplateRef(null);
  22. const { data, barColor, horizontal, showTooltip } = defineProps({
  23. data: {
  24. type: Array,
  25. required: true,
  26. },
  27. barColor: {
  28. type: String,
  29. default: () => getCssVar("primary"),
  30. },
  31. horizontal: {
  32. type: Boolean,
  33. default: false,
  34. },
  35. showTooltip: {
  36. type: Boolean,
  37. default: true,
  38. },
  39. });
  40. const chartOptions = computed(() => ({
  41. responsive: true,
  42. maintainAspectRatio: false,
  43. indexAxis: horizontal ? "y" : "x",
  44. plugins: {
  45. legend: {
  46. display: false,
  47. },
  48. tooltip: {
  49. enabled: showTooltip,
  50. displayColors: false,
  51. callbacks: {
  52. label: (context) => `${context.raw}`,
  53. },
  54. },
  55. },
  56. scales: {
  57. x: {
  58. display: false,
  59. grid: {
  60. display: false,
  61. },
  62. },
  63. y: {
  64. display: false,
  65. grid: {
  66. display: false,
  67. },
  68. beginAtZero: true,
  69. },
  70. },
  71. animation: {
  72. duration: 750,
  73. easing: "easeOutQuad",
  74. },
  75. }));
  76. const computedChartData = computed(() => ({
  77. labels: Array(data.length).fill(""),
  78. datasets: [
  79. {
  80. data: data,
  81. backgroundColor: barColor,
  82. borderRadius: 2,
  83. barThickness: 8,
  84. maxBarThickness: 10,
  85. },
  86. ],
  87. }));
  88. defineExpose({
  89. chart_ref,
  90. });
  91. </script>