PluginProbe
WebberZone Top 10 — Popular Posts / 4.3.0
WebberZone Top 10 — Popular Posts v4.3.0
4.5.2 4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 All 118 releases
top-10 / includes / admin / js / chart-data.js

chart-data.js in WebberZone Top 10 — Popular Posts 4.3.0, at includes/admin/js/chart-data.js

223 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 // Function to update the chart.
2 function updateChart() {
3 jQuery.post(
4 ajaxurl,
5 {
6 action: "tptn_chart_data",
7 security: tptn_chart_data.security,
8 from_date: jQuery("#datepicker-from").val(),
9 to_date: jQuery("#datepicker-to").val(),
10 network: tptn_chart_data.network,
11 },
12 function (data) {
13 var date = [];
14 var visits = [];
15
16 for (var i in data) {
17 date.push(data[i].date);
18 visits.push(data[i].visits);
19 }
20
21 // Determine chart type based on number of datapoints
22 var chartType = "bar";
23 var chartFill = false;
24 var backgroundColor = "#70c4e1";
25 var pointBackgroundColor = "#70c4e1";
26 var borderWidth = 0;
27 var pointBorderColor = "#70c4e1";
28 var pointRadius = 0;
29 var showDataLabels = true;
30
31 // If we have more than 50 datapoints, switch to area chart for better visualization
32 if (date.length > 100) {
33 chartType = "line";
34 chartFill = true;
35 backgroundColor = "rgba(112, 196, 225, 0.8)"; // Transparent blue
36 pointBackgroundColor = "#70c4e1"; // Solid blue dots
37 borderWidth = 2;
38 pointBorderColor = "#ffffff"; // White border around dots
39 pointRadius = 3;
40 showDataLabels = false; // Hide data labels for area chart to avoid clutter
41 }
42
43 // Update chart configuration if type needs to change
44 if (window.top10chart.config.type !== chartType) {
45 window.top10chart.config.type = chartType;
46 window.top10chart.data.datasets.forEach((dataset) => {
47 dataset.fill = chartFill;
48 dataset.backgroundColor = backgroundColor;
49 dataset.pointBackgroundColor = pointBackgroundColor;
50 dataset.borderWidth = borderWidth;
51 dataset.pointBorderColor = pointBorderColor;
52 dataset.pointRadius = pointRadius;
53 });
54
55 // Update data labels plugin display setting
56 if (window.top10chart.options && window.top10chart.options.plugins && window.top10chart.options.plugins.datalabels) {
57 window.top10chart.options.plugins.datalabels.display = showDataLabels;
58 }
59 }
60
61 window.top10chart.data.labels = date;
62 window.top10chart.data.datasets.forEach((dataset) => {
63 dataset.data = visits;
64 });
65 window.top10chart.update();
66 },
67 "json"
68 );
69 }
70
71 jQuery(document).ready(function ($) {
72 $.ajax({
73 type: "POST",
74 dataType: "json",
75 url: ajaxurl,
76 data: {
77 action: "tptn_chart_data",
78 security: tptn_chart_data.security,
79 from_date: $("#datepicker-from").val(),
80 to_date: $("#datepicker-to").val(),
81 network: tptn_chart_data.network,
82 },
83 success: function (data) {
84 var date = [];
85 var visits = [];
86
87 for (var i in data) {
88 date.push(data[i].date);
89 visits.push(data[i].visits);
90 }
91
92 // Determine chart type based on number of datapoints
93 var chartType = "bar";
94 var chartFill = false;
95 var backgroundColor = "#70c4e1";
96 var pointBackgroundColor = "#70c4e1";
97 var borderWidth = 0;
98 var pointBorderColor = "#70c4e1";
99 var pointRadius = 0;
100 var showDataLabels = true;
101
102 // If we have more than 50 datapoints, switch to area chart for better visualization
103 if (date.length > 50) {
104 chartType = "line";
105 chartFill = true;
106 backgroundColor = "rgba(112, 196, 225, 0.2)"; // Transparent blue
107 pointBackgroundColor = "#70c4e1"; // Solid blue dots
108 borderWidth = 2;
109 pointBorderColor = "#ffffff"; // White border around dots
110 pointRadius = 3;
111 showDataLabels = false; // Hide data labels for area chart to avoid clutter
112 }
113
114 var ctx = $("#visits");
115 var enableBarClick = !!(tptn_chart_data && parseInt(tptn_chart_data.enableBarClick, 10) === 1);
116 var config = {
117 type: chartType,
118 data: {
119 labels: date,
120 datasets: [
121 {
122 label: tptn_chart_data.datasetlabel,
123 backgroundColor: backgroundColor,
124 borderColor: "#70c4e1",
125 hoverBackgroundColor: "#ffbf00",
126 hoverBorderColor: "#ffbf00",
127 data: visits,
128 fill: chartFill,
129 pointBackgroundColor: pointBackgroundColor,
130 pointBorderColor: pointBorderColor,
131 pointRadius: pointRadius,
132 borderWidth: borderWidth,
133 },
134 ],
135 },
136 plugins: [ChartDataLabels],
137 options: {
138 plugins: {
139 title: {
140 text: tptn_chart_data.charttitle,
141 display: true,
142 },
143 legend: {
144 display: false,
145 position: "bottom",
146 },
147 datalabels: {
148 color: "#000000",
149 anchor: "end",
150 align: "top",
151 display: showDataLabels,
152 },
153 },
154 scales: {
155 x: {
156 type: "time",
157 time: {
158 tooltipFormat: "DD",
159 unit: "day",
160 displayFormats: {
161 day: "DD",
162 },
163 },
164 title: {
165 display: false,
166 labelString: "Date",
167 },
168 },
169 y: {
170 grace: "5%",
171 suggestedMin: 0,
172 display: true,
173 title: {
174 display: false,
175 text: tptn_chart_data.datasetlabel,
176 color: "#000",
177 padding: { top: 30, left: 0, right: 0, bottom: 0 },
178 },
179 },
180 },
181 },
182 };
183
184 if (enableBarClick) {
185 config.options.onClick = function (event, elements) {
186 if (elements.length > 0) {
187 var index = elements[0].index;
188 var clickedDate = date[index];
189 // Format date for URL (d+M+Y format expected by Statistics page).
190 var dateObj = new Date(clickedDate);
191 var day = String(dateObj.getDate()).padStart(2, "0");
192 var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
193 var month = monthNames[dateObj.getMonth()];
194 var year = dateObj.getFullYear();
195 var formattedDate = day + "+" + month + "+" + year;
196
197 // Build URL with date filters.
198 var url = tptn_chart_data.statsUrl + "&orderby=daily_count&order=desc&post-date-filter-from=" + formattedDate + "&post-date-filter-to=" + formattedDate;
199 window.open(url, "_blank");
200 }
201 };
202
203 config.options.onHover = function (event, elements) {
204 event.native.target.style.cursor = elements.length > 0 ? "pointer" : "default";
205 };
206
207 config.options.plugins.tooltip = {
208 callbacks: {
209 afterBody: function () {
210 return tptn_chart_data.clickBarHint;
211 },
212 },
213 };
214 }
215
216 window.top10chart = new Chart(ctx, config);
217 },
218 error: function (data) {
219 // console.log(data);
220 },
221 });
222 });
223