1- // //perform plotting
2- // // import {newPlot} from 'plotly.js/dist/plotly'
3- // import { Utils } from "../core/utils"
4- // const utils = new Utils()
5-
6-
7-
8- // /**
9- // * Plotting methods and Functions performed on Series and DataFrames
10- // */
11- // export class Plot {
12- // constructor() {}
13-
14- // plot(ndframe, div, config) {
15- // let params = Object.keys(config)
16- // let this_config = {}
17-
18- // if (!utils.__key_in_object(config, "layout")){
19- // this_config['layout'] = {}
20- // }
21-
22- // params.forEach(param => {
23- // this_config[param] = config[param]
24- // })
25-
26- // if (ndframe.series) {
27- // let x = ndframe.values
28- // let trace = {
29- // x: x,
30- // type: this_config["kind"],
31- // mode: this_config["mode"]
32- // }
33- // newPlot(div, [trace], this_config['layout']);
34-
35- // } else {
36- // //DataFrame
37- // if (utils.__key_in_object(this_config, 'x') && utils.__key_in_object(this_config, 'y')) {
38- // let x = ndframe[this_config['x']].values
39- // let y = ndframe[this_config['y']].values
40-
41- // let trace = {
42- // x: x,
43- // y: y,
44- // type: config["kind"],
45- // mode: config["mode"],
46- // }
47-
48- // let xaxis = {}; let yaxis = {}
49- // xaxis['title'] = this_config['x']
50- // yaxis['title'] = this_config['y']
51-
52- // this_config['layout']['xaxis'] = xaxis
53- // this_config['layout']['yaxis'] = yaxis
54-
55-
56- // newPlot(div, [trace], this_config['layout']);
57-
58- // } else {
59- // //plot all
60- // let y = ndframe.index
61- // let data = []
62- // ndframe.column_names.forEach(c_name => {
63- // let trace = {
64- // x: ndframe[c_name].values,
65- // y: y,
66- // type: this_config["kind"],
67- // mode: this_config["mode"],
68- // name: c_name
69- // };
70- // data.push(trace)
71-
72- // })
73- // newPlot(div, data, this_config['layout']);
74- // }
75-
76-
77- // }
78- // }
79-
80- // }
1+ //perform plotting
2+ import { newPlot } from 'plotly.js/dist/plotly'
3+ import { Utils } from "../core/utils"
4+ import { Series } from "../core/series"
5+
6+ const utils = new Utils ( )
7+
8+
9+
10+ /**
11+ * Plotting methods and Functions performed on Series and DataFrames
12+ */
13+ export class Plot {
14+ constructor ( ) { }
15+
16+ plot ( ndframe , div , config ) {
17+ let params = Object . keys ( config )
18+ let this_config = { }
19+
20+ params . forEach ( param => {
21+ this_config [ param ] = config [ param ]
22+ } )
23+
24+ if ( ! utils . __key_in_object ( config , "layout" ) ) {
25+ this_config [ 'layout' ] = { }
26+ }
27+
28+ if ( ndframe instanceof Series ) {
29+ let trace = { }
30+
31+ if ( this_config [ 'type' ] == 'histogram' ) {
32+ let x = ndframe . values
33+ trace [ "x" ] = x
34+ } else {
35+ let y = ndframe . values
36+ trace [ "y" ] = y
37+ }
38+
39+ params . forEach ( param => {
40+ trace [ param ] = config [ param ]
41+ } )
42+ newPlot ( div , [ trace ] , this_config [ 'layout' ] ) ;
43+
44+ } else {
45+
46+ //DataFrame
47+ if ( utils . __key_in_object ( this_config , 'x' ) && utils . __key_in_object ( this_config , 'y' ) ) {
48+ if ( ! ndframe . column_names . includes ( this_config [ 'x' ] ) ) {
49+ throw Error ( `Column Error: ${ this_config [ 'x' ] } not found in columns` )
50+ }
51+ if ( ! ndframe . column_names . includes ( this_config [ 'y' ] ) ) {
52+ throw Error ( `Column Error: ${ this_config [ 'y' ] } not found in columns` )
53+ }
54+
55+
56+ let x = ndframe [ this_config [ 'x' ] ] . values
57+ let y = ndframe [ this_config [ 'y' ] ] . values
58+
59+ let trace = {
60+ x : x ,
61+ y : y ,
62+ type : config [ "type" ] ,
63+ mode : config [ "mode" ] ,
64+ }
65+
66+ let xaxis = { } ; let yaxis = { }
67+ xaxis [ 'title' ] = this_config [ 'x' ]
68+ yaxis [ 'title' ] = this_config [ 'y' ]
69+
70+ this_config [ 'layout' ] [ 'xaxis' ] = xaxis
71+ this_config [ 'layout' ] [ 'yaxis' ] = yaxis
72+
73+
74+ newPlot ( div , [ trace ] , this_config [ 'layout' ] ) ;
75+
76+
77+ } else if ( this_config [ 'type' ] == 'pie' ) {
78+ if ( ! ndframe . column_names . includes ( this_config [ 'values' ] ) ) {
79+ throw Error ( `Column Error: ${ this_config [ 'values' ] } not found in columns` )
80+ }
81+ if ( ! ndframe . column_names . includes ( this_config [ 'labels' ] ) ) {
82+ throw Error ( `Column Error: ${ this_config [ 'labels' ] } not found in columns` )
83+ }
84+ let data = [ {
85+ values : ndframe [ this_config [ 'values' ] ] . values ,
86+ labels : ndframe [ this_config [ 'labels' ] ] . values ,
87+ type : 'pie'
88+ } ] ;
89+
90+ newPlot ( div , data , this_config [ 'layout' ] )
91+
92+ } else if ( this_config [ 'type' ] == 'table' ) {
93+ let header = { }
94+ let cells = { }
95+
96+ header [ 'values' ] = ndframe . column_names
97+ cells [ 'values' ] = ndframe . col_data
98+
99+ if ( this_config [ 'header_style' ] ) {
100+ Object . keys ( this_config [ 'header_style' ] ) . forEach ( param => {
101+ header [ param ] = this_config [ 'header_style' ] [ param ]
102+ } )
103+ }
104+
105+ if ( this_config [ 'cell_style' ] ) {
106+ Object . keys ( this_config [ 'cell_style' ] ) . forEach ( param => {
107+ header [ param ] = this_config [ 'cell_style' ] [ param ]
108+ } )
109+ }
110+ var data = [ {
111+ type : 'table' ,
112+ header : header ,
113+ cells : cells
114+ } ]
115+ newPlot ( div , data , this_config [ 'layout' ] ) ;
116+
117+ } else if ( this_config [ 'type' ] == 'box' ) {
118+ let cols_2_show = [ ]
119+ let data = [ ]
120+
121+ if ( this_config [ 'columns' ] != undefined ) {
122+ cols_2_show = this_config [ 'columns' ]
123+ cols_2_show . forEach ( col => {
124+ if ( ! ndframe . column_names . includes ( col ) ) {
125+ throw Error ( `Column Error: ${ this_config [ 'labels' ] } not found in columns` )
126+ }
127+ } )
128+ } else {
129+ cols_2_show = ndframe . column_names
130+ }
131+
132+
133+ cols_2_show . forEach ( col => {
134+ let col_idx = ndframe . column_names . indexOf ( col )
135+ let trace = [ ]
136+ trace [ 'y' ] = ndframe . col_data [ col_idx ]
137+ trace [ "type" ] = 'box'
138+ trace [ "name" ] = col
139+
140+ data . push ( trace )
141+ } )
142+ newPlot ( div , data , this_config [ 'layout' ] ) ;
143+
144+ } else {
145+ //plot all
146+ let x = ndframe . index
147+ let data = [ ]
148+ ndframe . column_names . forEach ( c_name => {
149+ let trace = { }
150+ trace [ "x" ] = x
151+ trace [ "y" ] = ndframe [ c_name ] . values
152+ trace [ 'name' ] = c_name
153+ params . forEach ( param => {
154+ trace [ param ] = config [ param ]
155+ } )
156+ data . push ( trace )
157+
158+ } )
159+ newPlot ( div , data , this_config [ 'layout' ] ) ;
160+ }
161+
162+
163+ }
164+ }
165+
166+ }
0 commit comments