Skip to content

Commit 9dcc941

Browse files
committed
add Pie Chart trace
- add examples - add section in mdBook Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent b6155ab commit 9dcc941

File tree

10 files changed

+567
-9
lines changed

10 files changed

+567
-9
lines changed

docs/book/src/SUMMARY.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
- [Basic Charts](./recipes/basic_charts.md)
1111
- [Scatter Plots](./recipes/basic_charts/scatter_plots.md)
1212
- [Line Charts](./recipes/basic_charts/line_charts.md)
13-
- [Bar Charts](./recipes/basic_charts/bar_charts.md)
14-
- [Sankey Diagrams](./recipes/basic_charts/sankey_diagrams.md)
13+
- [Bar Charts](./recipes/basic_charts/bar_charts.md)
14+
- [Pie Charts](./recipes/basic_charts/pie_charts.md)
15+
- [Sankey Diagrams](./recipes/basic_charts/sankey_diagrams.md)
1516
- [Statistical Charts](./recipes/statistical_charts.md)
1617
- [Error Bars](./recipes/statistical_charts/error_bars.md)
1718
- [Box Plots](./recipes/statistical_charts/box_plots.md)
1819
- [Histograms](./recipes/statistical_charts/histograms.md)
1920
- [Scientific Charts](./recipes/scientific_charts.md)
2021
- [Contour Plots](./recipes/scientific_charts/contour_plots.md)
21-
- [Heatmaps](./recipes/scientific_charts/heatmaps.md)
22+
- [Heatmaps](./recipes/scientific_charts/heatmaps.md)
2223
- [Financial Charts](./recipes/financial_charts.md)
2324
- [Time Series and Date Axes](./recipes/financial_charts/time_series_and_date_axes.md)
2425
- [Candlestick Charts](./recipes/financial_charts/candlestick_charts.md)

docs/book/src/recipes/basic_charts.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ Kind | Link
66
:---|:----:
77
Scatter Plots |[![Scatter Plots](./img/line_and_scatter_plot.png)](./basic_charts/scatter_plots.md)
88
Line Charts | [![Line Charts](./img/line_shape_options_for_interpolation.png)](./basic_charts/line_charts.md)
9-
Bar Charts | [![Scatter Plots](./img/bar_chart_with_error_bars.png)](./basic_charts/scatter_plots.md)
9+
Bar Charts | [![Bar Charts](./img/bar_chart_with_error_bars.png)](./basic_charts/scatter_plots.md)
10+
Pie Charts | [![Pie Charts](./img/pie_charts.png)](./basic_charts/pie_charts.md)
1011
Sankey Diagrams | [![Sankey Diagrams](./img/basic_sankey.png)](./basic_charts/sankey_diagrams.md)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Pie Charts
2+
3+
The following imports have been used to produce the plots below:
4+
5+
```rust,no_run
6+
use plotly::common::{Domain, Font, HoverInfo, Orientation};
7+
use plotly::layout::{
8+
Annotation, Layout, LayoutGrid},
9+
use plotly::layout::Layout;
10+
use plotly::{Pie, Plot};
11+
```
12+
13+
The `to_inline_html` method is used to produce the html plot displayed in this page.
14+
15+
16+
## Basic Pie Chart
17+
```rust,no_run
18+
{{#include ../../../../../examples/basic_charts/src/main.rs:basic_pie_chart}}
19+
```
20+
21+
{{#include ../../../../../examples/basic_charts/out/basic_pie_chart.html}}
22+
23+
## Grouped Pie Chart
24+
```rust,no_run
25+
{{#include ../../../../../examples/basic_charts/src/main.rs:grouped_donout_pie_charts}}
26+
```
27+
28+
{{#include ../../../../../examples/basic_charts/out/grouped_donout_pie_charts.html}}
29+
30+
## Pie Chart Text Control
31+
```rust,no_run
32+
{{#include ../../../../../examples/basic_charts/src/main.rs:pie_chart_text_control}}
33+
```
34+
35+
{{#include ../../../../../examples/basic_charts/out/pie_chart_text_control.html}}
36 KB
Loading

examples/basic_charts/src/main.rs

Lines changed: 133 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ use ndarray::Array;
44
use plotly::{
55
color::{NamedColor, Rgb, Rgba},
66
common::{
7-
ColorScale, ColorScalePalette, DashType, Fill, Font, Line, LineShape, Marker, Mode,
8-
Orientation, Pattern, PatternShape,
7+
ColorScale, ColorScalePalette, DashType, Domain, Fill, Font, HoverInfo, Line, LineShape,
8+
Marker, Mode, Orientation, Pattern, PatternShape,
9+
},
10+
layout::{
11+
Annotation, Axis, BarMode, CategoryOrder, Layout, LayoutGrid, Legend, TicksDirection,
12+
TraceOrder,
913
},
10-
layout::{Axis, BarMode, CategoryOrder, Layout, Legend, TicksDirection, TraceOrder},
1114
sankey::{Line as SankeyLine, Link, Node},
1215
traces::table::{Cells, Header},
13-
Bar, Plot, Sankey, Scatter, ScatterPolar, Table,
16+
Bar, Pie, Plot, Sankey, Scatter, ScatterPolar, Table,
1417
};
1518
use rand_distr::{Distribution, Normal, Uniform};
1619

@@ -819,6 +822,124 @@ fn table_chart(show: bool) -> Plot {
819822
}
820823
// ANCHOR_END: table_chart
821824

825+
// Pie Charts
826+
// ANCHOR: basic_pie_chart
827+
fn basic_pie_chart(show: bool) -> Plot {
828+
let values = vec![2, 3, 5];
829+
let labels = vec!["giraffes", "orangutans", "monkeys"];
830+
let t = Pie::new(values).labels(labels);
831+
let mut plot = Plot::new();
832+
plot.add_trace(t);
833+
834+
if show {
835+
plot.show();
836+
}
837+
plot
838+
}
839+
// ANCHOR_END: basic_pie_chart
840+
841+
// ANCHOR: pie_chart_text_control
842+
fn pie_chart_text_control(show: bool) -> Plot {
843+
let values = vec![2, 3, 4, 4];
844+
let labels = vec!["Wages", "Operating expenses", "Cost of sales", "Insurance"];
845+
let t = Pie::new(values)
846+
.labels(labels)
847+
.automargin(true)
848+
.show_legend(true)
849+
.text_position(plotly::common::Position::Outside)
850+
.name("Costs")
851+
.text_info("label+percent");
852+
let mut plot = Plot::new();
853+
plot.add_trace(t);
854+
855+
let layout = Layout::new().height(700).width(700).show_legend(true);
856+
plot.set_layout(layout);
857+
858+
if show {
859+
plot.show();
860+
}
861+
plot
862+
}
863+
// ANCHOR_END: pie_chart_text_control
864+
865+
// ANCHOR: grouped_donout_pie_charts
866+
fn grouped_donout_pie_charts(show: bool) -> Plot {
867+
let mut plot = Plot::new();
868+
869+
let values = vec![16, 15, 12, 6, 5, 4, 42];
870+
let labels = vec![
871+
"US",
872+
"China",
873+
"European Union",
874+
"Russian Federation",
875+
"Brazil",
876+
"India",
877+
"Rest of World",
878+
];
879+
let t = Pie::new(values)
880+
.labels(labels)
881+
.name("GHG Emissions")
882+
.hover_info(HoverInfo::All)
883+
.text("GHG")
884+
.hole(0.4)
885+
.domain(Domain::new().column(0));
886+
plot.add_trace(t);
887+
888+
let values = vec![27, 11, 25, 8, 1, 3, 25];
889+
let labels = vec![
890+
"US",
891+
"China",
892+
"European Union",
893+
"Russian Federation",
894+
"Brazil",
895+
"India",
896+
"Rest of World",
897+
];
898+
899+
let t = Pie::new(values)
900+
.labels(labels)
901+
.name("CO2 Emissions")
902+
.hover_info(HoverInfo::All)
903+
.text("CO2")
904+
.text_position(plotly::common::Position::Inside)
905+
.hole(0.4)
906+
.domain(Domain::new().column(1));
907+
plot.add_trace(t);
908+
909+
let layout = Layout::new()
910+
.title("Global Emissions 1990-2011")
911+
.height(400)
912+
.width(600)
913+
.annotations(vec![
914+
Annotation::new()
915+
.font(Font::new().size(20))
916+
.show_arrow(false)
917+
.text("GHG")
918+
.x(0.17)
919+
.y(0.5),
920+
Annotation::new()
921+
.font(Font::new().size(20))
922+
.show_arrow(false)
923+
.text("CO2")
924+
.x(0.82)
925+
.y(0.5),
926+
])
927+
.show_legend(false)
928+
.grid(
929+
LayoutGrid::new()
930+
.columns(2)
931+
.rows(1)
932+
.pattern(plotly::layout::GridPattern::Independent),
933+
);
934+
plot.set_layout(layout);
935+
936+
if show {
937+
plot.show();
938+
}
939+
plot
940+
}
941+
// ANCHOR_END: grouped_donout_pie_charts
942+
822943
fn write_example_to_html(plot: Plot, name: &str) {
823944
std::fs::create_dir_all("./out").unwrap();
824945
let html = plot.to_inline_html(Some(name));
@@ -869,4 +990,12 @@ fn main() {
869990

870991
// Sankey Diagrams
871992
write_example_to_html(basic_sankey_diagram(false), "basic_sankey_diagram");
993+
994+
// Pie Charts
995+
write_example_to_html(basic_pie_chart(false), "basic_pie_chart");
996+
write_example_to_html(pie_chart_text_control(false), "pie_chart_text_control");
997+
write_example_to_html(
998+
grouped_donout_pie_charts(false),
999+
"grouped_donout_pie_charts",
1000+
);
8721001
}

plotly/src/common/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,16 @@ pub enum ConstrainText {
153153

154154
#[derive(Serialize, Clone, Debug)]
155155
pub enum Orientation {
156+
#[serde(rename = "a")]
157+
Auto,
156158
#[serde(rename = "v")]
157159
Vertical,
158160
#[serde(rename = "h")]
159161
Horizontal,
162+
#[serde(rename = "r")]
163+
Radial,
164+
#[serde(rename = "t")]
165+
Tangential,
160166
}
161167

162168
#[derive(Serialize, Clone, Debug)]
@@ -225,6 +231,7 @@ pub enum PlotType {
225231
Surface,
226232
DensityMapbox,
227233
Table,
234+
Pie,
228235
}
229236

230237
#[derive(Serialize, Clone, Debug)]
@@ -273,6 +280,10 @@ pub enum Position {
273280
BottomCenter,
274281
#[serde(rename = "bottom right")]
275282
BottomRight,
283+
#[serde(rename = "inside")]
284+
Inside,
285+
#[serde(rename = "outside")]
286+
Outside,
276287
}
277288

278289
#[derive(Serialize, Clone, Debug)]

plotly/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub use traces::{
3636
// Bring the different trace types into the top-level scope
3737
pub use traces::{
3838
Bar, BoxPlot, Candlestick, Contour, DensityMapbox, HeatMap, Histogram, Image, Mesh3D, Ohlc,
39-
Sankey, Scatter, Scatter3D, ScatterMapbox, ScatterPolar, Surface, Table,
39+
Pie, Sankey, Scatter, Scatter3D, ScatterMapbox, ScatterPolar, Surface, Table,
4040
};
4141

4242
pub trait Restyle: serde::Serialize {}

plotly/src/plot.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ impl PartialEq for Plot {
585585
mod tests {
586586
use std::path::PathBuf;
587587

588+
#[cfg(feature = "kaleido")]
588589
use base64::{engine::general_purpose, Engine as _};
589590
use serde_json::{json, to_value};
590591

plotly/src/traces/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod histogram;
1010
pub mod image;
1111
pub mod mesh3d;
1212
mod ohlc;
13+
pub mod pie;
1314
pub mod sankey;
1415
mod scatter;
1516
mod scatter3d;
@@ -27,6 +28,7 @@ pub use heat_map::HeatMap;
2728
pub use histogram::Histogram;
2829
pub use mesh3d::Mesh3D;
2930
pub use ohlc::Ohlc;
31+
pub use pie::Pie;
3032
pub use sankey::Sankey;
3133
pub use scatter::Scatter;
3234
pub use scatter3d::Scatter3D;

0 commit comments

Comments
 (0)