Skip to content

Commit

Permalink
Added yScaleMin, yScaleMax and yScaleOffset as diagram parameters for…
Browse files Browse the repository at this point in the history
… skin.conf. [GH-49]
  • Loading branch information
Daveiano committed Aug 19, 2022
1 parent e0b61e6 commit 2f74e14
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 16 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: Tests

on:
push:
branches: [1.x, 2.x]
paths:
- "**.py"
- skins/**
Expand Down
3 changes: 3 additions & 0 deletions skins/weewx-wdc/skin.conf
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ SKIN_VERSION = 2.1.0
[[[windDir]]]
curve = "basis"
lineWidth = 0
yScaleMin = 0
yScaleMax = 360
[[[radiation]]]
curve = "basis"
yScaleOffset = 150
[[[UV]]]
curve = "step"
[[[rainRate]]]
Expand Down
14 changes: 10 additions & 4 deletions skins/weewx-wdc/src/js/diagrams/bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,17 @@ export const BarDiagram: FunctionComponent<DiagramBaseProps> = (
left: 55,
}}
maxValue={
Math.max(
...props.data[0].data.map((item): number => (item as { y: number }).y)
) + getyScaleOffset(props.observation)
props.nivoProps.yScaleMax
? parseFloat(props.nivoProps.yScaleMax)
: Math.max(
...props.data[0].data.map(
(item): number => (item as { y: number }).y
)
) + getyScaleOffset(props.observation, props.nivoProps.yScaleOffset)
}
minValue={
props.nivoProps.yScaleMin ? parseFloat(props.nivoProps.yScaleMin) : 0
}
minValue={0}
keys={["y"]}
tooltip={(point) => (
<TooltipBar
Expand Down
30 changes: 23 additions & 7 deletions skins/weewx-wdc/src/js/diagrams/line.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,22 @@ export const LineDiagram: FunctionComponent<DiagramBaseProps> = (
data={props.data}
enableArea={enableArea.includes(props.observation)}
areaOpacity={darkMode ? 0.75 : props.observation === "wind" ? 0.5 : 0.07}
areaBaselineValue={
areaBaselineValue0.includes(props.observation)
? 0
: Math.min(...combinedData.map((item) => item.y)) -
getyScaleOffset(props.observation)
}
areaBaselineValue={((): number => {
if (props.nivoProps.yScaleMin) {
return parseFloat(props.nivoProps.yScaleMin);
}

if (areaBaselineValue0.includes(props.observation)) {
return 0;
}

return (
Math.min(...combinedData.map((item) => item.y)) -
(props.nivoProps.yScaleOffset
? parseFloat(props.nivoProps.yScaleOffset)
: getyScaleOffset(props.observation))
);
})()}
enableCrosshair={true}
enablePoints={true}
enableSlices={props.data.length > 1 ? "x" : false}
Expand Down Expand Up @@ -198,7 +208,13 @@ export const LineDiagram: FunctionComponent<DiagramBaseProps> = (
type: "time",
format: "%s",
}}
yScale={getyScale(props.observation, combinedData)}
yScale={getyScale(
props.observation,
combinedData,
props.nivoProps.yScaleOffset,
props.nivoProps.yScaleMin,
props.nivoProps.yScaleMax
)}
xFormat="time:%Y/%m/%d %H:%M"
yFormat={(value) => `${value} ${props.unit}`}
theme={
Expand Down
3 changes: 3 additions & 0 deletions skins/weewx-wdc/src/js/diagrams/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type DiagramBaseProps = {
enablePoints?: boolean;
enableLabel?: boolean;
enableCrosshair?: boolean;
yScaleOffset?: string;
yScaleMin?: string;
yScaleMax?: string;
};
};

Expand Down
31 changes: 27 additions & 4 deletions skins/weewx-wdc/src/js/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export const areaBaselineValue0: string[] = [
"wind",
];

export const getyScaleOffset = (obs: string): number => {
export const getyScaleOffset = (obs: string, yScaleOffset?: string): number => {
if (yScaleOffset) {
return parseFloat(yScaleOffset);
}

let offset = 3;

if (obs === "humidity") {
Expand Down Expand Up @@ -52,6 +56,10 @@ export const getyScaleOffset = (obs: string): number => {
offset = 1;
}

if (obs === "pressure" || obs === "barometer" || obs === "altimeter") {
offset = 1;
}

if (
(obs === "pressure" || obs === "barometer") &&
(window as any).weewxWdcConfig.units.group_pressure === "inHg"
Expand All @@ -62,7 +70,13 @@ export const getyScaleOffset = (obs: string): number => {
return offset;
};

export const getyScale = (obs: string, data: Series[]): ScaleSpec => {
export const getyScale = (
obs: string,
data: Series[],
yScaleOffset?: string,
yScaleMin?: string,
yScaleMax?: string
): ScaleSpec => {
let staticMin: "auto" | number | undefined = undefined;
let staticMax: "auto" | number | undefined = undefined;

Expand Down Expand Up @@ -91,16 +105,25 @@ export const getyScale = (obs: string, data: Series[]): ScaleSpec => {
staticMin = 0;
}

if (yScaleMin) {
staticMin = yScaleMin === "auto" ? yScaleMin : parseFloat(yScaleMin);
}
if (yScaleMax) {
staticMax = yScaleMax === "auto" ? yScaleMax : parseFloat(yScaleMax);
}

return {
type: "linear",
min:
typeof staticMin === "number" || typeof staticMin === "string"
? staticMin
: Math.min(...data.map((item) => item.y)) - getyScaleOffset(obs),
: Math.min(...data.map((item) => item.y)) -
(yScaleOffset ? parseFloat(yScaleOffset) : getyScaleOffset(obs)),
max:
typeof staticMax === "number" || typeof staticMax === "string"
? staticMax
: Math.max(...data.map((item) => item.y)) + getyScaleOffset(obs),
: Math.max(...data.map((item) => item.y)) +
(yScaleOffset ? parseFloat(yScaleOffset) : getyScaleOffset(obs)),
};
};

Expand Down

0 comments on commit 2f74e14

Please sign in to comment.