Skip to content

Commit

Permalink
Fix: custom labels for bins
Browse files Browse the repository at this point in the history
Chore: renamed Dj.js node's display name to Charts and Plots
  • Loading branch information
mahesh-gfx committed Aug 27, 2024
1 parent 8d4eace commit 895531e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/nodes/src/nodeTypes/D3jsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class D3JsNode extends BaseNode {
static getNodeDefinition(): NodeDefinition {
return {
name: "D3JsNode",
displayName: "D3.js Visualization",
displayName: "Charts and Plots",
description: "Visualizes data using D3.js",
icon: "chart-bar",
color: "#4287f5",
Expand Down
59 changes: 48 additions & 11 deletions packages/nodes/src/nodeTypes/DataBinningNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class DataBinningNode extends BaseNode {
icon: "bin",
color: "#00A971",
inputs: ["data"],
outputs: ["binnedData"],
outputs: ["data"],
properties: [
{
displayName: "Binning Method",
Expand All @@ -40,6 +40,11 @@ export class DataBinningNode extends BaseNode {
type: "string",
default: "",
description: "Comma-separated list of custom bin edges",
displayOptions: {
show: {
binningMethod: ["custom"],
},
},
},
{
displayName: "Column to Bin",
Expand All @@ -48,6 +53,13 @@ export class DataBinningNode extends BaseNode {
default: "",
description: "The column to apply binning on",
},
{
displayName: "Bin Labels",
name: "binLabels",
type: "string",
default: "",
description: "Comma-separated list of labels for each bin",
},
],
version: 1,
};
Expand All @@ -59,6 +71,9 @@ export class DataBinningNode extends BaseNode {
const numberOfBins = this.data.properties?.numberOfBins;
const customBinEdges = this.data.properties?.customBinEdges;
const columnToBin = this.data.properties?.columnToBin;
const binLabels = this.data.properties?.binLabels
.split(",")
.map((label: any) => label.trim());

if (!data || !columnToBin) {
console.error("Invalid input or column specification");
Expand All @@ -68,34 +83,45 @@ export class DataBinningNode extends BaseNode {
let binnedData;
switch (binningMethod) {
case "equal-width":
binnedData = this.equalWidthBinning(data, columnToBin, numberOfBins);
binnedData = this.equalWidthBinning(
data,
columnToBin,
numberOfBins,
binLabels
);
break;
case "equal-frequency":
binnedData = this.equalFrequencyBinning(
data,
columnToBin,
numberOfBins
numberOfBins,
binLabels
);
break;
case "custom":
const edges = customBinEdges
.split(",")
.map((edge: any) => parseFloat(edge.trim()));
binnedData = this.customBinning(data, columnToBin, edges);
binnedData = this.customBinning(data, columnToBin, edges, binLabels);
break;
default:
throw new Error("Invalid binning method");
}

return {
binnedData: {
data: {
json: binnedData,
binary: null,
},
};
}

equalWidthBinning(data: any[], column: string, numberOfBins: number): any[] {
equalWidthBinning(
data: any[],
column: string,
numberOfBins: number,
binLabels: string[]
): any[] {
const values = data.map((row) => row[column]);
const min = Math.min(...values);
const max = Math.max(...values);
Expand All @@ -104,33 +130,44 @@ export class DataBinningNode extends BaseNode {
return data.map((row) => {
const value = row[column];
const binIndex = Math.floor((value - min) / binWidth);
row[column] = `Bin ${Math.min(binIndex, numberOfBins - 1)}`;
const label =
binLabels[binIndex] || `Bin ${Math.min(binIndex, numberOfBins - 1)}`;
row[`${column}_binned`] = label;
return row;
});
}

equalFrequencyBinning(
data: any[],
column: string,
numberOfBins: number
numberOfBins: number,
binLabels: string[]
): any[] {
const sortedData = [...data].sort((a, b) => a[column] - b[column]);
const binSize = Math.ceil(sortedData.length / numberOfBins);

return sortedData.map((row, index) => {
const binIndex = Math.floor(index / binSize);
row[column] = `Bin ${Math.min(binIndex, numberOfBins - 1)}`;
const label =
binLabels[binIndex] || `Bin ${Math.min(binIndex, numberOfBins - 1)}`;
row[`${column}_binned`] = label;
return row;
});
}

customBinning(data: any[], column: string, edges: number[]): any[] {
customBinning(
data: any[],
column: string,
edges: number[],
binLabels: string[]
): any[] {
return data.map((row) => {
const value = row[column];
const binIndex = edges.findIndex(
(edge, index) => value >= edge && value < (edges[index + 1] || Infinity)
);
row[column] = `Bin ${binIndex}`;
const label = binLabels[binIndex] || `Bin ${binIndex}`;
row[`${column}_binned`] = label;
return row;
});
}
Expand Down

0 comments on commit 895531e

Please sign in to comment.