1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*!
Charming is a powerful and versatile chart rendering library for Rust that
leverages the power of [Apache Echarts](https://echarts.apache.org/en/index.html)
to deliver high-quality data visualization. Built with the Rust programming
language, this library aims to provide the Rust ecosystem with an intuitive
and effective way to generate and visualize charts, using a declarative and
user-friendly API.
## Basic Usage
Refer to the documentation of the [`Chart`] struct for how to create a chart
with various components.
Once you create a chart, you can render it into various format. Charming
provides three types of renderers:
- **HTML renderer**: [`HtmlRenderer`] renders a chart into an HTML fragments and
offloads the actual rendering to user's web browser for an interactive,
seamless experience. This renderer is useful when you want to render a chart
on the client side, e.g., in a web application.
- **Image renderer**: [`ImageRenderer`] renders a chart into an image file. This
renderer makes use of an embed [deno_core](https://github.com/denoland/deno_core)
engine to execute the JavaScript code of Echarts and generate an image file.
This renderer is disabled by default, and you need to enable the `ssr`
(Server-Side Rendering) feature to use it.
To render raster images like PNG the `ssr-raster` feature must also be enabled.
- **WASM renderer**: `WasmRenderer` renders a chart in a WebAssembly runtime.
This renderer is disabled by default, and you need to enable the `wasm`
feature to use it. Note that the `wasm` feature and `ssr` feature are
mutually exclusive.
Here is an example of drawing a simple pie chart into an SVG file:
```rust
use charming::{
component::Legend,
element::ItemStyle,
series::{Pie, PieRoseType},
Chart, ImageRenderer
};
let chart = Chart::new()
.legend(Legend::new().top("bottom"))
.series(
Pie::new()
.name("Nightingale Chart")
.rose_type(PieRoseType::Radius)
.radius(vec!["50", "250"])
.center(vec!["50%", "50%"])
.item_style(ItemStyle::new().border_radius(8))
.data(vec![
(40.0, "rose 1"),
(38.0, "rose 2"),
(32.0, "rose 3"),
(30.0, "rose 4"),
(28.0, "rose 5"),
(26.0, "rose 6"),
(22.0, "rose 7"),
(18.0, "rose 8"),
]),
);
let mut renderer = ImageRenderer::new(1000, 800);
renderer.save(&chart, "/tmp/nightingale.svg");
```
## Themes
Charming supports a number of themes out of the box. You can use the
[`theme::Theme`] enum to specify a theme for your chart. For instance, the
following code snippet shows how to use the `Westeros` theme:
```rust
use charming::{Chart, ImageRenderer};
use charming::theme::Theme;
use charming::component::Title;
ImageRenderer::new(1000, 800).theme(Theme::Westeros).save(
&Chart::new().title(Title::new().text("Westeros")),
"/tmp/westeros.svg",
);
```
Future versions of Charming will support custom themes.
*/
pub use *;
use CharmingSetters;
use ;
use Dataset;
use ;
use ;
use ;
use Series;
/**
The chart representation.
## Anatomy of a Chart
A chart is a collection of different components, each of which is responsible
for rendering a specific part of the chart. Below is a sample chart with a
few components:
```txt
Sales Report
| # coffee
30| x x juice
| @ x @ @ milk
20| # @ x@ x@ #
| #x@ #x@ #x@ #x
10| #x@ #x@ #x@ #x@
| #x@ #x@ #x@ #x@
0+-----------------------------------------------------
Jan Feb Mar Apr
```
The chart above has the following components: **an x axis**, **an y axis**,
**a title** on the top center, and **a legend** on the top right.
The creation of charts in Charming is done in a builder-like fashion. Once you
get a hang of this pattern, you will find that it is very easy to compose a
chart. For instance, the following code snippet shows how to create the chart
above:
```rust
use charming::Chart;
use charming::component::{Axis, Legend, Title};
let chart = Chart::new()
.title(Title::new().text("Sales Report"))
.x_axis(Axis::new().data(vec!["Jan", "Feb", "Mar", "Apr"]))
.y_axis(Axis::new())
.legend(Legend::new().data(vec!["coffee", "juice", "milk"]));
```
## Components of a Chart
The following sections describe the components of a chart in detail.
### Title
[`Title`] is the title of a chart, including main title and subtitle. A chart
can have multiple titles, which is useful when you want to show multiple sub-
charts in a single chart.
```rust
use charming::Chart;
use charming::component::Title;
let chart = Chart::new()
.title(Title::new().text("Sales Report"));
```
### Legend
[`Legend`](crate::component::Legend) is the legend of a chart, which is used to show the meaning of the
symbols and colors in the chart. A chart can have multiple legends.
```rust
use charming::Chart;
use charming::component::Legend;
let chart = Chart::new()
.legend(Legend::new().data(vec!["coffee", "juice", "milk"]));
```
### Grid
[`Grid`] is the background grid in a cartesian coordinate system. A chart can
have multiple grids.
```rust
use charming::Chart;
use charming::component::Grid;
let chart = Chart::new()
.grid(Grid::new());
```
### X Axis and Y Axis
[`Axis`] is the axis in a cartesian coordinate system.
```rust
use charming::Chart;
use charming::component::Axis;
let chart = Chart::new()
.x_axis(Axis::new().data(vec!["Jan", "Feb", "Mar", "Apr"]))
.y_axis(Axis::new());
```
### Polar Coordinate
[`PolarCoordinate`] is the polar coordinate system. Polar coordinate can be used in
scatter and line charts. Every polar coordinate has an [`AngleAxis`] and a
[`RadiusAxis`].
### Radar Coordinate
[`RadarCoordinate`] is the radar coordinate system. Radar coordinate can be in
radar charts.
### Data Zoom
[`DataZoom`] is used for zooming a specific area, which enables user to view
data in different scales. A chart can have multiple data zooms.
### Visual Map
[`VisualMap`] is a visual encoding component. It maps data to visual channels,
such as color, symbol size or symbol shape. A chart can have multiple visual
maps.
### Tooltip
[`Tooltip`] is a floating box that appears when user hovers over a data item.
### AxisPointer
[`AxisPointer`] is a tool for displaying reference line and axis value under
mouse pointer.
### Toolbox
[`Toolbox`] is a feature toolbox that includes data view, save as image, data
zoom, restore, and reset.
*/