| 1 |
// ComboChart |
| 2 |
// DonutChart |
| 3 |
const CHART_DEFAULT_HINT_1PN = "FORMAT\n\nFirst column: string or date\nOther columns: numeric\n\nAt least 1 string or date and 1 numeric column required\n\nEXAMPLES\n\nselect City, Population from population\nselect City, Population, Density from population"; |
| 4 |
const CHART_DEFAULT_HINT_1 = "FORMAT\n\nFirst column: string or date\nSecond column: numeric\n\nAdditional columns are not used\n\nEXAMPLES\n\nselect City, Population from population"; |
| 5 |
const CHART_TYPES = { |
| 6 |
"BarChart": { |
| 7 |
label: "Bar Chart", |
| 8 |
hint: CHART_DEFAULT_HINT_1PN |
| 9 |
}, |
| 10 |
"ColumnChart": { |
| 11 |
label: "Column Chart", |
| 12 |
hint: CHART_DEFAULT_HINT_1PN |
| 13 |
}, |
| 14 |
"Histogram": { |
| 15 |
label: "Histogram", |
| 16 |
hint: CHART_DEFAULT_HINT_1PN |
| 17 |
}, |
| 18 |
"LineChart": { |
| 19 |
label: "Line Chart", |
| 20 |
hint: CHART_DEFAULT_HINT_1PN |
| 21 |
}, |
| 22 |
"PieChart": { |
| 23 |
label: "Pie Chart", |
| 24 |
hint: CHART_DEFAULT_HINT_1 |
| 25 |
}, |
| 26 |
"Gauge": { |
| 27 |
label: "Gauge", |
| 28 |
hint: CHART_DEFAULT_HINT_1 |
| 29 |
}, |
| 30 |
"Table": { |
| 31 |
label: "Table", |
| 32 |
hint: "FORMAT\n\nAny valid query\n\nEXAMPLES\n\nselect * from customers\nselect ename as 'Employee Name', sal as 'Salary' from emp" |
| 33 |
} |
| 34 |
}; |
| 35 |
|
| 36 |
const CHART_DEFAULT_LEGEND = 'bottom'; |
| 37 |
const CHART_ADD_LINENO = "Yes"; |
| 38 |
const CHART_DEFAULT_WIDTH = "*"; |
| 39 |
const CHART_DEFAULT_WIDTH_PX = 500; |
| 40 |
const CHART_DEFAULT_HEIGHT = "*"; |
| 41 |
const CHART_DEFAULT_HEIGHT_PX = 300; |
| 42 |
const CHARTAREA_DEFAULT_WIDTH = 100; |
| 43 |
const CHARTAREA_DEFAULT_HEIGHT = 100; |
| 44 |
const CHARTAREA_DEFAULT_TOP = 10; |
| 45 |
const CHARTAREA_DEFAULT_LEFT = 100; |
| 46 |
|
| 47 |
var googleChartsLoaded = false; |
| 48 |
var googleChartsObjects = {}; |
| 49 |
var cachedChartData = {}; |
| 50 |
|
| 51 |
function getChartData(widgetId, forceUpdate = false) { |
| 52 |
wpda_dbs = dashboardWidgets[widgetId].chartDbs; |
| 53 |
wpda_query = dashboardWidgets[widgetId].chartSql; |
| 54 |
jQuery.ajax({ |
| 55 |
type: "POST", |
| 56 |
url: wpda_chart_vars.wpda_ajaxurl + "?action=wpda_widget_chart_refresh", |
| 57 |
data: { |
| 58 |
wp_nonce: wpda_wpnonce_refresh, |
| 59 |
wpda_action: 'get_data', |
| 60 |
wpda_name: dashboardWidgets[widgetId].widgetName, |
| 61 |
wpda_force_update: forceUpdate |
| 62 |
} |
| 63 |
}).done( |
| 64 |
function(data) { |
| 65 |
if (data.status==="ERROR" && data.msg!==undefined) { |
| 66 |
alert("ERROR: " + data.msg); |
| 67 |
} else { |
| 68 |
if (data.error !== "") { |
| 69 |
alert("ERROR: " + data.error); |
| 70 |
} else { |
| 71 |
if (jQuery("#wpda_widget_container_" + widgetId).html() === "") { |
| 72 |
setUserChartSelection(widgetId); |
| 73 |
|
| 74 |
jQuery("#wpda_widget_chart_selection_" + widgetId + " option").remove(); |
| 75 |
jQuery.each(dashboardWidgets[widgetId].chartType, function (i, item) { |
| 76 |
jQuery("#wpda_widget_chart_selection_" + widgetId).append(jQuery("<option/>", { |
| 77 |
value: item, |
| 78 |
text: CHART_TYPES[item].label |
| 79 |
})); |
| 80 |
}); |
| 81 |
|
| 82 |
if (dashboardWidgets[widgetId].chartType.length > 1) { |
| 83 |
chartType = dashboardWidgets[widgetId].chartType[0]; |
| 84 |
} else { |
| 85 |
chartType = dashboardWidgets[widgetId].chartType[0]; |
| 86 |
} |
| 87 |
|
| 88 |
createChart( |
| 89 |
chartType, |
| 90 |
widgetId, |
| 91 |
data.cols, |
| 92 |
data.rows |
| 93 |
); |
| 94 |
} else { |
| 95 |
refreshChart(widgetId); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
function chartOptions(widgetId) { |
| 104 |
if ( |
| 105 |
dashboardWidgets[widgetId]!==undefined && |
| 106 |
dashboardWidgets[widgetId].chartOptions!==undefined && |
| 107 |
dashboardWidgets[widgetId].chartOptions!==null |
| 108 |
|
| 109 |
) { |
| 110 |
dashboardWidgets[widgetId].chartOptions.page = 'enable'; |
| 111 |
|
| 112 |
return dashboardWidgets[widgetId].chartOptions; |
| 113 |
} else { |
| 114 |
return { |
| 115 |
legend: { |
| 116 |
position: CHART_DEFAULT_LEGEND |
| 117 |
}, |
| 118 |
width: CHART_DEFAULT_WIDTH, |
| 119 |
height: CHART_DEFAULT_HEIGHT, |
| 120 |
page: 'enable' |
| 121 |
}; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
function createChart(outputType, widgetId, columns, rows) { |
| 126 |
cachedChartData[widgetId] = new google.visualization.DataTable({ |
| 127 |
cols: columns, |
| 128 |
rows: rows |
| 129 |
}); |
| 130 |
addChart(widgetId, outputType); |
| 131 |
} |
| 132 |
|
| 133 |
function refreshChart(widgetId) { |
| 134 |
jQuery("#wpda_widget_container_" + widgetId).empty(); |
| 135 |
addChart(widgetId, jQuery("#wpda_widget_chart_selection_" + widgetId).val()); |
| 136 |
} |
| 137 |
|
| 138 |
function printableVersion(url){ |
| 139 |
let win = window.open(); |
| 140 |
win.document.write('<iframe src="' + url + '" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen></iframe>'); |
| 141 |
} |
| 142 |
|
| 143 |
function addChart(widgetId, outputType) { |
| 144 |
var element = document.getElementById("wpda_widget_container_" + widgetId); |
| 145 |
googleChartsObjects[widgetId] = new google.visualization[outputType](element); |
| 146 |
|
| 147 |
google.visualization.events.addListener(googleChartsObjects[widgetId], 'ready', function () { |
| 148 |
jQuery("#wpda-widget-" + widgetId + " .wpda-chart-button-print").hide().off(); |
| 149 |
if (outputType!=="Table") { |
| 150 |
// Enable print button for charts |
| 151 |
jQuery("#wpda-widget-" + widgetId + " .wpda-chart-button-print").show().on("click", function() { |
| 152 |
printableVersion(googleChartsObjects[widgetId].getImageURI()); |
| 153 |
}); |
| 154 |
} |
| 155 |
|
| 156 |
// Create hyperlink with CSV from table data |
| 157 |
let csv = google.visualization.dataTableToCsv(cachedChartData[widgetId]); |
| 158 |
let url = "data:application/csv;charset=utf-8," + encodeURIComponent(csv); |
| 159 |
jQuery("#wpda-widget-" + widgetId + " .wpda-chart-button-export-link") |
| 160 |
.attr("href", url) |
| 161 |
.attr("download", "wp-data-access.csv"); |
| 162 |
|
| 163 |
// Open hyperlink on click |
| 164 |
jQuery("#wpda-widget-" + widgetId + " .wpda-chart-button-export").on("click", function() { |
| 165 |
jQuery("#wpda-widget-" + widgetId + " .wpda-chart-button-export-link")[0].click(); |
| 166 |
}); |
| 167 |
}); |
| 168 |
|
| 169 |
googleChartsObjects[widgetId].draw(cachedChartData[widgetId], chartOptions(widgetId)); |
| 170 |
} |
| 171 |
|
| 172 |
function setUserChartSelection(widgetId, action = null) { |
| 173 |
if (action==="hide") { |
| 174 |
// Hide in settings mode |
| 175 |
jQuery("#wpda_widget_container_" + widgetId).parent().find(".wpda_widget_chart_selection").hide(); |
| 176 |
} else { |
| 177 |
// Show in view mode |
| 178 |
jQuery("#wpda_widget_container_" + widgetId).parent().find(".wpda_widget_chart_selection").show(); |
| 179 |
} |
| 180 |
|
| 181 |
// Show drop down list only if it contains more than 1 option |
| 182 |
if (dashboardWidgets[widgetId] && dashboardWidgets[widgetId].chartType.length > 1) { |
| 183 |
jQuery("#wpda_widget_chart_selection_" + widgetId).show(); |
| 184 |
} else { |
| 185 |
jQuery("#wpda_widget_chart_selection_" + widgetId).hide(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
function chartTypeOption(id, widgetId) { |
| 190 |
return ` |
| 191 |
<li title="${CHART_TYPES[id].hint}" class="wpda_tooltip"> |
| 192 |
<input type="checkbox" id="${id}_${widgetId}" data-id="${id}"/> |
| 193 |
<label for="${id}_${widgetId}"> |
| 194 |
<img src="${wpda_chart_vars.wpda_chartdir}${id}.png"/> |
| 195 |
</label> |
| 196 |
</li>`; |
| 197 |
} |
| 198 |
|
| 199 |
function chartTypeOptions(chartTypes, widgetId) { |
| 200 |
list = ""; |
| 201 |
for (var i=0; i<chartTypes.length; i++) { |
| 202 |
list += chartTypeOption(chartTypes[i], widgetId); |
| 203 |
} |
| 204 |
return list; |
| 205 |
} |
| 206 |
|
| 207 |
function chartSettings(widgetId) { |
| 208 |
var query = ""; |
| 209 |
var chartTypes = Object.keys(CHART_TYPES); |
| 210 |
var realtime = 'checked'; |
| 211 |
var cache = ''; |
| 212 |
|
| 213 |
if (dashboardWidgets[widgetId]) { |
| 214 |
if (dashboardWidgets[widgetId].chartSql) { |
| 215 |
query = dashboardWidgets[widgetId].chartSql; |
| 216 |
} |
| 217 |
|
| 218 |
if (dashboardWidgets[widgetId].userChartTypeList) { |
| 219 |
chartTypesCurrent = dashboardWidgets[widgetId].userChartTypeList; |
| 220 |
for (var i=0; i<chartTypes.length; i++) { |
| 221 |
if (!chartTypesCurrent.includes(chartTypes[i])) { |
| 222 |
chartTypesCurrent.push(chartTypes[i]); |
| 223 |
} |
| 224 |
} |
| 225 |
chartTypes = chartTypesCurrent; |
| 226 |
} |
| 227 |
|
| 228 |
if (dashboardWidgets[widgetId].chartRefresh) { |
| 229 |
if (dashboardWidgets[widgetId].chartRefresh==='realtime') { |
| 230 |
realtime = 'checked'; |
| 231 |
cache = ''; |
| 232 |
} else { |
| 233 |
realtime = ''; |
| 234 |
cache = 'checked'; |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
if (jQuery("#wpda_widget_container_" + widgetId).is(":visible")) { |
| 240 |
jQuery("#wpda_widget_container_" + widgetId).hide(); |
| 241 |
setUserChartSelection(widgetId, "hide"); |
| 242 |
|
| 243 |
jQuery("#wpda_widget_container_" + widgetId).parent().append(` |
| 244 |
<div class="wpda-settings"> |
| 245 |
<div class="wpda-dashboard-chart-settings"> |
| 246 |
<fieldset class="wpda_fieldset"> |
| 247 |
<legend> |
| 248 |
SQL Query |
| 249 |
</legend> |
| 250 |
<div> |
| 251 |
<select id="wpda_chart_dbs_${widgetId}" class="wpda_chart_dbs"> |
| 252 |
${wpda_databases} |
| 253 |
</select> |
| 254 |
<button class="button wpda_insert_query_builder wpda_tooltip" title="Get SQL from Query Builder"><i class="fas fa-tools"></i> Query Builder</button> |
| 255 |
</div> |
| 256 |
<div> |
| 257 |
<textarea id="wpda_chart_sql_${widgetId}" class="wpda_chart_sql">${query}</textarea> |
| 258 |
</div> |
| 259 |
<div> |
| 260 |
<i class="fas fa-info-circle wpda_tooltip" style="font-size: initial; vertical-align: middle"></i> |
| 261 |
<span style="vertical-align: middle">Keep your data sets small. Use Data Tables for large data sets with server side processing.</span> |
| 262 |
</div> |
| 263 |
</fieldset> |
| 264 |
<fieldset class="wpda_fieldset"> |
| 265 |
<legend> |
| 266 |
Table or chart type |
| 267 |
</legend> |
| 268 |
<ul class="wpda_google_chart_types"> |
| 269 |
${chartTypeOptions(chartTypes, widgetId)} |
| 270 |
</ul> |
| 271 |
</fieldset> |
| 272 |
<fieldset class="wpda_fieldset" style="display:none" id="wpda_query_refresh_frequency_${widgetId}"> |
| 273 |
<legend> |
| 274 |
Query refresh frequency |
| 275 |
</legend> |
| 276 |
<div> |
| 277 |
<label> |
| 278 |
<input type="radio" id="wpda_refresh_frequency_${widgetId}" name="wpda_refresh_frequency_${widgetId}" value="realtime" ${realtime} /> |
| 279 |
Always show data in real time (does not cache query results) |
| 280 |
</label> |
| 281 |
<br/> |
| 282 |
<label> |
| 283 |
<input type="radio" name="wpda_refresh_frequency_${widgetId}" value="cache" ${cache} /> |
| 284 |
Update every |
| 285 |
<input type="number" id="wpda_refresh_frequency_cache_${widgetId}" value="24" style="vertical-align:baseline" /> |
| 286 |
<select id="wpda_refresh_frequency_unit_${widgetId}" style="vertical-align:baseline"> |
| 287 |
<option value="min">minutes</option> |
| 288 |
<option value="hours" selected>hours</option> |
| 289 |
<option value="days">days</option> |
| 290 |
</select> |
| 291 |
</label> |
| 292 |
</div> |
| 293 |
</fieldset> |
| 294 |
<div class="wpda-dashboard-chart-settings-buttons"> |
| 295 |
<button class="button button-primary wpda-button-ok"> |
| 296 |
<i class="fas fa-check wpda_icon_on_button"></i> |
| 297 |
OK |
| 298 |
</button> |
| 299 |
<button class="button wpda-button-cancel"> |
| 300 |
<i class="fas fa-times-circle wpda_icon_on_button"></i> |
| 301 |
Cancel |
| 302 |
</button> |
| 303 |
</div> |
| 304 |
</div> |
| 305 |
</div> |
| 306 |
`); |
| 307 |
|
| 308 |
if (wpda_chart_vars.wpda_premium==="true") { |
| 309 |
jQuery("#wpda_query_refresh_frequency_" + widgetId).show(); |
| 310 |
} |
| 311 |
|
| 312 |
if (dashboardWidgets[widgetId]) { |
| 313 |
if (dashboardWidgets[widgetId].chartDbs && dashboardWidgets[widgetId].chartDbs!==null) { |
| 314 |
jQuery("#wpda_chart_dbs_" + widgetId).val(dashboardWidgets[widgetId].chartDbs); |
| 315 |
} |
| 316 |
|
| 317 |
for (var i=0; i<dashboardWidgets[widgetId].chartType.length; i++) { |
| 318 |
jQuery("#" + dashboardWidgets[widgetId].chartType[i] + "_" + widgetId).prop("checked", true); |
| 319 |
} |
| 320 |
|
| 321 |
if (dashboardWidgets[widgetId].chartCache) { |
| 322 |
jQuery("#wpda_refresh_frequency_cache_" + widgetId).val(dashboardWidgets[widgetId].chartCache); |
| 323 |
} |
| 324 |
|
| 325 |
if (dashboardWidgets[widgetId].chartUnit) { |
| 326 |
jQuery("#wpda_refresh_frequency_unit_" + widgetId).val(dashboardWidgets[widgetId].chartUnit); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
jQuery("#wpda_widget_container_" + widgetId).parent().find(".wpda_insert_query_builder").on("click", function() { |
| 331 |
getSQLFromQueryBuilder(wpda_wpnonce_qb, widgetId); |
| 332 |
}) |
| 333 |
|
| 334 |
jQuery("#wpda_widget_container_" + widgetId).parent().find(".wpda-button-ok").on("click", function() { |
| 335 |
if (jQuery("#wpda_chart_sql_" + widgetId).val().trim()==='') { |
| 336 |
alert("Please enter a valid query"); |
| 337 |
return; |
| 338 |
} |
| 339 |
|
| 340 |
var obj = {}; |
| 341 |
obj.chartType = jQuery("#wpda-widget-" + widgetId + " ul.wpda_google_chart_types input[type='checkbox']:checked").map(function() { |
| 342 |
return jQuery(this).data("id"); |
| 343 |
}).get(); |
| 344 |
if (obj.chartType.length===0) { |
| 345 |
alert("You must select at least one table or chart type"); |
| 346 |
return; |
| 347 |
} |
| 348 |
|
| 349 |
obj.userChartTypeList = jQuery("#wpda-widget-" + widgetId + " ul.wpda_google_chart_types input[type='checkbox']").map(function() { |
| 350 |
return jQuery(this).data("id"); |
| 351 |
}).get(); |
| 352 |
obj.chartDbs = jQuery("#wpda_chart_dbs_" + widgetId).val(); |
| 353 |
obj.chartSql = jQuery("#wpda_chart_sql_" + widgetId).val(); |
| 354 |
if (jQuery("#wpda_refresh_frequency_" + widgetId).is(":checked")) { |
| 355 |
obj.chartRefresh = "realtime"; |
| 356 |
} else { |
| 357 |
obj.chartRefresh = "cache"; |
| 358 |
} |
| 359 |
obj.chartCache = jQuery("#wpda_refresh_frequency_cache_" + widgetId).val(); |
| 360 |
obj.chartUnit = jQuery("#wpda_refresh_frequency_unit_" + widgetId).val(); |
| 361 |
|
| 362 |
var share = null; |
| 363 |
if ( |
| 364 |
dashboardWidgets[widgetId]!==undefined && |
| 365 |
dashboardWidgets[widgetId].chartOptions!==undefined && |
| 366 |
dashboardWidgets[widgetId].chartOptions!==null |
| 367 |
) { |
| 368 |
obj.chartOptions = dashboardWidgets[widgetId].chartOptions; |
| 369 |
share = dashboardWidgets[widgetId].widgetShare; |
| 370 |
} |
| 371 |
|
| 372 |
jQuery("#wpda_widget_container_" + widgetId).empty(); // force update |
| 373 |
addDashboardWidget( |
| 374 |
widgetId, |
| 375 |
jQuery("#wpda-widget-" + widgetId).data("name"), |
| 376 |
"chart", |
| 377 |
share, |
| 378 |
obj |
| 379 |
); |
| 380 |
saveDashBoard(function() { getChartData(widgetId, true) }); |
| 381 |
|
| 382 |
jQuery("#wpda_widget_container_" + widgetId).show(); |
| 383 |
jQuery("#wpda_widget_container_" + widgetId).parent().find(".wpda-settings").remove(); |
| 384 |
}); |
| 385 |
|
| 386 |
jQuery("#wpda_widget_container_" + widgetId).parent().find(".wpda-button-cancel").on("click", function() { |
| 387 |
if (dashboardWidgets[widgetId] == undefined) { |
| 388 |
removePanelFromDashboardAction(jQuery(this).closest('.wpda-widget')); |
| 389 |
} else { |
| 390 |
jQuery("#wpda_widget_container_" + widgetId).parent().find(".wpda-settings").remove(); |
| 391 |
jQuery("#wpda_widget_container_" + widgetId).show(); |
| 392 |
setUserChartSelection(widgetId); |
| 393 |
} |
| 394 |
}); |
| 395 |
|
| 396 |
jQuery(".wpda_google_chart_types").sortable({ |
| 397 |
connectWith: ".wpda_google_chart_types", |
| 398 |
cursor: "move", |
| 399 |
opacity: 0.4, |
| 400 |
change: function(event, ui) { |
| 401 |
ui.placeholder.css({visibility: "visible", background : "#ccc"}); |
| 402 |
} |
| 403 |
}); |
| 404 |
|
| 405 |
jQuery(".wpda_tooltip").tooltip({ |
| 406 |
tooltipClass: "wpda_tooltip_dashboard", |
| 407 |
}); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
function chartLayout(widgetId) { |
| 412 |
var widgetName = ""; |
| 413 |
var chartTitle = ""; |
| 414 |
var charLineNo = CHART_ADD_LINENO; |
| 415 |
var chartLegend = CHART_DEFAULT_LEGEND; |
| 416 |
var chartWidth = CHART_DEFAULT_WIDTH; |
| 417 |
var chartWidthPx = CHART_DEFAULT_WIDTH_PX; |
| 418 |
var chartHeight = CHART_DEFAULT_HEIGHT; |
| 419 |
var chartHeightPx = CHART_DEFAULT_HEIGHT_PX; |
| 420 |
var chartArea = ""; |
| 421 |
var chartAreaWidth = CHARTAREA_DEFAULT_WIDTH; |
| 422 |
var chartAreaHeight = CHARTAREA_DEFAULT_HEIGHT; |
| 423 |
var chartAreaTop = CHARTAREA_DEFAULT_TOP; |
| 424 |
var chartAreaLeft = CHARTAREA_DEFAULT_LEFT; |
| 425 |
var chartAdvancedOptions = ""; |
| 426 |
|
| 427 |
if (dashboardWidgets[widgetId]!==undefined) { |
| 428 |
if (dashboardWidgets[widgetId].widgetName!==undefined) { |
| 429 |
widgetName = dashboardWidgets[widgetId].widgetName; |
| 430 |
} |
| 431 |
if ( |
| 432 |
dashboardWidgets[widgetId].chartOptions!==undefined && |
| 433 |
dashboardWidgets[widgetId].chartOptions!==null |
| 434 |
) { |
| 435 |
if (dashboardWidgets[widgetId].chartOptions.title!==undefined) { |
| 436 |
chartTitle = dashboardWidgets[widgetId].chartOptions.title; |
| 437 |
} |
| 438 |
if (dashboardWidgets[widgetId].chartOptions.showRowNumber!==undefined) { |
| 439 |
if ( |
| 440 |
dashboardWidgets[widgetId].chartOptions.showRowNumber==="true" || |
| 441 |
dashboardWidgets[widgetId].chartOptions.showRowNumber===true |
| 442 |
) { |
| 443 |
charLineNo = "Yes"; |
| 444 |
} else { |
| 445 |
charLineNo = "No"; |
| 446 |
} |
| 447 |
} |
| 448 |
if ( |
| 449 |
dashboardWidgets[widgetId].chartOptions.legend!==undefined && |
| 450 |
dashboardWidgets[widgetId].chartOptions.legend.position!==undefined |
| 451 |
) { |
| 452 |
chartLegend = dashboardWidgets[widgetId].chartOptions.legend.position; |
| 453 |
} |
| 454 |
if (dashboardWidgets[widgetId].chartOptions.width!==undefined) { |
| 455 |
chartWidth = dashboardWidgets[widgetId].chartOptions.width; |
| 456 |
if (chartWidth!=="*") { |
| 457 |
chartWidthPx = dashboardWidgets[widgetId].chartOptions.width; |
| 458 |
} |
| 459 |
} |
| 460 |
if (dashboardWidgets[widgetId].chartOptions.height!==undefined) { |
| 461 |
chartHeight = dashboardWidgets[widgetId].chartOptions.height; |
| 462 |
if (chartHeight!=="*") { |
| 463 |
chartHeightPx = dashboardWidgets[widgetId].chartOptions.height; |
| 464 |
} |
| 465 |
} |
| 466 |
if (dashboardWidgets[widgetId].chartOptions.chartArea!==undefined) { |
| 467 |
chartArea = "checked"; |
| 468 |
chartAreaWidth = dashboardWidgets[widgetId].chartOptions.chartArea.width.replace("%",""); |
| 469 |
chartAreaHeight = dashboardWidgets[widgetId].chartOptions.chartArea.height.replace("%",""); |
| 470 |
chartAreaTop = dashboardWidgets[widgetId].chartOptions.chartArea.top; |
| 471 |
chartAreaLeft = dashboardWidgets[widgetId].chartOptions.chartArea.left; |
| 472 |
} |
| 473 |
if (dashboardWidgets[widgetId].chartOptions.advancedOptions!==undefined) { |
| 474 |
chartAdvancedOptions = dashboardWidgets[widgetId].chartOptions.advancedOptions; |
| 475 |
} |
| 476 |
} |
| 477 |
} else { |
| 478 |
// Widget name not yet available on insert: grab title from widget header |
| 479 |
widgetName = jQuery("#wpda-widget-" + widgetId).data("name"); |
| 480 |
} |
| 481 |
|
| 482 |
var lineNoYes = charLineNo==="Yes" ? "checked" : ""; |
| 483 |
var lineNoNo = charLineNo!=="Yes" ? "checked" : ""; |
| 484 |
|
| 485 |
var fullWidth = chartWidth==="*" ? "checked" : ""; |
| 486 |
var customWidth = ! fullWidth ? "checked" : ""; |
| 487 |
|
| 488 |
var fullHeight = chartHeight==="*" ? "checked" : ""; |
| 489 |
var customHeight = ! fullHeight ? "checked" : ""; |
| 490 |
|
| 491 |
var dialogHtml = ` |
| 492 |
<div class="wpda-dailog-layout"> |
| 493 |
<fieldset class="wpda_fieldset"> |
| 494 |
<legend> |
| 495 |
Widget |
| 496 |
</legend> |
| 497 |
<div> |
| 498 |
<label htmlFor="wpda_chart_title_${widgetId}"> |
| 499 |
Title |
| 500 |
</label> |
| 501 |
<input type="text" |
| 502 |
id="wpda_chart_title_${widgetId}" |
| 503 |
value="${chartTitle}" |
| 504 |
placeholder="A title is optional..." |
| 505 |
/> |
| 506 |
</div> |
| 507 |
<div> |
| 508 |
<label for="wpda_chart_legend_${widgetId}"> |
| 509 |
Legend |
| 510 |
</label> |
| 511 |
<select id="wpda_chart_legend_${widgetId}"> |
| 512 |
<option value="top">top</option> |
| 513 |
<option value="bottom">bottom</option> |
| 514 |
<option value="right">right</option> |
| 515 |
<option value="left">left</option> |
| 516 |
<option value="none">none</option> |
| 517 |
</select> |
| 518 |
</div> |
| 519 |
<div> |
| 520 |
<label for="wpda_chart_lineno_${widgetId}"> |
| 521 |
Add row nr |
| 522 |
</label> |
| 523 |
<label> |
| 524 |
<input type="radio" |
| 525 |
value="yes" |
| 526 |
id="wpda_chart_lineno_yes_${widgetId}" |
| 527 |
name="wpda_chart_lineno_${widgetId}" |
| 528 |
${lineNoYes} |
| 529 |
/> Yes |
| 530 |
</label> |
| 531 |
<label> |
| 532 |
<input type="radio" |
| 533 |
value="no" |
| 534 |
name="wpda_chart_lineno_${widgetId}" |
| 535 |
${lineNoNo} |
| 536 |
/> No |
| 537 |
</label> |
| 538 |
<span style="float: right">(tables only)</span> |
| 539 |
</div> |
| 540 |
</fieldset> |
| 541 |
<br/> |
| 542 |
<fieldset class="wpda_fieldset"> |
| 543 |
<legend> |
| 544 |
<span onclick="toggleOption(jQuery(this))"> |
| 545 |
<i class="wpda-fieldset-expand fas fa-plus-circle wpda_icon_on_button"></i> |
| 546 |
Widget size |
| 547 |
</span> |
| 548 |
</legend> |
| 549 |
<div style="display: none"> |
| 550 |
<label htmlFor="wpda_chart_width_${widgetId}"> |
| 551 |
Width |
| 552 |
</label> |
| 553 |
<input type="radio" |
| 554 |
value="*" |
| 555 |
id="wpda_chart_width_select_${widgetId}" |
| 556 |
name="wpda_chart_width_select_${widgetId}" |
| 557 |
${fullWidth} |
| 558 |
/> |
| 559 |
<input type="text" |
| 560 |
value="Fit to container (100%)" |
| 561 |
readOnly |
| 562 |
/> |
| 563 |
</div> |
| 564 |
<div style="display: none"> |
| 565 |
<label></label> |
| 566 |
<input type="radio" value="val" name="wpda_chart_width_select_${widgetId}" ${customWidth} /> |
| 567 |
<input type="number" id="wpda_chart_width_${widgetId}" value="${chartWidthPx}"/> px |
| 568 |
</div> |
| 569 |
<div style="display: none"> |
| 570 |
<label htmlFor="wpda_chart_height_${widgetId}"> |
| 571 |
Height |
| 572 |
</label> |
| 573 |
<input type="radio" |
| 574 |
value="*" |
| 575 |
id="wpda_chart_height_select_${widgetId}" |
| 576 |
name="wpda_chart_height_select_${widgetId}" |
| 577 |
${fullHeight} |
| 578 |
/> |
| 579 |
<input type="text" |
| 580 |
value="Fit to container (100%)" |
| 581 |
readOnly |
| 582 |
/> |
| 583 |
</div> |
| 584 |
<div style="display: none"> |
| 585 |
<label></label> |
| 586 |
<input type="radio" value="val" name="wpda_chart_height_select_${widgetId}" ${customHeight} /> |
| 587 |
<input type="number" id="wpda_chart_height_${widgetId}" value="${chartHeightPx}"/> px |
| 588 |
</div> |
| 589 |
</fieldset> |
| 590 |
<br/> |
| 591 |
<fieldset class="wpda_fieldset"> |
| 592 |
<legend> |
| 593 |
<span onclick="toggleOption(jQuery(this))"> |
| 594 |
<i class="wpda-fieldset-expand fas fa-plus-circle wpda_icon_on_button"></i> |
| 595 |
Widget size |
| 596 |
</span> |
| 597 |
</legend> |
| 598 |
<div style="display: none"> |
| 599 |
<label></label> |
| 600 |
<input type="checkbox" id="wpda_chartarea_${widgetId}" ${chartArea} /> Chart area (uncheck to use defaults) |
| 601 |
</div> |
| 602 |
<div style="display: none" style="height: 10px"> |
| 603 |
</div> |
| 604 |
<div style="display: none"> |
| 605 |
<label for="wpda_chartarea_width_${widgetId}"> |
| 606 |
Width |
| 607 |
</label> |
| 608 |
<input id="wpda_chartarea_width_${widgetId}" type="number" min="0" max="100" value="${chartAreaWidth}" /> % |
| 609 |
</div> |
| 610 |
<div style="display: none"> |
| 611 |
<label for="wpda_chartarea_height_${widgetId}"> |
| 612 |
Height |
| 613 |
</label> |
| 614 |
<input id="wpda_chartarea_height_${widgetId}" type="number" min="0" max="100" value="${chartAreaHeight}" /> % |
| 615 |
</div> |
| 616 |
<div style="display: none"> |
| 617 |
<label for="wpda_chartarea_top_${widgetId}"> |
| 618 |
Top |
| 619 |
</label> |
| 620 |
<input id="wpda_chartarea_top_${widgetId}" type="number" min="0" max="100" value="${chartAreaTop}" /> px |
| 621 |
</div> |
| 622 |
<div style="display: none"> |
| 623 |
<label for="wpda_chartarea_left_${widgetId}"> |
| 624 |
Left |
| 625 |
</label> |
| 626 |
<input id="wpda_chartarea_left_${widgetId}" type="number" min="0" max="100" value="${chartAreaLeft}" /> px |
| 627 |
</div> |
| 628 |
</fieldset> |
| 629 |
<br/> |
| 630 |
<fieldset class="wpda_fieldset"> |
| 631 |
<legend> |
| 632 |
<span onclick="toggleOption(jQuery(this))"> |
| 633 |
<i class="wpda-fieldset-expand fas fa-plus-circle wpda_icon_on_button"></i> |
| 634 |
Advanced chart options |
| 635 |
</span> |
| 636 |
</legend> |
| 637 |
<div style="display: none"> |
| 638 |
<div style="display: none"> |
| 639 |
<span style="display: inline-flex; flex-direction: row; justify-content: space-between; width: 100%; align-items: center;"> |
| 640 |
<span> |
| 641 |
Must be valid JSON |
| 642 |
</span> |
| 643 |
<span style="float: right"> |
| 644 |
<button type="button" class="ui-button" onclick="checkAdvancedOptions('${widgetId}')">Check JSON validity</button> |
| 645 |
</span> |
| 646 |
</span> |
| 647 |
</div> |
| 648 |
<textarea id="wpda_chart_advanced_options_${widgetId}">${chartAdvancedOptions}</textarea> |
| 649 |
<div style="display: none"> |
| 650 |
Add chart options in JSON format |
| 651 |
<a href="https://docs.legacy.wpdataaccess.com/docs/chart-widgets/#advanced-chart-options" target="_blank"> |
| 652 |
(read more...) |
| 653 |
</a> |
| 654 |
</div> |
| 655 |
</div> |
| 656 |
</fieldset> |
| 657 |
</div> |
| 658 |
`; |
| 659 |
|
| 660 |
var dialog = jQuery("<div/>").html(dialogHtml).dialog({ |
| 661 |
title: "Widget layout: " + widgetName, |
| 662 |
width: "max-content", |
| 663 |
modal: true, |
| 664 |
buttons: { |
| 665 |
"OK": function() { |
| 666 |
saveOptions(widgetId); |
| 667 |
refreshChart(widgetId); |
| 668 |
dialog.dialog("destroy"); |
| 669 |
}, |
| 670 |
"Apply": function() { |
| 671 |
saveOptions(widgetId); |
| 672 |
refreshChart(widgetId); |
| 673 |
}, |
| 674 |
"Cancel": function() { |
| 675 |
dialog.dialog("destroy"); |
| 676 |
} |
| 677 |
} |
| 678 |
}); |
| 679 |
|
| 680 |
wpda_add_icons_to_dialog_buttons(); |
| 681 |
|
| 682 |
jQuery('#wpda_chart_legend_' + widgetId + ' option[value="' + chartLegend + '"]').prop("selected", true); |
| 683 |
} |
| 684 |
|
| 685 |
function checkAdvancedOptions(widgetId) { |
| 686 |
let advancedOptions = jQuery("#wpda_chart_advanced_options_" + widgetId).val(); |
| 687 |
try { |
| 688 |
JSON.parse(advancedOptions.replace(/ {4}|[\t\n\r]/gm,'')); |
| 689 |
alert("This looks like valid JSON!\nChart options are not checked."); |
| 690 |
} catch (e) { |
| 691 |
if (advancedOptions.trim()!=="") { |
| 692 |
console.log("WP Data Access ERROR:"); |
| 693 |
console.log(e); |
| 694 |
alert(e); |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
function toggleOption(elem) { |
| 700 |
if (elem.find("i").hasClass("fa-plus-circle")) { |
| 701 |
elem.find("i").removeClass("fa-plus-circle"); |
| 702 |
elem.find("i").addClass("fa-minus-circle"); |
| 703 |
} else { |
| 704 |
elem.find("i").removeClass("fa-minus-circle"); |
| 705 |
elem.find("i").addClass("fa-plus-circle"); |
| 706 |
} |
| 707 |
|
| 708 |
elem.closest("fieldset").find("div").toggle(); |
| 709 |
} |
| 710 |
|
| 711 |
function saveOptions(widgetId) { |
| 712 |
var options = {}; |
| 713 |
|
| 714 |
options.title = jQuery("#wpda_chart_title_" + widgetId).val(); |
| 715 |
options.legend = {}; |
| 716 |
options.legend.position = jQuery("#wpda_chart_legend_" + widgetId).val(); |
| 717 |
if (jQuery("#wpda_chart_width_select_" + widgetId).is(":checked")) { |
| 718 |
options.width = "*"; |
| 719 |
} else { |
| 720 |
options.width = jQuery("#wpda_chart_width_" + widgetId).val(); |
| 721 |
} |
| 722 |
if (jQuery("#wpda_chart_height_select_" + widgetId).is(":checked")) { |
| 723 |
options.height = "*"; |
| 724 |
} else { |
| 725 |
options.height = jQuery("#wpda_chart_height_" + widgetId).val(); |
| 726 |
} |
| 727 |
|
| 728 |
if (jQuery("#wpda_chartarea_" + widgetId).is(":checked")) { |
| 729 |
options.chartArea = {}; |
| 730 |
options.chartArea.width = jQuery("#wpda_chartarea_width_" + widgetId).val() + "%"; |
| 731 |
options.chartArea.height = jQuery("#wpda_chartarea_height_" + widgetId).val() + "%"; |
| 732 |
options.chartArea.top = jQuery("#wpda_chartarea_top_" + widgetId).val(); |
| 733 |
options.chartArea.left = jQuery("#wpda_chartarea_left_" + widgetId).val(); |
| 734 |
} |
| 735 |
|
| 736 |
options.advancedOptions = jQuery("#wpda_chart_advanced_options_" + widgetId).val(); |
| 737 |
if (options.advancedOptions.trim()!=="") { |
| 738 |
try { |
| 739 |
options = Object.assign( |
| 740 |
options, |
| 741 |
JSON.parse(options.advancedOptions.replace(/ {4}|[\t\n\r]/gm,'')) |
| 742 |
); |
| 743 |
} catch (e) { |
| 744 |
console.log("WP Data Access ERROR:"); |
| 745 |
console.log(e); |
| 746 |
alert(e); |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
options.showRowNumber = jQuery("#wpda_chart_lineno_yes_" + widgetId).is(":checked"); |
| 751 |
|
| 752 |
dashboardWidgets[widgetId].chartOptions = options; |
| 753 |
saveDashBoard(); |
| 754 |
} |
| 755 |
|
| 756 |
jQuery(function() { |
| 757 |
google.charts.load( |
| 758 |
"current", { |
| 759 |
"packages": [ |
| 760 |
"table", |
| 761 |
"corechart", |
| 762 |
"gauge" |
| 763 |
] |
| 764 |
} |
| 765 |
); |
| 766 |
|
| 767 |
google.charts.setOnLoadCallback(function() { |
| 768 |
googleChartsLoaded = true; |
| 769 |
}); |
| 770 |
}); |
| 771 |
|