Skip to content

Commit 99596b0

Browse files
authored
Reduce duplication in drawGrid (#8647)
1 parent a6b3b99 commit 99596b0

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

src/core/core.scale.js

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,41 +1617,46 @@ export default class Scale extends Element {
16171617
const items = me._gridLineItems || (me._gridLineItems = me._computeGridLineItems(chartArea));
16181618
let i, ilen;
16191619

1620+
const drawLine = (p1, p2, style) => {
1621+
if (!style.width || !style.color) {
1622+
return;
1623+
}
1624+
ctx.save();
1625+
ctx.lineWidth = style.width;
1626+
ctx.strokeStyle = style.color;
1627+
ctx.setLineDash(style.borderDash || []);
1628+
ctx.lineDashOffset = style.borderDashOffset;
1629+
1630+
ctx.beginPath();
1631+
ctx.moveTo(p1.x, p1.y);
1632+
ctx.lineTo(p2.x, p2.y);
1633+
ctx.stroke();
1634+
ctx.restore();
1635+
};
1636+
16201637
if (grid.display) {
16211638
for (i = 0, ilen = items.length; i < ilen; ++i) {
16221639
const item = items[i];
1623-
const {color, tickColor, tickWidth, width} = item;
1624-
1625-
if (width && color && grid.drawOnChartArea) {
1626-
ctx.save();
1627-
ctx.lineWidth = width;
1628-
ctx.strokeStyle = color;
1629-
if (ctx.setLineDash) {
1630-
ctx.setLineDash(item.borderDash);
1631-
ctx.lineDashOffset = item.borderDashOffset;
1632-
}
16331640

1634-
ctx.beginPath();
1635-
ctx.moveTo(item.x1, item.y1);
1636-
ctx.lineTo(item.x2, item.y2);
1637-
ctx.stroke();
1638-
ctx.restore();
1641+
if (grid.drawOnChartArea) {
1642+
drawLine(
1643+
{x: item.x1, y: item.y1},
1644+
{x: item.x2, y: item.y2},
1645+
item
1646+
);
16391647
}
16401648

1641-
if (tickWidth && tickColor && grid.drawTicks) {
1642-
ctx.save();
1643-
ctx.lineWidth = tickWidth;
1644-
ctx.strokeStyle = tickColor;
1645-
if (ctx.setLineDash) {
1646-
ctx.setLineDash(item.tickBorderDash);
1647-
ctx.lineDashOffset = item.tickBorderDashOffset;
1648-
}
1649-
1650-
ctx.beginPath();
1651-
ctx.moveTo(item.tx1, item.ty1);
1652-
ctx.lineTo(item.tx2, item.ty2);
1653-
ctx.stroke();
1654-
ctx.restore();
1649+
if (grid.drawTicks) {
1650+
drawLine(
1651+
{x: item.tx1, y: item.ty1},
1652+
{x: item.tx2, y: item.ty2},
1653+
{
1654+
color: item.tickColor,
1655+
width: item.tickWidth,
1656+
borderDash: item.tickBorderDash,
1657+
borderDashOffset: item.tickBorderDashOffset
1658+
}
1659+
);
16551660
}
16561661
}
16571662
}
@@ -1672,13 +1677,10 @@ export default class Scale extends Element {
16721677
y2 = _alignPixel(chart, me.bottom, lastLineWidth) + lastLineWidth / 2;
16731678
x1 = x2 = borderValue;
16741679
}
1675-
1676-
ctx.lineWidth = axisWidth;
1677-
ctx.strokeStyle = edgeOpts.borderColor;
1678-
ctx.beginPath();
1679-
ctx.moveTo(x1, y1);
1680-
ctx.lineTo(x2, y2);
1681-
ctx.stroke();
1680+
drawLine(
1681+
{x: x1, y: y1},
1682+
{x: x2, y: y2},
1683+
{width: axisWidth, color: edgeOpts.borderColor});
16821684
}
16831685
}
16841686

0 commit comments

Comments
 (0)