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: add point shapes #988

Merged
merged 17 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
refactor: cleanup shapes code
  • Loading branch information
markov00 committed Jan 27, 2021
commit ce1239de28837bd997cc5f2f1c75dbcbe2b29bb7
17 changes: 4 additions & 13 deletions src/chart_types/xy_chart/renderer/canvas/points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,9 @@ import { PointGeometry } from '../../../../utils/geometry';
import { PointStyle, GeometryStateStyle, PointShape } from '../../../../utils/themes/theme';
import { RgbObject } from '../../../partition_chart/layout/utils/color_library_wrappers';
import { renderCircle } from './primitives/arc';
import { renderCross, renderSquare, renderTriangle } from './primitives/shapes';
import { renderShape } from './primitives/shapes';
import { withPanelTransform } from './utils/panel_transform';

const shapeRenderers = {
[PointShape.Circle]: renderCircle,
[PointShape.AngledCross]: renderCross(45),
[PointShape.Cross]: renderCross(0),
[PointShape.Diamond]: renderSquare(45),
[PointShape.Square]: renderSquare(0),
[PointShape.Triangle]: renderTriangle(0),
};

/**
* Renders points from single series
*
Expand All @@ -54,16 +45,16 @@ export function renderPoints(ctx: CanvasRenderingContext2D, points: PointGeometr
color: applyOpacity(style.stroke.color, opacity),
};

const circle: Circle = {
const coordinates: Circle = {
x: x + transform.x,
y: y + transform.y,
radius,
};

return [circle, fill, stroke, style.shape];
return [coordinates, fill, stroke, style.shape];
})
.sort(([{ radius: a }], [{ radius: b }]) => b - a)
.forEach(([circle, fill, stroke, shape]) => shapeRenderers[shape](ctx, circle, fill, stroke));
.forEach(([coordinates, fill, stroke, shape]) => renderShape(ctx, shape, coordinates, fill, stroke));
}

/**
Expand Down
72 changes: 21 additions & 51 deletions src/chart_types/xy_chart/renderer/canvas/primitives/shapes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,29 @@
*/
import { Circle, Fill, Stroke } from '../../../../../geoms/types';
import { withContext } from '../../../../../renderers/canvas';
import { cross, square, triangle } from '../../shapes_paths';
import { PointShape } from '../../../../../utils/themes/theme';
import { ShapeRendererFn } from '../../shapes_paths';
import { fillAndStroke } from './utils';

/** @internal */
export function renderCross(rotation = 45) {
return (ctx: CanvasRenderingContext2D, shape: Circle, fill?: Fill, stroke?: Stroke) => {
if (!stroke) {
return;
}
withContext(ctx, (ctx) => {
const { x, y, radius } = shape;
ctx.translate(x, y);
ctx.rotate((rotation * Math.PI) / 180);
ctx.beginPath();
const path = new Path2D(cross(radius));
fillAndStroke(ctx, undefined, stroke, path);
});
};
}

/** @internal */
export function renderSquare(rotation = 0) {
return (ctx: CanvasRenderingContext2D, shape: Circle, fill?: Fill, stroke?: Stroke) => {
if (!stroke || !fill) {
return;
}
withContext(ctx, (ctx) => {
const { x, y, radius } = shape;

ctx.translate(x, y);
ctx.rotate((rotation * Math.PI) / 180);
ctx.beginPath();
const path = new Path2D(square(radius));
fillAndStroke(ctx, fill, stroke, path);
});
};
}

/** @internal */
export function renderTriangle(rotation = 0) {
return (ctx: CanvasRenderingContext2D, shape: Circle, fill?: Fill, stroke?: Stroke) => {
if (!stroke || !fill) {
return;
}

withContext(ctx, (ctx) => {
const { x, y, radius } = shape;
export function renderShape(
ctx: CanvasRenderingContext2D,
shape: PointShape,
coordinates: Circle,
fill?: Fill,
stroke?: Stroke,
) {
if (!stroke || !fill) {
return;
}
withContext(ctx, (ctx) => {
const [pathFn, rotation] = ShapeRendererFn[shape];
const { x, y, radius } = coordinates;
ctx.translate(x, y);
ctx.rotate((rotation * Math.PI) / 180);
ctx.beginPath();

ctx.translate(x, y);
ctx.rotate((rotation * Math.PI) / 180);
ctx.beginPath();
const path = new Path2D(triangle(radius));
fillAndStroke(ctx, fill, stroke, path);
});
};
const path = new Path2D(pathFn(radius));
fillAndStroke(ctx, fill, stroke, path);
});
}
193 changes: 0 additions & 193 deletions src/chart_types/xy_chart/renderer/canvas/styles/point.test.ts

This file was deleted.

67 changes: 0 additions & 67 deletions src/chart_types/xy_chart/renderer/canvas/styles/point.ts

This file was deleted.

Loading