BarChart.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <div v-bind="$attrs" class="chart-wrapper full-width full-height">
  3. <q-resize-observer @resize="onResize" />
  4. <div v-if="hasData" class="chart-container">
  5. <Bar
  6. ref="chart_ref"
  7. :options="chartBarOptions"
  8. :data="chartBarData"
  9. :plugins="[ChartDataLabels]"
  10. />
  11. </div>
  12. <div v-else class="no-data-container">
  13. <span :class="textColor">{{ $t("http.errors.no_records_found") }}</span>
  14. </div>
  15. </div>
  16. </template>
  17. <script setup>
  18. import { ref, computed } from "vue";
  19. import { Bar } from "vue-chartjs";
  20. import {
  21. Chart as ChartJS,
  22. Title,
  23. Tooltip,
  24. Legend,
  25. BarElement,
  26. CategoryScale,
  27. LinearScale,
  28. } from "chart.js";
  29. import ChartDataLabels from "chartjs-plugin-datalabels";
  30. import { base64ToJPEG } from "src/helpers/convertBase64Image";
  31. import { useQuasar, colors, getCssVar } from "quasar";
  32. ChartJS.register(
  33. Title,
  34. Tooltip,
  35. Legend,
  36. BarElement,
  37. CategoryScale,
  38. LinearScale,
  39. );
  40. const $q = useQuasar();
  41. const chart_ref = ref(null);
  42. const { lighten } = colors;
  43. const props = defineProps({
  44. data: {
  45. type: Object,
  46. default: () => ({
  47. chart_data: [],
  48. current_total: 0,
  49. }),
  50. },
  51. dataSetLabel: {
  52. type: String,
  53. default: "Quantidade",
  54. },
  55. labelX: {
  56. type: String,
  57. default: "Categorias",
  58. },
  59. labelY: {
  60. type: String,
  61. default: "Valores",
  62. },
  63. showLegend: {
  64. type: Boolean,
  65. default: false,
  66. },
  67. title: {
  68. type: String,
  69. default: "Título",
  70. },
  71. backgroundColors: {
  72. type: Array,
  73. default: null,
  74. },
  75. });
  76. const onResize = () => {
  77. if (chart_ref.value?.chart) {
  78. setTimeout(() => {
  79. chart_ref.value.chart.resize();
  80. }, 50);
  81. }
  82. };
  83. const textColor = computed(() => {
  84. return $q.dark.isActive ? "text-white" : "text-black";
  85. });
  86. const labelColor = computed(() => {
  87. return $q.dark.isActive ? "#ffffff" : "#000000";
  88. });
  89. const gridColor = computed(() => {
  90. return $q.dark.isActive ? "rgba(255, 255, 255, 0.1)" : "rgba(0, 0, 0, 0.1)";
  91. });
  92. const dataLabelColor = computed(() => {
  93. return $q.dark.isActive ? "#ffffff" : "#000000";
  94. });
  95. const hasData = computed(() => {
  96. return props.data?.chart_data && props.data.chart_data.length > 0;
  97. });
  98. const chartLabels = computed(() => {
  99. return props.data?.chart_data?.map((item) => item.label) || [];
  100. });
  101. const chartValues = computed(() => {
  102. return props.data?.chart_data?.map((item) => item.value) || [];
  103. });
  104. const chartThemeColors = computed(() => {
  105. if (props.backgroundColors) {
  106. return props.backgroundColors;
  107. }
  108. const primaryColor = getCssVar("primary");
  109. if (!primaryColor) return [];
  110. const numColors = chartValues.value.length;
  111. const step = numColors > 0 ? 50 / numColors : 0;
  112. return Array.from({ length: numColors }, (_, i) =>
  113. lighten(primaryColor, i * step),
  114. );
  115. });
  116. const chartBarData = computed(() => ({
  117. labels: chartLabels.value,
  118. datasets: [
  119. {
  120. label: props.dataSetLabel,
  121. data: chartValues.value,
  122. backgroundColor: chartThemeColors.value,
  123. borderColor: chartThemeColors.value,
  124. borderWidth: 1,
  125. },
  126. ],
  127. }));
  128. const chartBarOptions = computed(() => ({
  129. responsive: true,
  130. maintainAspectRatio: false,
  131. plugins: {
  132. legend: {
  133. display: props.showLegend,
  134. position: "top",
  135. labels: {
  136. color: labelColor.value,
  137. font: {
  138. size: 14,
  139. },
  140. },
  141. },
  142. datalabels: {
  143. color: dataLabelColor.value,
  144. anchor: "end",
  145. align: "top",
  146. offset: 4,
  147. font: {
  148. size: 12,
  149. weight: "bold",
  150. },
  151. formatter: (value) => {
  152. return value > 0 ? value : "";
  153. },
  154. },
  155. tooltip: {
  156. backgroundColor: $q.dark.isActive
  157. ? "rgba(0, 0, 0, 0.8)"
  158. : "rgba(255, 255, 255, 0.9)",
  159. titleColor: labelColor.value,
  160. bodyColor: labelColor.value,
  161. borderColor: labelColor.value,
  162. borderWidth: 1,
  163. },
  164. },
  165. scales: {
  166. x: {
  167. display: true,
  168. title: {
  169. display: !!props.labelX,
  170. text: props.labelX,
  171. color: labelColor.value,
  172. font: {
  173. size: 14,
  174. },
  175. },
  176. grid: {
  177. color: gridColor.value,
  178. tickColor: gridColor.value,
  179. },
  180. ticks: {
  181. color: labelColor.value,
  182. font: {
  183. size: 12,
  184. },
  185. },
  186. },
  187. y: {
  188. display: true,
  189. title: {
  190. display: !!props.labelY,
  191. text: props.labelY,
  192. color: labelColor.value,
  193. font: {
  194. size: 14,
  195. },
  196. },
  197. suggestedMin: 0,
  198. grid: {
  199. color: gridColor.value,
  200. tickColor: gridColor.value,
  201. },
  202. ticks: {
  203. color: labelColor.value,
  204. font: {
  205. size: 12,
  206. },
  207. stepSize: 1,
  208. },
  209. },
  210. },
  211. }));
  212. const downloadImage = () => {
  213. const image = chart_ref.value.chart?.toBase64Image("image/jpeg", 1);
  214. base64ToJPEG(image, props.title || "bar-chart");
  215. };
  216. defineExpose({
  217. downloadImage,
  218. chart_ref,
  219. });
  220. </script>
  221. <style scoped>
  222. .chart-wrapper {
  223. position: relative;
  224. }
  225. .chart-container {
  226. position: absolute;
  227. top: 10px;
  228. left: 0;
  229. right: 0;
  230. bottom: 0;
  231. width: 100%;
  232. height: 100%;
  233. }
  234. .no-data-container {
  235. position: absolute;
  236. top: 0;
  237. left: 0;
  238. right: 0;
  239. bottom: 0;
  240. display: flex;
  241. align-items: center;
  242. justify-content: center;
  243. padding: 1.5rem;
  244. }
  245. </style>