'use strict';
/**
* todo: chart height as option
*
*/
var $ = require('jquery'),
utils = require('./utils.js'),
yUtils = require('yasgui-utils');
var root = module.exports = function(yasr) {
var options = $.extend(true, {}, root.defaults);
var id = yasr.container.closest('[id]').attr('id');
var chartWrapper = null;
var editor = null;
var initEditor = function(callback) {
var google = require('google');
editor = new google.visualization.ChartEditor();
google.visualization.events.addListener(editor, 'ok', function() {
var tmp;
chartWrapper = editor.getChartWrapper();
tmp = chartWrapper.getDataTable();
chartWrapper.setDataTable(null);
//ugly: need to parse json string to json obj again, as google chart does not provide access to object directly
options.chartConfig = JSON.parse(chartWrapper.toJSON());
//remove container ID though, for portability
if (options.chartConfig.containerId) delete options.chartConfig['containerId'];
yasr.store();
chartWrapper.setDataTable(tmp);
chartWrapper.setOption("width", options.width);
chartWrapper.setOption("height", options.height);
chartWrapper.draw();
yasr.updateHeader();
});
if (callback) callback();
};
return {
name: "Google Chart",
hideFromSelection: false,
priority: 7,
options: options,
getPersistentSettings: function() {
return {
chartConfig: options.chartConfig,
motionChartState: options.motionChartState
}
},
setPersistentSettings: function(persSettings) {
if (persSettings['chartConfig']) options.chartConfig = persSettings['chartConfig'];
if (persSettings['motionChartState']) options.motionChartState = persSettings['motionChartState'];
},
canHandleResults: function(yasr) {
var results, variables;
return (results = yasr.results) != null && (variables = results.getVariables()) && variables.length > 0;
},
getDownloadInfo: function() {
if (!yasr.results) return null;
var svgEl = yasr.resultsContainer.find('svg');
if (svgEl.length > 0) {
return {
getContent: function() {
if (svgEl[0].outerHTML) {
return svgEl[0].outerHTML;
} else {
//outerHTML not supported. use workaround
return $('
').append(svgEl.clone()).html();
}
},
filename: "queryResults.svg",
contentType: "image/svg+xml",
buttonTitle: "Download SVG Image"
};
}
//ok, not a svg. is it a table?
var $table = yasr.resultsContainer.find('.google-visualization-table-table');
if ($table.length > 0) {
return {
getContent: function() {
return $table.tableToCsv();
},
filename: "queryResults.csv",
contentType: "text/csv",
buttonTitle: "Download as CSV"
};
}
},
getEmbedHtml: function() {
if (!yasr.results) return null;
var svgEl = yasr.resultsContainer.find('svg')
.clone() //create clone, as we'd like to remove height/width attributes
.removeAttr('height').removeAttr('width')
.css('height', '').css('width', '');
if (svgEl.length == 0) return null;
var htmlString = svgEl[0].outerHTML;
if (!htmlString) {
//outerHTML not supported. use workaround
htmlString = $('
').append(svgEl.clone()).html();
}
//wrap in div, so users can more easily tune width/height
//don't use jquery, so we can easily influence indentation
return '
\n' + htmlString + '\n
';
},
draw: function() {
var doDraw = function() {
//clear previous results (if any)
yasr.resultsContainer.empty();
var wrapperId = id + '_gchartWrapper';
yasr.resultsContainer.append(
$('