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

Reduce duplication in drawGrid #8647

Merged
merged 1 commit into from Mar 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 38 additions & 36 deletions src/core/core.scale.js
Expand Up @@ -1617,41 +1617,46 @@ export default class Scale extends Element {
const items = me._gridLineItems || (me._gridLineItems = me._computeGridLineItems(chartArea));
let i, ilen;

const drawLine = (p1, p2, style) => {
if (!style.width || !style.color) {
return;
}
ctx.save();
ctx.lineWidth = style.width;
ctx.strokeStyle = style.color;
ctx.setLineDash(style.borderDash || []);
ctx.lineDashOffset = style.borderDashOffset;

ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.stroke();
ctx.restore();
};

if (grid.display) {
for (i = 0, ilen = items.length; i < ilen; ++i) {
const item = items[i];
const {color, tickColor, tickWidth, width} = item;

if (width && color && grid.drawOnChartArea) {
ctx.save();
ctx.lineWidth = width;
ctx.strokeStyle = color;
if (ctx.setLineDash) {
ctx.setLineDash(item.borderDash);
ctx.lineDashOffset = item.borderDashOffset;
}

ctx.beginPath();
ctx.moveTo(item.x1, item.y1);
ctx.lineTo(item.x2, item.y2);
ctx.stroke();
ctx.restore();
if (grid.drawOnChartArea) {
drawLine(
{x: item.x1, y: item.y1},
{x: item.x2, y: item.y2},
item
);
}

if (tickWidth && tickColor && grid.drawTicks) {
ctx.save();
ctx.lineWidth = tickWidth;
ctx.strokeStyle = tickColor;
if (ctx.setLineDash) {
ctx.setLineDash(item.tickBorderDash);
ctx.lineDashOffset = item.tickBorderDashOffset;
}

ctx.beginPath();
ctx.moveTo(item.tx1, item.ty1);
ctx.lineTo(item.tx2, item.ty2);
ctx.stroke();
ctx.restore();
if (grid.drawTicks) {
drawLine(
{x: item.tx1, y: item.ty1},
{x: item.tx2, y: item.ty2},
{
color: item.tickColor,
width: item.tickWidth,
borderDash: item.tickBorderDash,
borderDashOffset: item.tickBorderDashOffset
}
);
}
}
}
Expand All @@ -1672,13 +1677,10 @@ export default class Scale extends Element {
y2 = _alignPixel(chart, me.bottom, lastLineWidth) + lastLineWidth / 2;
x1 = x2 = borderValue;
}

ctx.lineWidth = axisWidth;
ctx.strokeStyle = edgeOpts.borderColor;
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
drawLine(
{x: x1, y: y1},
{x: x2, y: y2},
{width: axisWidth, color: edgeOpts.borderColor});
}
}

Expand Down