This is an automated email from the ASF dual-hosted git repository.

ovilia pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/echarts-from-mermaid.git


The following commit(s) were added to refs/heads/main by this push:
     new 5ec2b43  feat: parse axis
5ec2b43 is described below

commit 5ec2b4360ec11906da11b7f8489769fede757c71
Author: Ovilia <[email protected]>
AuthorDate: Thu Feb 27 17:50:53 2025 +0800

    feat: parse axis
---
 src/base/parseAxis.test.ts | 52 ++++++++++++++++++++++++++++++++++++++++++++++
 src/base/parseAxis.ts      | 30 ++++++++++++++++++++++++++
 src/base/util.test.ts      | 32 ++++++++++++++++++++++++++++
 src/base/util.ts           | 33 +++++++++++++++++++++++++++++
 4 files changed, 147 insertions(+)

diff --git a/src/base/parseAxis.test.ts b/src/base/parseAxis.test.ts
new file mode 100644
index 0000000..dd3c24d
--- /dev/null
+++ b/src/base/parseAxis.test.ts
@@ -0,0 +1,52 @@
+import { parseAxis } from './parseAxis';
+
+describe('parseAxis', () => {
+  it('should parse category axis', () => {
+    const axis = parseAxis('x-axis [cat1, cat2, cat3]');
+    expect(axis).toEqual({
+      key: 'x',
+      type: 'category',
+      categories: ['cat1', 'cat2', 'cat3'],
+    });
+  });
+
+  it('should parse category axis with quotes', () => {
+    const axis = parseAxis('x-axis ["cat1", cat2, "cat3"]');
+    expect(axis).toEqual({
+      key: 'x',
+      type: 'category',
+      categories: ['cat1', 'cat2', 'cat3'],
+    });
+  });
+
+  it('should parse value axis', () => {
+    const axis = parseAxis('y-axis 100 --> 200');
+    expect(axis).toEqual({
+      key: 'y',
+      type: 'value',
+      min: 100,
+      max: 200,
+    });
+  });
+
+  it('should parse category axis with name', () => {
+    const axis = parseAxis('x-axis "Title" [cat1, cat2, cat3]');
+    expect(axis).toEqual({
+      key: 'x',
+      type: 'category',
+      name: 'Title',
+      categories: ['cat1', 'cat2', 'cat3'],
+    });
+  });
+
+  it('should parse value axis with name', () => {
+    const axis = parseAxis('y-axis "Revenue" 100 --> 200');
+    expect(axis).toEqual({
+      key: 'y',
+      type: 'value',
+      name: 'Revenue',
+      min: 100,
+      max: 200,
+    });
+  });
+});
diff --git a/src/base/parseAxis.ts b/src/base/parseAxis.ts
new file mode 100644
index 0000000..b96b914
--- /dev/null
+++ b/src/base/parseAxis.ts
@@ -0,0 +1,30 @@
+import { parseArray } from './util';
+
+export interface AxisDefinition {
+  type: 'value' | 'category';
+  key: 'x' | 'y';
+  categories?: string[];
+  min?: number;
+  max?: number;
+  name?: string;
+}
+
+const axisKeys = ['x', 'y'] as const;
+
+export function parseAxis(line: string): AxisDefinition {
+  let [axis, ...rest] = line.split(/\s+/);
+  const axisType = axisKeys.find((key) => axis.startsWith(key)) ?? 'x';
+
+  let title;
+  if (rest.length > 0 && rest[0].startsWith('"')) {
+    title = rest[0].replace(/"/g, '');
+    rest = rest.slice(1);
+  }
+
+  const result = parseArray(rest.join(' '));
+  return {
+    key: axisType,
+    ...result,
+    name: title,
+  };
+}
diff --git a/src/base/util.test.ts b/src/base/util.test.ts
new file mode 100644
index 0000000..64f5c54
--- /dev/null
+++ b/src/base/util.test.ts
@@ -0,0 +1,32 @@
+import { parseArray } from './util';
+
+describe('parseArray', () => {
+  it('should parse array', () => {
+    expect(parseArray('[abc, de, fg, 34]')).toEqual({
+      type: 'category',
+      categories: ['abc', 'de', 'fg', '34'],
+    });
+  });
+
+  it('should parse array awalys as string', () => {
+    expect(parseArray('[1, 2, 3]')).toEqual({
+      type: 'category',
+      categories: ['1', '2', '3'],
+    });
+  });
+
+  it('should parse array with quotes', () => {
+    expect(parseArray('["abc", "de", "fg", "34"]')).toEqual({
+      type: 'category',
+      categories: ['abc', 'de', 'fg', '34'],
+    });
+  });
+
+  it('should parse array with range', () => {
+    expect(parseArray('100 --> 200')).toEqual({
+      type: 'value',
+      min: 100,
+      max: 200,
+    });
+  });
+});
diff --git a/src/base/util.ts b/src/base/util.ts
new file mode 100644
index 0000000..b6b474f
--- /dev/null
+++ b/src/base/util.ts
@@ -0,0 +1,33 @@
+/**
+ * Axis line can be:
+ * x-axis [jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec]
+ * y-axis "Revenue (in $)" 4000 --> 11000
+ * y-axis 4000 --> 11000
+ */
+
+export type ParseArrayResult = {
+  type: 'category' | 'value';
+  categories?: string[];
+  min?: number;
+  max?: number;
+};
+
+export function parseArray(str: string): ParseArrayResult {
+  if (str.indexOf('-->') !== -1) {
+    const [min, max] = str.split('-->').map((s) => Number(s.trim()));
+    return {
+      type: 'value',
+      min,
+      max,
+    };
+  }
+
+  return {
+    type: 'category',
+    categories: str
+      .replace(/^\[|\]$/g, '')
+      .split(',')
+      .map((s) => s.trim())
+      .map((s) => s.replace(/^"|"$/g, '')),
+  };
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to