/*
* Copyright 2012 OSBI Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Renders a chart for each workspace
*/
var Chart = Backbone.View.extend({
events: {
'click .keep' : 'keepVisible'
},
cccOptions: {
type: "BarChart",
stacked: true
},
data: null,
hasProcessed: null,
getChartProperties: function(chartName) {
//var self = this;
var ret = [];
_.each(ChartProperties, function(property) {
if (property.ChartObject == chartName) {
ret.push(property);
}
});
return ret;
},
initialize: function(args) {
this.workspace = args.workspace;
// Create a unique ID for use as the CSS selector
this.id = _.uniqueId("chart_");
$(this.el).attr({ id: this.id });
//$('
' + " " + '
').appendTo($(this.el));
$('').appendTo($(this.el));
this.cccOptions.canvas = 'canvas_' + this.id;
this.cccOptions = this.getQuickOptions(this.cccOptions);
this.data = null;
// Bind table rendering to query result event
_.bindAll(this, "receive_data", "process_data", "show", "getData", "render_view", "render_chart", "render_chart_delayed", "getQuickOptions","exportChart","block_ui");
var self = this;
this.workspace.bind('query:run', function() {
if (! $(self.workspace.querytoolbar.el).find('.render_chart').hasClass('on')) {
return false;
}
self.data = {};
self.data.resultset = [];
self.data.metadata = [];
$(self.el).find('.canvas_wrapper').hide();
return false;
});
this.workspace.bind('query:fetch', this.block_ui);
this.workspace.bind('query:result', this.receive_data);
var pseudoForm = "
";
if (isIE) {
pseudoForm = "
";
}
this.nav =$(pseudoForm);
$(this.el).append(this.nav);
/*
// Create navigation
var exportoptions = "Export to: " +
"
PNG , " +
"
PDF , " +
//"
TIFF , " +
"
SVG , " +
"
JPG " +
"
";
var chartnav = exportoptions + "
";
if (typeof isIE !== "undefined") {
chartnav = "
";
}
// Create navigation
this.nav = $(chartnav).css({
'padding-bottom': '10px'
});
this.nav.find('a.type').css({
color: '#666',
'margin-right': '5px',
'text-decoration': 'none',
'border': '1px solid #ccc',
padding: '5px'
});
this.nav.find('a.export').click(this.exportChart);
this.nav.find('a').css({
color: '#666',
'margin-right': '5px',
'text-decoration': 'none',
'border': '1px solid #ccc',
padding: '5px'
});
*/
/* XXX - enable again later
$(this.nav).append('');
this.editor = new ChartEditor({ workspace : this.workspace,
ChartProperties : ChartProperties,
ChartTypes: ChartTypes,
data : this.getData,
getChartProperties : this.getChartProperties});
$(this.nav).find('.chart_editor').append($(this.editor.el));
*/
},
block_ui: function() {
if (! $(this.workspace.querytoolbar.el).find('.render_chart').hasClass('on')) {
return;
}
//Saiku.ui.block("Updating chart data....");
},
exportChart: function(type) {
var svgContent = new XMLSerializer().serializeToString($('svg')[0]);
var rep = ' 5) {
if(options.type === "HeatGridChart") {
options.xAxisSize = 150;
} else if(options.orientation !== "horizontal") {
options.extensionPoints = _.extend(Object.create(options.extensionPoints || {}),
{
xAxisLabel_textAngle: -Math.PI/2,
xAxisLabel_textAlign: "right",
xAxisLabel_textBaseline: "middle"
});
}
}
return options;
},
render_chart: function() {
_.delay(this.render_chart_delayed, 0, this);
return false;
},
render_chart_delayed: function() {
if (!$(this.workspace.querytoolbar.el).find('.render_chart').hasClass('on') || !this.hasProcessed) {
return;
}
$(this.workspace.toolbar.el).find('.i3GeoMap').removeClass('on');
/* DEBUGGING
this.med = new Date().getTime();
$(this.el).prepend(" | chart render (" + (this.med - this.call_time) + ")" );
this.call_time = undefined;
*/
var workspaceResults = $(this.workspace.el).find(".workspace_results");
var isSmall = (this.data != null && this.data.height < 80 && this.data.width < 80);
var isMedium = (this.data != null && this.data.height < 300 && this.data.width < 300);
var isBig = (!isSmall && !isMedium);
var animate = false;
var hoverable = isSmall;
var runtimeChartDefinition = _.clone(this.cccOptions);
if (isBig) {
if (runtimeChartDefinition.hasOwnProperty('extensionPoints') && runtimeChartDefinition.extensionPoints.hasOwnProperty('line_interpolate'))
delete runtimeChartDefinition.extensionPoints.line_interpolate;
if (runtimeChartDefinition.hasOwnProperty('extensionPoints') && runtimeChartDefinition.extensionPoints.hasOwnProperty('area_interpolate'))
delete runtimeChartDefinition.extensionPoints.area_interpolate;
}
runtimeChartDefinition = _.extend(runtimeChartDefinition, {
width: workspaceResults.width() - 40,
height: workspaceResults.height() - 40,
hoverable: hoverable,
animate: animate
});
/* XXX - enable later
var start = new Date().getTime();
this.editor.chartDefinition = _.clone(this.cccOptions);
this.editor.set_chart("pvc." + this.cccOptions.type);
this.editor.render_chart_properties("pvc." + this.cccOptions.type, this.editor.chartDefinition);
*/
this.chart = new pvc[runtimeChartDefinition.type](runtimeChartDefinition);
/* DEBUGGING
this.med3 = new Date().getTime();
$(this.el).prepend(" pvc (" + (this.med3 - this.med) + ")" );
*/
this.chart.setData(this.data, {
crosstabMode: true,
seriesInRows: false
});
try {
if (animate) {
$(this.el).find('.canvas_wrapper').show();
}
this.chart.render();
/* DEBUGGING
var med2 = new Date().getTime();
$(this.el).prepend(" done (" + (med2 - this.med) + ")" );
*/
} catch (e) {
$(this.el).text("Could not render chart");
}
this.workspace.processing.hide();
this.workspace.adjust();
if (animate) {
return false;
}
// $('#nav_' + this.id).show();
if (isIE || isBig) {
$(this.el).find('.canvas_wrapper').show();
} else {
$(this.el).find('.canvas_wrapper').fadeIn(400);
}
return false;
},
receive_data: function(args) {
if (! $(this.workspace.querytoolbar.el).find('.render_chart').hasClass('on')) {
return;
}
return _.delay(this.process_data, 0, args);
},
process_data: function(args) {
this.data = {};
this.data.resultset = [];
this.data.metadata = [];
this.data.height = 0;
this.data.width = 0;
if (typeof args == "undefined" || typeof args.data == "undefined" ||
($(this.workspace.el).is(':visible') && !$(this.el).is(':visible'))) {
return;
}
if (args.data != null && args.data.error != null) {
return this.workspace.error(args);
}
// Check to see if there is data
if (args.data == null || (args.data.cellset && args.data.cellset.length === 0)) {
return this.workspace.no_results(args);
}
var cellset = args.data.cellset;
if (cellset && cellset.length > 0) {
/* DEBUGGING
var start = new Date().getTime();
this.call_time = start;
$(this.el).prepend(" | chart process");
*/
var lowest_level = 0;
var data_start = 0;
for (var row = 0; data_start == 0 && row < cellset.length; row++) {
this.data.metadata = [];
for (var field = 0; field < cellset[row].length; field++) {
var firstHeader = [];
while (cellset[row][field].type == "COLUMN_HEADER" && cellset[row][field].value == "null") {
row++;
}
if (cellset[row][field].type == "ROW_HEADER_HEADER") {
while(cellset[row][field].type == "ROW_HEADER_HEADER") {
firstHeader.push(cellset[row][field].value);
field++;
}
this.data.metadata.push({
colIndex: 0,
colType: "String",
colName: converteHtmlEntities(firstHeader.join('/'))
});
lowest_level = field - 1;
}
if (cellset[row][field].type == "COLUMN_HEADER" && cellset[row][field].value != "null") {
var lowest_col_header = 0;
var colheader = [];
while(lowest_col_header <= row) {
colheader.push(cellset[lowest_col_header][field].value);
lowest_col_header++;
}
this.data.metadata.push({
colIndex: field - lowest_level + 1,
colType: "Numeric",
colName: converteHtmlEntities(colheader.join('/'))
});
data_start = row+1;
}
}
}
var labelsSet = {};
for (var row = data_start; row < cellset.length; row++) {
if (cellset[row][0].value !== "") {
var record = [];
this.data.width = cellset[row].length - lowest_level + 1;
var label = "";
for (var labelCol = lowest_level; labelCol >= 0; labelCol--) {
var lastKnownUpperLevelRow = row;
while(cellset[lastKnownUpperLevelRow] && cellset[lastKnownUpperLevelRow][labelCol].value === 'null') {
--lastKnownUpperLevelRow;
}
if(cellset[lastKnownUpperLevelRow]) {
if (label == "") {
label = cellset[lastKnownUpperLevelRow][labelCol].value;
} else {
label = cellset[lastKnownUpperLevelRow][labelCol].value + " / " + label;
}
}
}
if(label in labelsSet) {
labelsSet[label] = labelsSet[label]+1;
label = label + ' [' + (labelsSet[label] + 1) + ']';
} else {
labelsSet[label] = 0;
}
record.push(label);
for (var col = lowest_level + 1; col < cellset[row].length; col++) {
var cell = cellset[row][col];
var value = cell.value || 0;
// check if the resultset contains the raw value, if not try to parse the given value
var raw = cell.properties.raw;
if (raw && raw !== "null") {
value = parseFloat(raw);
} else if (typeof(cell.value) !== "number" && parseFloat(cell.value.replace(/[^a-zA-Z 0-9.]+/g,''))) {
value = parseFloat(cell.value.replace(/[^a-zA-Z 0-9.]+/g,''));
}
record.push(value);
}
this.data.resultset.push(record);
}
}
//makeSureUniqueLabels(this.data.resultset);
this.hasProcessed = true;
this.data.height = this.data.resultset.length;
this.cccOptions = this.getQuickOptions(this.cccOptions);
this.render_chart();
} else {
$(this.el).find('.canvas_wrapper').text("No results").show();
this.workspace.processing.hide();
this.workspace.adjust();
}
}
});
function converteHtmlEntities(texto){
var s = ["á","ã","à","é","í","ó","õ","ú","ç","Á","Ã","À","É","Í","Ó","Õ","Ú","Ç"],
e = ["á","ã","à","é","í","ó","õ","ú","ç","Á","Ã","À","É","Í","Ó","Õ","Ú","Ç"],
n = e.length,
i;
for(i=0;i