// ComboChart // DonutChart 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"; 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"; const CHART_TYPES = { "BarChart": { label: "Bar Chart", hint: CHART_DEFAULT_HINT_1PN }, "ColumnChart": { label: "Column Chart", hint: CHART_DEFAULT_HINT_1PN }, "Histogram": { label: "Histogram", hint: CHART_DEFAULT_HINT_1PN }, "LineChart": { label: "Line Chart", hint: CHART_DEFAULT_HINT_1PN }, "PieChart": { label: "Pie Chart", hint: CHART_DEFAULT_HINT_1 }, "Gauge": { label: "Gauge", hint: CHART_DEFAULT_HINT_1 }, "Table": { label: "Table", hint: "FORMAT\n\nAny valid query\n\nEXAMPLES\n\nselect * from customers\nselect ename as 'Employee Name', sal as 'Salary' from emp" } }; const CHART_DEFAULT_LEGEND = 'bottom'; const CHART_ADD_LINENO = "Yes"; const CHART_DEFAULT_WIDTH = "*"; const CHART_DEFAULT_WIDTH_PX = 500; const CHART_DEFAULT_HEIGHT = "*"; const CHART_DEFAULT_HEIGHT_PX = 300; const CHARTAREA_DEFAULT_WIDTH = 100; const CHARTAREA_DEFAULT_HEIGHT = 100; const CHARTAREA_DEFAULT_TOP = 10; const CHARTAREA_DEFAULT_LEFT = 100; var googleChartsLoaded = false; var googleChartsObjects = {}; var cachedChartData = {}; function getChartData(widgetId, forceUpdate = false) { wpda_dbs = dashboardWidgets[widgetId].chartDbs; wpda_query = dashboardWidgets[widgetId].chartSql; jQuery.ajax({ type: "POST", url: wpda_chart_vars.wpda_ajaxurl + "?action=wpda_widget_chart_refresh", data: { wp_nonce: wpda_wpnonce_refresh, wpda_action: 'get_data', wpda_name: dashboardWidgets[widgetId].widgetName, wpda_force_update: forceUpdate } }).done( function(data) { if (data.status==="ERROR" && data.msg!==undefined) { alert("ERROR: " + data.msg); } else { if (data.error !== "") { alert("ERROR: " + data.error); } else { if (jQuery("#wpda_widget_container_" + widgetId).html() === "") { setUserChartSelection(widgetId); jQuery("#wpda_widget_chart_selection_" + widgetId + " option").remove(); jQuery.each(dashboardWidgets[widgetId].chartType, function (i, item) { jQuery("#wpda_widget_chart_selection_" + widgetId).append(jQuery("", { value: item, text: CHART_TYPES[item].label })); }); if (dashboardWidgets[widgetId].chartType.length > 1) { chartType = dashboardWidgets[widgetId].chartType[0]; } else { chartType = dashboardWidgets[widgetId].chartType[0]; } createChart( chartType, widgetId, data.cols, data.rows ); } else { refreshChart(widgetId); } } } } ); } function chartOptions(widgetId) { if ( dashboardWidgets[widgetId]!==undefined && dashboardWidgets[widgetId].chartOptions!==undefined && dashboardWidgets[widgetId].chartOptions!==null ) { dashboardWidgets[widgetId].chartOptions.page = 'enable'; return dashboardWidgets[widgetId].chartOptions; } else { return { legend: { position: CHART_DEFAULT_LEGEND }, width: CHART_DEFAULT_WIDTH, height: CHART_DEFAULT_HEIGHT, page: 'enable' }; } } function createChart(outputType, widgetId, columns, rows) { cachedChartData[widgetId] = new google.visualization.DataTable({ cols: columns, rows: rows }); addChart(widgetId, outputType); } function refreshChart(widgetId) { jQuery("#wpda_widget_container_" + widgetId).empty(); addChart(widgetId, jQuery("#wpda_widget_chart_selection_" + widgetId).val()); } function printableVersion(url){ let win = window.open(); win.document.write(''); } function addChart(widgetId, outputType) { var element = document.getElementById("wpda_widget_container_" + widgetId); googleChartsObjects[widgetId] = new google.visualization[outputType](element); google.visualization.events.addListener(googleChartsObjects[widgetId], 'ready', function () { jQuery("#wpda-widget-" + widgetId + " .wpda-chart-button-print").hide().off(); if (outputType!=="Table") { // Enable print button for charts jQuery("#wpda-widget-" + widgetId + " .wpda-chart-button-print").show().on("click", function() { printableVersion(googleChartsObjects[widgetId].getImageURI()); }); } // Create hyperlink with CSV from table data let csv = google.visualization.dataTableToCsv(cachedChartData[widgetId]); let url = "data:application/csv;charset=utf-8," + encodeURIComponent(csv); jQuery("#wpda-widget-" + widgetId + " .wpda-chart-button-export-link") .attr("href", url) .attr("download", "wp-data-access.csv"); // Open hyperlink on click jQuery("#wpda-widget-" + widgetId + " .wpda-chart-button-export").on("click", function() { jQuery("#wpda-widget-" + widgetId + " .wpda-chart-button-export-link")[0].click(); }); }); googleChartsObjects[widgetId].draw(cachedChartData[widgetId], chartOptions(widgetId)); } function setUserChartSelection(widgetId, action = null) { if (action==="hide") { // Hide in settings mode jQuery("#wpda_widget_container_" + widgetId).parent().find(".wpda_widget_chart_selection").hide(); } else { // Show in view mode jQuery("#wpda_widget_container_" + widgetId).parent().find(".wpda_widget_chart_selection").show(); } // Show drop down list only if it contains more than 1 option if (dashboardWidgets[widgetId] && dashboardWidgets[widgetId].chartType.length > 1) { jQuery("#wpda_widget_chart_selection_" + widgetId).show(); } else { jQuery("#wpda_widget_chart_selection_" + widgetId).hide(); } } function chartTypeOption(id, widgetId) { return `