Skip to content

Commit 4056dc3

Browse files
authored
New line options (#67)
* update dependencies * add new options for line * fix error: replace let with const * update synatx + default value for graphType
1 parent a78f7e8 commit 4056dc3

5 files changed

Lines changed: 3736 additions & 5579 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"yaml": "^2.1.3"
3434
},
3535
"dependencies": {
36-
"function-plot": "^1.22.9-0",
37-
"obsidian": "0.12.2"
36+
"function-plot": "^1.23.1",
37+
"obsidian": "^0.16.3"
3838
}
3939
}

src/app/CreatePlotModal.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Chart } from "function-plot";
22
import { FunctionPlotDatum } from "function-plot/dist/types";
33
import { Editor, Modal, Setting } from "obsidian";
44
import { DEFAULT_PLOT_OPTIONS, rendererOptions } from "../common/defaults";
5-
import { PlotOptions, rendererType } from "../common/types";
5+
import { PlotOptions, rendererType, Line } from "../common/types";
66
import type ObsidianFunctionPlot from "../main";
77
import { createPlot, renderPlotAsInteractive } from "../common/utils";
88

@@ -34,11 +34,17 @@ export default class CreatePlotModal extends Modal {
3434
this.plot.options.yAxis.domain = this.options.bounds.slice(2, 4);
3535
this.plot.options.grid = this.options.grid;
3636
this.plot.options.data = this.options.functions.map(
37-
(f): FunctionPlotDatum => {
38-
return {
39-
fn: f.split("=")[1],
40-
graphType: "polyline",
41-
};
37+
(line): FunctionPlotDatum => {
38+
// use polyline by default
39+
const lineProperties: Line = {graphType: "polyline"};
40+
41+
line.split("@").forEach((property) => {
42+
const tup = property.split("=");
43+
const value = tup[1].trim()
44+
lineProperties[tup[0].trim()] = value.startsWith("[") ? JSON.parse(value) : value;
45+
});
46+
47+
return lineProperties;
4248
}
4349
);
4450
// redirect errors within function-plot to debug

src/common/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ export type rendererType = "interactive"; //| "image";
1111
*/
1212
export type chartType = Chart & EventEmitter;
1313

14+
/**
15+
* An interface specifying the properties of a line.
16+
*/
17+
export interface Line {
18+
fn?: string;
19+
graphType?: "polyline" | "interval" | "scatter";
20+
nSamples?: number;
21+
range?: [number, number];
22+
closed?: boolean
23+
color?: string
24+
}
25+
1426
/**
1527
* An interface specifying the options for a plot.
1628
*/

src/common/utils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Editor, parseYaml } from "obsidian";
22
import type ObsidianFunctionPlot from "../main";
3-
import { PlotOptions } from "./types";
3+
import { PlotOptions, Line } from "./types";
44
import { type FunctionPlotOptions } from "function-plot/dist/types";
55
import createStylingPlugin from "../plugins/styling";
66
import functionPlot, { Chart } from "function-plot";
@@ -73,7 +73,17 @@ export async function createPlot(
7373
label: options.yLabel,
7474
},
7575
data: options.functions.map((line) => {
76-
return { fn: line.split("=")[1], graphType: "polyline" };
76+
// use polyline by default
77+
const lineProperties: Line = {graphType: "polyline"};
78+
79+
line.split("@").forEach((property) => {
80+
const tup = property.split("=");
81+
const value = tup[1].trim()
82+
// Using JSON.parse here to convert "range" value from string to real array
83+
lineProperties[tup[0].trim()] = value.startsWith("[") ? JSON.parse(value) : value;
84+
});
85+
86+
return lineProperties;
7787
}),
7888
};
7989
const plot = functionPlot(fPlotOptions);

0 commit comments

Comments
 (0)