@@ -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,138 @@ 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 , 4 ] ;
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: basic_pie_chart_labels
842+ fn basic_pie_chart_labels ( show : bool ) -> Plot {
843+ let labels = [ "giraffes" , "giraffes" , "orangutans" , "monkeys" ] ;
844+ let t = Pie :: < u32 > :: from_labels ( & labels) ;
845+ let mut plot = Plot :: new ( ) ;
846+ plot. add_trace ( t) ;
847+
848+ if show {
849+ plot. show ( ) ;
850+ }
851+ plot
852+ }
853+ // ANCHOR_END: basic_pie_chart_labels
854+
855+ // ANCHOR: pie_chart_text_control
856+ fn pie_chart_text_control ( show : bool ) -> Plot {
857+ let values = vec ! [ 2 , 3 , 4 , 4 ] ;
858+ let labels = vec ! [ "Wages" , "Operating expenses" , "Cost of sales" , "Insurance" ] ;
859+ let t = Pie :: new ( values)
860+ . labels ( labels)
861+ . automargin ( true )
862+ . show_legend ( true )
863+ . text_position ( plotly:: common:: Position :: Outside )
864+ . name ( "Costs" )
865+ . text_info ( "label+percent" ) ;
866+ let mut plot = Plot :: new ( ) ;
867+ plot. add_trace ( t) ;
868+
869+ let layout = Layout :: new ( ) . height ( 700 ) . width ( 700 ) . show_legend ( true ) ;
870+ plot. set_layout ( layout) ;
871+
872+ if show {
873+ plot. show ( ) ;
874+ }
875+ plot
876+ }
877+ // ANCHOR_END: pie_chart_text_control
878+
879+ // ANCHOR: grouped_donout_pie_charts
880+ fn grouped_donout_pie_charts ( show : bool ) -> Plot {
881+ let mut plot = Plot :: new ( ) ;
882+
883+ let values = vec ! [ 16 , 15 , 12 , 6 , 5 , 4 , 42 ] ;
884+ let labels = vec ! [
885+ "US" ,
886+ "China" ,
887+ "European Union" ,
888+ "Russian Federation" ,
889+ "Brazil" ,
890+ "India" ,
891+ "Rest of World" ,
892+ ] ;
893+ let t = Pie :: new ( values)
894+ . labels ( labels)
895+ . name ( "GHG Emissions" )
896+ . hover_info ( HoverInfo :: All )
897+ . text ( "GHG" )
898+ . hole ( 0.4 )
899+ . domain ( Domain :: new ( ) . column ( 0 ) ) ;
900+ plot. add_trace ( t) ;
901+
902+ let values = vec ! [ 27 , 11 , 25 , 8 , 1 , 3 , 25 ] ;
903+ let labels = vec ! [
904+ "US" ,
905+ "China" ,
906+ "European Union" ,
907+ "Russian Federation" ,
908+ "Brazil" ,
909+ "India" ,
910+ "Rest of World" ,
911+ ] ;
912+
913+ let t = Pie :: new ( values)
914+ . labels ( labels)
915+ . name ( "CO2 Emissions" )
916+ . hover_info ( HoverInfo :: All )
917+ . text ( "CO2" )
918+ . text_position ( plotly:: common:: Position :: Inside )
919+ . hole ( 0.4 )
920+ . domain ( Domain :: new ( ) . column ( 1 ) ) ;
921+ plot. add_trace ( t) ;
922+
923+ let layout = Layout :: new ( )
924+ . title ( "Global Emissions 1990-2011" )
925+ . height ( 400 )
926+ . width ( 600 )
927+ . annotations ( vec ! [
928+ Annotation :: new( )
929+ . font( Font :: new( ) . size( 20 ) )
930+ . show_arrow( false )
931+ . text( "GHG" )
932+ . x( 0.17 )
933+ . y( 0.5 ) ,
934+ Annotation :: new( )
935+ . font( Font :: new( ) . size( 20 ) )
936+ . show_arrow( false )
937+ . text( "CO2" )
938+ . x( 0.82 )
939+ . y( 0.5 ) ,
940+ ] )
941+ . show_legend ( false )
942+ . grid (
943+ LayoutGrid :: new ( )
944+ . columns ( 2 )
945+ . rows ( 1 )
946+ . pattern ( plotly:: layout:: GridPattern :: Independent ) ,
947+ ) ;
948+ plot. set_layout ( layout) ;
949+
950+ if show {
951+ plot. show ( ) ;
952+ }
953+ plot
954+ }
955+ // ANCHOR_END: grouped_donout_pie_charts
956+
822957fn write_example_to_html ( plot : Plot , name : & str ) {
823958 std:: fs:: create_dir_all ( "./out" ) . unwrap ( ) ;
824959 let html = plot. to_inline_html ( Some ( name) ) ;
@@ -869,4 +1004,13 @@ fn main() {
8691004
8701005 // Sankey Diagrams
8711006 write_example_to_html ( basic_sankey_diagram ( false ) , "basic_sankey_diagram" ) ;
1007+
1008+ // Pie Charts
1009+ write_example_to_html ( basic_pie_chart ( false ) , "basic_pie_chart" ) ;
1010+ write_example_to_html ( basic_pie_chart_labels ( false ) , "basic_pie_chart_labels" ) ;
1011+ write_example_to_html ( pie_chart_text_control ( false ) , "pie_chart_text_control" ) ;
1012+ write_example_to_html (
1013+ grouped_donout_pie_charts ( false ) ,
1014+ "grouped_donout_pie_charts" ,
1015+ ) ;
8721016}
0 commit comments