Skip to content

Commit 8292c4c

Browse files
committed
add Pie Chart trace
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent b6155ab commit 8292c4c

File tree

6 files changed

+529
-6
lines changed

6 files changed

+529
-6
lines changed

examples/basic_charts/src/main.rs

+134-5
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: donout_pie_subplot_charts
866+
fn donout_pie_subplot_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: donout_pie_subplot_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));
@@ -857,7 +978,7 @@ fn main() {
857978
write_example_to_html(filled_lines(false), "filled_lines");
858979

859980
// Bar Charts
860-
write_example_to_html(basic_bar_chart(false), "basic_bar_chart");
981+
write_example_to_html(basic_bar_chart(true), "basic_bar_chart");
861982
write_example_to_html(grouped_bar_chart(false), "grouped_bar_chart");
862983
write_example_to_html(stacked_bar_chart(false), "stacked_bar_chart");
863984
write_example_to_html(table_chart(false), "table_chart");
@@ -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(true), "basic_pie_chart");
996+
write_example_to_html(pie_chart_text_control(true), "pie_chart_text_control");
997+
write_example_to_html(
998+
donout_pie_subplot_charts(false),
999+
"donout_pie_subplot_charts",
1000+
);
8721001
}

plotly/src/common/mod.rs

+11
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

+1-1
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

+1
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

+2
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)