Skip to content

Commit

Permalink
Add event (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc authored Oct 4, 2024
1 parent 774d471 commit ace19bd
Show file tree
Hide file tree
Showing 10 changed files with 554 additions and 80 deletions.
51 changes: 51 additions & 0 deletions CONTRIBUTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Contributing

## Development install

Note: You will need `markdownify`, `autodoc-traits`, `sphinx` to generate the code

The `jlpm` command is JupyterLab's pinned version of
[yarn](https://yarnpkg.com/) that is installed with JupyterLab. You may use
`yarn` or `npm` in lieu of `jlpm` below.

```bash
# Clone the repo to your local environment
# Change directory to the ipecharts directory
# Install package in development mode
pip install -e "."
# Link your development version of the extension with JupyterLab
jupyter labextension develop . --overwrite
# Rebuild extension Typescript source after making changes
jlpm build
```

You can watch the source directory and run JupyterLab at the same time in different terminals to watch for changes in the extension's source and automatically rebuild the extension.

```bash
# Watch the source directory in one terminal, automatically rebuilding when needed
jlpm watch
# Run JupyterLab in another terminal
jupyter lab
```

With the watch command running, every saved change will immediately be built locally and available in your running JupyterLab. Refresh JupyterLab to load the change in your browser (you may need to wait several seconds for the extension to be rebuilt).

By default, the `jlpm build` command generates the source maps for this extension to make it easier to debug using the browser dev tools. To also generate source maps for the JupyterLab core extensions, you can run the following command:

```bash
jupyter lab build --minimize=False
```

### Development uninstall

```bash
pip uninstall ipecharts
```

In development mode, you will also need to remove the symlink created by `jupyter labextension develop`
command. To find its location, you can run `jupyter labextension list` to figure out where the `labextensions`
folder is located. Then you can remove the symlink named `ipecharts` within that folder.

### Packaging the extension

See [RELEASE](RELEASE.md)
72 changes: 27 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ display(button, chart)

![ipechart2](./docs/source/images/ipechart3.gif)

### Customize the chart initialization

Both `EChartsWidget` and `EChartsRawWidget` classes can customize the [Echarts init parameters](https://echarts.apache.org/en/api.html#echarts.init), for example:

```python

chart = EChartsWidget(
option=option, renderer="svg", width="300px", height="300px", use_dirty_rect=True
)

```

The init parameters need to be converted to the snake case format.

### Customize the chart container style

Both `EChartsWidget` and `EChartsRawWidget` classes allow you to customize the style of the chart container by setting the style attribute. The style attribute accepts a dictionary where keys are CSS property names in camelCase or kebab-case (as strings), and values are the corresponding CSS values.
Expand Down Expand Up @@ -222,54 +236,22 @@ chart.style = {

https://github.com/user-attachments/assets/e4245101-8dff-40a9-a4d4-1f75a06b88c4

## Contributing

### Development install

Note: You will need `markdownify`, `autodoc-traits`, `sphinx` to generate the code
### Event handling

The `jlpm` command is JupyterLab's pinned version of
[yarn](https://yarnpkg.com/) that is installed with JupyterLab. You may use
`yarn` or `npm` in lieu of `jlpm` below.

```bash
# Clone the repo to your local environment
# Change directory to the ipecharts directory
# Install package in development mode
pip install -e "."
# Link your development version of the extension with JupyterLab
jupyter labextension develop . --overwrite
# Rebuild extension Typescript source after making changes
jlpm build
```
Event-handling functions can be added to `EChartsWidget` and `EChartsRawWidget` using the same syntax as [in the Javascript version](https://echarts.apache.org/handbook/en/concepts/event/):

You can watch the source directory and run JupyterLab at the same time in different terminals to watch for changes in the extension's source and automatically rebuild the extension.

```bash
# Watch the source directory in one terminal, automatically rebuilding when needed
jlpm watch
# Run JupyterLab in another terminal
jupyter lab
```
```python
chart = EChartsWidget(option=option)

With the watch command running, every saved change will immediately be built locally and available in your running JupyterLab. Refresh JupyterLab to load the change in your browser (you may need to wait several seconds for the extension to be rebuilt).
def callback(params):
print(params)

By default, the `jlpm build` command generates the source maps for this extension to make it easier to debug using the browser dev tools. To also generate source maps for the JupyterLab core extensions, you can run the following command:
# Add event handlers
chart.on('click', None, callback) # Listen to all click event
chart.on('click', 'series.line', callback) # Using string query
chart.on('mouseover', {'seriesIndex': 1, 'name': 'xx'}, callback) # Using object query

```bash
jupyter lab build --minimize=False
# Remove event handlers
chart.off('click') # Remove all handler on click event
chart.off('mouseover', callback) # Remove selected handler.
```

### Development uninstall

```bash
pip uninstall ipecharts
```

In development mode, you will also need to remove the symlink created by `jupyter labextension develop`
command. To find its location, you can run `jupyter labextension list` to figure out where the `labextensions`
folder is located. Then you can remove the symlink named `ipecharts` within that folder.

### Packaging the extension

See [RELEASE](RELEASE.md)
36 changes: 36 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ By using the Traitlets to configure your widget. You can use `EChartsWidget` wit
.. note::
Reference to the series configurations can be found `here <https://ipecharts.readthedocs.io/en/latest/api/ipecharts.option.seriesitems.html>`_, but it is more legible to use directly the `documentation of Echarts <https://echarts.apache.org/en/option.html>`_.


Customize the chart initialization
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Both ``EChartsWidget`` and ``EChartsRawWidget`` classes can customize the `Echarts init parameters <https://echarts.apache.org/en/api.html#echarts.init>`, for example:

.. code-block:: python
chart = EChartsWidget(
option=option, renderer="svg", width="300px", height="300px", use_dirty_rect=True
)
The init parameters need to be converted to the snake case format.

Customize the chart container style
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -221,6 +235,28 @@ After the widget has been created and displayed, you can update its style by mod
# The widget will automatically update to reflect the new styles.
Event handling
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Event-handling functions can be added to ``EChartsWidget`` and ``EChartsRawWidget`` using the same syntax as `in the Javascript version <https://echarts.apache.org/handbook/en/concepts/event/>`:

.. code-block:: python
chart = EChartsWidget(option=option)
def callback(params):
print(params)
# Add event handlers
chart.on('click', None, callback) # Listen to all click event
chart.on('click', 'series.line', callback) # Using string query
chart.on('mouseover', {'seriesIndex': 1, 'name': 'xx'}, callback) # Using object query
# Remove event handlers
chart.off('click') # Remove all handler on click event
chart.off('mouseover', callback) # Remove selected handler.
API Reference
********************************

Expand Down
139 changes: 139 additions & 0 deletions example/event.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "e1cb5ebc-28e1-4839-87d6-d0fd9cb6991c",
"metadata": {},
"outputs": [],
"source": [
"from ipecharts.echarts import EChartsWidget, EChartsRawWidget\n",
"import json\n",
"import numpy\n",
"from ipecharts.option import Option, XAxis, YAxis, Legend, Tooltip\n",
"from ipecharts.option.series import Bar\n",
"from ipywidgets.widgets import Button"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2ae706ab-ae66-49ec-b389-edddea804671",
"metadata": {},
"outputs": [],
"source": [
"line = Bar()\n",
"line2 = Bar()\n",
"option = Option(\n",
" xAxis=XAxis(type=\"category\"),\n",
" yAxis=YAxis(type=\"value\"),\n",
" series=[line, line2],\n",
" tooltip=Tooltip(),\n",
" legend=Legend()\n",
")\n",
"line.data = [1.,4.6,3.,6,4,7,9,3,9,3]\n",
"line2.data = [10.,2.6,13.,4,7,2,5,13,9,5]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a83f0754-facd-4e7f-bd91-d2566dfcc77d",
"metadata": {},
"outputs": [],
"source": [
"chart = EChartsWidget(option=option)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "81f3047b-51f2-4975-b81d-99a35373c01e",
"metadata": {},
"outputs": [],
"source": [
"chart"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "530683e3-0386-4262-9df0-460500250dbf",
"metadata": {},
"outputs": [],
"source": [
"def callback(params):\n",
" print('####', params)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1dbbf62c-84be-4b31-a3ab-ddef9bc5dd84",
"metadata": {},
"outputs": [],
"source": [
"chart.on('click', None, callback)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "089c9319-f8d5-4156-a9b6-3a566be6a407",
"metadata": {},
"outputs": [],
"source": [
"chart.off('click')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b928fa5-1e4c-4c4a-8c6d-8d9e7977315d",
"metadata": {},
"outputs": [],
"source": [
"chart.on('mouseover', {'seriesIndex': 1}, callback)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05b541b9-470e-499b-97ce-c815e5b43e1e",
"metadata": {},
"outputs": [],
"source": [
"chart.off('mouseover')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9b819710-aad2-4294-9d7b-a87c7d116fe4",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit ace19bd

Please sign in to comment.