Skip to content

Commit 2333c32

Browse files
[15.0][MIG] web_widget_mpld3_chart
1 parent 6bd33bc commit 2333c32

File tree

16 files changed

+20774
-1602
lines changed

16 files changed

+20774
-1602
lines changed

web_widget_mpld3_chart/README.rst

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ Web Widget mpld3 Chart
1414
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
1515
:alt: License: LGPL-3
1616
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github
17-
:target: https://github.com/OCA/web/tree/14.0/web_widget_mpld3_chart
17+
:target: https://github.com/OCA/web/tree/15.0/web_widget_mpld3_chart
1818
:alt: OCA/web
1919
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
20-
:target: https://translation.odoo-community.org/projects/web-14-0/web-14-0-web_widget_mpld3_chart
20+
:target: https://translation.odoo-community.org/projects/web-15-0/web-15-0-web_widget_mpld3_chart
2121
:alt: Translate me on Weblate
2222
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
23-
:target: https://runbot.odoo-community.org/runbot/162/14.0
23+
:target: https://runbot.odoo-community.org/runbot/162/15.0
2424
:alt: Try me on Runbot
2525

2626
|badge1| |badge2| |badge3| |badge4| |badge5|
@@ -48,9 +48,14 @@ Usage
4848

4949
To insert a mpld3 chart in a view proceed as follows:
5050

51+
#. You should inherit from abstract class abstract.mpld3.parser::
52+
53+
_name = 'res.partner'
54+
_inherit = ['res.partner', 'abstract.mpld3.parser']
55+
5156
#. Import the required libraries::
5257

53-
import matplotlib.pyplot as plt, mpld3
58+
import matplotlib.pyplot as plt
5459

5560
#. Declare a text computed field like this::
5661

@@ -66,7 +71,7 @@ To insert a mpld3 chart in a view proceed as follows:
6671
# Design your mpld3 figure:
6772
plt.scatter([1, 10], [5, 9])
6873
figure = plt.figure()
69-
rec.mpld3_chart = mpld3.fig_to_html(figure)
74+
rec.mpld3_chart = self.convert_figure_to_json(figure)
7075

7176
#. In the view, add something like this wherever you want to display your
7277
mpld3 chart::
@@ -81,7 +86,7 @@ Bug Tracker
8186
Bugs are tracked on `GitHub Issues <https://github.com/OCA/web/issues>`_.
8287
In case of trouble, please check there if your issue has already been reported.
8388
If you spotted it first, help us smashing it by providing a detailed and welcomed
84-
`feedback <https://github.com/OCA/web/issues/new?body=module:%20web_widget_mpld3_chart%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
89+
`feedback <https://github.com/OCA/web/issues/new?body=module:%20web_widget_mpld3_chart%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
8590

8691
Do not contact contributors directly about support or help with technical issues.
8792

@@ -97,6 +102,7 @@ Contributors
97102
~~~~~~~~~~~~
98103

99104
* Jordi Ballester Alomar <jordi.ballester@forgeflow.com>
105+
* Christopher Ormaza <chris.ormaza@forgeflow.com>
100106

101107
Other credits
102108
~~~~~~~~~~~~~
@@ -119,6 +125,17 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
119125
mission is to support the collaborative development of Odoo features and
120126
promote its widespread use.
121127

122-
This module is part of the `OCA/web <https://github.com/OCA/web/tree/14.0/web_widget_mpld3_chart>`_ project on GitHub.
128+
.. |maintainer-JordiBForgeFlow| image:: https://github.com/JordiBForgeFlow.png?size=40px
129+
:target: https://github.com/JordiBForgeFlow
130+
:alt: JordiBForgeFlow
131+
.. |maintainer-ChrisOForgeFlow| image:: https://github.com/ChrisOForgeFlow.png?size=40px
132+
:target: https://github.com/ChrisOForgeFlow
133+
:alt: ChrisOForgeFlow
134+
135+
Current `maintainers <https://odoo-community.org/page/maintainer-role>`__:
136+
137+
|maintainer-JordiBForgeFlow| |maintainer-ChrisOForgeFlow|
138+
139+
This module is part of the `OCA/web <https://github.com/OCA/web/tree/15.0/web_widget_mpld3_chart>`_ project on GitHub.
123140

124141
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

web_widget_mpld3_chart/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
2+
from . import models

web_widget_mpld3_chart/__manifest__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@
66
"category": "Hidden",
77
"summary": "This widget allows to display charts using MPLD3 library.",
88
"author": "ForgeFlow, Odoo Community Association (OCA)",
9-
"version": "14.0.1.0.0",
9+
"version": "15.0.1.0.0",
1010
"website": "https://github.com/OCA/web",
1111
"depends": ["web"],
12-
"data": ["views/web_widget_mpld3_chart.xml"],
13-
"external_dependencies": {"python": ["mpld3"]},
12+
"data": [],
13+
"external_dependencies": {"python": ["mpld3", "beautifulsoup4"]},
1414
"auto_install": False,
15+
"development_status": "Beta",
16+
"maintainers": ["JordiBForgeFlow", "ChrisOForgeFlow"],
1517
"license": "LGPL-3",
18+
"assets": {
19+
"web.assets_backend": [
20+
"web_widget_mpld3_chart/static/src/js/web_widget_mpld3_chart.esm.js",
21+
],
22+
},
1623
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import abstract_mpld3_parser
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2022 ForgeFlow S.L.
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
4+
5+
import json
6+
import logging
7+
8+
from odoo import api, models
9+
10+
_logger = logging.getLogger(__name__)
11+
12+
try:
13+
import mpld3
14+
from bs4 import BeautifulSoup
15+
except (ImportError, IOError) as err:
16+
_logger.debug(err)
17+
18+
19+
class AbstractMpld3Parser(models.AbstractModel):
20+
21+
_name = "abstract.mpld3.parser"
22+
_description = "Utility to parse ploot figure to json data for widget Mpld3"
23+
24+
@api.model
25+
def convert_figure_to_json(self, figure):
26+
html_string = mpld3.fig_to_html(figure, no_extras=True, include_libraries=False)
27+
soup = BeautifulSoup(html_string, "lxml")
28+
json_data = {
29+
"style": soup.style.decode(),
30+
"div": soup.div.get("id"),
31+
"script": soup.script.decode_contents(),
32+
}
33+
return json.dumps(json_data)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
* Jordi Ballester Alomar <jordi.ballester@forgeflow.com>
2+
* Christopher Ormaza <chris.ormaza@forgeflow.com>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
* This module uses the library `mpld3 <https://github.com/mpld3/mpld3>`__
22
which is under the open-source BSD 3-clause "New" or "Revised" License.
33
Copyright (c) 2013, Jake Vanderplas
4+
* This module uses the library `BeautifulSoup 4 <https://pypi.org/project/beautifulsoup4/>`__
5+
which is under the open-source MIT License.
6+
Copyright (c) 2014, Leonard Richardson
7+
48
* Odoo Community Association (OCA)

web_widget_mpld3_chart/readme/USAGE.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
To insert a mpld3 chart in a view proceed as follows:
22

3+
#. You should inherit from abstract class abstract.mpld3.parser::
4+
5+
_name = 'res.partner'
6+
_inherit = ['res.partner', 'abstract.mpld3.parser']
7+
38
#. Import the required libraries::
49

5-
import matplotlib.pyplot as plt, mpld3
10+
import matplotlib.pyplot as plt
611

712
#. Declare a text computed field like this::
813

@@ -18,7 +23,7 @@ To insert a mpld3 chart in a view proceed as follows:
1823
# Design your mpld3 figure:
1924
plt.scatter([1, 10], [5, 9])
2025
figure = plt.figure()
21-
rec.mpld3_chart = mpld3.fig_to_html(figure)
26+
rec.mpld3_chart = self.convert_figure_to_json(figure)
2227

2328
#. In the view, add something like this wherever you want to display your
2429
mpld3 chart::

web_widget_mpld3_chart/static/description/index.html

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
44
<head>
55
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6-
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
6+
<meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
77
<title>Web Widget mpld3 Chart</title>
88
<style type="text/css">
99

@@ -367,7 +367,7 @@ <h1 class="title">Web Widget mpld3 Chart</h1>
367367
!! This file is generated by oca-gen-addon-readme !!
368368
!! changes will be overwritten. !!
369369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
370-
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/web/tree/14.0/web_widget_mpld3_chart"><img alt="OCA/web" src="https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/web-14-0/web-14-0-web_widget_mpld3_chart"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/162/14.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
370+
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/web/tree/15.0/web_widget_mpld3_chart"><img alt="OCA/web" src="https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/web-15-0/web-15-0-web_widget_mpld3_chart"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/162/15.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
371371
<p>This module adds the possibility to insert mpld3 charts into Odoo standard views.
372372
This is an interactive D3js-based viewer which brings matplotlib graphics to the browser.</p>
373373
<p>If you want to see some samples of mpld3’s capabilities follow this <a class="reference external" href="http://mpld3.github.io/">link</a>.</p>
@@ -397,9 +397,15 @@ <h1><a class="toc-backref" href="#id1">Installation</a></h1>
397397
<h1><a class="toc-backref" href="#id2">Usage</a></h1>
398398
<p>To insert a mpld3 chart in a view proceed as follows:</p>
399399
<ol class="arabic">
400+
<li><p class="first">You should inherit from abstract class abstract.mpld3.parser:</p>
401+
<pre class="literal-block">
402+
_name = 'res.partner'
403+
_inherit = ['res.partner', 'abstract.mpld3.parser']
404+
</pre>
405+
</li>
400406
<li><p class="first">Import the required libraries:</p>
401407
<pre class="literal-block">
402-
import matplotlib.pyplot as plt, mpld3
408+
import matplotlib.pyplot as plt
403409
</pre>
404410
</li>
405411
<li><p class="first">Declare a text computed field like this:</p>
@@ -417,7 +423,7 @@ <h1><a class="toc-backref" href="#id2">Usage</a></h1>
417423
# Design your mpld3 figure:
418424
plt.scatter([1, 10], [5, 9])
419425
figure = plt.figure()
420-
rec.mpld3_chart = mpld3.fig_to_html(figure)
426+
rec.mpld3_chart = self.convert_figure_to_json(figure)
421427
</pre>
422428
</li>
423429
<li><p class="first">In the view, add something like this wherever you want to display your
@@ -435,7 +441,7 @@ <h1><a class="toc-backref" href="#id3">Bug Tracker</a></h1>
435441
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/web/issues">GitHub Issues</a>.
436442
In case of trouble, please check there if your issue has already been reported.
437443
If you spotted it first, help us smashing it by providing a detailed and welcomed
438-
<a class="reference external" href="https://github.com/OCA/web/issues/new?body=module:%20web_widget_mpld3_chart%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
444+
<a class="reference external" href="https://github.com/OCA/web/issues/new?body=module:%20web_widget_mpld3_chart%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
439445
<p>Do not contact contributors directly about support or help with technical issues.</p>
440446
</div>
441447
<div class="section" id="credits">
@@ -450,6 +456,7 @@ <h2><a class="toc-backref" href="#id5">Authors</a></h2>
450456
<h2><a class="toc-backref" href="#id6">Contributors</a></h2>
451457
<ul class="simple">
452458
<li>Jordi Ballester Alomar &lt;<a class="reference external" href="mailto:jordi.ballester&#64;forgeflow.com">jordi.ballester&#64;forgeflow.com</a>&gt;</li>
459+
<li>Christopher Ormaza &lt;<a class="reference external" href="mailto:chris.ormaza&#64;forgeflow.com">chris.ormaza&#64;forgeflow.com</a>&gt;</li>
453460
</ul>
454461
</div>
455462
<div class="section" id="other-credits">
@@ -468,7 +475,9 @@ <h2><a class="toc-backref" href="#id8">Maintainers</a></h2>
468475
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
469476
mission is to support the collaborative development of Odoo features and
470477
promote its widespread use.</p>
471-
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/web/tree/14.0/web_widget_mpld3_chart">OCA/web</a> project on GitHub.</p>
478+
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainers</a>:</p>
479+
<p><a class="reference external" href="https://github.com/JordiBForgeFlow"><img alt="JordiBForgeFlow" src="https://github.com/JordiBForgeFlow.png?size=40px" /></a> <a class="reference external" href="https://github.com/ChrisOForgeFlow"><img alt="ChrisOForgeFlow" src="https://github.com/ChrisOForgeFlow.png?size=40px" /></a></p>
480+
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/web/tree/15.0/web_widget_mpld3_chart">OCA/web</a> project on GitHub.</p>
472481
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
473482
</div>
474483
</div>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/** @odoo-module **/
2+
3+
import basicFields from "web.basic_fields";
4+
import fieldRegistry from "web.field_registry";
5+
6+
const Mpld3ChartWidget = basicFields.FieldChar.extend({
7+
jsLibs: [
8+
"/web_widget_mpld3_chart/static/src/lib/d3/d3.v5.js",
9+
"/web_widget_mpld3_chart/static/src/lib/mpld3/mpld3.v0.5.7.js",
10+
],
11+
_renderReadonly: function () {
12+
try {
13+
const val = JSON.parse(this.value);
14+
const new_div = document.createElement("div");
15+
new_div.setAttribute("id", val.div);
16+
this.$el.html(new_div);
17+
this.$el.ready(function () {
18+
const script = document.createElement("script");
19+
script.setAttribute("type", "text/javascript");
20+
if ("textContent" in script) script.textContent = val.script;
21+
else script.text = val.script;
22+
$("head").append(script);
23+
});
24+
} catch (error) {
25+
return this._super(...arguments);
26+
}
27+
},
28+
});
29+
30+
fieldRegistry.add("mpld3_chart", Mpld3ChartWidget);
31+
export default Mpld3ChartWidget;

0 commit comments

Comments
 (0)