Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(graphProcessor): add getMinMax method #118

Merged
merged 4 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Graph/Graph.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ParsedNode } from "../helpers/nodeParser";

export type GraphType = "indicator" | "indicatorField" | "line" | "bar" | "pie";
export type RangeType = "auto" | "full";
export type RangeType = "auto" | "full" | "default";

export class Graph {
_string: string | null = null;
Expand All @@ -24,7 +24,7 @@ export class Graph {
return this._interval;
}

_y_range: RangeType = "full";
_y_range: RangeType = "default";
get y_range(): RangeType {
return this._y_range;
}
Expand All @@ -37,7 +37,7 @@ export class Graph {
}
if (element.attributes.y_range) {
const range = element.attributes.y_range;
if (range === "auto" || range === "full") {
if (range === "auto" || range === "full" || range === "default") {
this._y_range = range;
}
}
Expand Down
54 changes: 51 additions & 3 deletions src/Graph/processor/graphProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export type GroupedValues = Record<
{ label: string; entries: Array<Record<string, any>> }
>;

type GraphValues = {
value: number;
[key: string]: any;
};

export const labelsForOperator = {
count: "count",
"+": "sum",
Expand All @@ -21,6 +26,23 @@ export type ProcessGraphDataOpts = {
uninformedString: string;
};

export type YAxisOpts = {
mode?: "auto" | "default" | "full" | "slider";
valueOpts?: MinMaxValues;
};

export type MinMaxValues = {
min: number;
max: number;
};

export type Result = {
data: any[];
isGroup: boolean;
isStack: boolean;
yAxisOpts?: YAxisOpts;
};

export const processGraphData = ({
ooui,
values,
Expand All @@ -40,8 +62,7 @@ export const processGraphData = ({
fields,
});

const data: Array<Record<string, any>> = [];

const data: GraphValues[] = [];
// We iterate through the y axis items found in the ooui object
ooui.y.forEach((yField) => {
// We iterate now for every single key of the grouped results by x
Expand Down Expand Up @@ -173,11 +194,23 @@ export const processGraphData = ({
finalData = adjustedUninformedData.sort((a, b) => b.value - a.value);
}

return {
const result: Result = {
data: finalData,
isGroup: isStack || isGroup,
isStack,
};

if (ooui.type === "line" && ooui.y_range) {
result.yAxisOpts = {
mode: ooui.y_range,
};
if (ooui.y_range === "auto") {
const { min, max } = getMinMax(finalData);
result.yAxisOpts.valueOpts = { min, max };
}
}

return result;
};

export function getValuesForYField({
Expand Down Expand Up @@ -306,3 +339,18 @@ export function getYAxisFieldname({
// return yAxis.name + "_" + labelsForOperator[yAxis.operator];
return yAxis.name;
}

export function getMinMax(values: GraphValues[], margin: number = 0.1) {
if (values.length === 0) {
throw new Error("The values array cannot be empty.");
}
const valueList = values.map((d) => d.value);
const minValue = Math.min(...valueList);
const maxValue = Math.max(...valueList);
const calculatedMargin = (maxValue - minValue) * margin;

return {
min: minValue - calculatedMargin,
max: maxValue + calculatedMargin,
};
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ import type { GraphType, Operator } from "./Graph";
import * as graphProcessor from "./Graph/processor/graphProcessor";
import * as graphFieldUtils from "./Graph/processor/fieldUtils";

import type { YAxisOpts, MinMaxValues } from "./Graph/processor/graphProcessor";

export {
Avatar,
Char,
Expand Down Expand Up @@ -130,4 +132,6 @@ export {
SearchFieldTypes,
Time,
Alert,
YAxisOpts,
MinMaxValues,
};
95 changes: 95 additions & 0 deletions src/spec/graphProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getAllObjectsInGroupedValues,
getValuesGroupedByField,
processGraphData,
getMinMax,
} from "../Graph/processor/graphProcessor";
import { GraphChart, parseGraph } from "../Graph";
import { it, expect, describe } from "vitest";
Expand Down Expand Up @@ -199,6 +200,30 @@ describe("in processGraphData method", () => {
expect(data.some((entry) => entry.x === false)).toBeFalsy();
});

describe("when processing a line chart with y_range", () => {
describe("if y_range is auto", () => {
it("should return yAxisOpts with mode auto, min and max values", () => {
const xml = `<?xml version="1.0"?>
<graph type="line" y_range="auto" timerange="day">
<field name="date" axis="x"/>
<field name="v" operator="+" axis="y"/>
</graph>`;
const parsedGraph = parseGraph(xml) as GraphChart;
const values = [
{ date: "2024-01-01", v: 10 },
{ date: "2024-01-02", v: 20 },
{ date: "2024-01-03", v: 30 },
];
const fields = { date: { type: "date" }, v: { type: "integer" } };
const result = processGraphData({ ooui: parsedGraph, values, fields });
expect(result.yAxisOpts).toBeDefined();
expect(result.yAxisOpts?.mode).toBe("auto");
expect(result.yAxisOpts?.valueOpts?.min).toBe(8);
expect(result.yAxisOpts?.valueOpts?.max).toBe(32);
});
});
});

it("should do basic test with one y axis with label", () => {
const { data, isGroup, isStack } = getGraphData(
`<?xml version="1.0"?>
Expand Down Expand Up @@ -424,6 +449,76 @@ describe("in processGraphData method", () => {
});
});

describe("in getMinMax method", () => {
it("should return correct min and max with default margin", () => {
const data = [{ value: 10 }, { value: 20 }, { value: 30 }];

const result = getMinMax(data);

// Calculant els valors esperats
const minValue = 10;
const maxValue = 30;
const margin = (maxValue - minValue) * 0.1;
const expected = {
min: minValue - margin,
max: maxValue + margin,
};

expect(result).toEqual(expected);
});

it("should return correct min and max with custom margin", () => {
const data = [{ value: 10 }, { value: 20 }, { value: 30 }];

const result = getMinMax(data, 0.2);

// Calculant els valors esperats
const minValue = 10;
const maxValue = 30;
const margin = (maxValue - minValue) * 0.2;
const expected = {
min: minValue - margin,
max: maxValue + margin,
};

expect(result).toEqual(expected);
});

it("should return correct min and max for single element", () => {
const data = [{ value: 10 }];

const result = getMinMax(data);

const expected = {
min: 10,
max: 10,
};

expect(result).toEqual(expected);
});

it("should handle negative values correctly", () => {
const data = [{ value: -10 }, { value: 0 }, { value: 10 }];

const result = getMinMax(data);

// Calculant els valors esperats
const minValue = -10;
const maxValue = 10;
const margin = (maxValue - minValue) * 0.1;
const expected = {
min: minValue - margin,
max: maxValue + margin,
};

expect(result).toEqual(expected);
});

it("should throw an error for empty array", () => {
expect(() => getMinMax([])).toThrow("The values array cannot be empty.");
});
});

function getModelData(model: string) {
const modelObj = models.find((m) => m.key === model);
if (!modelObj) {
Expand Down
Loading