Pie Charts
The following imports have been used to produce the plots below:
#![allow(unused)] fn main() { use plotly::common::{Domain, Font, HoverInfo, Orientation}; use plotly::layout::{ Annotation, Layout, LayoutGrid}, use plotly::layout::Layout; use plotly::{Pie, Plot}; }
The to_inline_html
method is used to produce the html plot displayed in this page.
Basic Pie Chart
#![allow(unused)] fn main() { fn basic_pie_chart(show: bool) -> Plot { let values = vec![2, 3, 4]; let labels = vec!["giraffes", "orangutans", "monkeys"]; let t = Pie::new(values).labels(labels); let mut plot = Plot::new(); plot.add_trace(t); if show { plot.show(); } plot } }
#![allow(unused)] fn main() { fn basic_pie_chart_labels(show: bool) -> Plot { let labels = ["giraffes", "giraffes", "orangutans", "monkeys"]; let t = Pie::<u32>::from_labels(&labels); let mut plot = Plot::new(); plot.add_trace(t); if show { plot.show(); } plot } }
Grouped Pie Chart
#![allow(unused)] fn main() { fn grouped_donout_pie_charts(show: bool) -> Plot { let mut plot = Plot::new(); let values = vec![16, 15, 12, 6, 5, 4, 42]; let labels = vec![ "US", "China", "European Union", "Russian Federation", "Brazil", "India", "Rest of World", ]; let t = Pie::new(values) .labels(labels) .name("GHG Emissions") .hover_info(HoverInfo::All) .text("GHG") .hole(0.4) .domain(Domain::new().column(0)); plot.add_trace(t); let values = vec![27, 11, 25, 8, 1, 3, 25]; let labels = vec![ "US", "China", "European Union", "Russian Federation", "Brazil", "India", "Rest of World", ]; let t = Pie::new(values) .labels(labels) .name("CO2 Emissions") .hover_info(HoverInfo::All) .text("CO2") .text_position(plotly::common::Position::Inside) .hole(0.4) .domain(Domain::new().column(1)); plot.add_trace(t); let layout = Layout::new() .title("Global Emissions 1990-2011") .height(400) .width(600) .annotations(vec![ Annotation::new() .font(Font::new().size(20)) .show_arrow(false) .text("GHG") .x(0.17) .y(0.5), Annotation::new() .font(Font::new().size(20)) .show_arrow(false) .text("CO2") .x(0.82) .y(0.5), ]) .show_legend(false) .grid( LayoutGrid::new() .columns(2) .rows(1) .pattern(plotly::layout::GridPattern::Independent), ); plot.set_layout(layout); if show { plot.show(); } plot } }
Pie Chart Text Control
#![allow(unused)] fn main() { fn pie_chart_text_control(show: bool) -> Plot { let values = vec![2, 3, 4, 4]; let labels = vec!["Wages", "Operating expenses", "Cost of sales", "Insurance"]; let t = Pie::new(values) .labels(labels) .automargin(true) .show_legend(true) .text_position(plotly::common::Position::Outside) .name("Costs") .text_info("label+percent"); let mut plot = Plot::new(); plot.add_trace(t); let layout = Layout::new().height(700).width(700).show_legend(true); plot.set_layout(layout); if show { plot.show(); } plot } }