@@ -4,13 +4,16 @@ use ndarray::Array;
44use 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} ;
1518use 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+
822943fn 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}
0 commit comments