Skip to content

Commit

Permalink
refactor: add filter after formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rshen91 committed Mar 5, 2020
1 parent f4eafa5 commit f7a1aa0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
70 changes: 34 additions & 36 deletions src/chart_types/xy_chart/utils/axis_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ import {
getAxisTickLabelPadding,
isVerticalGrid,
isHorizontalGrid,
removeDupeTickLabels,
} from './axis_utils';
import { CanvasTextBBoxCalculator } from '../../../utils/bbox/canvas_text_bbox_calculator';
import { SvgTextBBoxCalculator } from '../../../utils/bbox/svg_text_bbox_calculator';
import { niceTimeFormatter } from '../../../utils/data/formatters';
import { mergeYCustomDomainsByGroupId } from '../state/selectors/merge_y_custom_domains';
import { ChartTypes } from '../..';
import { SpecTypes } from '../../../specs/settings';
import { DateTime, Settings } from 'luxon';

describe('Axis computational utils', () => {
const mockedRect = {
Expand Down Expand Up @@ -186,7 +184,7 @@ describe('Axis computational utils', () => {
expect(axisDimensions).toEqual(axis1Dims);

const computeScalelessSpec = () => {
computeAxisTicksDimensions(ungroupedAxisSpec, xDomain, [yDomain], 1, bboxCalculator, 0, axes);
computeAxisTicksDimensions(ungroupedAxisSpec, xDomain, [yDomain], 1, bboxCalculator, 0, axes, undefined, false);
};

const ungroupedAxisSpec = { ...verticalAxisSpec, groupId: 'foo' };
Expand Down Expand Up @@ -525,39 +523,39 @@ describe('Axis computational utils', () => {
];
expect(visibleOverlappingTicksAndLabels).toEqual(expectedVisibleOverlappingTicksAndLabels);
});
test('should remove duplicate tick labels when tick values repeat', () => {
const tickValues = [
1546329600000,
1546329600000,
1546329600000,
1546329600000,
1546416000000,
1546502400000,
1546588800000,
1546675200000,
1546761600000,
1546848000000,
1546934400000,
1547020800000,
];
const tickLabels = [
'2019-01-01 08:00:00',
'2019-01-02 08:00:00',
'2019-01-03 08:00:00',
'2019-01-04 08:00:00',
'2019-01-05 08:00:00',
'2019-01-06 08:00:00',
'2019-01-07 08:00:00',
'2019-01-08 08:00:00',
'2019-01-09 08:00:00',
];
const tickFormat = (d: number) => {
Settings.defaultZoneName = 'utc';
return DateTime.fromMillis(d).toFormat('yyyy-MM-dd HH:mm:ss');
};

expect(removeDupeTickLabels(tickValues, tickFormat)).toEqual(tickLabels);
});
// test('should remove duplicate tick labels when tick values repeat', () => {
// const tickValues = [
// 1546329600000,
// 1546329600000,
// 1546329600000,
// 1546329600000,
// 1546416000000,
// 1546502400000,
// 1546588800000,
// 1546675200000,
// 1546761600000,
// 1546848000000,
// 1546934400000,
// 1547020800000,
// ];
// const tickLabels = [
// '2019-01-01 08:00:00',
// '2019-01-02 08:00:00',
// '2019-01-03 08:00:00',
// '2019-01-04 08:00:00',
// '2019-01-05 08:00:00',
// '2019-01-06 08:00:00',
// '2019-01-07 08:00:00',
// '2019-01-08 08:00:00',
// '2019-01-09 08:00:00',
// ];
// const tickFormat = (d: number) => {
// Settings.defaultZoneName = 'utc';
// return DateTime.fromMillis(d).toFormat('yyyy-MM-dd HH:mm:ss');
// };

// expect(removeDupeTickLabels(tickValues, tickFormat)).toEqual(tickLabels);
// });
test('should compute min max range for on 0 deg bottom', () => {
const minMax = getMinMaxRange(Position.Bottom, 0, {
width: 100,
Expand Down
23 changes: 10 additions & 13 deletions src/chart_types/xy_chart/utils/axis_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export function computeAxisTicksDimensions(
axisConfig,
tickLabelPadding,
axisSpec.tickLabelRotation,
axisSpec.duplicateTicks,
{
timeZone: xDomain.timeZone,
},
Expand Down Expand Up @@ -138,6 +139,7 @@ export function getScaleForAxisSpec(
range,
ticks: axisSpec.ticks,
integersOnly: axisSpec.integersOnly,
duplicateTicks: axisSpec.duplicateTicks,
});
if (yScales.has(axisSpec.groupId)) {
return yScales.get(axisSpec.groupId)!;
Expand All @@ -152,6 +154,7 @@ export function getScaleForAxisSpec(
enableHistogramMode,
ticks: axisSpec.ticks,
integersOnly: axisSpec.integersOnly,
duplicateTicks: axisSpec.duplicateTicks,
});
}
}
Expand Down Expand Up @@ -213,10 +216,16 @@ export function computeTickDimensions(
axisConfig: AxisConfig,
tickLabelPadding: number,
tickLabelRotation = 0,
duplicateTicks: boolean = false,
tickFormatOptions?: TickFormatterOptions,
) {
const tickValues = scale.ticks();
const tickLabels = removeDupeTickLabels(tickValues, tickFormat, tickFormatOptions);
const duplicateTickLabels = tickValues.map((d) => {
return tickFormat(d, tickFormatOptions);
});
const tickLabels = duplicateTicks
? duplicateTickLabels
: duplicateTickLabels.filter((value, index) => duplicateTickLabels.indexOf(value) === index);
const {
tickLabelStyle: { fontFamily, fontSize },
} = axisConfig;
Expand All @@ -240,18 +249,6 @@ export function computeTickDimensions(
};
}

export function removeDupeTickLabels(
tickValues: Array<number | string>,
tickFormat: TickFormatter,
tickFormatOptions?: TickFormatterOptions,
) {
return tickValues
.filter((value: number | string, index: number) => tickValues.indexOf(value) === index)
.map((d) => {
return tickFormat(d, tickFormatOptions);
});
}

/**
* Gets the computed x/y coordinates & alignment properties for an axis tick label.
* @param isVerticalAxis if the axis is vertical (in contrast to horizontal)
Expand Down
2 changes: 2 additions & 0 deletions src/chart_types/xy_chart/utils/scales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ interface XScaleOptions {
enableHistogramMode?: boolean;
ticks?: number;
integersOnly?: boolean;
duplicateTicks?: boolean;
}

/**
Expand Down Expand Up @@ -122,6 +123,7 @@ interface YScaleOptions {
range: [number, number];
ticks?: number;
integersOnly?: boolean;
duplicateTicks?: boolean;
}
/**
* Compute the y scales, one per groupId for the y axis.
Expand Down
3 changes: 3 additions & 0 deletions src/chart_types/xy_chart/utils/specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,14 @@ export interface AxisSpec extends Spec {
style?: AxisStyle;
/** Show only integar values **/
integersOnly?: boolean;
/** Remove duplicate ticks, default is false*/
duplicateTicks?: boolean;
}

export type TickFormatterOptions = {
timeZone?: string;
};

export type TickFormatter = (value: any, options?: TickFormatterOptions) => string;

export interface AxisStyle {
Expand Down

0 comments on commit f7a1aa0

Please sign in to comment.