@@ -4,13 +4,16 @@ use ndarray::Array;
4
4
use plotly:: {
5
5
color:: { NamedColor , Rgb , Rgba } ,
6
6
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 ,
9
13
} ,
10
- layout:: { Axis , BarMode , CategoryOrder , Layout , Legend , TicksDirection , TraceOrder } ,
11
14
sankey:: { Line as SankeyLine , Link , Node } ,
12
15
traces:: table:: { Cells , Header } ,
13
- Bar , Plot , Sankey , Scatter , ScatterPolar , Table ,
16
+ Bar , Pie , Plot , Sankey , Scatter , ScatterPolar , Table ,
14
17
} ;
15
18
use rand_distr:: { Distribution , Normal , Uniform } ;
16
19
@@ -819,6 +822,124 @@ fn table_chart(show: bool) -> Plot {
819
822
}
820
823
// ANCHOR_END: table_chart
821
824
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
+
822
943
fn write_example_to_html ( plot : Plot , name : & str ) {
823
944
std:: fs:: create_dir_all ( "./out" ) . unwrap ( ) ;
824
945
let html = plot. to_inline_html ( Some ( name) ) ;
@@ -857,7 +978,7 @@ fn main() {
857
978
write_example_to_html ( filled_lines ( false ) , "filled_lines" ) ;
858
979
859
980
// 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" ) ;
861
982
write_example_to_html ( grouped_bar_chart ( false ) , "grouped_bar_chart" ) ;
862
983
write_example_to_html ( stacked_bar_chart ( false ) , "stacked_bar_chart" ) ;
863
984
write_example_to_html ( table_chart ( false ) , "table_chart" ) ;
@@ -869,4 +990,12 @@ fn main() {
869
990
870
991
// Sankey Diagrams
871
992
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
+ ) ;
872
1001
}
0 commit comments