Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: doc/extension charts #347

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed docs/manuals/gui/extension.md
Empty file.
184 changes: 184 additions & 0 deletions docs/userman/gui/extension/chart.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# Chart

In this section, we’ll dive into creating and integrating charts using [`Plotly`](https://plotly.com/graphing-libraries/) in your extension library.
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
Charts are invaluable for visualizing data, offering clear and interactive insights that enhance the user experience.
We’ll walk through the steps to implement different types of charts, configure their properties,
and seamlessly embed them into your application.

## Prerequisites

Before you start creating charts, ensure you have the following prerequisites:

- **Install the necessary libraries:** To use Plotly.js with React, you’ll need to install the react-plotly.js library.
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved

```bash
$ npm install react-plotly.js plotly.js
```

!!!Note
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
When using TypeScript, you’ll need to install the type definitions for Plotly.js as well.

```bash
$ npm install --dev @types/react-plotly.js
```

## Declaring the element {data-source="gui:doc/extension/example_library/example_library.py#L69"}
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved

To create a chart element, you need to define the element in your extension library.
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved

```python title="example_library.py"
from taipy.gui.extension import Element, ElementLibrary, ElementProperty, PropertyType

class ExampleLibrary(ElementLibrary):
def __init__(self) -> None:
# Initialize the set of visual elements for this extension library
self.elements = {
"dashboard": Element(
"data",
{
"data": ElementProperty(PropertyType.dict),
"layout": ElementProperty(PropertyType.dict),
},
)
}
```

The detailed explanation of the code is as follows:

- The *dashboard* element includes two properties: *data* and *layout*.
- The *data* property has the type `PropertyType.dict`, meaning it holds a dictionary of data.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can PropertyType.dict be a crossref?

- The *layout* property has the type `PropertyType.dict`, meaning it holds a dictionary of layout properties.

## Creating the React component {data-source="gui:doc/extension/example_library/front-end/src/Dashboard.tsx"}
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved

The React component for the *dashboard* element is defined as follows:

```tsx title="Dashboard.tsx"
import React, { useMemo } from "react";
import { useDynamicJsonProperty, } from "taipy-gui";

import Plot from "react-plotly.js";
import { Data, Layout } from "plotly.js";

interface DashboardProps {
data?: string;
defaultData?: string;
layout?: string;
defaultLayout?: string;
}

const Dashboard = (props: DashboardProps) => {
const value = useDynamicJsonProperty(props.data, props.defaultData || "", {} as Partial<Data>);
const dashboardLayout = useDynamicJsonProperty(props.layout, props.defaultLayout || "", {} as Partial<Layout>);

const data = useMemo(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A short comment explaining why we're using Memos would be nice

if (Array.isArray(value)) {
return value as Data[];
}
return [] as Data[];
}, [value]);

const baseLayout = useMemo(() => {
const layout = {
...dashboardLayout,
};
return layout as Partial<Layout>;
}, [dashboardLayout]);

return (
<div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the div?

<Plot data={data} layout={baseLayout} />
</div>
);
};

export default Dashboard;
```

The detailed explanation of the code is as follows:

- The **Dashboard** component accepts two props: *data* and *layout*.
- The [`useDynamicJsonProperty()`](../../../refmans/reference_guiext/functions/useDynamicJsonProperty.md) hook is used to handle dynamic properties. It accepts the following arguments:

- *data*: The dynamic property that holds the data for the chart.
- *defaultData*: The default value for the data property.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- *defaultData*: The default value for the data property.
- *defaultData*: The default value for the *data* property.

- *layout*: The dynamic property that holds the layout properties for the chart.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- *layout*: The dynamic property that holds the layout properties for the chart.
- *layout*: The dynamic property that holds the *layout* properties for the chart.

- *defaultLayout*: The default value for the layout property.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- *defaultLayout*: The default value for the layout property.
- *defaultLayout*: The default value for the *layout* property.


- The **Plot** component utilizes the [`react-plotly.js`](https://github.com/plotly/react-plotly.js) library to render the chart. It accepts two props:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- The **Plot** component utilizes the [`react-plotly.js`](https://github.com/plotly/react-plotly.js) library to render the chart. It accepts two props:
- The **Plot** component utilizes the [`react-plotly.js`](https://github.com/plotly/react-plotly.js) library to render the chart. It accepts two attributes:


- *data*: An array of data objects to define the chart's datasets.
- *layout*: An object specifying the layout properties of the chart.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- *layout*: An object specifying the layout properties of the chart.
- *layout*: An object specifying the layout of the chart.


For more information on how to use `react-plotly.js`, refer to the official documentation [here](https://github.com/plotly/react-plotly.js).



## Exporting the React component {data-source="gui:doc/extension/example_library/front-end/src/index.ts"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Exporting the React component {data-source="gui:doc/extension/example_library/front-end/src/index.ts"}
## Exporting the React component {data-source="gui/extension/example_library/front-end/src/index.ts"}


When the component is entirely defined, it must be exported by the library's JavaScript bundle.
This is done by adding the export directive in the file *<project dir>/<package dir>front-end/src/index.ts*.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is done by adding the export directive in the file *<project dir>/<package dir>front-end/src/index.ts*.
This is done by adding the `export` directive in the file *<project dir>/<package dir>front-end/src/index.ts*.

is there a missing '/' before "<package_dir>"?


```ts title="index.ts"
import Dashboard from "./Dashboard";

export { Dashboard };
```

## Configuring the webpack configuration {data-source="gui:doc/extension/example_library/front-end/webpack.config.js#L39"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Configuring the webpack configuration {data-source="gui:doc/extension/example_library/front-end/webpack.config.js#L39"}
## Configuring the webpack configuration {data-source="gui/extension/example_library/front-end/webpack.config.js#L39"}


To bundle the React component, you must configure the webpack configuration file.
Since Plotly.js is a JavaScript library, you need to include JavaScript in the webpack’s resolve.extensions setting.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Since Plotly.js is a JavaScript library, you need to include JavaScript in the webpack’s resolve.extensions setting.
Since Plotly.js is a JavaScript library, you need to include JavaScript in the webpack’s *resolve.extensions* setting.


```js title="webpack.config.js"
module.exports = {
// ...
resolve: {
extensions: [".ts", ".tsx", ".js"],
},
// ...
};
```

After configuring the webpack file, you can build the library as mentioned in the [Building the front-end module](dynamic_element/index.md) section.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure about the crossref?


## Using the element {data-source="gui:doc/extension/example_library/dashboard.py"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Using the element {data-source="gui:doc/extension/example_library/dashboard.py"}
## Using the element {data-source="gui/extension/example_library/dashboard.py"}


To use the *dashboard* element in a Python script, you can follow the example below:

```python title="dashboard.py"
trace1 = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename 'trace' to 'set', to align with the name.
The point is: at the dataset level, we're not talking about traces. This happens only when displayed.

"x": [1, 2, 3, 4, 4, 4, 8, 9, 10],
"type": "box",
"name": "Set 1"
}

trace2 = {
"x": [2, 3, 3, 3, 3, 5, 6, 6, 7],
"type": "box",
"name": "Set 2"
}

data = [trace1, trace2]

layout = {
"title": {
"text": "Horizontal Box Plot"
}
}

page = """
<|{data}|dashboard|layout={layout}|>
"""
```

In this example, we define two traces and a layout for a horizontal box plot.
When you run the script, the *dashboard* element will display like this:

<figure>
<img src="../dashboard.png" alt="Horizontal Box Plot">
<figcaption>Horizontal Box Plot</figcaption>
</figure>



Binary file added docs/userman/gui/extension/dashboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/userman/gui/extension/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,6 @@ make your way from one example to the next.
- [Using scalar properties](dynamic_element/scalar_props.md)
- [Using List of Values](list_of_values.md)
- [Using tabular data](tabular_data.md)
- [Using charts](chart.md)
- [Accessing assets](accessing_assets.md)
- [Packaging an element library](extension_packaging.md)
1 change: 1 addition & 0 deletions mkdocs.yml_template
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ nav:
- "Scalar properties": userman/gui/extension/dynamic_element/scalar_props.md
- "List of Values Elements": userman/gui/extension/list_of_values.md
- "Tabular Elements": userman/gui/extension/tabular_data.md
- "Chart Elements": userman/gui/extension/chart.md
- "Accessing assets": userman/gui/extension/accessing_assets.md
- "Packaging extensions": userman/gui/extension/extension_packaging.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we move this section (packaging) as soon as possible?


Expand Down
Loading