charts_rs/lib.rs
1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13//! Charts support multi charts: bar chart, horizontal bar chart,
14//! line chart, pie chart, radar chart and table.
15//!
16//! It supports four themes and very easy to use.
17//! Each attribute can be customized, it can be save as svg or png.
18//! The chart can be new from json, which sets default value if the field is undefined.
19//!
20//! # New a bar chart from json, the other charts also support the function.
21//! ```rust
22//! use charts_rs::{BarChart};
23//! let bar_chart = BarChart::from_json(
24//! r###"{
25//! "width": 630,
26//! "height": 410,
27//! "margin": {
28//! "left": 10,
29//! "top": 5,
30//! "right": 10
31//! },
32//! "title_text": "Bar Chart",
33//! "title_font_color": "#345",
34//! "title_align": "right",
35//! "sub_title_text": "demo",
36//! "legend_align": "left",
37//! "series_list": [
38//! {
39//! "name": "Email",
40//! "label_show": true,
41//! "data": [120.0, 132.0, 101.0, 134.0, 90.0, 230.0, 210.0]
42//! },
43//! {
44//! "name": "Union Ads",
45//! "data": [220.0, 182.0, 191.0, 234.0, 290.0, 330.0, 310.0]
46//! }
47//! ],
48//! "x_axis_data": [
49//! "Mon",
50//! "Tue",
51//! "Wed",
52//! "Thu",
53//! "Fri",
54//! "Sat",
55//! "Sun"
56//! ]
57//! }"###,
58//! ).unwrap();
59//! println!("{}", bar_chart.svg().unwrap());
60//! ```
61//!
62//! # New bar chart with theme
63//!
64//! There are four themes: echart, dark, ant and grafana.
65//! The echart is default theme.
66//! ```rust
67//! use charts_rs::{BarChart, Series, THEME_GRAFANA};
68//! let bar_chart = BarChart::new_with_theme(
69//! vec![
70//! ("Email", vec![120.0, 132.0, 101.0, 134.0, 90.0, 230.0, 210.0]).into(),
71//! ],
72//! vec![
73//! "Mon".to_string(),
74//! "Tue".to_string(),
75//! "Wed".to_string(),
76//! "Thu".to_string(),
77//! "Fri".to_string(),
78//! "Sat".to_string(),
79//! "Sun".to_string(),
80//! ],
81//! THEME_GRAFANA,
82//! );
83//! println!("{}", bar_chart.svg().unwrap());
84//!```
85//!
86//!
87//! # Add more font
88//! The fonts will be initialized once, it can be changed before used.
89//! ```rust
90//! use charts_rs::{get_or_try_init_fonts, BarChart};
91//! let data = include_bytes!("./Roboto.ttf") as &[u8];
92//! get_or_try_init_fonts(Some(vec![data])).unwrap();
93//! let bar_chart = BarChart::from_json(
94//! r###"{
95//! "width": 630,
96//! "height": 410,
97//! "font_family": "test",
98//! "margin": {
99//! "left": 10,
100//! "top": 5,
101//! "right": 10
102//! },
103//! "title_text": "Bar Chart",
104//! "title_font_color": "#345",
105//! "title_align": "right",
106//! "sub_title_text": "demo",
107//! "legend_align": "left",
108//! "series_list": [
109//! {
110//! "name": "Email",
111//! "label_show": true,
112//! "data": [120.0, 132.0, 101.0, 134.0, 90.0, 230.0, 210.0]
113//! },
114//! {
115//! "name": "Union Ads",
116//! "data": [220.0, 182.0, 191.0, 234.0, 290.0, 330.0, 310.0]
117//! }
118//! ],
119//! "x_axis_data": [
120//! "Mon",
121//! "Tue",
122//! "Wed",
123//! "Thu",
124//! "Fri",
125//! "Sat",
126//! "Sun"
127//! ]
128//! }"###,
129//! ).unwrap();
130//! println!("{}", bar_chart.svg().unwrap());
131//! ```
132//!
133//! # Basic bar chart
134//! ```rust
135//! use charts_rs::{BarChart, Series, Box};
136//! let mut bar_chart = BarChart::new(
137//! vec![
138//! Series::new(
139//! "Email".to_string(),
140//! vec![120.0, 132.0, 101.0, 134.0, 90.0, 230.0, 210.0],
141//! ),
142//! Series::new(
143//! "Union Ads".to_string(),
144//! vec![220.0, 182.0, 191.0, 234.0, 290.0, 330.0, 310.0],
145//! ),
146//! Series::new(
147//! "Direct".to_string(),
148//! vec![320.0, 332.0, 301.0, 334.0, 390.0, 330.0, 320.0],
149//! ),
150//! Series::new(
151//! "Search Engine".to_string(),
152//! vec![820.0, 932.0, 901.0, 934.0, 1290.0, 1330.0, 1320.0],
153//! )
154//! ],
155//! vec![
156//! "Mon".to_string(),
157//! "Tue".to_string(),
158//! "Wed".to_string(),
159//! "Thu".to_string(),
160//! "Fri".to_string(),
161//! "Sat".to_string(),
162//! "Sun".to_string(),
163//! ],
164//! );
165//! bar_chart.y_axis_configs[0].axis_width = Some(55.0);
166//! bar_chart.title_text = "Bar Chart".to_string();
167//! bar_chart.legend_margin = Some(Box {
168//! top: 35.0,
169//! bottom: 10.0,
170//! ..Default::default()
171//! });
172//! println!("{}", bar_chart.svg().unwrap());
173//!```
174//!
175//! # Bar line mixin chart
176//! ```rust
177//! use charts_rs::{BarChart, Series, Box, SeriesCategory};
178//! let mut bar_chart = BarChart::new(
179//! vec![
180//! Series::new(
181//! "Email".to_string(),
182//! vec![120.0, 132.0, 101.0, 134.0, 90.0, 230.0, 210.0],
183//! ),
184//! Series::new(
185//! "Union Ads".to_string(),
186//! vec![220.0, 182.0, 191.0, 234.0, 290.0, 330.0, 310.0],
187//! ),
188//! Series::new(
189//! "Direct".to_string(),
190//! vec![320.0, 332.0, 301.0, 334.0, 390.0, 330.0, 320.0],
191//! ),
192//! Series::new(
193//! "Search Engine".to_string(),
194//! vec![820.0, 932.0, 901.0, 934.0, 1290.0, 1330.0, 1320.0],
195//! )
196//! ],
197//! vec![
198//! "Mon".to_string(),
199//! "Tue".to_string(),
200//! "Wed".to_string(),
201//! "Thu".to_string(),
202//! "Fri".to_string(),
203//! "Sat".to_string(),
204//! "Sun".to_string(),
205//! ],
206//! );
207//! bar_chart.series_list[0].category = Some(SeriesCategory::Line);
208//! bar_chart.y_axis_configs[0].axis_width = Some(55.0);
209//! bar_chart.title_text = "Bar Line Chart".to_string();
210//! bar_chart.legend_margin = Some(Box {
211//! top: 35.0,
212//! bottom: 10.0,
213//! ..Default::default()
214//! });
215//! println!("{}", bar_chart.svg().unwrap());
216//! ```
217//!
218//!
219//! # Basic horizontal bar
220//! ```rust
221//! use charts_rs::{HorizontalBarChart, Series, Align};
222//! let mut horizontal_bar_chart = HorizontalBarChart::new(
223//! vec![
224//! Series::new(
225//! "2011".to_string(),
226//! vec![18203.0, 23489.0, 29034.0, 104970.0, 131744.0, 630230.0],
227//! ),
228//! Series::new(
229//! "2012".to_string(),
230//! vec![19325.0, 23438.0, 31000.0, 121594.0, 134141.0, 681807.0],
231//! ),
232//! ],
233//! vec![
234//! "Brazil".to_string(),
235//! "Indonesia".to_string(),
236//! "USA".to_string(),
237//! "India".to_string(),
238//! "China".to_string(),
239//! "World".to_string(),
240//! ],
241//! );
242//! horizontal_bar_chart.title_text = "World Population".to_string();
243//! horizontal_bar_chart.margin.right = 15.0;
244//! horizontal_bar_chart.series_list[0].label_show = true;
245//! horizontal_bar_chart.title_align = Align::Left;
246//! println!("{}", horizontal_bar_chart.svg().unwrap());
247//! ```
248//!
249//! # Basic line chart
250//! ```rust
251//! use charts_rs::{LineChart, Box};
252//! let mut line_chart = LineChart::new(
253//! vec![
254//! ("Email", vec![120.0, 132.0, 101.0, 134.0, 90.0, 230.0, 210.0]).into(),
255//! ("Union Ads", vec![220.0, 182.0, 191.0, 234.0, 290.0, 330.0, 310.0]).into(),
256//! ("Direct", vec![320.0, 332.0, 301.0, 334.0, 390.0, 330.0, 320.0]).into(),
257//! ("Search Engine", vec![820.0, 932.0, 901.0, 934.0, 1290.0, 1330.0, 1320.0]).into(),
258//! ],
259//! vec![
260//! "Mon".to_string(),
261//! "Tue".to_string(),
262//! "Wed".to_string(),
263//! "Thu".to_string(),
264//! "Fri".to_string(),
265//! "Sat".to_string(),
266//! "Sun".to_string(),
267//! ],
268//! );
269//! line_chart.title_text = "Stacked Area Chart".to_string();
270//! line_chart.sub_title_text = "Hello World".to_string();
271//! line_chart.legend_margin = Some(Box {
272//! top: 50.0,
273//! bottom: 10.0,
274//! ..Default::default()
275//! });
276//! line_chart.series_list[3].label_show = true;
277//! println!("{}", line_chart.svg().unwrap());
278//! ```
279//!
280//! # Basic pie chart
281//! ```rust
282//! use charts_rs::{PieChart};
283//! let mut pie_chart = PieChart::new(vec![
284//! ("rose 1", vec![40.0]).into(),
285//! ("rose 2", vec![38.0]).into(),
286//! ("rose 3", vec![32.0]).into(),
287//! ("rose 4", vec![30.0]).into(),
288//! ("rose 5", vec![28.0]).into(),
289//! ("rose 6", vec![26.0]).into(),
290//! ("rose 7", vec![22.0]).into(),
291//! ("rose 8", vec![18.0]).into(),
292//! ]);
293//! pie_chart.title_text = "Nightingale Chart".to_string();
294//! pie_chart.sub_title_text = "Fake Data".to_string();
295//! println!("{}", pie_chart.svg().unwrap());
296//! ```
297//!
298//! # Basic radar chart
299//! ```rust
300//! use charts_rs::{RadarChart};
301//! let radar_chart = RadarChart::new(
302//! vec![
303//! ("Allocated Budget", vec![4200.0, 3000.0, 20000.0, 35000.0, 50000.0, 18000.0, 9000.0]).into(),
304//! ("Actual Spending", vec![5000.0, 14000.0, 28000.0, 26000.0, 42000.0, 21000.0, 7000.0]).into(),
305//! ],
306//! vec![
307//! ("Sales", 6500.0).into(),
308//! ("Administration", 16000.0).into(),
309//! ("Information Technology", 30000.0).into(),
310//! ("Customer Support", 38000.0).into(),
311//! ("Development", 52000.0).into(),
312//! ("Marketing", 25000.0).into(),
313//! ("Online", 10000.0).into(),
314//! ],
315//! );
316//! println!("{}", radar_chart.svg().unwrap());
317//! ```
318//!
319//! # Basic table
320//! ```rust
321//! use charts_rs::{TableChart};
322//! let mut table_chart = TableChart::new(vec![
323//! vec![
324//! "Name".to_string(),
325//! "Price".to_string(),
326//! "Change".to_string(),
327//! ],
328//! vec![
329//! "Datadog Inc".to_string(),
330//! "97.32".to_string(),
331//! "-7.49%".to_string(),
332//! ],
333//! vec![
334//! "Hashicorp Inc".to_string(),
335//! "28.66".to_string(),
336//! "-9.25%".to_string(),
337//! ],
338//! vec![
339//! "Gitlab Inc".to_string(),
340//! "51.63".to_string(),
341//! "+4.32%".to_string(),
342//! ],
343//! ]);
344//! table_chart.title_text = "NASDAQ".to_string();
345//! println!("{}", table_chart.svg().unwrap());
346//! ```
347
348mod charts;
349pub use charts::*;
350
351const VERSION: &str = env!("CARGO_PKG_VERSION");
352pub fn version() -> String {
353 VERSION.to_string()
354}