Skip to content

Commit

Permalink
Merge pull request #239 from woodworker/aspect-ratio
Browse files Browse the repository at this point in the history
added aspect ratio parameter for graphs
  • Loading branch information
lazyguru authored Nov 6, 2023
2 parents 2e2de02 + c8d73bb commit 4d08b45
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/InputParameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ These key-value pairs are placed under the root of the code block.
| `textValueMap` | A container key for multiple text-value mapping | | |
| `fixedScale` | Uniform scaling factor to the graph dimensions | 1 | 1.0 |
| `fitPanelWidth` | Auto-fit the width of the chart to the container | 1 | false |
| `aspectRatio` | Change the 1:1 aspect ratio of the graph | number:number | 1:1 |
| `margin` | Four margins (top\|right\|bottom\|left) of the graph | 1~4 | 10 |
| `line` | A container key for parameters related to the line chart | | |
| `bar` | A container key for parameters related to the bar chart | | |
Expand Down
2 changes: 2 additions & 0 deletions examples/WeightTracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ searchTarget: weight
folder: diary
startDate: 2021-01-01
endDate: 2021-01-31
aspectRatio: 20:9
fitPanelWidth: 1
line:
title: Weight Log
yAxisLabel: Weight
Expand Down
18 changes: 18 additions & 0 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ export class RenderInfo {

fixedScale: number;
fitPanelWidth: boolean;
aspectRatio: AspectRatio;

output: any[];
line: LineInfo[];
Expand Down Expand Up @@ -620,6 +621,7 @@ export class RenderInfo {
this.textValueMap = {};

this.dataAreaSize = new Size(300, 300);
this.aspectRatio = new AspectRatio(1, 1);
this.margin = new Margin(10, 10, 10, 10); // top, right, bottom, left

this.fixedScale = 1.0;
Expand Down Expand Up @@ -974,6 +976,22 @@ export class Size {
}
}

export class AspectRatio {
x: number;
y: number;

constructor(x: number, y: number) {
this.x = x;
this.y = y;
}

public recalculateSize(size: Size): Size {
let aspectRatio = this.x / this.y;
let width = parseFloat((size.width * aspectRatio).toFixed(2))
return new Size(width, size.height);
}
}

export class Margin {
top: number;
right: number;
Expand Down
14 changes: 14 additions & 0 deletions src/parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
BulletInfo,
Dataset,
CustomDatasetInfo,
AspectRatio,
} from "./data";
import { TFolder, normalizePath } from "obsidian";
import { parseYaml } from "obsidian";
Expand Down Expand Up @@ -1521,6 +1522,19 @@ export function getRenderInfoFromYaml(
renderInfo.fitPanelWidth = yaml.fitPanelWidth;
}

// aspectRatio
if (typeof yaml.aspectRatio === "string") {
// yaml.fitPanelWidth
let ratioRegEx = /([0-9]*):([0-9]*)/;
let parts = yaml.aspectRatio.match(ratioRegEx);
parts.shift();
parts = parts.map((i: string)=>parseInt(i,10));
if (parts.length==2) {
renderInfo.aspectRatio = new AspectRatio(parts[0], parts[1]);
renderInfo.dataAreaSize = renderInfo.aspectRatio.recalculateSize(renderInfo.dataAreaSize)
}
}

// margin
let retMargin = getNumberArrayFromInput("margin", yaml.margin, 4, 10, true);
if (typeof retMargin === "string") {
Expand Down

0 comments on commit 4d08b45

Please sign in to comment.