-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] chart: add custom tooltip for charts
This commit replaces the chartJS default tooltip by a custom tooltip that is prettier (and matches the Odoo style). closes #5246 Task: 4351271 Signed-off-by: Rémi Rahir (rar) <[email protected]>
- Loading branch information
1 parent
c6fc50f
commit df4cb38
Showing
6 changed files
with
208 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/helpers/figures/charts/runtime/chart_custom_tooltip.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { App, Component, blockDom } from "@odoo/owl"; | ||
import { _t } from "../../../../translation"; | ||
|
||
/** | ||
* Custom tooltip for the charts. Mostly copied from Odoo's custom tooltip, with some slight changes to make it work | ||
* with o-spreadsheet chart data and CSS. | ||
* | ||
* https://github.com/odoo/odoo/blob/18.0/addons/web/static/src/views/graph/graph_renderer.xml | ||
*/ | ||
const templates = /* xml */ ` | ||
<templates> | ||
<t t-name="o-spreadsheet-CustomTooltip"> | ||
<div | ||
class="o-chart-custom-tooltip border rounded px-2 py-1 pe-none mw-100 position-absolute text-nowrap shadow opacity-100"> | ||
<table class="overflow-hidden m-0"> | ||
<thead> | ||
<tr> | ||
<th class="o-tooltip-title align-baseline border-0 text-truncate" t-esc="title" t-attf-style="max-width: {{ labelsMaxWidth }}"/> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr t-foreach="tooltipItems" t-as="tooltipItem" t-key="tooltipItem_index"> | ||
<td> | ||
<span | ||
class="badge ps-2 py-2 rounded-0 align-middle" | ||
t-attf-style="background-color: {{ tooltipItem.boxColor }}" | ||
> </span> | ||
<small | ||
t-if="tooltipItem.label" | ||
class="o-tooltip-label d-inline-block text-truncate align-middle smaller ms-2" | ||
t-esc="tooltipItem.label" | ||
t-attf-style="max-width: {{ labelsMaxWidth }}" | ||
/> | ||
</td> | ||
<td class="o-tooltip-value ps-2 fw-bolder text-end"> | ||
<small class="smaller d-inline-block text-truncate align-middle" t-attf-style="max-width: {{ valuesMaxWidth }}"> | ||
<t t-esc="tooltipItem.value"/> | ||
<t t-if="tooltipItem.percentage"> | ||
( | ||
<t t-esc="tooltipItem.percentage"/> | ||
%) | ||
</t> | ||
</small> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
</t> | ||
</templates> | ||
`; | ||
|
||
const app = new App(Component, { templates, translateFn: _t }); | ||
|
||
export function renderToString(templateName: string, context: any = {}) { | ||
return render(templateName, context).innerHTML; | ||
} | ||
|
||
function render(templateName: string, context: any = {}) { | ||
const templateFn = app.getTemplate(templateName); | ||
const bdom = templateFn(context, {}); | ||
const div = document.createElement("div"); | ||
blockDom.mount(bdom, div); | ||
return div; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters