@@ -583,6 +583,112 @@ export class Plot {
583583 }
584584
585585
586+ /**
587+ * Makes Pie Plots from two Columns in a DataFrame.
588+ * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
589+ * @param {string } div Name of the div to show the plot
590+ * @param {Object } config configuration options for making Plots, supports Plotly parameters
591+ */
592+ pie ( config = { } ) {
593+
594+ let ret_params = this . __get_plot_params ( config )
595+ let this_config = ret_params [ 0 ]
596+
597+
598+ if ( this . ndframe instanceof Series ) {
599+ let data = [ {
600+ values : this . ndframe . values ,
601+ labels : this . ndframe . index ,
602+ type : 'pie' ,
603+ name : this_config [ 'labels' ] ,
604+ hoverinfo : 'label+percent+name' ,
605+ automargin : true
606+ } ] ;
607+
608+ newPlot ( this . div , data , this_config [ 'layout' ] )
609+
610+ } else if ( utils . __key_in_object ( this_config , 'values' ) && utils . __key_in_object ( this_config , 'labels' ) ) {
611+ if ( ! this . ndframe . column_names . includes ( this_config [ 'labels' ] ) ) {
612+ throw Error ( `Column Error: ${ this_config [ 'labels' ] } not found in columns. labels name must be one of [ ${ this . ndframe . column_names } ]` )
613+ }
614+ if ( ! this . ndframe . column_names . includes ( this_config [ 'values' ] ) ) {
615+ throw Error ( `Column Error: ${ this_config [ 'values' ] } not found in columns. value name must be one of [ ${ this . ndframe . column_names } ]` )
616+ }
617+ let data = [ {
618+ values : this . ndframe [ this_config [ 'values' ] ] . values ,
619+ labels : this . ndframe [ this_config [ 'labels' ] ] . values ,
620+ type : 'pie' ,
621+ name : this_config [ 'labels' ] ,
622+ hoverinfo : 'label+percent+name' ,
623+ automargin : true
624+ } ] ;
625+
626+ newPlot ( this . div , data , this_config [ 'layout' ] )
627+
628+ } else {
629+ let cols_to_plot ;
630+
631+ if ( utils . __key_in_object ( this_config , "columns" ) ) {
632+ cols_to_plot = this . ____check_if_cols_exist ( this_config [ 'columns' ] )
633+ } else {
634+ cols_to_plot = this . ndframe . column_names
635+ }
636+
637+ if ( utils . __key_in_object ( this_config , 'row_pos' ) ) {
638+ if ( this_config [ 'row_pos' ] . length != cols_to_plot . length - 1 ) {
639+ throw Error ( `Lenght of row_pos array must be equal to number of columns. Got ${ this_config [ 'row_pos' ] . length } , expected ${ cols_to_plot . length - 1 } ` )
640+ }
641+ } else {
642+ let temp_arr = [ ]
643+ for ( let i = 0 ; i < cols_to_plot . length - 1 ; i ++ ) {
644+ temp_arr . push ( 0 )
645+ }
646+ this_config [ 'row_pos' ] = temp_arr
647+
648+ }
649+
650+ if ( utils . __key_in_object ( this_config , 'col_pos' ) ) {
651+ if ( this_config [ 'col_pos' ] . length != cols_to_plot . length - 1 ) {
652+ throw Error ( `Lenght of col_pos array must be equal to number of columns. Got ${ this_config [ 'col_pos' ] . length } , expected ${ cols_to_plot . length - 1 } ` )
653+ }
654+ } else {
655+ let temp_arr = [ ]
656+ for ( let i = 0 ; i < cols_to_plot . length - 1 ; i ++ ) {
657+ temp_arr . push ( i )
658+ }
659+ this_config [ 'col_pos' ] = temp_arr
660+
661+ }
662+ let data = [ ]
663+
664+ cols_to_plot . forEach ( ( c_name , i ) => {
665+ let trace = { }
666+ trace [ "values" ] = this . ndframe [ c_name ] . values
667+ trace [ 'labels' ] = this . ndframe [ this_config [ 'labels' ] ] . values
668+ trace [ 'name' ] = c_name
669+ trace [ 'type' ] = "pie"
670+ trace [ 'domain' ] = { row : this_config [ 'row_pos' ] [ i ] , column : this_config [ 'col_pos' ] [ i ] }
671+ trace [ "hoverinfo" ] = 'label+percent+name'
672+ trace [ 'textposition' ] = "outside"
673+ trace [ 'automargin' ] = true
674+ data . push ( trace )
675+
676+ } )
677+
678+ if ( ! utils . __key_in_object ( this_config , "grid" ) ) {
679+ //set default grid
680+ let size = Number ( ( this . ndframe . shape [ 1 ] / 2 ) . toFixed ( ) ) + 1
681+ this_config [ 'grid' ] = { rows : size , columns : size }
682+ }
683+ this_config [ 'layout' ] [ 'grid' ] = this_config [ 'grid' ]
684+ newPlot ( this . div , data , this_config [ 'layout' ] ) ;
685+
686+
687+ }
688+
689+ }
690+
691+
586692 __get_plot_params ( config ) {
587693 let params = Object . keys ( config )
588694 let this_config = { }
0 commit comments