From e860847af5ceb5ec6b0a82a70b2250d4bff668ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20Fefli=C5=84ski?= Date: Tue, 27 Jan 2026 13:16:42 +0100 Subject: [PATCH] Grafika --- moje-statystyki.php | 155 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) diff --git a/moje-statystyki.php b/moje-statystyki.php index e291fd7..7c19712 100644 --- a/moje-statystyki.php +++ b/moje-statystyki.php @@ -211,6 +211,15 @@ function mystat_add_admin_menu() { 'mystat-yearly-summary', // Slug podmenu 'mystat_yearly_summary_page'// Funkcja renderująca ); + + add_submenu_page( + 'moje-statystyki', // Slug rodzica + 'Infografika', // Tytuł strony + 'Infografika', // Tytuł w podmenu + 'manage_options', // Wymagane uprawnienia + 'mystat-infographic', // Slug podmenu + 'mystat_infographic_page' // Funkcja renderująca + ); } function mystat_dashboard_page() { @@ -591,6 +600,152 @@ function mystat_get_track_from_gpx_url( $gpx_url ) { return $points; } +function mystat_infographic_page() { + global $wpdb; + $table_activities = $wpdb->prefix . 'mystat_activities'; + $table_categories = $wpdb->prefix . 'mystat_categories'; + + $current_year = isset( $_GET['year'] ) ? intval( $_GET['year'] ) : current_time( 'Y' ); + + // Pobierz dostępne lata z bazy danych + $available_years = $wpdb->get_col( "SELECT DISTINCT YEAR(date) FROM $table_activities ORDER BY YEAR(date) DESC" ); + if ( empty( $available_years ) ) { + $available_years = [current_time('Y')]; // Domyślny rok, jeśli brak danych + } + + // --- 1. Statystyki ogólne (wszystkie lata) --- + $overall_stats = $wpdb->get_row(" + SELECT + SUM(distance) as total_distance, + SEC_TO_TIME(SUM(TIME_TO_SEC(duration))) as total_duration, + SUM(total_elevation_gain) as total_elevation_gain, + COUNT(id) as total_activities + FROM $table_activities + "); + + // --- 2. Statystyki dla wybranego roku --- + $yearly_stats = $wpdb->get_row($wpdb->prepare(" + SELECT + SUM(distance) as total_distance, + SEC_TO_TIME(SUM(TIME_TO_SEC(duration))) as total_duration, + SUM(total_elevation_gain) as total_elevation_gain, + COUNT(id) as total_activities + FROM $table_activities + WHERE YEAR(date) = %d + ", $current_year)); + + // --- 3. Dane dla wykresu kołowego (dystans per kategoria dla wybranego roku) --- + $category_distance_data = $wpdb->get_results($wpdb->prepare(" + SELECT c.name as category_name, c.color, SUM(a.distance) as total_distance + FROM $table_activities a + LEFT JOIN $table_categories c ON a.category_id = c.id + WHERE YEAR(a.date) = %d + GROUP BY c.name, c.color + HAVING SUM(a.distance) > 0 + ORDER BY total_distance DESC + ", $current_year)); + + $chart_labels = []; + $chart_data = []; + $chart_colors = []; + foreach ($category_distance_data as $data) { + $chart_labels[] = $data->category_name; + $chart_data[] = round((float)$data->total_distance, 2); + $chart_colors[] = $data->color; + } + + // Włączenie skryptów dla Chart.js + wp_enqueue_script('chart-js', 'https://cdn.jsdelivr.net/npm/chart.js', [], null, true); + wp_register_script('mystat-infographic-chart-loader', false); + wp_enqueue_script('mystat-infographic-chart-loader'); + wp_add_inline_script('mystat-infographic-chart-loader', ' + document.addEventListener("DOMContentLoaded", function() { + const ctx = document.getElementById("mystatCategoryPieChart"); + if (!ctx) return; + new Chart(ctx, { + type: "pie", + data: { + labels: ' . json_encode($chart_labels) . ', + datasets: [{ + data: ' . json_encode($chart_data) . ', + backgroundColor: ' . json_encode($chart_colors) . ', + hoverOffset: 4 + }] + }, + options: { + responsive: true, + maintainAspectRatio: false, + plugins: { + legend: { + position: "right", + }, + title: { + display: true, + text: "Dystans wg kategorii w ' . esc_js($current_year) . '" + } + } + } + }); + }); + '); + + ?> +
+

Infografika Statystyk Sportowych

+ +
+
+
+ + + + +
+
+
+ +
+

Statystyki Ogólne (wszystkie lata)

+
+

Dystans

total_distance, 2, ',', ' '); ?> km

+

Czas

total_duration); ?>

+

Wznios

total_elevation_gain, 0, ',', ' '); ?> m

+

Aktywności

total_activities, 0, ',', ' '); ?>

+
+
+ +
+

Statystyki dla

+
+

Dystans

total_distance, 2, ',', ' '); ?> km

+

Czas

total_duration); ?>

+

Wznios

total_elevation_gain, 0, ',', ' '); ?> m

+

Aktywności

total_activities, 0, ',', ' '); ?>

+
+
+ +
+

Rozkład Dystansu wg Kategorii w

+
+
+ +
+
+
+
+ +