From b6564900adb67531b08bf7ab1b6ae8c435380855 Mon Sep 17 00:00:00 2001 From: "vda (OpenERP)" Date: Fri, 15 Apr 2011 12:12:41 +0530 Subject: [PATCH] [FIX] Graph According to type. bzr revid: vda@tinyerp.com-20110415064241-3rq913vloy9mchc1 --- addons/base_graph/__openerp__.py | 9 +- addons/base_graph/controllers/main.py | 72 +---- addons/base_graph/static/src/graph.js | 162 ----------- addons/base_graph/static/src/js/graph.js | 265 ++++++++++++++++++ .../base_graph/static/src/xml/base_graph.xml | 17 ++ 5 files changed, 292 insertions(+), 233 deletions(-) delete mode 100644 addons/base_graph/static/src/graph.js create mode 100644 addons/base_graph/static/src/js/graph.js create mode 100644 addons/base_graph/static/src/xml/base_graph.xml diff --git a/addons/base_graph/__openerp__.py b/addons/base_graph/__openerp__.py index 4a4059d9642..c393953b9fd 100644 --- a/addons/base_graph/__openerp__.py +++ b/addons/base_graph/__openerp__.py @@ -1,7 +1,10 @@ { "name": "Base Graph", "version": "2.0", - "depends": [], - "js": ["static/*/js/*.js"], - "css": [], + "depends": ['base'], + "js": [ + "static/lib/dhtmlxGraph/codebase/dhtmlxchart.js", + "static/src/js/graph.js"], + "css": ["static/lib/dhtmlxGraph/codebase/dhtmlxchart.css"], + "active": True } \ No newline at end of file diff --git a/addons/base_graph/controllers/main.py b/addons/base_graph/controllers/main.py index ac7d2c8173f..516bdb85189 100644 --- a/addons/base_graph/controllers/main.py +++ b/addons/base_graph/controllers/main.py @@ -1,74 +1,10 @@ -import glob, os -from xml.etree import ElementTree -import math -import simplejson +from base.controllers.main import View import openerpweb -import time -import datetime -from base.controllers.main import Xml2Json - -COLOR_PALETTE = ['#f57900', '#cc0000', '#d400a8', '#75507b', '#3465a4', '#73d216', '#c17d11', '#edd400', - '#fcaf3e', '#ef2929', '#ff00c9', '#ad7fa8', '#729fcf', '#8ae234', '#e9b96e', '#fce94f', - '#ff8e00', '#ff0000', '#b0008c', '#9000ff', '#0078ff', '#00ff00', '#e6ff00', '#ffff00', - '#905000', '#9b0000', '#840067', '#510090', '#0000c9', '#009b00', '#9abe00', '#ffc900', ] - -_colorline = ['#%02x%02x%02x' % (25 + ((r + 10) % 11) * 23, 5 + ((g + 1) % 11) * 20, 25 + ((b + 4) % 11) * 23) for r in range(11) for g in range(11) for b in range(11) ] - -def choice_colors(n): - if n > len(COLOR_PALETTE): - return _colorline[0:-1:len(_colorline) / (n + 1)] - elif n: - return COLOR_PALETTE[:n] - return [] - -class GraphView(openerpweb.Controller): +class GraphView(View): _cp_path = "/base_graph/graphview" - date_start = None - date_delay = None - date_stop = None - color_field = None - - day_length = 8 - fields = {} - events = [] - calendar_fields = {} - ids = [] - model = '' - domain = [] - context = {} - event_res = [] - colors = {} - color_values = [] - @openerpweb.jsonrequest def load(self, req, model, view_id): - m = req.session.model(model) - r = m.fields_view_get(view_id, 'graph') - r["arch"] = Xml2Json.convert_to_structure(r["arch"]) - return {'fields_view':r} - - @openerpweb.jsonrequest - def get_events(self, req, **kw): - self.model = kw['model'] - self.fields = kw['fields'] - - model = req.session.model(self.model) - event_ids = model.search([]) - return self.create_event(event_ids, model) - - def create_event(self, event_ids, model): - self.events = model.read(event_ids, self.fields.values()) - - result = [] - for i, evt in enumerate(self.events): - key = evt[self.fields.values()[1]] - if isinstance(key, list): - evt[self.fields.values()[1]] = key = key[-1] - - self.events[i].pop('id') - result.append(evt) - - return result - + fields_view = self.fields_view_get(req, model, view_id, 'graph') + return {'fields_view': fields_view} \ No newline at end of file diff --git a/addons/base_graph/static/src/graph.js b/addons/base_graph/static/src/graph.js deleted file mode 100644 index d7d8b1c7ac9..00000000000 --- a/addons/base_graph/static/src/graph.js +++ /dev/null @@ -1,162 +0,0 @@ -/*--------------------------------------------------------- - * OpenERP base_graph - *---------------------------------------------------------*/ - -openerp.base.graph = function (openerp) { -openerp.base.views.add('graph', 'openerp.base.GraphView'); -openerp.base.GraphView = openerp.base.Controller.extend({ - - init: function(view_manager, session, element_id, dataset, view_id) { - - this._super(session, element_id); - this.view_manager = view_manager; - this.dataset = dataset; - this.model = dataset.model; - this.view_id = view_id; - this.fields_views = {}; - this.widgets = {}; - this.widgets_counter = 0; - this.fields = {}; - this.datarecord = {}; - this.calendar_fields = {}; - }, - do_show: function () { - // TODO: re-trigger search - this.$element.show(); - }, - do_hide: function () { - this.$element.hide(); - }, - start: function() { - this.rpc("/base_graph/graphview/load", {"model": this.model, "view_id": this.view_id}, this.on_loaded); - }, - on_loaded: function(data) { - this.fields_view = data.fields_view; - var self = this; - this.name = this.fields_view.name || this.fields_view.arch.attrs.string; - this.view_id = this.fields_view.view_id; - this.fields['id'] = this.fields_view.arch.children[0].attrs.name; - this.fields['total'] = this.fields_view.arch.children[1].attrs.name; - this.graph_type = this.fields_view.arch.attrs.type; - - this.rpc('/base_graph/graphview/get_events', - {'model': this.model, - 'fields': this.fields - }, - function(res) { - self.create_graph(res); - }) - this.$element.html(QWeb.render("GraphView", {"view": this, "fields_view": this.fields_view})); - }, - create_graph: function(res) { - //if (this.graph_type == "bar"){ - var barChart = new dhtmlXChart({ - view:"bar", - container:"barchart", - value:"#"+this.fields['total']+"#", - color:"#9abe00", - width:40, - tooltip: "#"+this.fields['id']+"#", - xAxis:{ - template:"#"+this.fields['id']+"#" - }, - yAxis:{ - title: "Total" - } - }); - barChart.parse(res, "json"); - //} - var pieChart = new dhtmlXChart({ - view: "pie", - container: "piechart", - value: "#"+this.fields['total']+"#", - label: "#"+this.fields['id']+"#", - pieInnerText: "#"+this.fields['total']+"#", - gradient: true, - radius: 90, - x: 280, - y: 150 - }); - pieChart.parse(res, "json"); - - /*static data for stackedbar chart*/ - var data = [{ - total: "2.0", - total1: "0.0", - stage: "Lost" - }, { - total: "1.0", - total1: "2.0", - stage: "Negotiation" - }, { - total: "4.0", - total1: "2.0", - stage: "New" - }, { - total: "3.0", - total1: "0.0", - stage: "Proposition" - }, { - total: "1.0", - total1: "1.0", - stage: "Qualification" - }, { - total: "1.0", - total1: "0.0", - stage: "Won" - }]; - - var stackedChart = new dhtmlXChart({ - view: "stackedBar", - container: "stackedchart", - value: "#total#", - label: "#total#", - width: 60, - tooltip: { - template: "#total#" - }, - xAxis: { - template: "#stage#" - }, - yAxis: { - title: "User" - }, - gradient: "3d", - color: "#66cc33", - legend: { - values: [{ - text: "Administrator", - color: "#66cc33" - }, { - text: "Demo User", - color: "#ff9933" - }], - valign: "top", - align: "right", - width: 120, - layout: "y", - marker: { - width: 15, - type: "round" - } - } - }); - stackedChart.addSeries({ - value: "#total1#", - color: "#ff9933", - label: "#total1#", - tooltip: { - template: "#total1#" - } - }); - stackedChart.parse(data, "json"); - }, - - -}); - -// here you may tweak globals object, if any, and play with on_* or do_* callbacks on them - -}; - -// vim:et fdc=0 fdl=0: diff --git a/addons/base_graph/static/src/js/graph.js b/addons/base_graph/static/src/js/graph.js new file mode 100644 index 00000000000..b7e82c58279 --- /dev/null +++ b/addons/base_graph/static/src/js/graph.js @@ -0,0 +1,265 @@ +/*--------------------------------------------------------- + * OpenERP base_graph + *---------------------------------------------------------*/ + +openerp.base_graph = function (openerp) { +QWeb.add_template('/base_graph/static/src/xml/base_graph.xml'); +openerp.base.views.add('graph', 'openerp.base_graph.GraphView'); +openerp.base_graph.GraphView = openerp.base.Controller.extend({ + + init: function(view_manager, session, element_id, dataset, view_id) { + + this._super(session, element_id); + this.view_manager = view_manager; + this.dataset = dataset; + this.model = this.dataset.model; + this.view_id = view_id; + }, + do_show: function () { + // TODO: re-trigger search + this.$element.show(); + }, + do_hide: function () { + this.$element.hide(); + }, + start: function() { + this.rpc("/base_graph/graphview/load", {"model": this.model, "view_id": this.view_id}, this.on_loaded); + }, + on_loaded: function(data) { + var self = this; + this.fields_view = data.fields_view; + this.name = this.fields_view.name || this.fields_view.arch.attrs.string; + this.view_id = this.fields_view.view_id; + this.chart = this.fields_view.arch.attrs.type || 'pie'; + this.fields = this.fields_view.fields; + this.chart_info_fields = []; + this.operator_field = ''; + this.group_field = ''; + this.orientation = this.fields_view.arch.attrs.orientation || ''; + for(fld in this.fields_view.arch.children) { + if (this.fields_view.arch.children[fld].attrs.operator) { + this.operator_field = this.fields_view.arch.children[fld].attrs.name; + } + else if (this.fields_view.arch.children[fld].attrs.group) { + this.group_field = this.fields_view.arch.children[fld].attrs.name + } + else { + this.chart_info_fields.push(this.fields_view.arch.children[fld].attrs.name) + } + } + + this.load_chart(); + }, + + load_chart: function() { + var self = this; + this.dataset.read_ids( + this.dataset.ids, + this.fields, + function(result) { + self.schedule_chart(result) + } + ) + }, + + schedule_chart: function(result) { + + this.$element.html(QWeb.render("GraphView", {"fields_view": this.fields_view, "chart": this.chart})); + + console.log('fields view >>>>>>',this.fields_view) + + for(res in result) { + for(fld in result[res]) { + if(typeof result[res][fld] == 'object') { + result[res][fld] = result[res][fld][result[res][fld].length - 1] + } + } + } + if(this.chart == 'bar') { + var xAxis = {}; + var yAxis = {}; + + if (this.orientation && this.orientation == 'horizontal') { + this.chart = "barH"; + xAxis:{template:"#"+this.operator_field+"#"}; + yAxis:{title: this.chart_info_fields[0]}; + } else { + xAxis:{template:"#"+this.chart_info_fields[0]+"#"}; + yAxis:{title: this.operator_field}; + } + return this.schedule_bar(result, xAxis, yAxis, "barchart") + } else if(this.chart == "pie") { + return this.schedule_pie(result, "piechart"); + } + + }, + + + schedule_bar: function(result, xAxis, yAxis, container) { + var chart = new dhtmlXChart({ + view: this.chart, + container: container, + value: "#"+this.operator_field+"#", + color:"#d2ed7e", + width:30, + gradient:"3d", + tooltip: "#"+this.chart_info_fields[0]+"#", + xAxis: xAxis, + yAxis: yAxis, + legend: { + align:"right", + valign:"top", + marker:{ + type:"round", + width:12 + }, + template:"#"+this.chart_info_fields[0]+"#" + } + }); + if(this.group_field) { +// var map_fld = this.chart_info_fields[0]; +// chart.group({ +// by: "#"+this.group_field+"#", +// map:{ +// map_fld :["#"+this.operator_field+"#", "sum"] +// } +// }); + } + chart.parse(result, "json"); + }, + + schedule_pie: function(result, container) { + var chart = new dhtmlXChart({ + view:"pie", + container:container, + value:"#"+this.operator_field+"#", + color:"#d2ed7e", + label:"#"+this.chart_info_fields[0]+"#", + pieInnerText:"#"+this.operator_field+"#", + gradient:"3d", + legend: { + width: 100, + align:"right", + valign:"top", + marker:{ + type:"round", + width:12 + }, + template:"#"+this.chart_info_fields[0]+"#" + } + }); + chart.parse(result,"json"); + }, + + create_graph: function(res) { + //if (this.graph_type == "bar"){ + var barChart = new dhtmlXChart({ + view:"bar", + container:"barchart", + value:"#"+this.fields['total']+"#", + color:"#9abe00", + width:40, + tooltip: "#"+this.fields['id']+"#", + xAxis:{ + template:"#"+this.fields['id']+"#" + }, + yAxis:{ + title: "Total" + } + }); + barChart.parse(res, "json"); + //} + var pieChart = new dhtmlXChart({ + view: "pie", + container: "piechart", + value: "#"+this.fields['total']+"#", + label: "#"+this.fields['id']+"#", + pieInnerText: "#"+this.fields['total']+"#", + gradient: true, + radius: 90, + x: 280, + y: 150 + }); + pieChart.parse(res, "json"); + + /*static data for stackedbar chart*/ + var data = [{ + total: "2.0", + total1: "0.0", + stage: "Lost" + }, { + total: "1.0", + total1: "2.0", + stage: "Negotiation" + }, { + total: "4.0", + total1: "2.0", + stage: "New" + }, { + total: "3.0", + total1: "0.0", + stage: "Proposition" + }, { + total: "1.0", + total1: "1.0", + stage: "Qualification" + }, { + total: "1.0", + total1: "0.0", + stage: "Won" + }]; + + var stackedChart = new dhtmlXChart({ + view: "stackedBar", + container: "stackedchart", + value: "#total#", + label: "#total#", + width: 60, + tooltip: { + template: "#total#" + }, + xAxis: { + template: "#stage#" + }, + yAxis: { + title: "User" + }, + gradient: "3d", + color: "#66cc33", + legend: { + values: [{ + text: "Administrator", + color: "#66cc33" + }, { + text: "Demo User", + color: "#ff9933" + }], + valign: "top", + align: "right", + width: 120, + layout: "y", + marker: { + width: 15, + type: "round" + } + } + }); + stackedChart.addSeries({ + value: "#total1#", + color: "#ff9933", + label: "#total1#", + tooltip: { + template: "#total1#" + } + }); + stackedChart.parse(data, "json"); + }, + + +}); + +// here you may tweak globals object, if any, and play with on_* or do_* callbacks on them + +}; + +// vim:et fdc=0 fdl=0: diff --git a/addons/base_graph/static/src/xml/base_graph.xml b/addons/base_graph/static/src/xml/base_graph.xml new file mode 100644 index 00000000000..bfab098cd62 --- /dev/null +++ b/addons/base_graph/static/src/xml/base_graph.xml @@ -0,0 +1,17 @@ + \ No newline at end of file