Fixed prettier issues

This commit is contained in:
Subhash Halder 2023-07-04 20:45:22 +05:30
parent 89cfa17b07
commit 1c8497474a
10 changed files with 82 additions and 83 deletions

View File

@ -8,14 +8,14 @@ export type SimplePlotDataType = [string | number, number][];
export interface LinePlotData { export interface LinePlotData {
type: 'line'; type: 'line';
strokeFill: string, strokeFill: string;
strokeWidth: number, strokeWidth: number;
data: SimplePlotDataType; data: SimplePlotDataType;
} }
export interface BarPlotData { export interface BarPlotData {
type: 'bar' type: 'bar';
fill: string, fill: string;
data: SimplePlotDataType; data: SimplePlotDataType;
} }
@ -30,7 +30,7 @@ export interface BandAxisDataType {
categories: string[]; categories: string[];
} }
export interface LinearAxisDataType{ export interface LinearAxisDataType {
title: string; title: string;
min: number; min: number;
max: number; max: number;
@ -42,7 +42,6 @@ export function isBandAxisData(data: any): data is BandAxisDataType {
return data.categories && Array.isArray(data.categories); return data.categories && Array.isArray(data.categories);
} }
export interface XYChartData { export interface XYChartData {
xAxis: AxisDataType; xAxis: AxisDataType;
yAxis: AxisDataType; yAxis: AxisDataType;

View File

@ -1,13 +1,16 @@
import { XYChartConfig } from '../../../../config.type.js'; import { XYChartConfig } from '../../../../config.type.js';
import { import {
BoundingRect, BoundingRect,
ChartComponent, ChartComponent,
Dimension, Dimension,
DrawableElem, DrawableElem,
Point, Point,
XYChartData, XYChartData,
} from '../Interfaces.js'; } from '../Interfaces.js';
import { ITextDimensionCalculator, TextDimensionCalculatorWithFont } from '../TextDimensionCalculator.js'; import {
ITextDimensionCalculator,
TextDimensionCalculatorWithFont,
} from '../TextDimensionCalculator.js';
export class ChartTitle implements ChartComponent { export class ChartTitle implements ChartComponent {
private boundingRect: BoundingRect; private boundingRect: BoundingRect;
@ -30,7 +33,10 @@ export class ChartTitle implements ChartComponent {
this.boundingRect.y = point.y; this.boundingRect.y = point.y;
} }
calculateSpace(availableSpace: Dimension): Dimension { calculateSpace(availableSpace: Dimension): Dimension {
const titleDimension = this.textDimensionCalculator.getDimension([this.chartData.title], this.chartConfig.titleFontSize); const titleDimension = this.textDimensionCalculator.getDimension(
[this.chartData.title],
this.chartConfig.titleFontSize
);
const widthRequired = Math.max(titleDimension.width, availableSpace.width); const widthRequired = Math.max(titleDimension.width, availableSpace.width);
const heightRequired = titleDimension.height + 2 * this.chartConfig.titlePadding; const heightRequired = titleDimension.height + 2 * this.chartConfig.titlePadding;
if ( if (

View File

@ -58,8 +58,8 @@ export abstract class BaseAxis implements IAxis {
} }
recalculateOuterPaddingToDrawBar(): void { recalculateOuterPaddingToDrawBar(): void {
if((0.7 * this.getTickDistance()) > (this.outerPadding * 2) ) { if (0.7 * this.getTickDistance() > this.outerPadding * 2) {
this.outerPadding = Math.floor((0.7 * this.getTickDistance())/2); this.outerPadding = Math.floor((0.7 * this.getTickDistance()) / 2);
} }
this.recalculateScale(); this.recalculateScale();
} }
@ -267,7 +267,11 @@ export abstract class BaseAxis implements IAxis {
data: this.getTickValues().map((tick) => ({ data: this.getTickValues().map((tick) => ({
text: tick.toString(), text: tick.toString(),
x: this.getScaleValue(tick), x: this.getScaleValue(tick),
y: this.boundingRect.y + this.boundingRect.height - this.axisConfig.lablePadding - this.axisConfig.tickLength, y:
this.boundingRect.y +
this.boundingRect.height -
this.axisConfig.lablePadding -
this.axisConfig.tickLength,
fill: this.axisConfig.labelFill, fill: this.axisConfig.labelFill,
fontSize: this.axisConfig.labelFontSize, fontSize: this.axisConfig.labelFontSize,
rotation: 0, rotation: 0,
@ -282,7 +286,9 @@ export abstract class BaseAxis implements IAxis {
type: 'path', type: 'path',
groupTexts: ['bottom-axis', 'ticks'], groupTexts: ['bottom-axis', 'ticks'],
data: this.getTickValues().map((tick) => ({ data: this.getTickValues().map((tick) => ({
path: `M ${this.getScaleValue(tick)},${y + this.boundingRect.height} L ${this.getScaleValue(tick)},${ path: `M ${this.getScaleValue(tick)},${
y + this.boundingRect.height
} L ${this.getScaleValue(tick)},${
y + this.boundingRect.height - this.axisConfig.tickLength y + this.boundingRect.height - this.axisConfig.tickLength
}`, }`,
strokeFill: this.axisConfig.tickFill, strokeFill: this.axisConfig.tickFill,
@ -316,7 +322,7 @@ export abstract class BaseAxis implements IAxis {
return this.getDrawaableElementsForLeftAxis(); return this.getDrawaableElementsForLeftAxis();
} }
if (this.axisPosition === 'right') { if (this.axisPosition === 'right') {
throw Error("Drawing of right axis is not implemented"); throw Error('Drawing of right axis is not implemented');
} }
if (this.axisPosition === 'bottom') { if (this.axisPosition === 'bottom') {
return this.getDrawaableElementsForBottomAxis(); return this.getDrawaableElementsForBottomAxis();

View File

@ -1,9 +1,5 @@
import { XYChartAxisConfig } from '../../../../../config.type.js'; import { XYChartAxisConfig } from '../../../../../config.type.js';
import { import { AxisDataType, ChartComponent, isBandAxisData } from '../../Interfaces.js';
AxisDataType,
ChartComponent,
isBandAxisData,
} from '../../Interfaces.js';
import { TextDimensionCalculatorWithFont } from '../../TextDimensionCalculator.js'; import { TextDimensionCalculatorWithFont } from '../../TextDimensionCalculator.js';
import { BandAxis } from './BandAxis.js'; import { BandAxis } from './BandAxis.js';
import { LinearAxis } from './LinearAxis.js'; import { LinearAxis } from './LinearAxis.js';
@ -19,7 +15,11 @@ export interface IAxis extends ChartComponent {
setRange(range: [number, number]): void; setRange(range: [number, number]): void;
} }
export function getAxis(data: AxisDataType, axisConfig: XYChartAxisConfig, fontFamily?: string): IAxis { export function getAxis(
data: AxisDataType,
axisConfig: XYChartAxisConfig,
fontFamily?: string
): IAxis {
const textDimansionCalculator = new TextDimensionCalculatorWithFont(fontFamily); const textDimansionCalculator = new TextDimensionCalculatorWithFont(fontFamily);
if (isBandAxisData(data)) { if (isBandAxisData(data)) {
return new BandAxis(axisConfig, data.categories, data.title, textDimansionCalculator); return new BandAxis(axisConfig, data.categories, data.title, textDimansionCalculator);

View File

@ -1,9 +1,5 @@
import { XYChartConfig } from '../../../../../config.type.js'; import { XYChartConfig } from '../../../../../config.type.js';
import { import { BarPlotData, BoundingRect, DrawableElem } from '../../Interfaces.js';
BarPlotData,
BoundingRect,
DrawableElem,
} from '../../Interfaces.js';
import { IAxis } from '../axis/index.js'; import { IAxis } from '../axis/index.js';
export class BarPlot { export class BarPlot {

View File

@ -1,7 +1,10 @@
import { XYChartConfig } from '../../../../../config.type.js'; import { XYChartConfig } from '../../../../../config.type.js';
import { BoundingRect, DrawableElem } from '../../Interfaces.js'; import { BoundingRect, DrawableElem } from '../../Interfaces.js';
export class PlotBorder { export class PlotBorder {
constructor(private boundingRect: BoundingRect, private orientation: XYChartConfig['chartOrientation']) {} constructor(
private boundingRect: BoundingRect,
private orientation: XYChartConfig['chartOrientation']
) {}
getDrawableElement(): DrawableElem[] { getDrawableElement(): DrawableElem[] {
const { x, y, width, height } = this.boundingRect; const { x, y, width, height } = this.boundingRect;

View File

@ -1,10 +1,4 @@
import { import { XYChartData, Dimension, BoundingRect, DrawableElem, Point } from '../../Interfaces.js';
XYChartData,
Dimension,
BoundingRect,
DrawableElem,
Point,
} from '../../Interfaces.js';
import { IAxis } from '../axis/index.js'; import { IAxis } from '../axis/index.js';
import { ChartComponent } from '../../Interfaces.js'; import { ChartComponent } from '../../Interfaces.js';
import { LinePlot } from './LinePlot.js'; import { LinePlot } from './LinePlot.js';
@ -12,9 +6,8 @@ import { PlotBorder } from './PlotBorder.js';
import { BarPlot } from './BarPlot.js'; import { BarPlot } from './BarPlot.js';
import { XYChartConfig } from '../../../../../config.type.js'; import { XYChartConfig } from '../../../../../config.type.js';
export interface IPlot extends ChartComponent { export interface IPlot extends ChartComponent {
setAxes(xAxis: IAxis, yAxis: IAxis): void setAxes(xAxis: IAxis, yAxis: IAxis): void;
} }
export class Plot implements IPlot { export class Plot implements IPlot {
@ -22,10 +15,7 @@ export class Plot implements IPlot {
private xAxis?: IAxis; private xAxis?: IAxis;
private yAxis?: IAxis; private yAxis?: IAxis;
constructor( constructor(private chartConfig: XYChartConfig, private chartData: XYChartData) {
private chartConfig: XYChartConfig,
private chartData: XYChartData,
) {
this.boundingRect = { this.boundingRect = {
x: 0, x: 0,
y: 0, y: 0,
@ -51,33 +41,43 @@ export class Plot implements IPlot {
}; };
} }
getDrawableElements(): DrawableElem[] { getDrawableElements(): DrawableElem[] {
if(!(this.xAxis && this.yAxis)) { if (!(this.xAxis && this.yAxis)) {
throw Error("Axes must be passed to render Plots"); throw Error('Axes must be passed to render Plots');
} }
const drawableElem: DrawableElem[] = [ const drawableElem: DrawableElem[] = [
...new PlotBorder(this.boundingRect, this.chartConfig.chartOrientation).getDrawableElement() ...new PlotBorder(this.boundingRect, this.chartConfig.chartOrientation).getDrawableElement(),
]; ];
for(const plot of this.chartData.plots) { for (const plot of this.chartData.plots) {
switch(plot.type) { switch (plot.type) {
case 'line': { case 'line':
const linePlot = new LinePlot(plot, this.xAxis, this.yAxis, this.chartConfig.chartOrientation); {
drawableElem.push(...linePlot.getDrawableElement()) const linePlot = new LinePlot(
} plot,
break; this.xAxis,
case 'bar': { this.yAxis,
const barPlot = new BarPlot(plot, this.boundingRect, this.xAxis, this.yAxis, this.chartConfig.chartOrientation) this.chartConfig.chartOrientation
drawableElem.push(...barPlot.getDrawableElement()); );
} drawableElem.push(...linePlot.getDrawableElement());
break; }
break;
case 'bar':
{
const barPlot = new BarPlot(
plot,
this.boundingRect,
this.xAxis,
this.yAxis,
this.chartConfig.chartOrientation
);
drawableElem.push(...barPlot.getDrawableElement());
}
break;
} }
} }
return drawableElem; return drawableElem;
} }
} }
export function getPlotComponent( export function getPlotComponent(chartConfig: XYChartConfig, chartData: XYChartData): IPlot {
chartConfig: XYChartConfig,
chartData: XYChartData,
): IPlot {
return new Plot(chartConfig, chartData); return new Plot(chartConfig, chartData);
} }

View File

@ -1,14 +1,10 @@
// @ts-ignore: TODO Fix ts errors // @ts-ignore: TODO Fix ts errors
import { XYChartConfig } from '../../../config.type.js'; import { XYChartConfig } from '../../../config.type.js';
import { log } from '../../../logger.js'; import { log } from '../../../logger.js';
import { import { DrawableElem, XYChartData } from './Interfaces.js';
DrawableElem,
XYChartData,
} from './Interfaces.js';
import { Orchestrator } from './Orchestrator.js'; import { Orchestrator } from './Orchestrator.js';
export class XYChartBuilder { export class XYChartBuilder {
static build(config: XYChartConfig, chartData: XYChartData): DrawableElem[] { static build(config: XYChartConfig, chartData: XYChartData): DrawableElem[] {
log.trace(`Build start with Config: ${JSON.stringify(config, null, 2)}`); log.trace(`Build start with Config: ${JSON.stringify(config, null, 2)}`);
log.trace(`Build start with ChartData: ${JSON.stringify(chartData, null, 2)}`); log.trace(`Build start with ChartData: ${JSON.stringify(chartData, null, 2)}`);

View File

@ -111,20 +111,19 @@ describe('Testing xychart jison file', () => {
str = 'xychart-beta \nx-axis xAxisName [ "cat1" , cat2 ] \n '; str = 'xychart-beta \nx-axis xAxisName [ "cat1" , cat2 ] \n ';
expect(parserFnConstructor(str)).not.toThrow(); expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('xAxisName'); expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('xAxisName');
expect(mockDB.setXAxisBand).toHaveBeenCalledWith(["cat1", "cat2"]); expect(mockDB.setXAxisBand).toHaveBeenCalledWith(['cat1', 'cat2']);
clearMocks(); clearMocks();
str = `xychart-beta \n x-axis "this is x axis" [category1, "category 2", category3]\n`;
str = `xychart-beta \n x-axis "this is x axis" [category1, "category 2", category3]\n`
expect(parserFnConstructor(str)).not.toThrow(); expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('this is x axis'); expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('this is x axis');
expect(mockDB.setXAxisBand).toHaveBeenCalledWith(["category1", "category 2", "category3"]); expect(mockDB.setXAxisBand).toHaveBeenCalledWith(['category1', 'category 2', 'category3']);
clearMocks(); clearMocks();
str = 'xychart-beta \nx-axis xAxisName [ "cat1 with space" , cat2 , cat3] \n '; str = 'xychart-beta \nx-axis xAxisName [ "cat1 with space" , cat2 , cat3] \n ';
expect(parserFnConstructor(str)).not.toThrow(); expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('xAxisName'); expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('xAxisName');
expect(mockDB.setXAxisBand).toHaveBeenCalledWith(["cat1 with space", "cat2", "cat3"]); expect(mockDB.setXAxisBand).toHaveBeenCalledWith(['cat1 with space', 'cat2', 'cat3']);
clearMocks(); clearMocks();
str = 'xychart-beta \nx-axis xAxisName [ "cat1 with space" , cat2 asdf , cat3] \n '; str = 'xychart-beta \nx-axis xAxisName [ "cat1 with space" , cat2 asdf , cat3] \n ';
@ -176,8 +175,7 @@ describe('Testing xychart jison file', () => {
expect(mockDB.setLineData).toHaveBeenCalledWith('lineTitle with space', [23, -45, 56.6]); expect(mockDB.setLineData).toHaveBeenCalledWith('lineTitle with space', [23, -45, 56.6]);
// set line data without title // set line data without title
str = str = 'xychart-beta\nx-axis xAxisName\ny-axis yAxisName\n line [ +23 , -45 , 56.6 ] ';
'xychart-beta\nx-axis xAxisName\ny-axis yAxisName\n line [ +23 , -45 , 56.6 ] ';
expect(parserFnConstructor(str)).not.toThrow(); expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith('yAxisName'); expect(mockDB.setYAxisTitle).toHaveBeenCalledWith('yAxisName');
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('xAxisName'); expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('xAxisName');
@ -206,8 +204,7 @@ describe('Testing xychart jison file', () => {
clearMocks(); clearMocks();
// set bar data without title // set bar data without title
str = str = 'xychart-beta\nx-axis xAxisName\ny-axis yAxisName\n bar [ +23 , -45 , 56.6 ] ';
'xychart-beta\nx-axis xAxisName\ny-axis yAxisName\n bar [ +23 , -45 , 56.6 ] ';
expect(parserFnConstructor(str)).not.toThrow(); expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith('yAxisName'); expect(mockDB.setYAxisTitle).toHaveBeenCalledWith('yAxisName');
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('xAxisName'); expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('xAxisName');
@ -243,7 +240,7 @@ describe('Testing xychart jison file', () => {
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith('yaxisText'); expect(mockDB.setYAxisTitle).toHaveBeenCalledWith('yaxisText');
expect(mockDB.setYAxisRangeData).toHaveBeenCalledWith(10, 150); expect(mockDB.setYAxisRangeData).toHaveBeenCalledWith(10, 150);
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('this is x axis'); expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('this is x axis');
expect(mockDB.setXAxisBand).toHaveBeenCalledWith(["category1", "category 2", "category3"]); expect(mockDB.setXAxisBand).toHaveBeenCalledWith(['category1', 'category 2', 'category3']);
expect(mockDB.setBarData).toHaveBeenCalledWith('barTitle1', [23, 45, 56.6]); expect(mockDB.setBarData).toHaveBeenCalledWith('barTitle1', [23, 45, 56.6]);
expect(mockDB.setBarData).toHaveBeenCalledWith('barTitle2', [13, 42, 56.89]); expect(mockDB.setBarData).toHaveBeenCalledWith('barTitle2', [13, 42, 56.89]);
expect(mockDB.setLineData).toHaveBeenCalledWith('lineTitle1', [11, 45.5, 67, 23]); expect(mockDB.setLineData).toHaveBeenCalledWith('lineTitle1', [11, 45.5, 67, 23]);

View File

@ -12,11 +12,7 @@ import {
clear as commonClear, clear as commonClear,
} from '../../commonDb.js'; } from '../../commonDb.js';
import { XYChartBuilder } from './chartBuilder/index.js'; import { XYChartBuilder } from './chartBuilder/index.js';
import { import { DrawableElem, XYChartData, isBandAxisData } from './chartBuilder/Interfaces.js';
DrawableElem,
XYChartData,
isBandAxisData,
} from './chartBuilder/Interfaces.js';
import { XYChartConfig } from '../../config.type.js'; import { XYChartConfig } from '../../config.type.js';
const config = configApi.getConfig(); const config = configApi.getConfig();