PluginProbe
Chartify – WordPress Chart Plugin / 3.0.1
Chartify – WordPress Chart Plugin v3.0.1
3.8.1 3.8.0 3.7.9 3.7.8 3.7.7 3.7.6 3.7.5 trunk 1.0.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 All 84 releases
chart-builder / public / js / chart-builder-public-chartjs.js

chart-builder-public-chartjs.js in Chartify – WordPress Chart Plugin 3.0.1, at public/js/chart-builder-public-chartjs.js

293 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function($) {
2 'use strict';
3
4 function ChartBuilderChartJs(element, options) {
5 this.el = element;
6 this.$el = $(element);
7 this.htmlClassPrefix = 'ays-chart-';
8 this.htmlNamePrefix = 'ays_';
9 this.uniqueId;
10 this.dbData = undefined;
11 this.chartSourceData = undefined;
12 this.chartObj = undefined;
13 this.chartOptions = null;
14 this.chartData = null;
15 this.chartTempData = null;
16 this.chartType = 'pie_chart';
17 this.chartId = null;
18 this.chartSourceType = 'chart-js';
19 this.chartObject = null;
20
21 this.chartSources = {
22 'pie_chart' : 'Pie Chart',
23 'bar_chart' : 'Bar Chart',
24 'line_chart' : 'Line Chart',
25 }
26
27 this.init();
28
29 return this;
30 }
31
32 ChartBuilderChartJs.prototype.init = function() {
33 var _this = this;
34 _this.uniqueId = _this.$el.data('id');
35
36 if ( typeof window['aysChartOptions'+_this.uniqueId] != 'undefined' ) {
37 _this.dbData = JSON.parse( atob( window['aysChartOptions'+_this.uniqueId]['aysChartOptions'] ) );
38 }
39
40 _this.setEvents();
41 }
42
43 ChartBuilderChartJs.prototype.setEvents = function(e){
44 var _this = this;
45
46 _this.chartId = _this.dbData.id;
47 _this.chartType = _this.dbData.chart_type;
48 _this.chartSourceType = _this.dbData.chartSourceType;
49 _this.chartData = _this.dbData.source;
50
51 _this.loadChartBySource();
52 _this.resizeChart();
53
54 $(document).on('click', '.elementor-tab-title, .e-n-tab-title, .ays-load-chart-source', function (e) {
55 _this.loadChartBySource();
56 });
57 $(document).on('change', '.ays-load-chart-source', function (e) {
58 _this.loadChartBySource();
59 });
60 }
61
62 // Load charts by given type main function
63 ChartBuilderChartJs.prototype.loadChartBySource = function(){
64 var _this = this;
65
66 if(typeof _this.chartType !== undefined && _this.chartType){
67 switch (_this.chartType) {
68 case 'pie_chart':
69 _this.pieChartView();
70 break;
71 case 'bar_chart':
72 _this.barChartView();
73 break;
74 case 'line_chart':
75 _this.lineChartView();
76 break;
77 default:
78 _this.pieChartView();
79 break;
80 }
81 }
82 }
83
84 // Load chart by pie chart
85 ChartBuilderChartJs.prototype.pieChartView = function(){
86 var _this = this;
87 var getChartSource = _this.dbData.source;
88
89 var dataTypes = _this.chartConvertData( getChartSource );
90
91 var settings = _this.dbData.options;
92 var nSettings = _this.configOptionsForCharts(settings);
93
94 // var ctx = document.getElementById(_this.htmlClassPrefix + _this.uniqueId + '-canvas');
95 var parentId = _this.htmlClassPrefix + _this.chartType + '-' + _this.uniqueId;
96 var canvasId = parentId + '-canvas';
97
98 var parentElement = document.getElementById(parentId);
99 var canvasElement = document.getElementById(canvasId);
100
101 if (!canvasElement) {
102 canvasElement = document.createElement('canvas');
103 canvasElement.id = canvasId;
104 parentElement.appendChild(canvasElement);
105 }
106
107 var ctx = canvasElement;
108
109 _this.chartObject = new Chart(ctx, {
110 type: 'pie',
111 data: {
112 labels: dataTypes?.labels,
113 datasets: dataTypes?.dataSets,
114 },
115 options: {
116 radius: nSettings.outerRadius,
117 spacing: nSettings.sliceSpacing,
118 circumference: nSettings.circumference,
119 rotation: nSettings.startAngle,
120 }
121 });
122
123 _this.resizeChart();
124 }
125
126 // Load chart by bar chart
127 ChartBuilderChartJs.prototype.barChartView = function(){
128 var _this = this;
129 var getChartSource = _this.dbData.source;
130
131 var dataTypes = _this.chartConvertData( getChartSource );
132
133 var settings = _this.dbData.options;
134 var nSettings = _this.configOptionsForCharts(settings);
135
136 // var ctx = document.getElementById(_this.htmlClassPrefix + _this.uniqueId + '-canvas');
137 var parentId = _this.htmlClassPrefix + _this.chartType + '-' + _this.uniqueId;
138 var canvasId = parentId + '-canvas';
139
140 var parentElement = document.getElementById(parentId);
141 var canvasElement = document.getElementById(canvasId);
142
143 if (!canvasElement) {
144 canvasElement = document.createElement('canvas');
145 canvasElement.id = canvasId;
146 parentElement.appendChild(canvasElement);
147 }
148
149 var ctx = canvasElement;
150
151 _this.chartObject = new Chart(ctx, {
152 type: 'bar',
153 data: {
154 labels: dataTypes?.labels,
155 datasets: dataTypes?.dataSets,
156 },
157 options: {}
158 });
159
160 _this.resizeChart();
161 }
162
163 // Load chart by line chart
164 ChartBuilderChartJs.prototype.lineChartView = function(){
165 var _this = this;
166 var getChartSource = _this.dbData.source;
167
168 var dataTypes = _this.chartConvertData( getChartSource );
169
170 var settings = _this.dbData.options;
171 var nSettings = _this.configOptionsForCharts(settings);
172
173 // var ctx = document.getElementById(_this.htmlClassPrefix + _this.uniqueId + '-canvas');
174 var parentId = _this.htmlClassPrefix + _this.chartType + '-' + _this.uniqueId;
175 var canvasId = parentId + '-canvas';
176
177 var parentElement = document.getElementById(parentId);
178 var canvasElement = document.getElementById(canvasId);
179
180 if (!canvasElement) {
181 canvasElement = document.createElement('canvas');
182 canvasElement.id = canvasId;
183 parentElement.appendChild(canvasElement);
184 }
185
186 var ctx = canvasElement;
187
188 _this.chartObject = new Chart(ctx, {
189 type: 'line',
190 data: {
191 labels: dataTypes?.labels,
192 datasets: dataTypes?.dataSets,
193 },
194 options: {}
195 });
196
197 _this.resizeChart();
198 }
199
200 /*
201 Configure all settings for all chart types
202 Getting settings for each chart type in respective function
203 */
204 ChartBuilderChartJs.prototype.configOptionsForCharts = function (settings) {
205 var newSettings = {};
206
207 newSettings.outerRadius = settings['outer_radius'];
208 newSettings.sliceSpacing = settings['slice_spacing'];
209 newSettings.circumference = settings['circumference'];
210 newSettings.startAngle = settings['start_angle'];
211
212 return newSettings;
213 }
214
215 // Detect window resize moment to draw charts responsively
216 ChartBuilderChartJs.prototype.resizeChart = function(){
217 var _this = this;
218
219 //create trigger to resizeEnd event
220 $(window).resize(function() {
221 if(this.resizeTO) clearTimeout(this.resizeTO);
222 this.resizeTO = setTimeout(function() {
223 $(this).trigger('resizeEnd');
224 }, 100);
225 });
226
227 //redraw graph when window resize is completed
228 $(window).on('resizeEnd', function() {
229 _this.chartObject.resize();
230 });
231 }
232
233 // Load chart by pie chart
234 ChartBuilderChartJs.prototype.chartConvertData = function( data ){
235 var _this = this;
236 var dataTypes = [];
237
238 var dataTypes = Array.from({ length: data[0].length }, () => []);
239 for (var i = 0; i < data.length; i++) {
240 for (var j = 0; j < data[i].length; j++) {
241 if (i === 0) {
242 dataTypes[j].push(data[i][j]);
243 } else {
244 dataTypes[j].push(i === 0 || j === 0 ? data[i][j] : +data[i][j]);
245 }
246 }
247 }
248
249 var labels = dataTypes.shift();
250 labels.shift();
251 var dataSets = dataTypes.map(item => ({
252 label: item[0],
253 data: item.slice(1).map(Number),
254 }));
255
256 return {labels, dataSets};
257 }
258
259 // Update chart data and display immediately
260 ChartBuilderChartJs.prototype.updateChartData = function( newData ){
261 var _this = this;
262 var newData = _this.chartConvertData( _this.chartData );
263
264 _this.chartObject.data = {
265 ..._this.chartObject.data,
266 labels: newData?.labels,
267 datasets: newData?.dataSets,
268 },
269 _this.chartObject.update();
270 }
271
272 ChartBuilderChartJs.prototype.htmlDecode = function (input) {
273 var e = document.createElement('div');
274 e.innerHTML = input;
275 return e.childNodes[0].nodeValue;
276 }
277
278 $.fn.ChartBuilderChartJsMain = function(options) {
279 return this.each(function() {
280 if (!$.data(this, 'ChartBuilderChartJsMain')) {
281 $.data(this, 'ChartBuilderChartJsMain', new ChartBuilderChartJs(this, options));
282 } else {
283 try {
284 $(this).data('ChartBuilderChartJsMain').init();
285 } catch (err) {
286 console.error('ChartBuilderChartJsMain has not initiated properly');
287 }
288 }
289 });
290 };
291
292 })(jQuery);
293