Highcharts Graph Widget
Seamless integration between Odoo and Highcharts JS v11, embed interactive, production-quality charts directly inside any form view.
Features
- Drop-in
<widget>tag: no JavaScript required in your module - Supports all Highcharts chart types: line, spline, pie, variable-pie, bar, 3D, and more
- Multiple widgets per form view, each bound to a different model method
- Works on any model: just add a method that returns a Highcharts config dict
- Built with Owl 2 and Odoo's
view_widgetsregistry
How to use it
Step 1: Add a method to your model that returns a valid Highcharts configuration dictionary:
# -*- coding: utf-8 -*-
from odoo import models
class TestGraph(models.Model):
_name = "highcharts_widget.test_graph"
_description = "Testing graph feature..."
def compute_graph_data(self):
self.ensure_one()
return {
"chart": {
"type": "variablepie"
},
"title": {
"text": "Countries compared by population density and total area, 2022.",
"align": "center"
},
"tooltip": {
"headerFormat": "",
"pointFormat": '\u25CF {point.name}
Area (square km): {point.y}
Population density (people per square km): {point.z}
'
},
"series": [{
"minPointSize": 10,
"innerSize": "20%",
"zMin": 0,
"name": "countries",
"borderRadius": 5,
"data": [
{"name": "Spain", "y": 505992, "z": 92},
{"name": "France", "y": 551695, "z": 119},
{"name": "Poland", "y": 312679, "z": 121},
{"name": "Czech Republic", "y": 78865, "z": 136},
{"name": "Italy", "y": 301336, "z": 200},
{"name": "Switzerland", "y": 41284, "z": 213},
{"name": "Germany", "y": 357114, "z": 235},
],
"colors": ["#4caefe","#3dc3e8","#2dd9db","#1feeaf","#0ff3a0","#00e887","#23e274"]
}]
}
Step 2: Declare the widget in your form view XML using the name and method attributes:
<form>
<sheet>
<div>
<widget name="graph_widget" method="compute_graph_data"/>
</div>
</sheet>
</form>
You can place multiple <widget> tags in the same form, each bound to a different method.
The chart renders automatically when the record is saved and has a valid ID.
Another example: Spline chart on hr.employee
class HrEmployeePrivate(models.Model):
_inherit = "hr.employee"
def compute_graph_data(self):
self.ensure_one()
return {
"chart": {"type": "spline", "inverted": True},
"title": {"text": "Atmosphere Temperature by Altitude", "align": "left"},
"subtitle": {"text": "According to the Standard Atmosphere Model", "align": "left"},
"xAxis": {
"reversed": False,
"title": {"enabled": True, "text": "Altitude"},
"labels": {"format": "{value} km"},
"maxPadding": 0.05,
"showLastLabel": True
},
"yAxis": {
"title": {"text": "Temperature"},
"labels": {"format": "{value}°"},
"lineWidth": 2
},
"legend": {"enabled": False},
"tooltip": {"headerFormat": "<b>{series.name}</b><br/>", "pointFormat": "{point.x} km: {point.y}°C"},
"series": [{
"name": "Temperature",
"data": [[0,15],[10,-50],[20,-56.5],[30,-46.5],[40,-22.1],[50,-2.5],[60,-27.7],[70,-55.7],[80,-76.5]]
}]
}
<!-- Inject the widget into the employee form -->
<record id="view_employee_form_inherit" model="ir.ui.view">
<field name="name">view_employee_form_inherit</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_form"/>
<field name="arch" type="xml">
<xpath expr="//div[@id='o_work_employee_container']" position="after">
<widget name="graph_widget" method="compute_graph_data"/>
</xpath>
</field>
</record>
Alejandro Cora González
alek.cora.glez@gmail.com
Odoo Proprietary License v1.0 This software and associated files (the "Software") may only be used (executed, modified, executed after modifications) if you have purchased a valid license from the authors, typically via Odoo Apps, or if you have received a written agreement from the authors of the Software (see the COPYRIGHT file). You may develop Odoo modules that use the Software as a library (typically by depending on it, importing it and using its resources), but without copying any source code or material from the Software. You may distribute those modules under the license of your choice, provided that this license is compatible with the terms of the Odoo Proprietary License (For example: LGPL, MIT, or proprietary licenses similar to this one). It is forbidden to publish, distribute, sublicense, or sell copies of the Software or modified copies of the Software. The above copyright notice and this permission notice must be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Please log in to comment on this module