Skip to content

Commit

Permalink
Merge pull request #1346 from natankeddem/echarts
Browse files Browse the repository at this point in the history
Adding ECharts Element
  • Loading branch information
falkoschindler committed Aug 18, 2023
2 parents 93764f8 + e3e15d2 commit 68499ec
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 0 deletions.
1 change: 1 addition & 0 deletions DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- socket.io: 4.7.1
- es-module-shims: 1.7.3
- aggrid: 30.0.3
- echarts: 5.4.3
- highcharts: 11.1.0
- mermaid: 10.2.4
- nipplejs: 0.10.1
Expand Down
31 changes: 31 additions & 0 deletions nicegui/elements/echart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default {
template: "<div></div>",
mounted() {
setTimeout(() => {
this.chart = echarts.init(this.$el);
this.chart.setOption(this.options);
this.chart.resize();
}, 0); // NOTE: wait for window.path_prefix to be set in app.mounted()
},
beforeDestroy() {
this.destroyChart();
},
beforeUnmount() {
this.destroyChart();
},
methods: {
update_chart() {
if (this.chart) {
this.chart.setOption(this.options);
}
},
destroyChart() {
if (this.chart) {
this.chart.dispose();
}
},
},
props: {
options: Object,
},
};
27 changes: 27 additions & 0 deletions nicegui/elements/echart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Dict

from ..element import Element


class EChart(Element, component='echart.js', libraries=['lib/echarts/echarts.min.js']):

def __init__(self, options: Dict) -> None:
"""Apache EChart
An element to create a chart using `ECharts <https://echarts.apache.org/>`_.
Updates can be pushed to the chart by changing the `options` property.
After data has changed, call the `update` method to refresh the chart.
:param options: dictionary of EChart options
"""
super().__init__()
self._props['options'] = options
self._classes = ['nicegui-echart']

@property
def options(self) -> Dict:
return self._props['options']

def update(self) -> None:
super().update()
self.run_method('update_chart')
1 change: 1 addition & 0 deletions nicegui/elements/lib/echarts/echarts.js.map

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions nicegui/elements/lib/echarts/echarts.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions nicegui/static/nicegui.css
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
width: 100%;
height: 16rem;
}
.nicegui-echart {
width: 100%;
height: 16rem;
}
.nicegui-scroll-area {
width: 100%;
height: 16rem;
Expand Down
2 changes: 2 additions & 0 deletions nicegui/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'dark_mode',
'date',
'dialog',
'echart',
'expansion',
'grid',
'html',
Expand Down Expand Up @@ -111,6 +112,7 @@
from .elements.dark_mode import DarkMode as dark_mode
from .elements.date import Date as date
from .elements.dialog import Dialog as dialog
from .elements.echart import EChart as echart
from .elements.expansion import Expansion as expansion
from .elements.grid import Grid as grid
from .elements.html import Html as html
Expand Down
7 changes: 7 additions & 0 deletions npm.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
"package/dist/": ""
}
},
"echarts": {
"destination": "elements/lib",
"keep": ["package/dist/echarts\\.min\\.js", "package/dist/echarts\\.js\\.map"],
"rename": {
"package/dist/": ""
}
},
"highcharts": {
"destination": "elements/lib",
"keep": [
Expand Down
1 change: 1 addition & 0 deletions website/documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def captions_and_overlays_demo():
load_demo(ui.table)
load_demo(ui.aggrid)
load_demo(ui.chart)
load_demo(ui.echart)
if 'matplotlib' in optional_features:
load_demo(ui.pyplot)
load_demo(ui.line_plot)
Expand Down
21 changes: 21 additions & 0 deletions website/more_documentation/echart_documentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from nicegui import ui


def main_demo() -> None:
from random import random

echart = ui.echart({
'xAxis': {'type': 'value'},
'yAxis': {'type': 'category', 'data': ['A', 'B'], 'inverse': True},
'legend': {'textStyle': {'color': 'gray'}},
'series': [
{'type': 'bar', 'name': 'Alpha', 'data': [0.1, 0.2]},
{'type': 'bar', 'name': 'Beta', 'data': [0.3, 0.4]},
],
})

def update():
echart.options['series'][0]['data'][0] = random()
echart.update()

ui.button('Update', on_click=update)

0 comments on commit 68499ec

Please sign in to comment.