You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.5 KiB

  1. openerp.web_graph_improved = function(instance) {
  2. instance.web_graph.Graph.include({
  3. bar: function() {
  4. var self = this,
  5. dim_x = this.pivot.rows.groupby.length,
  6. dim_y = this.pivot.cols.groupby.length,
  7. show_controls = (this.width > 400 && this.height > 300 && dim_x + dim_y >=2),
  8. data;
  9. // No groupby
  10. if ((dim_x === 0) && (dim_y === 0)) {
  11. data = [{key: _t('Total'), values:[{
  12. x: _t('Total'),
  13. y: this.pivot.get_total()[0],
  14. }]}];
  15. // Only column groupbys
  16. } else if ((dim_x === 0) && (dim_y >= 1)){
  17. data = _.map(this.pivot.get_cols_with_depth(1), function (header) {
  18. return {
  19. key: header.title,
  20. values: [{x:header.title, y: self.pivot.get_total(header)[0]}]
  21. };
  22. });
  23. // Just 1 row groupby
  24. } else if ((dim_x === 1) && (dim_y === 0)) {
  25. data = _.map(self.pivot.measures, function(measure, i) {
  26. var series = _.map(self.pivot.main_row().children, function (pt) {
  27. var value = self.pivot.get_total(pt)[i],
  28. title = (pt.title !== undefined) ? pt.title : _t('Undefined');
  29. return {x: title, y: value};
  30. });
  31. return {key: self.pivot.measures[i].string, values:series};
  32. });
  33. // 1 row groupby and some col groupbys
  34. } else if ((dim_x === 1) && (dim_y >= 1)) {
  35. data = _.map(this.pivot.get_cols_with_depth(1), function (colhdr) {
  36. var values = _.map(self.pivot.get_rows_with_depth(1), function (header) {
  37. return {
  38. x: header.title || _t('Undefined'),
  39. y: self.pivot.get_values(header.id, colhdr.id)[0] || 0
  40. };
  41. });
  42. return {key: colhdr.title || _t('Undefined'), values: values};
  43. });
  44. // At least two row groupby
  45. } else {
  46. var keys = _.uniq(_.map(this.pivot.get_rows_with_depth(2), function (hdr) {
  47. return hdr.title || _t('Undefined');
  48. }));
  49. data = _.map(keys, function (key) {
  50. var values = _.map(self.pivot.get_rows_with_depth(1), function (hdr) {
  51. var subhdr = _.find(hdr.children, function (child) {
  52. return ((child.title === key) || ((child.title === undefined) && (key === _t('Undefined'))));
  53. });
  54. return {
  55. x: hdr.title || _t('Undefined'),
  56. y: (subhdr) ? self.pivot.get_total(subhdr)[0] : 0
  57. };
  58. });
  59. return {key:key, values: values};
  60. });
  61. }
  62. nv.addGraph(function () {
  63. var chart = nv.models.multiBarChart()
  64. .stacked(self.bar_ui === 'stack')
  65. .showControls(show_controls);
  66. d3.select(self.svg)
  67. .datum(data)
  68. .attr('width', self.width)
  69. .attr('height', self.height)
  70. .call(chart);
  71. nv.utils.windowResize(chart.update);
  72. return chart;
  73. });
  74. }
  75. });
  76. }