Skip to content

Commit c50e83b

Browse files
committed
add box plot
1 parent 8dc0fec commit c50e83b

1 file changed

Lines changed: 117 additions & 2 deletions

File tree

danfojs/src/plotting/plot.js

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ export class Plot {
594594
let ret_params = this.__get_plot_params(config)
595595
let this_config = ret_params[0]
596596

597-
597+
598598
if (this.ndframe instanceof Series) {
599599
let data = [{
600600
values: this.ndframe.values,
@@ -636,7 +636,7 @@ export class Plot {
636636

637637
if (utils.__key_in_object(this_config, 'row_pos')) {
638638
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}`)
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}`)
640640
}
641641
} else {
642642
let temp_arr = []
@@ -689,6 +689,117 @@ export class Plot {
689689
}
690690

691691

692+
693+
/**
694+
* Plot Box plots from Series or DataFrame as lines.
695+
* Uses the Plotly as backend, so supoorts Plotly's configuration parameters
696+
* @param {Object} config configuration options for making Plots, supports Plotly parameters
697+
*/
698+
box(config = {}) {
699+
700+
let ret_params = this.__get_plot_params(config)
701+
let this_config = ret_params[0]
702+
let params = ret_params[1]
703+
704+
if (this.ndframe instanceof Series) {
705+
let trace = {}
706+
let y = this.ndframe.values
707+
708+
params.forEach(param => {
709+
if (!param == "layout") {
710+
trace[param] = config[param]
711+
}
712+
})
713+
714+
trace["y"] = y
715+
trace['type'] = "box"
716+
717+
newPlot(this.div, [trace], this_config['layout']);
718+
719+
} else {
720+
//check if plotting two columns against each other
721+
if (utils.__key_in_object(this_config, 'x') && utils.__key_in_object(this_config, 'y')) {
722+
if (!this.ndframe.column_names.includes(this_config['x'])) {
723+
throw Error(`Column Error: ${this_config['x']} not found in columns`)
724+
}
725+
if (!this.ndframe.column_names.includes(this_config['y'])) {
726+
throw Error(`Column Error: ${this_config['y']} not found in columns`)
727+
}
728+
729+
730+
let x = this.ndframe[this_config['x']].values
731+
let y = this.ndframe[this_config['y']].values
732+
733+
let trace = {}
734+
trace["x"] = x
735+
trace['y'] = y
736+
trace['type'] = 'box'
737+
738+
739+
let xaxis = {}; let yaxis = {}
740+
xaxis['title'] = this_config['x']
741+
yaxis['title'] = this_config['y']
742+
743+
this_config['layout']['xaxis'] = xaxis
744+
this_config['layout']['yaxis'] = yaxis
745+
746+
newPlot(this.div, [trace], this_config['layout']);
747+
748+
} else if (utils.__key_in_object(this_config, 'x') || utils.__key_in_object(this_config, 'y')) {
749+
//plot single column specified in either of param [x | y] against index
750+
let trace = {}
751+
752+
params.forEach(param => {
753+
if (!param == "layout") {
754+
trace[param] = config[param]
755+
}
756+
})
757+
758+
if (utils.__key_in_object(this_config, 'x')) {
759+
trace['x'] = this.ndframe[this_config['x']].values
760+
trace['y'] = this.ndframe.index
761+
trace['type'] = 'box'
762+
} else {
763+
trace['x'] = this.ndframe.index
764+
trace['y'] = this_config['y']
765+
trace['type'] = 'box'
766+
}
767+
768+
newPlot(this.div, [trace], this_config['layout']);
769+
770+
} else {
771+
//plot columns against index
772+
let data = []
773+
let cols_to_plot;
774+
775+
if (utils.__key_in_object(this_config, "columns")) {
776+
cols_to_plot = this.____check_if_cols_exist(this_config['columns'])
777+
} else {
778+
cols_to_plot = this.ndframe.column_names
779+
}
780+
781+
cols_to_plot.forEach(c_name => {
782+
let trace = {}
783+
784+
params.forEach(param => { //TODO accept individual configuration for traces
785+
trace[param] = config[param]
786+
})
787+
trace["y"] = this.ndframe[c_name].values
788+
trace['name'] = c_name
789+
trace['type'] = 'box'
790+
data.push(trace)
791+
792+
})
793+
newPlot(this.div, data, this_config['layout']);
794+
795+
}
796+
797+
}
798+
799+
800+
}
801+
802+
692803
__get_plot_params(config) {
693804
let params = Object.keys(config)
694805
let this_config = {}
@@ -715,4 +826,8 @@ export class Plot {
715826
return cols
716827
}
717828

829+
830+
831+
832+
718833
}

0 commit comments

Comments
 (0)