Skip to content

Commit 8dc0fec

Browse files
committed
add pie plot
1 parent 7614e8d commit 8dc0fec

2 files changed

Lines changed: 117 additions & 10 deletions

File tree

danfojs/src/core/series.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export class Series extends NDframe {
109109
// let counts = [...Array(idx.length).keys()] //set index
110110
//get random sampled numbers
111111
// let index_arr = utils.__range(0,counts.length)
112-
let rand_nums = utils.__shuffle(num,idx)
112+
let rand_nums = utils.__shuffle(num, idx)
113113
// console.log(rand_nums)
114114
rand_nums.map(i => {
115115
new_values.push(values[i])
@@ -547,7 +547,7 @@ export class Series extends NDframe {
547547
let range_idx = utils.__range(0, this.index.length - 1)
548548
let sorted_idx = dsu(range_idx, arr_obj);
549549

550-
sorted_idx.forEach(idx=>{
550+
sorted_idx.forEach(idx => {
551551
sorted_arr.push(this.values[idx])
552552
})
553553
if (options['ascending']) {
@@ -1297,14 +1297,15 @@ export class Series extends NDframe {
12971297

12981298

12991299
/**
1300-
* Make plots of Series or DataFrame.
1301-
* Uses the Plotly as backend, so supoorts Plotly's configuration parameters
1302-
* @param {string} div Name of the div to show the plot
1303-
* @param {Object} config configuration options for making Plots, supports Plotly parameters
1304-
*/
1305-
plot(div, config = {}) {
1306-
const plt = new Plot()
1307-
plt.plot(this, div, config)
1300+
* Make plots of Series or DataFrame.
1301+
* Uses the Plotly as backend, so supports Plotly's configuration parameters
1302+
* @param {string} div Name of the div to show the plot
1303+
* @returns {Class} Plot class that expoese different plot type
1304+
*/
1305+
plot(div) {
1306+
const plt = new Plot(this, div)
1307+
return plt
13081308
}
1309+
13091310
}
13101311

danfojs/src/plotting/plot.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)