DashboardChannelsPanel.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <q-card class="panel-card panel-card--soft panel-card--channels" flat>
  3. <div class="panel-title">Canais de Aquisição</div>
  4. <div class="channel-chart-wrap">
  5. <div class="channel-chart-box">
  6. <Pie :data="chartData" :options="chartOptions" />
  7. </div>
  8. </div>
  9. <div class="channel-legend">
  10. <div
  11. v-for="channel in legendItems"
  12. :key="channel.key"
  13. class="channel-legend-item"
  14. >
  15. <span class="channel-dot" :style="{ backgroundColor: channel.color }" />
  16. <span class="channel-name">{{ channel.label }}</span>
  17. <span class="channel-total">
  18. {{ formatInteger(channel.reservations_count) }} reservas
  19. </span>
  20. </div>
  21. </div>
  22. </q-card>
  23. </template>
  24. <script setup>
  25. import { computed } from "vue";
  26. import { Pie } from "vue-chartjs";
  27. import { formatInteger } from "src/helpers/utils";
  28. const props = defineProps({
  29. channels: {
  30. type: Array,
  31. default: () => [],
  32. },
  33. });
  34. const fixedChannelColors = Object.freeze({
  35. airbnb: "#FF385C",
  36. booking: "#60A5FA",
  37. decolar: "#22C55E",
  38. direto: "#F59E0B",
  39. vrbo: "#0EA5E9",
  40. sem_canal: "#B0BEC5",
  41. raniery_kohler: "#A855F7",
  42. });
  43. const fallbackChannelPalette = Object.freeze([
  44. "#A855F7",
  45. "#8B5CF6",
  46. "#C084FC",
  47. "#9333EA",
  48. "#D946EF",
  49. "#7C3AED",
  50. "#E879F9",
  51. "#6D28D9",
  52. ]);
  53. const normalize = (channel) => {
  54. const value = String(channel ?? "").toLowerCase();
  55. if (value.includes("airbnb")) {
  56. return "airbnb";
  57. }
  58. if (value.includes("booking")) {
  59. return "booking";
  60. }
  61. if (value.includes("decolar")) {
  62. return "decolar";
  63. }
  64. if (
  65. value.includes("sem canal") ||
  66. value.includes("sem_canal") ||
  67. value.includes("no channel")
  68. ) {
  69. return "sem_canal";
  70. }
  71. if (value.includes("raniery kohler")) {
  72. return "raniery_kohler";
  73. }
  74. if (
  75. value.includes("direto") ||
  76. value.includes("direct") ||
  77. value.includes("site") ||
  78. value.includes("whatsapp") ||
  79. value.includes("instagram")
  80. ) {
  81. return "direto";
  82. }
  83. if (value.includes("vrbo")) {
  84. return "vrbo";
  85. }
  86. return "outros";
  87. };
  88. const buildFallbackColor = (channel) => {
  89. const seed = String(channel ?? "").trim().toLowerCase();
  90. let hash = 0;
  91. for (let index = 0; index < seed.length; index += 1) {
  92. hash = (hash * 31 + seed.charCodeAt(index)) % fallbackChannelPalette.length;
  93. }
  94. return fallbackChannelPalette[hash];
  95. };
  96. const resolveChannelColor = (channel) => {
  97. const normalizedChannel = normalize(channel);
  98. return fixedChannelColors[normalizedChannel] ?? buildFallbackColor(channel);
  99. };
  100. const legendItems = computed(() =>
  101. props.channels.map((c) => ({
  102. key: String(c.channel ?? ""),
  103. label: c.channel,
  104. reservations_count: Number(c.reservations_count ?? 0),
  105. color: resolveChannelColor(c.channel),
  106. })),
  107. );
  108. const hasChannels = computed(() => legendItems.value.length > 0);
  109. const chartData = computed(() => ({
  110. labels: hasChannels.value
  111. ? legendItems.value.map((item) => item.label)
  112. : ["Sem dados"],
  113. datasets: [
  114. {
  115. data: hasChannels.value
  116. ? legendItems.value.map((item) => item.reservations_count)
  117. : [1],
  118. backgroundColor: hasChannels.value
  119. ? legendItems.value.map((item) => item.color)
  120. : ["#D9E3E7"],
  121. // borderColor: "#FFFFFF",
  122. borderWidth: 0,
  123. },
  124. ],
  125. }));
  126. const chartOptions = computed(() => ({
  127. responsive: true,
  128. maintainAspectRatio: false,
  129. plugins: {
  130. legend: { display: false },
  131. tooltip: { enabled: false },
  132. },
  133. }));
  134. </script>
  135. <style scoped>
  136. .panel-title {
  137. margin-bottom: 16px;
  138. font-size: 19px;
  139. font-weight: 400;
  140. color: #08514c;
  141. }
  142. .channel-chart-wrap {
  143. display: flex;
  144. justify-content: center;
  145. min-width: 0;
  146. padding: 8px 0 16px;
  147. }
  148. .channel-chart-box {
  149. width: min(120px, 100%);
  150. height: 120px;
  151. margin-bottom: 74px;
  152. }
  153. .channel-legend {
  154. display: grid;
  155. grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  156. gap: 14px;
  157. justify-items: center;
  158. text-align: center;
  159. min-width: 0;
  160. }
  161. .channel-legend-item {
  162. display: flex;
  163. flex-direction: column;
  164. align-items: center;
  165. gap: 4px;
  166. min-width: 0;
  167. vertical-align: middle;
  168. }
  169. .channel-dot {
  170. display: inline-flex;
  171. width: 10px;
  172. height: 10px;
  173. border-radius: 50%;
  174. }
  175. .channel-name {
  176. display: inline-flex;
  177. align-items: center;
  178. gap: 8px;
  179. font-size: 15px;
  180. font-weight: 600;
  181. color: #273136;
  182. min-width: 0;
  183. overflow-wrap: anywhere;
  184. text-align: center;
  185. }
  186. .channel-total {
  187. font-size: 14px;
  188. font-weight: 400;
  189. color: #5f6d73;
  190. overflow-wrap: anywhere;
  191. }
  192. @media (max-width: 640px) {
  193. .channel-chart-wrap {
  194. padding: 0 0 12px;
  195. }
  196. .channel-chart-box {
  197. width: min(160px, 100%);
  198. height: 160px;
  199. margin-bottom: 24px;
  200. }
  201. .channel-legend {
  202. grid-template-columns: minmax(0, 1fr);
  203. gap: 12px;
  204. }
  205. .channel-name {
  206. font-size: 14px;
  207. }
  208. .channel-total {
  209. font-size: 13px;
  210. }
  211. }
  212. </style>