index.phtml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. declare(strict_types=1);
  3. /** @var FreshRSS_ViewStats $this */
  4. $this->partial('aside_subscription');
  5. ?>
  6. <main class="post">
  7. <div class="link-back-wrapper">
  8. <a class="link-back" href="<?= _url('index', 'index') ?>"><?= _t('gen.action.back_to_rss_feeds') ?></a>
  9. </div>
  10. <h1><?= _t('admin.stats.main') ?></h1>
  11. <div class="stat-grid">
  12. <div class="stat half">
  13. <h2><?= _t('admin.stats.entry_repartition') ?></h2>
  14. <div class="table-wrapper scrollbar-thin">
  15. <table>
  16. <thead>
  17. <tr>
  18. <th> </th>
  19. <th><?= _t('admin.stats.main_stream') ?></th>
  20. <th><?= _t('admin.stats.all_feeds') ?></th>
  21. </tr>
  22. </thead>
  23. <tbody>
  24. <tr>
  25. <th><?= _t('admin.stats.status_total') ?></th>
  26. <td class="numeric"><?= format_number($this->repartitions['main_stream']['total'] ?? -1) ?></td>
  27. <td class="numeric"><?= format_number($this->repartitions['all_feeds']['total'] ?? -1) ?></td>
  28. </tr>
  29. <tr>
  30. <th><?= _t('admin.stats.status_read') ?></th>
  31. <td class="numeric"><?= format_number($this->repartitions['main_stream']['count_reads'] ?? -1) ?></td>
  32. <td class="numeric"><?= format_number($this->repartitions['all_feeds']['count_reads'] ?? -1) ?></td>
  33. </tr>
  34. <tr>
  35. <th><?= _t('admin.stats.status_unread') ?></th>
  36. <td class="numeric"><?= format_number($this->repartitions['main_stream']['count_unreads'] ?? -1) ?></td>
  37. <td class="numeric"><?= format_number($this->repartitions['all_feeds']['count_unreads'] ?? -1) ?></td>
  38. </tr>
  39. <tr>
  40. <th><?= _t('admin.stats.status_favorites') ?></th>
  41. <td class="numeric"><?= format_number($this->repartitions['main_stream']['count_favorites'] ?? -1) ?></td>
  42. <td class="numeric"><?= format_number($this->repartitions['all_feeds']['count_favorites'] ?? -1) ?></td>
  43. </tr>
  44. </tbody>
  45. </table>
  46. </div>
  47. </div>
  48. <div class="stat half">
  49. <h2><?= _t('admin.stats.top_feed') ?></h2>
  50. <div class="table-wrapper scrollbar-thin">
  51. <table>
  52. <thead>
  53. <tr>
  54. <th><?= _t('admin.stats.feed') ?></th>
  55. <th><?= _t('admin.stats.category') ?></th>
  56. <th><?= _t('admin.stats.entry_count') ?></th>
  57. <th><?= _t('admin.stats.percent_of_total') ?></th>
  58. </tr>
  59. </thead>
  60. <tbody>
  61. <?php foreach ($this->topFeed as $feed): ?>
  62. <tr>
  63. <td><a href="<?= _url('stats', 'repartition', 'id', $feed['id']) ?>"><?= $feed['name'] ?></a></td>
  64. <td><?= $feed['category'] ?></td>
  65. <td class="numeric"><?= format_number($feed['count']) ?></td>
  66. <td class="numeric"><?php
  67. if (!empty($this->repartitions['all_feeds']['total'])) {
  68. echo format_number($feed['count'] / $this->repartitions['all_feeds']['total'] * 100, 1);
  69. }
  70. ?></td>
  71. </tr>
  72. <?php endforeach; ?>
  73. </tbody>
  74. </table>
  75. </div>
  76. </div>
  77. <div class="stat">
  78. <h2><?= _t('admin.stats.entry_per_day') ?></h2>
  79. <div>
  80. <canvas id="statsEntriesPerDay"></canvas>
  81. <script class="jsonData-stats" type="application/json">
  82. <?= json_encode([
  83. 'canvasID' => 'statsEntriesPerDay',
  84. 'charttype' => 'barWithAverage',
  85. 'labelBarChart' => _t('admin.stats.entry_count'),
  86. 'dataBarChart' => $this->entryCount,
  87. 'labelAverage' => 'Average (' . $this->average . ')',
  88. 'dataAverage' => $this->average,
  89. 'xAxisLabels' => $this->last30DaysLabels,
  90. ], JSON_UNESCAPED_UNICODE)
  91. ?></script>
  92. </div>
  93. </div>
  94. <?php
  95. // Function to generate a color palette
  96. /**
  97. * Generate a color palette.
  98. *
  99. * @param int $count The number of colors to generate.
  100. * @return array<int, string> An array of HSL color strings.
  101. */
  102. function generateColorPalette(int $count): array {
  103. $colors = [];
  104. for ($i = 0; $i < $count; $i++) {
  105. $hue = ($i / $count) * 360; // Distribute colors evenly around the color wheel
  106. $saturation = 70; // Fixed saturation
  107. $lightness = 50; // Fixed lightness
  108. $colors[] = "hsl($hue, {$saturation}%, {$lightness}%)";
  109. }
  110. return $colors;
  111. }
  112. // 1. Get all unique category labels and sort them
  113. $allLabels = array_unique(array_merge($this->feedByCategory['label'], $this->entryByCategory['label']));
  114. sort($allLabels); // Ensure consistent order
  115. // 2. Generate a color palette based on the number of unique categories
  116. $colorPalette = generateColorPalette(count($allLabels));
  117. // 3. Map categories to colors
  118. $colorMap = array_combine($allLabels, $colorPalette);
  119. // 4. Align data and labels for both charts
  120. $feedData = array_fill_keys($allLabels, 0); // Initialize data with all categories
  121. foreach ($this->feedByCategory['label'] as $index => $label) {
  122. $feedData[$label] = $this->feedByCategory['data'][$index];
  123. }
  124. $entryData = array_fill_keys($allLabels, 0); // Initialize data with all categories
  125. foreach ($this->entryByCategory['label'] as $index => $label) {
  126. $entryData[$label] = $this->entryByCategory['data'][$index];
  127. }
  128. // Final data and labels
  129. $feedLabels = array_keys($feedData);
  130. $feedColors = array_map(fn($label) => $colorMap[$label], $feedLabels);
  131. $feedValues = array_values($feedData);
  132. $entryLabels = array_keys($entryData);
  133. $entryColors = array_map(fn($label) => $colorMap[$label], $entryLabels);
  134. $entryValues = array_values($entryData);
  135. ?>
  136. <div class="stat half">
  137. <h2><?= _t('admin.stats.feed_per_category') ?></h2>
  138. <div>
  139. <canvas id="statsFeedsPerCategory"></canvas>
  140. <script class="jsonData-stats" type="application/json">
  141. <?= json_encode([
  142. 'canvasID' => 'statsFeedsPerCategory',
  143. 'charttype' => 'doughnut',
  144. 'data' => $feedValues,
  145. 'labels' => $feedLabels,
  146. 'backgroundColor' => $feedColors,
  147. ], JSON_UNESCAPED_UNICODE); ?>
  148. </script>
  149. </div>
  150. </div>
  151. <div class="stat half">
  152. <h2><?= _t('admin.stats.entry_per_category') ?></h2>
  153. <div>
  154. <canvas id="statsEntriesPerCategory"></canvas>
  155. <script class="jsonData-stats" type="application/json">
  156. <?= json_encode([
  157. 'canvasID' => 'statsEntriesPerCategory',
  158. 'charttype' => 'doughnut',
  159. 'data' => $entryValues,
  160. 'labels' => $entryLabels,
  161. 'backgroundColor' => $entryColors,
  162. ], JSON_UNESCAPED_UNICODE); ?>
  163. </script>
  164. </div>
  165. </div>
  166. </div>
  167. </main>
  168. <script src="../scripts/statsWithChartjs.js?<?= @filemtime(PUBLIC_PATH . '/scripts/statsWithChartjs.js') ?>"></script>