Made the axis title optional

This commit is contained in:
Subhash Halder
2023-09-02 18:08:59 +05:30
parent de2aa9d740
commit e0418eb661
4 changed files with 61 additions and 10 deletions

View File

@@ -15,6 +15,17 @@
<body>
<h1>XY Charts demos</h1>
<pre class="mermaid">
xychart-beta
title "Sales Revenue (in $)"
x-axis [jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec]
y-axis "Revenue (in $)" 4000 --> 11000
bar [5000, 6000, 7500, 8200, 9500, 10500, 11000, 10200, 9200, 8500, 7000, 6000]
line [5000, 6000, 7500, 8200, 9500, 10500, 11000, 10200, 9200, 8500, 7000, 6000]
</pre>
<hr />
<pre class="mermaid">
xychart-beta horizontal
title "Basic xychart"
@@ -24,8 +35,6 @@
line [23, 46, 75, 43]
</pre>
<hr />
<h1>XY Charts demos</h1>
<pre class="mermaid">
xychart-beta

View File

@@ -93,7 +93,7 @@ export abstract class BaseAxis implements Axis {
this.showTick = true;
availableHeight -= this.axisConfig.tickLength;
}
if (this.axisConfig.showTitle) {
if (this.axisConfig.showTitle && this.title) {
const spaceRequired = this.textDimensionCalculator.getMaxDimension(
[this.title],
this.axisConfig.labelFontSize
@@ -126,7 +126,7 @@ export abstract class BaseAxis implements Axis {
this.showTick = true;
availableWidth -= this.axisConfig.tickLength;
}
if (this.axisConfig.showTitle) {
if (this.axisConfig.showTitle && this.title) {
const spaceRequired = this.textDimensionCalculator.getMaxDimension(
[this.title],
this.axisConfig.labelFontSize

View File

@@ -55,8 +55,8 @@
<string>[^"]* return "STR";
"[" return 'SQUARE_BRACES_START'
"]" return 'SQUARE_BRACES_END'
"[" return 'SQUARE_BRACES_START'
"]" return 'SQUARE_BRACES_END'
[A-Za-z]+ return 'ALPHA';
":" return 'COLON';
\+ return 'PLUS';
@@ -117,8 +117,13 @@ commaSeparatedNumbers
parseXAxis
: text {yy.setXAxisTitle($text);}
| text bandData {yy.setXAxisTitle($text); yy.setXAxisBand($bandData);}
| text NUMBER_WITH_DECIMAL ARROW_DELIMITER NUMBER_WITH_DECIMAL {yy.setXAxisTitle($text); yy.setXAxisRangeData(Number($NUMBER_WITH_DECIMAL1), Number($NUMBER_WITH_DECIMAL2));}
| text xAxisData {yy.setXAxisTitle($text);}
| xAxisData {yy.setXAxisTitle({type: 'text', text: ''});}
;
xAxisData
: bandData {yy.setXAxisBand($bandData);}
| NUMBER_WITH_DECIMAL ARROW_DELIMITER NUMBER_WITH_DECIMAL {yy.setXAxisRangeData(Number($NUMBER_WITH_DECIMAL1), Number($NUMBER_WITH_DECIMAL2));}
;
bandData
@@ -131,8 +136,13 @@ commaSeparatedTexts
;
parseYAxis
: text {yy.setYAxisTitle($text);}
| text NUMBER_WITH_DECIMAL ARROW_DELIMITER NUMBER_WITH_DECIMAL {yy.setYAxisTitle($text); yy.setYAxisRangeData(Number($NUMBER_WITH_DECIMAL1), Number($NUMBER_WITH_DECIMAL2));}
: text {yy.setYAxisTitle($text);}
| text yAxisData {yy.setYAxisTitle($text);}
| yAxisData {yy.setYAxisTitle({type: "text", text: ""});}
;
yAxisData
: NUMBER_WITH_DECIMAL ARROW_DELIMITER NUMBER_WITH_DECIMAL {yy.setYAxisRangeData(Number($NUMBER_WITH_DECIMAL1), Number($NUMBER_WITH_DECIMAL2));}
;
eol

View File

@@ -128,6 +128,16 @@ describe('Testing xychart jison file', () => {
expect(mockDB.setXAxisRangeData).toHaveBeenCalledWith(45.5, 0.34);
});
it('parse x-axis without axisname and range data', () => {
const str = 'xychart-beta \nx-axis 45.5 --> 1.34 \n';
expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith({
text: '',
type: 'text',
});
expect(mockDB.setXAxisRangeData).toHaveBeenCalledWith(45.5, 1.34);
});
it('parse x-axis with axis name and category data', () => {
const str = 'xychart-beta \nx-axis xAxisName [ "cat1" , cat2a ] \n ';
expect(parserFnConstructor(str)).not.toThrow();
@@ -144,6 +154,22 @@ describe('Testing xychart jison file', () => {
]);
});
it('parse x-axis without axisname and category data', () => {
const str = 'xychart-beta \nx-axis [ "cat1" , cat2a ] \n ';
expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith({
text: '',
type: 'text',
});
expect(mockDB.setXAxisBand).toHaveBeenCalledWith([
{
text: 'cat1',
type: 'text',
},
{ text: 'cat2a', type: 'text' },
]);
});
it('parse x-axis throw error if unbalanced bracket', () => {
let str = 'xychart-beta \nx-axis xAxisName [ "cat1" [ cat2a ] \n ';
expect(parserFnConstructor(str)).toThrow();
@@ -218,6 +244,12 @@ describe('Testing xychart jison file', () => {
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith({ text: 'yAxisName', type: 'text' });
expect(mockDB.setYAxisRangeData).toHaveBeenCalledWith(45.5, 33);
});
it('parse y-axis without axisname with range data', () => {
const str = 'xychart-beta \ny-axis 45.5 --> 33 \n';
expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith({ text: '', type: 'text' });
expect(mockDB.setYAxisRangeData).toHaveBeenCalledWith(45.5, 33);
});
it('parse y-axis with axis name with range data with only decimal part', () => {
const str = 'xychart-beta \ny-axis yAxisName 45.5 --> .33 \n';
expect(parserFnConstructor(str)).not.toThrow();