PluginProbe
Chartify – WordPress Chart Plugin / 1.0.0
Chartify – WordPress Chart Plugin v1.0.0
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 3.1.6 All 83 releases
chart-builder / public / js / chart-builder-public-plugin.js

chart-builder-public-plugin.js in Chartify – WordPress Chart Plugin 1.0.0, at public/js/chart-builder-public-plugin.js

251 lines 6.8 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 AysChartPlugin(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
17 this.chartSources = {
18 'bar_chart' : 'Bar Chart',
19 'pie_chart' : 'Pie Chart',
20 'column_chart' : 'Column Chart'
21 }
22
23 this.init();
24
25 return this;
26 }
27
28 AysChartPlugin.prototype.init = function() {
29 var _this = this;
30 _this.uniqueId = _this.$el.data('id');
31
32 if ( typeof window.aysChartOptions != 'undefined' ) {
33 _this.dbData = JSON.parse( atob( window.aysChartOptions[ _this.uniqueId ] ) );
34 }
35
36 _this.setEvents();
37 }
38
39 AysChartPlugin.prototype.setEvents = function(e){
40 var _this = this;
41
42 var chartType = _this.dbData.chart_type;
43
44 _this.loadChartBySource( chartType );
45 }
46
47 // Load charts by given type main function
48 AysChartPlugin.prototype.loadChartBySource = function( chartType ){
49 var _this = this;
50
51 if( ! chartType ){
52 chartType = _this.chartSourceData.chartType;
53 }
54
55 if(typeof chartType !== undefined && chartType){
56 switch (chartType) {
57 case 'pie_chart':
58 _this.pieChartView( chartType );
59 break;
60 case 'bar_chart':
61 _this.barChartView( chartType );
62 break;
63 case 'column_chart':
64 _this.columnChartView( chartType );
65 break;
66 default:
67 _this.pieChartView( chartType );
68 break;
69 }
70 }
71 }
72
73 // Load chart by pie chart
74 AysChartPlugin.prototype.pieChartView = function( chartType ){
75 var _this = this;
76 var getChartSource = _this.dbData.source;
77
78 var dataTypes = _this.chartConvertData( getChartSource );
79
80 var settings = _this.dbData.options;
81 var chartFontSize = settings['font_size'];
82 var tooltipTrigger = settings['tooltip_trigger'];
83 var showColorCode = (settings['show_color_code'] == 'on') ? true : false;
84
85
86 /* == Google part == */
87 google.charts.load('current', {'packages':['corechart']});
88 google.charts.setOnLoadCallback(drawChart);
89
90 function drawChart() {
91 _this.chartData = google.visualization.arrayToDataTable( dataTypes );
92
93 _this.chartOptions = {
94 backgroundColor: 'transparent',
95 fontSize: chartFontSize,
96 tooltip: {
97 trigger: tooltipTrigger,
98 showColorCode: showColorCode
99 }
100 };
101
102 // height: 250,
103 // chartArea: {
104 // width: '80%',
105 // height: '80%'
106 // },
107
108 _this.chartObj = new google.visualization.PieChart( document.getElementById(_this.htmlClassPrefix + chartType) );
109
110 _this.chartObj.draw( _this.chartData, _this.chartOptions );
111 _this.resizeChart();
112 }
113 /* */
114 }
115
116 // Load chart by bar chart
117 AysChartPlugin.prototype.barChartView = function( chartType ){
118 var _this = this;
119 var getChartSource = _this.dbData.source;
120 var dataTypes = _this.chartConvertData( getChartSource );
121
122 var settings = _this.dbData.options;
123 var chartFontSize = settings['font_size'];
124 var tooltipTrigger = settings['tooltip_trigger'];
125 var showColorCode = (settings['show_color_code'] == 'on') ? true : false;
126
127 /* == Google part == */
128 google.charts.load('current', {'packages':['corechart']});
129 google.charts.setOnLoadCallback(drawChart);
130
131 function drawChart() {
132 _this.chartData = google.visualization.arrayToDataTable(dataTypes);
133
134 _this.chartOptions = {
135 backgroundColor: 'transparent',
136 fontSize: chartFontSize,
137 legend: { position: 'none' },
138 tooltip: {
139 trigger: tooltipTrigger,
140 showColorCode: showColorCode
141 }
142 };
143
144 _this.chartObj = new google.visualization.BarChart(document.getElementById(_this.htmlClassPrefix + chartType));
145
146 _this.chartObj.draw( _this.chartData, _this.chartOptions );
147 _this.resizeChart();
148 }
149 /* */
150 }
151
152 // Load chart by column chart
153 AysChartPlugin.prototype.columnChartView = function( chartType ){
154 var _this = this;
155 var getChartSource = _this.dbData.source;
156
157 // Collect data in new array for chart rendering (Column chart)
158 var dataTypes = _this.chartConvertData( getChartSource );
159
160 var settings = _this.dbData.options;
161 var chartFontSize = settings['font_size'];
162 var tooltipTrigger = settings['tooltip_trigger'];
163 var showColorCode = (settings['show_color_code'] == 'on') ? true : false;
164
165 /* == Google part == */
166 google.charts.load('current', {'packages':['corechart']});
167 google.charts.setOnLoadCallback(drawChart);
168
169 function drawChart() {
170 _this.chartData = google.visualization.arrayToDataTable(dataTypes);
171
172 _this.chartOptions = {
173 backgroundColor: 'transparent',
174 fontSize: chartFontSize,
175 legend: { position: 'none' },
176 tooltip: {
177 trigger: tooltipTrigger,
178 showColorCode: showColorCode
179 }
180 };
181
182 _this.chartObj = new google.visualization.ColumnChart(document.getElementById(_this.htmlClassPrefix + chartType));
183
184 _this.chartObj.draw( _this.chartData, _this.chartOptions );
185 _this.resizeChart();
186 }
187 /* */
188 }
189
190 // Detect window resize moment to draw charts responsively
191 AysChartPlugin.prototype.resizeChart = function(){
192 var _this = this;
193
194 //create trigger to resizeEnd event
195 $(window).resize(function() {
196 if(this.resizeTO) clearTimeout(this.resizeTO);
197 this.resizeTO = setTimeout(function() {
198 $(this).trigger('resizeEnd');
199 }, 100);
200 });
201
202 //redraw graph when window resize is completed
203 $(window).on('resizeEnd', function() {
204 _this.chartObj.draw( _this.chartData, _this.chartOptions );
205 });
206
207 }
208
209 // Load chart by pie chart
210 AysChartPlugin.prototype.chartConvertData = function( data ){
211 var _this = this;
212 // var dataTypes = [['Option', 'Value', { role: 'style' }]];
213 var dataTypes = [['Option', 'Value']];
214
215 // var defaultColors = _this.chartSourceData.settings['colors'];
216
217 // Collect data in new array for chart rendering
218 for ( var key in data ) {
219 if ( data.hasOwnProperty( key ) ) {
220 // var columnColor = (key <= 10) ? defaultColors[key] : Math.floor( Math.random() * 16777215 ).toString(16);
221 dataTypes.push([
222 data[key][0], +(data[key][1])
223 ]);
224 }
225 }
226
227 return dataTypes;
228 }
229
230 // Update chart data and display immediately
231 AysChartPlugin.prototype.updateChartData = function( newData ){
232 var _this = this;
233 _this.chartObj.draw( newData, _this.chartOptions );
234 }
235
236 $.fn.AysChartBuilder = function(options) {
237 return this.each(function() {
238 if (!$.data(this, 'AysChartBuilder')) {
239 $.data(this, 'AysChartBuilder', new AysChartPlugin(this, options));
240 } else {
241 try {
242 $(this).data('AysChartBuilder').init();
243 } catch (err) {
244 console.error('AysChartBuilder has not initiated properly');
245 }
246 }
247 });
248 };
249
250 })(jQuery);
251