Skip to content

Commit 4a8cdbd

Browse files
committed
Refactor plotting module to only appear in browser version
1 parent a15d7af commit 4a8cdbd

File tree

12 files changed

+170
-196
lines changed

12 files changed

+170
-196
lines changed

src/danfojs-base/core/frame.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { _genericMathOp } from "./math.ops";
2020
import Groupby from '../aggregators/groupby';
2121
import ErrorThrower from "../shared/errors"
2222
import { _iloc, _loc } from "./indexing";
23-
import { PlotlyLib } from "../plotting";
2423
import Utils from "../shared/utils"
2524
import NDframe from "./generic";
2625
import { table } from "table";
@@ -3360,21 +3359,6 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
33603359

33613360
}
33623361

3363-
/**
3364-
* Exposes functions for creating charts from a DataFrame.
3365-
* Charts are created using the Plotly.js library, so all Plotly's configuration parameters are available.
3366-
* @param divId name of the HTML Div to render the chart in.
3367-
*/
3368-
plot(divId: string) {
3369-
//TODO: Add support for check plot library to use. So we can support other plot library like d3, vega, etc
3370-
if (utils.isBrowserEnv()) {
3371-
const plt = new PlotlyLib(this, divId);
3372-
return plt;
3373-
} else {
3374-
throw new Error("Not supported in NodeJS");
3375-
}
3376-
}
3377-
33783362
/**
33793363
* Access a single value for a row/column pair by integer position.
33803364
* Similar to {@link iloc}, in that both provide integer-based lookups.

src/danfojs-base/core/series.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { DATA_TYPES } from '../shared/defaults'
1919
import { _genericMathOp } from "./math.ops";
2020
import ErrorThrower from "../shared/errors"
2121
import { _iloc, _loc } from "./indexing";
22-
import { PlotlyLib } from "../plotting";
2322
import Utils from "../shared/utils"
2423
import NDframe from "./generic";
2524
import { table } from "table";
@@ -30,12 +29,6 @@ import {
3029
ArrayType1D,
3130
BaseDataOptionType,
3231
SeriesInterface,
33-
CsvOutputOptionsBrowser,
34-
ExcelOutputOptionsBrowser,
35-
JsonOutputOptionsBrowser,
36-
CsvOutputOptionsNode,
37-
ExcelOutputOptionsNode,
38-
JsonOutputOptionsNode,
3932
mapParam
4033
} from "../shared/types";
4134

@@ -192,7 +185,7 @@ export default class Series extends NDframe implements SeriesInterface {
192185
if (this.shape[0] - rows < 0) {
193186
throw new Error("ParamError: Number of rows cannot be greater than available rows in data")
194187
}
195-
188+
196189
const startIdx = this.shape[0] - rows
197190
return this.iloc([`${startIdx}:`])
198191
}
@@ -2135,28 +2128,6 @@ export default class Series extends NDframe implements SeriesInterface {
21352128
return dummyEncode(this, options)
21362129
}
21372130

2138-
2139-
/**
2140-
* Exposes functions for creating charts from a Series.
2141-
* Charts are created using the Plotly.js library, so all Plotly's configuration parameters are available.
2142-
* @param divId name of the HTML Div to render the chart in.
2143-
* @example
2144-
* ```
2145-
* const sf = new Series([1, 2, 3, 4, 5]);
2146-
* sf.plot("myDiv").line() //renders the chart in the div with id "myDiv"
2147-
* ```
2148-
*/
2149-
plot(divId: string) {
2150-
//TODO: Add support for check plot library to use
2151-
// So we can support other plot library like d3, vega, etc
2152-
if (utils.isBrowserEnv()) {
2153-
const plt = new PlotlyLib(this, divId);
2154-
return plt;
2155-
} else {
2156-
throw new Error("Not supported in NodeJS");
2157-
}
2158-
}
2159-
21602131
/**
21612132
* Access a single value for a row index.
21622133
* Similar to iloc, in that both provide index-based lookups.

src/danfojs-base/io/browser/io.excel.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,11 @@ import {
1919
ExcelInputOptionsBrowser
2020
} from "../../shared/types"
2121
import { DataFrame, NDframe, Series } from '../../'
22-
23-
let XLSX: any;
24-
25-
try {
26-
XLSX = require("xlsx");
27-
} catch (err) {
28-
console.info(`xlsx not found. Please run "npm install xlsx" or "yarn add xlsx" in order to work with Excel files.`)
29-
30-
}
22+
import {
23+
read,
24+
writeFile,
25+
utils
26+
} from "xlsx";
3127

3228
/**
3329
* Reads a JSON file from local or remote location into a DataFrame.
@@ -64,9 +60,9 @@ const $readExcel = async (file: any, options?: ExcelInputOptionsBrowser) => {
6460
}
6561
response.arrayBuffer().then(arrBuf => {
6662
const arrBufInt8 = new Uint8Array(arrBuf);
67-
const workbook = XLSX.read(arrBufInt8, { type: "array" })
63+
const workbook = read(arrBufInt8, { type: "array" })
6864
const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
69-
const data = XLSX.utils.sheet_to_json(worksheet);
65+
const data = utils.sheet_to_json(worksheet);
7066
const df = new DataFrame(data, frameConfig);
7167
resolve(df);
7268
});
@@ -78,9 +74,9 @@ const $readExcel = async (file: any, options?: ExcelInputOptionsBrowser) => {
7874
} else if (file instanceof File) {
7975
const arrBuf = await file.arrayBuffer()
8076
const arrBufInt8 = new Uint8Array(arrBuf);
81-
const workbook = XLSX.read(arrBufInt8, { type: "array" })
77+
const workbook = read(arrBufInt8, { type: "array" })
8278
const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
83-
const data = XLSX.utils.sheet_to_json(worksheet);
79+
const data = utils.sheet_to_json(worksheet);
8480
const df = new DataFrame(data, frameConfig);
8581
return df;
8682
} else {
@@ -122,10 +118,10 @@ const $toExcel = (df: NDframe | DataFrame | Series, options?: ExcelOutputOptions
122118
data = [cols, ...row]
123119
}
124120

125-
const worksheet = XLSX.utils.aoa_to_sheet(data);
126-
const wb = XLSX.utils.book_new();
127-
XLSX.utils.book_append_sheet(wb, worksheet, sheetName);
128-
XLSX.writeFile(wb, `${fileName}`)
121+
const worksheet = utils.aoa_to_sheet(data);
122+
const wb = utils.book_new();
123+
utils.book_append_sheet(wb, worksheet, sheetName);
124+
writeFile(wb, `${fileName}`)
129125
};
130126

131127
export {

src/danfojs-base/io/node/io.excel.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
import { ArrayType1D, ArrayType2D, ExcelInputOptionsNode, ExcelOutputOptionsNode } from "../../shared/types"
1616
import { DataFrame, NDframe, Series } from '../../'
1717
import fetch from "node-fetch";
18-
import XLSX from 'xlsx';
19-
18+
import {
19+
read,
20+
writeFile,
21+
readFile,
22+
utils
23+
} from "xlsx";
2024
/**
2125
* Reads a JSON file from local or remote location into a DataFrame.
2226
* @param filePath URL or local file path to JSON file.
@@ -57,9 +61,9 @@ const $readExcel = async (filePath: string, options: ExcelInputOptionsNode = {})
5761
}
5862
response.arrayBuffer().then(arrBuf => {
5963
const arrBufInt8 = new Uint8Array(arrBuf);
60-
const workbook = XLSX.read(arrBufInt8, { type: "array" })
64+
const workbook = read(arrBufInt8, { type: "array" })
6165
const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
62-
const data = XLSX.utils.sheet_to_json(worksheet);
66+
const data = utils.sheet_to_json(worksheet);
6367
const df = new DataFrame(data, frameConfig);
6468
resolve(df);
6569
});
@@ -70,9 +74,9 @@ const $readExcel = async (filePath: string, options: ExcelInputOptionsNode = {})
7074

7175
} else {
7276
return new Promise(resolve => {
73-
const workbook = XLSX.readFile(filePath);
77+
const workbook = readFile(filePath);
7478
const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
75-
const data = XLSX.utils.sheet_to_json(worksheet);
79+
const data = utils.sheet_to_json(worksheet);
7680
const df = new DataFrame(data, frameConfig);
7781
resolve(df);
7882
});
@@ -113,10 +117,10 @@ const $toExcel = (df: NDframe | DataFrame | Series, options?: ExcelOutputOptions
113117
data = [cols, ...row]
114118
}
115119

116-
const worksheet = XLSX.utils.aoa_to_sheet(data);
117-
const wb = XLSX.utils.book_new();
118-
XLSX.utils.book_append_sheet(wb, worksheet, sheetName);
119-
XLSX.writeFile(wb, `${filePath}`)
120+
const worksheet = utils.aoa_to_sheet(data);
121+
const wb = utils.book_new();
122+
utils.book_append_sheet(wb, worksheet, sheetName);
123+
writeFile(wb, `${filePath}`)
120124
};
121125

122126
export {

src/danfojs-base/plotting/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,8 @@ import {
2525
import Series from "../core/series";
2626
import DataFrame from "../core/frame";
2727
import { PlotConfigObject, IPlotlyLib } from "../shared/types"
28+
import Plotly from "plotly.js-dist-min";
2829

29-
let Plotly: IPlotlyLib;
30-
31-
if (typeof window !== "undefined") {
32-
//check if in browser environment and require "plotly.js-dist-min" module
33-
Plotly = require("plotly.js-dist-min") as IPlotlyLib;
34-
35-
}
3630

3731
class PlotlyLib implements IPlotlyLib {
3832
divId: string;

src/danfojs-base/shared/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ export interface SeriesInterface extends NDframeInterface {
180180
prefixSeparator?: string | Array<string>,
181181
inplace?: boolean
182182
}): DataFrame
183-
plot(divId: string): IPlotlyLib
184183
iat(index: number): number | string | boolean | undefined
185184
at(index: string | number): number | string | boolean | undefined
186185
}
@@ -325,7 +324,6 @@ export interface DataFrameInterface extends NDframeInterface {
325324
prefixSeparator?: string | Array<string>,
326325
inplace?: boolean
327326
}): DataFrame | void
328-
plot(divId: string): IPlotlyLib
329327
iat(row: number, column: number): number | string | boolean | undefined
330328
at(row: string | number, column: string): number | string | boolean | undefined
331329
}

src/danfojs-browser/package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,18 @@
2323
"@tensorflow/tfjs": "^3.13.0",
2424
"mathjs": "9.4.4",
2525
"papaparse": "^5.3.1",
26-
"table": "6.7.1"
27-
},
28-
"peerDependencies": {
26+
"table": "6.7.1",
2927
"plotly.js-dist-min": "2.8.0",
3028
"xlsx": "0.17.2"
3129
},
3230
"scripts": {
3331
"test": "karma start --single-run --browsers ChromeHeadless karma.conf.js",
34-
"test:clean": "yarn build:clean && yarn add xlsx@0.17.2 -P && yarn run test",
32+
"test:clean": "yarn build:clean && yarn run test",
3533
"build": "node ./scripts/prebuild.js && tsc && yarn run bundle",
3634
"build:clean": "rimraf ./dist && rimraf ./lib && node ./scripts/prebuild.js && yarn run build",
3735
"dev": "nodemon",
3836
"lint": "eslint ./src",
39-
"bundle": "webpack --mode production",
37+
"bundle": "webpack --mode development",
4038
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
4139
"coverage": "nyc report --reporter=text-lcov | coveralls && nyc report --reporter=lcov",
4240
"patch": "npm version patch"

0 commit comments

Comments
 (0)