PluginProbe
WebberZone Top 10 — Popular Posts / trunk
WebberZone Top 10 — Popular Posts vtrunk
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 1.6.1 1.6.2 All 116 releases
top-10 / includes / admin / js / chart-data.js

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

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