PieChart.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. <Pie
  6. ref="chart_ref"
  7. :options="chartPieOptions"
  8. :data="chartPieData"
  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 { Chart as ChartJS, ArcElement, Tooltip, Legend } from "chart.js";
  20. import { Pie } from "vue-chartjs";
  21. import ChartDataLabels from "chartjs-plugin-datalabels";
  22. import { base64ToJPEG } from "src/helpers/convertBase64Image";
  23. import { useQuasar, getCssVar, colors } from "quasar";
  24. ChartJS.register(ArcElement, Tooltip, Legend);
  25. const $q = useQuasar();
  26. const { lighten } = colors;
  27. const props = defineProps({
  28. data: {
  29. type: Object,
  30. default: () => ({
  31. chart_data: [],
  32. current_total: 0,
  33. }),
  34. },
  35. dataSetLabel: {
  36. type: String,
  37. default: "Dados",
  38. },
  39. title: {
  40. type: String,
  41. default: "Título",
  42. },
  43. backgroundColors: {
  44. type: Array,
  45. default: null,
  46. },
  47. });
  48. const chart_ref = ref(null);
  49. const onResize = () => {
  50. if (chart_ref.value?.chart) {
  51. setTimeout(() => {
  52. chart_ref.value.chart.resize();
  53. }, 50);
  54. }
  55. };
  56. const textColor = computed(() => {
  57. return $q.dark.isActive ? "text-white" : "text-black";
  58. });
  59. const labelColor = computed(() => {
  60. return $q.dark.isActive ? "#ffffff" : "#000000";
  61. });
  62. const dataLabelColor = computed(() => {
  63. return $q.dark.isActive ? "#ffffff" : "#000000";
  64. });
  65. const hasData = computed(() => {
  66. return props.data?.chart_data && props.data.chart_data.length > 0;
  67. });
  68. const chartLabels = computed(() => {
  69. return props.data?.chart_data?.map((item) => item.label) || [];
  70. });
  71. const chartValues = computed(() => {
  72. return props.data?.chart_data?.map((item) => item.value) || [];
  73. });
  74. const chartPercentages = computed(() => {
  75. const total = props.data?.current_total || 0;
  76. if (total === 0) return [];
  77. return (
  78. props.data?.chart_data?.map((item) =>
  79. Math.round((item.value / total) * 100),
  80. ) || []
  81. );
  82. });
  83. const chartThemeColors = computed(() => {
  84. if (props.backgroundColors) {
  85. return props.backgroundColors;
  86. }
  87. const primaryColor = getCssVar("primary");
  88. if (!primaryColor) return [];
  89. const numColors = chartValues.value.length;
  90. const step = numColors > 0 ? 50 / numColors : 0;
  91. return Array.from({ length: numColors }, (_, i) =>
  92. lighten(primaryColor, i * step),
  93. );
  94. });
  95. const chartPieData = computed(() => ({
  96. labels: chartLabels.value,
  97. datasets: [
  98. {
  99. label: props.dataSetLabel,
  100. data: chartPercentages.value,
  101. backgroundColor: chartThemeColors.value,
  102. },
  103. ],
  104. }));
  105. const chartPieOptions = computed(() => ({
  106. responsive: true,
  107. maintainAspectRatio: false,
  108. plugins: {
  109. legend: {
  110. position: "bottom",
  111. labels: {
  112. color: labelColor.value,
  113. font: {
  114. size: 12,
  115. },
  116. padding: 20,
  117. },
  118. },
  119. datalabels: {
  120. color: dataLabelColor.value,
  121. font: {
  122. size: 12,
  123. weight: "bold",
  124. },
  125. formatter: (value) => {
  126. return value > 0 ? value + "%" : "";
  127. },
  128. },
  129. tooltip: {
  130. backgroundColor: $q.dark.isActive
  131. ? "rgba(0, 0, 0, 0.8)"
  132. : "rgba(255, 255, 255, 0.9)",
  133. titleColor: labelColor.value,
  134. bodyColor: labelColor.value,
  135. borderColor: labelColor.value,
  136. borderWidth: 1,
  137. callbacks: {
  138. label: (context) => {
  139. const label = context.label || "";
  140. const percentage = context.parsed;
  141. const actualValue = chartValues.value[context.dataIndex];
  142. return `${label}: ${actualValue} (${percentage}%)`;
  143. },
  144. },
  145. },
  146. },
  147. }));
  148. const downloadImage = () => {
  149. const image = chart_ref.value.chart?.toBase64Image("image/jpeg", 1);
  150. base64ToJPEG(image, props.title || "pie-chart");
  151. };
  152. defineExpose({
  153. downloadImage,
  154. chart_ref,
  155. });
  156. </script>
  157. <style scoped>
  158. .chart-wrapper {
  159. position: relative;
  160. }
  161. .chart-container {
  162. position: absolute;
  163. top: 10px;
  164. left: 0;
  165. right: 0;
  166. bottom: 0;
  167. width: 100%;
  168. height: 100%;
  169. }
  170. .no-data-container {
  171. position: absolute;
  172. top: 0;
  173. left: 0;
  174. right: 0;
  175. bottom: 0;
  176. display: flex;
  177. align-items: center;
  178. justify-content: center;
  179. padding: 1.5rem;
  180. }
  181. </style>