Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug/5293_Allow_NaN_For_XYchart #5369

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ export class LinePlot {
let path: string | null;
if (this.orientation === 'horizontal') {
path = line()
.defined((d) => !isNaN(d[0]) && !isNaN(d[1]))
.y((d) => d[0])
.x((d) => d[1])(finalData);
} else {
path = line()
.defined((d) => !isNaN(d[0]) && !isNaN(d[1]))
.x((d) => d[0])
.y((d) => d[1])(finalData);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/mermaid/src/diagrams/xychart/parser/xychart.jison
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

"[" return 'SQUARE_BRACES_START'
"]" return 'SQUARE_BRACES_END'
"NaN" return 'NAN';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NaN is rather JS-originated, while many programming languages use their own keywords. Why not simply omitting the value?

[A-Za-z]+ return 'ALPHA';
":" return 'COLON';
\+ return 'PLUS';
Expand Down Expand Up @@ -116,6 +117,8 @@ plotData
commaSeparatedNumbers
: NUMBER_WITH_DECIMAL COMMA commaSeparatedNumbers { $$ = [Number($NUMBER_WITH_DECIMAL), ...$commaSeparatedNumbers] }
| NUMBER_WITH_DECIMAL { $$ = [Number($NUMBER_WITH_DECIMAL)] }
| NAN COMMA commaSeparatedNumbers { $$ = [Number($NAN), ...$commaSeparatedNumbers] }
| NAN { $$ = [Number($NAN)] }
Comment on lines +120 to +121
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is a wrong place to represent NaN, because commaSeparatedNumbers represents the list, and does not necessarily have to deal with all the possible values. Adding more things apart from numbers will inflate the grammar. My suggestion is to combine NUMBER_WITH_DECIMAL and NAN into a separate rule called optionalValue, but my suggestion is optional, I guess 😃

;

parseXAxis
Expand Down
93 changes: 93 additions & 0 deletions packages/mermaid/src/diagrams/xychart/parser/xychart.jison.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,97 @@ describe('Testing xychart jison file', () => {
[45, 99, 12]
);
});
it('parse bar Data with NaN', () => {
const str =
'xychart-beta\nx-axis xAxisName\ny-axis yAxisName\n bar barTitle [23, NaN, 56.6, .22]';
expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith({ text: 'yAxisName', type: 'text' });
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith({ text: 'xAxisName', type: 'text' });
expect(mockDB.setBarData).toHaveBeenCalledWith({ text: 'barTitle', type: 'text' }, [
23,
NaN,
56.6,
0.22,
]);
});
it('parse line Data with NaN', () => {
const str = 'xychart-beta\nx-axis xAxisName\ny-axis yAxisName\n line lineTitle [23, NaN, 56.6]';
expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.setLineData).toHaveBeenCalledWith({ text: 'lineTitle', type: 'text' }, [
23,
NaN,
56.6,
]);
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith({ text: 'xAxisName', type: 'text' });
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith({ text: 'yAxisName', type: 'text' });
});
it('parse multiple bar and line variant 1 with NaN', () => {
const str =
'xychart-beta\nx-axis xAxisName\ny-axis yAxisName\n bar barTitle1 [23, 45, NaN] \n line lineTitle1 [11, NaN, 67, 23] \n bar barTitle2 [NaN, 42, 56.89] \n line lineTitle2 [NaN, 99, 012]';
expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith({ text: 'yAxisName', type: 'text' });
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith({ text: 'xAxisName', type: 'text' });
expect(mockDB.setBarData).toHaveBeenCalledWith({ text: 'barTitle1', type: 'text' }, [
23,
45,
NaN,
]);
expect(mockDB.setBarData).toHaveBeenCalledWith({ text: 'barTitle2', type: 'text' }, [
NaN,
42,
56.89,
]);
expect(mockDB.setLineData).toHaveBeenCalledWith({ text: 'lineTitle1', type: 'text' }, [
11,
NaN,
67,
23,
]);
expect(mockDB.setLineData).toHaveBeenCalledWith({ text: 'lineTitle2', type: 'text' }, [
NaN,
99,
12,
]);
});
it('parse multiple bar and line variant 2 with NaN', () => {
const str = `
xychart-beta horizontal
title Basic xychart
x-axis "this is x axis" [category1, "category 2", category3]
y-axis yaxisText 10 --> 150
bar barTitle1 [NaN, 45, 56.6]
line lineTitle1 [11, NaN, 67, 23]
bar barTitle2 [13, 42, NaN]
line lineTitle2 [45, NaN, 012]`;
expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith({ text: 'yaxisText', type: 'text' });
expect(mockDB.setYAxisRangeData).toHaveBeenCalledWith(10, 150);
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith({ text: 'this is x axis', type: 'text' });
expect(mockDB.setXAxisBand).toHaveBeenCalledWith([
{ text: 'category1', type: 'text' },
{ text: 'category 2', type: 'text' },
{ text: 'category3', type: 'text' },
]);
expect(mockDB.setBarData).toHaveBeenCalledWith({ text: 'barTitle1', type: 'text' }, [
NaN,
45,
56.6,
]);
expect(mockDB.setBarData).toHaveBeenCalledWith({ text: 'barTitle2', type: 'text' }, [
13,
42,
NaN,
]);
expect(mockDB.setLineData).toHaveBeenCalledWith({ text: 'lineTitle1', type: 'text' }, [
11,
NaN,
67,
23,
]);
expect(mockDB.setLineData).toHaveBeenCalledWith({ text: 'lineTitle2', type: 'text' }, [
45,
NaN,
12,
]);
});
});
4 changes: 2 additions & 2 deletions packages/mermaid/src/diagrams/xychart/xychartRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ export const draw = (txt: string, id: string, _version: string, diagObj: Diagram
.append('rect')
.attr('x', (data) => data.x)
.attr('y', (data) => data.y)
.attr('width', (data) => data.width)
.attr('height', (data) => data.height)
.attr('width', (data) => (!Number.isNaN(data.width) ? data.width : 0))
.attr('height', (data) => (!Number.isNaN(data.height) ? data.height : 0))
.attr('fill', (data) => data.fill)
.attr('stroke', (data) => data.strokeFill)
.attr('stroke-width', (data) => data.strokeWidth);
Expand Down
Loading