Skip to content

Commit

Permalink
feat(graphProcessor): add yAxisProps to result of processGraphData
Browse files Browse the repository at this point in the history
  • Loading branch information
ecarreras committed Jul 16, 2024
1 parent 92426a8 commit 68afb25
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
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
20 changes: 15 additions & 5 deletions src/Graph/processor/graphProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,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 @@ -178,11 +177,24 @@ export const processGraphData = ({
finalData = adjustedUninformedData.sort((a, b) => b.value - a.value);
}

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

if (ooui.type === "line" && ooui.y_range) {
result.yAxisProps = {

Check failure on line 187 in src/Graph/processor/graphProcessor.ts

View workflow job for this annotation

GitHub Actions / build

Property 'yAxisProps' does not exist on type '{ data: GraphValues[]; isGroup: boolean; isStack: boolean; }'.
mode: ooui.y_range,
};
if (ooui.y_range === "auto") {
const { min, max } = getMinMax(finalData);
result.yAxisProps.min = min;

Check failure on line 192 in src/Graph/processor/graphProcessor.ts

View workflow job for this annotation

GitHub Actions / build

Property 'yAxisProps' does not exist on type '{ data: GraphValues[]; isGroup: boolean; isStack: boolean; }'.
result.yAxisProps.max = max;

Check failure on line 193 in src/Graph/processor/graphProcessor.ts

View workflow job for this annotation

GitHub Actions / build

Property 'yAxisProps' does not exist on type '{ data: GraphValues[]; isGroup: boolean; isStack: boolean; }'.
}
}

return result;
};

export function getValuesForYField({
Expand Down Expand Up @@ -321,8 +333,6 @@ export function getMinMax(values: GraphValues[], margin: number = 0.1) {
const maxValue = Math.max(...valueList);
const calculatedMargin = (maxValue - minValue) * margin;

console.log(calculatedMargin);

return {
min: minValue - calculatedMargin,
max: maxValue + calculatedMargin,
Expand Down
23 changes: 23 additions & 0 deletions src/spec/graphProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,29 @@ 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 yAxisProps 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.yAxisProps.mode).toBe("auto");
expect(result.yAxisProps.min).toBe(8);
expect(result.yAxisProps.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

0 comments on commit 68afb25

Please sign in to comment.