Skip to content

Commit e385ae5

Browse files
authored
Merge pull request leonhma#133 from hrrs01/v2
Added basic legend support
2 parents a68edd0 + 77e8756 commit e385ae5

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

src/common/defaults.ts

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export const DEFAULT_CONSTANT_INPUTS: ConstantInputs = {
8484
*/
8585
export const DEFAULT_PLOT_INPUTS: PlotInputs = {
8686
constants: {},
87+
legends: false,
8788
xAxis: {
8889
label: undefined,
8990
domain: {

src/common/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type DeepNonNullable<T> = {
1818

1919
export interface FunctionInputs {
2020
id?: string;
21+
name?: string;
2122
scope: { [_: string]: number };
2223
fnType: "linear" | "vector" | "polar";
2324
fn?: string;
@@ -52,6 +53,7 @@ export interface ConstantInputs {
5253
export interface PlotInputs {
5354
data: FunctionInputs[];
5455
constants: { [_: string]: ConstantInputs };
56+
legends: boolean;
5557
xAxis: {
5658
label?: string;
5759
domain: {

src/common/utils.ts

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Editor, parseYaml } from "obsidian";
22
import type ObsidianFunctionPlot from "../main";
3+
import type { Selection } from "d3";
34
import type {
45
FunctionInputs,
56
PlotInputs,
@@ -18,6 +19,7 @@ import type {
1819
FunctionPlotOptions,
1920
} from "function-plot/dist/types";
2021
import { FunctionPlot } from "../fnplot";
22+
import type { Chart } from "function-plot";
2123

2224
export function gcd(a: number, b: number): number {
2325
return !b ? a : gcd(b, a % b);
@@ -104,6 +106,23 @@ export function toFunctionPlotOptions(
104106
options.yAxis.domain.max ?? FALLBACK_PLOT_INPUTS.yAxis.domain.max,
105107
],
106108
},
109+
plugins: [(chart: Chart) => {
110+
if(!options.legends) return;
111+
chart.root.append("text").attr("class", "top-left-legend");
112+
let text_color = "#00ff00";
113+
let legends: {name: string, color: string}[] = [];
114+
chart.options.data?.forEach((datum, index, arr) => {
115+
legends.push({name: (options.data[index].name) || "", color: datum.color || ""});
116+
})
117+
const tll: any = chart.root.select(".top-left-legend");
118+
console.log(legends);
119+
tll.selectAll("tspan").remove();
120+
legends.forEach((legend, index, arr) => {
121+
tll.attr("y", (chart.meta.margin?.top || 20) / 2)
122+
.attr("x", chart.meta.margin?.left || 10)
123+
tll.append("tspan").attr('fill', legend.color).text("█ " + legend.name + "\n");;
124+
})
125+
}],
107126
grid: options.grid ?? undefined,
108127
disableZoom: options.disableZoom ?? undefined,
109128
};
@@ -230,6 +249,7 @@ export function parseYAMLCodeBlock(content: string): PlotInputs {
230249
return {
231250
constants: {},
232251
title: header.title ?? DEFAULT_PLOT_INPUTS.title,
252+
legends: false,
233253
xAxis: {
234254
label: header.xLabel ?? FALLBACK_PLOT_INPUTS.xAxis.label,
235255
domain: header.bounds

src/components/PlotModal/Function.svelte

+5-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
<option value="vector">vector</option>
3434
</Dropdown>
3535

36+
<TextInput placeholder="Name" width="10ch" bind:value={datum.name} />
37+
3638
{#if datum.fnType === "linear"}
3739
<TextInput placeholder="f(x)=" width="20ch" bind:value={datum.fn} />
3840
{/if}
@@ -112,8 +114,8 @@
112114
<style lang="scss">
113115
.functionplot-item-data {
114116
display: inline-grid;
115-
grid-template-columns: min-content auto repeat(3, min-content);
116-
column-gap: 1em;
117+
grid-template-columns: repeat(6, min-content);
118+
column-gap: 0.5em;
117119
place-items: center start;
118120
width: 100%;
119121
padding: 0.5em;
@@ -125,7 +127,7 @@
125127
.functionplot-nums-inputs {
126128
display: flex;
127129
flex-direction: row;
128-
column-gap: 0.6em;
130+
column-gap: 0.5em;
129131
width: min-content;
130132
}
131133
</style>

src/components/PlotModal/PlotModal.svelte

+3
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@
145145
<SettingItem name="Disable Zoom">
146146
<Switch bind:checked={options.disableZoom} />
147147
</SettingItem>
148+
<SettingItem name="Show Legends">
149+
<Switch bind:checked={options.legends} />
150+
</SettingItem>
148151
<SettingItem name="Show Grid">
149152
<Switch bind:checked={options.grid} />
150153
</SettingItem>

0 commit comments

Comments
 (0)