Skip to content

Commit

Permalink
Fix auto axis on line graph with smooth fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbonner committed Dec 14, 2024
1 parent 916894e commit ea33971
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/hub/controllers/LineGraphController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
LineGraphRendererCommand_NumericField
} from "../../shared/renderers/LineGraphRenderer";
import { NoopUnitConversion, UnitConversionPreset, convertWithPreset } from "../../shared/units";
import { clampValue, createUUID, scaleValue } from "../../shared/util";
import { clampValue, createUUID, scaleValueClamped } from "../../shared/util";
import SourceList from "../SourceList";
import { LineGraphController_DiscreteConfig, LineGraphController_NumericConfig } from "./LineGraphController_Config";
import TabController from "./TabController";
Expand Down Expand Up @@ -387,7 +387,7 @@ export default class LineGraphController implements TabController {
case "smooth":
// Interpolate to displayed value
if (data.timestamps.length >= 2) {
data.values[0] = scaleValue(
data.values[0] = scaleValueClamped(
timeRange[0],
[data.timestamps[0], data.timestamps[1]],
[data.values[0], data.values[1]]
Expand All @@ -414,12 +414,14 @@ export default class LineGraphController implements TabController {
break;
case "smooth":
// Interpolate to displayed value
data.values[data.values.length - 1] = scaleValue(
timeRange[1],
[data.timestamps[data.timestamps.length - 2], data.timestamps[data.timestamps.length - 1]],
[data.values[data.values.length - 2], data.values[data.values.length - 1]]
);
data.timestamps[data.timestamps.length - 1] = timeRange[1];
if (data.timestamps.length >= 2) {
data.values[data.values.length - 1] = scaleValueClamped(
timeRange[1],
[data.timestamps[data.timestamps.length - 2], data.timestamps[data.timestamps.length - 1]],
[data.values[data.values.length - 2], data.values[data.values.length - 1]]
);
data.timestamps[data.timestamps.length - 1] = timeRange[1];
}
break;
}
} else if (
Expand Down
7 changes: 7 additions & 0 deletions src/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ export function scaleValue(value: number, oldRange: [number, number], newRange:
return ((value - oldRange[0]) / (oldRange[1] - oldRange[0])) * (newRange[1] - newRange[0]) + newRange[0];
}

/** Converts a value between two ranges. */
export function scaleValueClamped(value: number, oldRange: [number, number], newRange: [number, number]): number {
return (
clampValue((value - oldRange[0]) / (oldRange[1] - oldRange[0]), 0, 1) * (newRange[1] - newRange[0]) + newRange[0]
);
}

/** Converts a value between two ranges, with caching for better performance. */
export class ValueScaler {
private a: number;
Expand Down

0 comments on commit ea33971

Please sign in to comment.