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

feat(axis): option to hide duplicate axes #370

Merged
merged 7 commits into from
Sep 19, 2019
Merged
Changes from 1 commit
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
61 changes: 30 additions & 31 deletions src/chart_types/xy_chart/store/chart_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,35 @@ export type CursorUpdateListener = (event?: CursorEvent) => void;
export type RenderChangeListener = (isRendered: boolean) => void;
export type BasicListener = () => undefined | void;

export const isDuplicateAxis = (
monfera marked this conversation as resolved.
Show resolved Hide resolved
{ position, title }: AxisSpec,
{ tickLabels }: AxisTicksDimensions,
tickMap: Map<AxisId, AxisTicksDimensions>,
specMap: Map<AxisId, AxisSpec>,
): boolean => {
const firstTickLabel = tickLabels[0];
const lastTickLabel = tickLabels.slice(-1)[0];

let hasDuplicate = false;
tickMap.forEach(({ tickLabels: axisTickLabels }, axisId) => {
if (
!hasDuplicate &&
axisTickLabels &&
tickLabels.length === axisTickLabels.length &&
firstTickLabel === axisTickLabels[0] &&
lastTickLabel === axisTickLabels.slice(-1)[0]
) {
const spec = specMap.get(axisId);

if (spec && spec.position === position && ((!title && !spec.title) || title === spec.title)) {
Copy link
Member

Choose a reason for hiding this comment

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

can we simplify this using spec && spec.position === position && title == spec.title?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

see 2474776

hasDuplicate = true;
}
}
});

return hasDuplicate;
};

export class ChartStore {
constructor(id?: string) {
this.id = id || uuid.v4();
Expand Down Expand Up @@ -845,35 +874,6 @@ export class ChartStore {
this.annotationSpecs.delete(annotationId);
}

isDuplicateAxis(
{ position, title }: AxisSpec,
{ tickLabels }: AxisTicksDimensions,
tickMap: Map<AxisId, AxisTicksDimensions>,
specMap: Map<AxisId, AxisSpec>,
): boolean {
const firstTickLabel = tickLabels[0];
const lastTickLabel = tickLabels.slice(-1)[0];

let hasDuplicate = false;
tickMap.forEach(({ tickLabels: axisTickLabels }, axisId) => {
if (
!hasDuplicate &&
axisTickLabels &&
tickLabels.length === axisTickLabels.length &&
firstTickLabel === axisTickLabels[0] &&
lastTickLabel === axisTickLabels.slice(-1)[0]
) {
const spec = specMap.get(axisId);

if (spec && spec.position === position && ((!title && !spec.title) || title === spec.title)) {
hasDuplicate = true;
}
}
});

return hasDuplicate;
}

computeChart() {
this.chartInitialized.set(false);
// compute only if parent dimensions are computed
Expand Down Expand Up @@ -959,8 +959,7 @@ export class ChartStore {

if (
dimensions &&
(!this.hideDuplicateAxes ||
!this.isDuplicateAxis(axisSpec, dimensions, this.axesTicksDimensions, this.axesSpecs))
(!this.hideDuplicateAxes || !isDuplicateAxis(axisSpec, dimensions, this.axesTicksDimensions, this.axesSpecs))
) {
this.axesTicksDimensions.set(id, dimensions);
}
Expand Down