|
1 | | -import type { FunctionPlotAnnotation, FunctionPlotDatum } from "function-plot"; |
| 1 | +import type { |
| 2 | + FunctionPlotAnnotation, |
| 3 | + FunctionPlotDatum, |
| 4 | + FunctionPlotOptions, |
| 5 | +} from "function-plot"; |
2 | 6 | import { cloneDeep } from "lodash-es"; |
3 | 7 |
|
4 | 8 | export type ValueLabel = { value: string; label: string; default?: boolean }; |
@@ -422,3 +426,74 @@ export function toInternalAnnotation(items: FunctionPlotAnnotation[]) { |
422 | 426 | }); |
423 | 427 | return cloned; |
424 | 428 | } |
| 429 | + |
| 430 | +export type InternalGraphOptions = { |
| 431 | + xAxis: { |
| 432 | + invert: boolean; |
| 433 | + label: string; |
| 434 | + type: "linear" | "log"; |
| 435 | + }; |
| 436 | + yAxis: { |
| 437 | + invert: boolean; |
| 438 | + label: string; |
| 439 | + type: "linear" | "log"; |
| 440 | + }; |
| 441 | + grid: boolean; |
| 442 | + title: string; |
| 443 | +}; |
| 444 | + |
| 445 | +export function toInternalGraphOptions( |
| 446 | + original: Partial<FunctionPlotOptions> |
| 447 | +): InternalGraphOptions { |
| 448 | + const { xAxis, yAxis, title, grid } = original; |
| 449 | + return { |
| 450 | + xAxis: { |
| 451 | + invert: xAxis?.invert ?? false, |
| 452 | + label: xAxis?.label ?? "", |
| 453 | + type: xAxis?.type ?? "linear", |
| 454 | + }, |
| 455 | + yAxis: { |
| 456 | + invert: yAxis?.invert ?? false, |
| 457 | + label: yAxis?.label ?? "", |
| 458 | + type: yAxis?.type ?? "linear", |
| 459 | + }, |
| 460 | + title: title ?? "", |
| 461 | + grid: grid ?? false, |
| 462 | + }; |
| 463 | +} |
| 464 | + |
| 465 | +function removeUndefined(obj: object) { |
| 466 | + Object.entries(obj).forEach(([key, value]) => { |
| 467 | + if (value === undefined) { |
| 468 | + delete (<any>obj)[key]; |
| 469 | + } else if (typeof value === "object" && value !== null) { |
| 470 | + removeUndefined(value); |
| 471 | + } |
| 472 | + }); |
| 473 | +} |
| 474 | + |
| 475 | +export function toOriginalGraphOptions( |
| 476 | + internal: InternalGraphOptions |
| 477 | +): Partial<FunctionPlotOptions> { |
| 478 | + const { xAxis, yAxis, title, grid } = internal; |
| 479 | + const checkIfDefault = (value: any, defaultValue: any) => |
| 480 | + value === defaultValue ? undefined : value; |
| 481 | + const cloned: Partial<FunctionPlotOptions> = { |
| 482 | + xAxis: { |
| 483 | + invert: checkIfDefault(xAxis.invert, false), |
| 484 | + label: checkIfDefault(xAxis.label, ""), |
| 485 | + type: checkIfDefault(xAxis.type, "linear"), |
| 486 | + }, |
| 487 | + yAxis: { |
| 488 | + invert: checkIfDefault(yAxis.invert, false), |
| 489 | + label: checkIfDefault(yAxis.label, ""), |
| 490 | + type: checkIfDefault(yAxis.type, "linear"), |
| 491 | + }, |
| 492 | + title: checkIfDefault(title, ""), |
| 493 | + grid: checkIfDefault(grid, false), |
| 494 | + }; |
| 495 | + removeUndefined(cloned); |
| 496 | + if (Object.keys(cloned.xAxis ?? {}).length === 0) delete cloned.xAxis; |
| 497 | + if (Object.keys(cloned.yAxis ?? {}).length === 0) delete cloned.yAxis; |
| 498 | + return cloned; |
| 499 | +} |
0 commit comments