Skip to content

Commit 898ee66

Browse files
committed
update plot type methods
1 parent a21df38 commit 898ee66

1 file changed

Lines changed: 101 additions & 20 deletions

File tree

danfojs/src/plotting/plot.js

Lines changed: 101 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//perform plotting
22
import { newPlot } from 'plotly.js/dist/plotly'
33
import { Utils } from "../core/utils"
4+
import { Series } from "../core/series"
5+
46
const utils = new Utils()
57

68

@@ -15,31 +17,42 @@ export class Plot {
1517
let params = Object.keys(config)
1618
let this_config = {}
1719

18-
if (!utils.__key_in_object(config, "layout")) {
19-
this_config['layout'] = {}
20-
}
21-
2220
params.forEach(param => {
2321
this_config[param] = config[param]
2422
})
2523

26-
if (ndframe.series) {
27-
let x = ndframe.values
24+
if (!utils.__key_in_object(config, "layout")) {
25+
this_config['layout'] = {}
26+
}
27+
28+
if (ndframe instanceof Series) {
2829
let trace = {}
29-
trace["x"] = x
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+
}
3038

3139
params.forEach(param => {
32-
if (!param == 'layout') {
33-
trace[param] = config[param]
34-
}
40+
trace[param] = config[param]
3541
})
36-
37-
3842
newPlot(div, [trace], this_config['layout']);
3943

4044
} else {
45+
4146
//DataFrame
4247
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+
4356
let x = ndframe[this_config['x']].values
4457
let y = ndframe[this_config['y']].values
4558

@@ -60,18 +73,86 @@ export class Plot {
6073

6174
newPlot(div, [trace], this_config['layout']);
6275

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+
63144
} else {
64145
//plot all
65-
let y = ndframe.index
146+
let x = ndframe.index
66147
let data = []
67148
ndframe.column_names.forEach(c_name => {
68-
let trace = {
69-
x: ndframe[c_name].values,
70-
y: y,
71-
type: this_config["type"],
72-
mode: this_config["mode"],
73-
name: c_name
74-
};
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+
})
75156
data.push(trace)
76157

77158
})

0 commit comments

Comments
 (0)