Skip to content

Commit 8b1c940

Browse files
committed
Create gridLines plugin
1 parent b64fd5d commit 8b1c940

File tree

9 files changed

+390
-57
lines changed

9 files changed

+390
-57
lines changed

src/chart.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ require('./charts/Chart.Scatter')(Chart);
5252
var plugins = [];
5353

5454
plugins.push(
55-
require('./plugins/plugin.filler')(Chart),
56-
require('./plugins/plugin.legend')(Chart),
57-
require('./plugins/plugin.title')(Chart)
55+
require('./plugins/plugin.filler')(Chart),
56+
require('./plugins/plugin.gridlines')(Chart),
57+
require('./plugins/plugin.legend')(Chart),
58+
require('./plugins/plugin.title')(Chart)
5859
);
5960

6061
Chart.plugins.register(plugins);

src/core/core.scale.js

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ module.exports = function(Chart) {
672672
},
673673

674674
// Actually draw the scale on the canvas
675-
// @param {rectangle} chartArea : the area of the chart to draw full grid lines on
675+
// @param {rectangle} chartArea : the area of the chart to draw all ticks and labels on
676676
draw: function(chartArea) {
677677
var me = this;
678678
var options = me.options;
@@ -732,7 +732,7 @@ module.exports = function(Chart) {
732732
}
733733

734734
// Common properties
735-
var tx1, ty1, tx2, ty2, x1, y1, x2, y2, labelX, labelY;
735+
var tx1, ty1, tx2, ty2, labelX, labelY;
736736
var textAlign = 'middle';
737737
var textBaseline = 'middle';
738738
var tickPadding = optionTicks.padding;
@@ -760,11 +760,9 @@ module.exports = function(Chart) {
760760

761761
labelX = me.getPixelForTick(index) + optionTicks.labelOffset; // x values for optionTicks (need to consider offsetLabel option)
762762

763-
tx1 = tx2 = x1 = x2 = xLineValue;
763+
tx1 = tx2 = xLineValue;
764764
ty1 = yTickStart;
765765
ty2 = yTickEnd;
766-
y1 = chartArea.top;
767-
y2 = chartArea.bottom;
768766
} else {
769767
var isLeft = options.position === 'left';
770768
var labelXOffset;
@@ -789,26 +787,20 @@ module.exports = function(Chart) {
789787

790788
tx1 = xTickStart;
791789
tx2 = xTickEnd;
792-
x1 = chartArea.left;
793-
x2 = chartArea.right;
794-
ty1 = ty2 = y1 = y2 = yLineValue;
790+
ty1 = ty2 = yLineValue;
795791
}
796792

797793
itemsToDraw.push({
798794
tx1: tx1,
799795
ty1: ty1,
800796
tx2: tx2,
801797
ty2: ty2,
802-
x1: x1,
803-
y1: y1,
804-
x2: x2,
805-
y2: y2,
806798
labelX: labelX,
807799
labelY: labelY,
808-
glWidth: lineWidth,
809-
glColor: lineColor,
810-
glBorderDash: borderDash,
811-
glBorderDashOffset: borderDashOffset,
800+
tmWidth: lineWidth,
801+
tmColor: lineColor,
802+
tmBorderDash: borderDash,
803+
tmBorderDashOffset: borderDashOffset,
812804
rotation: -1 * labelRotationRadians,
813805
label: label,
814806
major: tick.major,
@@ -817,15 +809,30 @@ module.exports = function(Chart) {
817809
});
818810
});
819811

820-
// Draw all of the tick labels, tick marks, and grid lines at the correct places
812+
// When offsetGridLines is enabled, there is one less tick mark than
813+
// there are tick labels, thefore it has to be manually added
814+
if (gridLines.offsetGridLines) {
815+
itemsToDraw.push({
816+
tx1: !isHorizontal ? xTickStart : chartArea.right,
817+
ty1: !isHorizontal ? chartArea.bottom : yTickStart,
818+
tx2: !isHorizontal ? xTickEnd : chartArea.right,
819+
ty2: !isHorizontal ? chartArea.bottom : yTickEnd,
820+
tmWidth: gridLines.lineWidth,
821+
tmColor: gridLines.color,
822+
tmBorderDash: gridLines.borderDash,
823+
tmBorderDashOffset: gridLines.borderDashOffset
824+
});
825+
}
826+
827+
// Draw all of the tick labels and tick marks at the correct places
821828
helpers.each(itemsToDraw, function(itemToDraw) {
822829
if (gridLines.display) {
823830
context.save();
824-
context.lineWidth = itemToDraw.glWidth;
825-
context.strokeStyle = itemToDraw.glColor;
831+
context.lineWidth = itemToDraw.tmWidth;
832+
context.strokeStyle = itemToDraw.tmColor;
826833
if (context.setLineDash) {
827-
context.setLineDash(itemToDraw.glBorderDash);
828-
context.lineDashOffset = itemToDraw.glBorderDashOffset;
834+
context.setLineDash(itemToDraw.tmBorderDash);
835+
context.lineDashOffset = itemToDraw.tmBorderDashOffset;
829836
}
830837

831838
context.beginPath();
@@ -835,16 +842,11 @@ module.exports = function(Chart) {
835842
context.lineTo(itemToDraw.tx2, itemToDraw.ty2);
836843
}
837844

838-
if (gridLines.drawOnChartArea) {
839-
context.moveTo(itemToDraw.x1, itemToDraw.y1);
840-
context.lineTo(itemToDraw.x2, itemToDraw.y2);
841-
}
842-
843845
context.stroke();
844846
context.restore();
845847
}
846848

847-
if (optionTicks.display) {
849+
if (optionTicks.display && itemToDraw.label !== undefined && itemToDraw.label !== '') {
848850
// Make sure we draw text in the correct color and font
849851
context.save();
850852
context.translate(itemToDraw.labelX, itemToDraw.labelY);
@@ -900,32 +902,6 @@ module.exports = function(Chart) {
900902
context.fillText(scaleLabel.labelString, 0, 0);
901903
context.restore();
902904
}
903-
904-
if (gridLines.drawBorder) {
905-
// Draw the line at the edge of the axis
906-
context.lineWidth = helpers.valueAtIndexOrDefault(gridLines.lineWidth, 0);
907-
context.strokeStyle = helpers.valueAtIndexOrDefault(gridLines.color, 0);
908-
var x1 = me.left;
909-
var x2 = me.right;
910-
var y1 = me.top;
911-
var y2 = me.bottom;
912-
913-
var aliasPixel = helpers.aliasPixel(context.lineWidth);
914-
if (isHorizontal) {
915-
y1 = y2 = options.position === 'top' ? me.bottom : me.top;
916-
y1 += aliasPixel;
917-
y2 += aliasPixel;
918-
} else {
919-
x1 = x2 = options.position === 'left' ? me.right : me.left;
920-
x1 += aliasPixel;
921-
x2 += aliasPixel;
922-
}
923-
924-
context.beginPath();
925-
context.moveTo(x1, y1);
926-
context.lineTo(x2, y2);
927-
context.stroke();
928-
}
929905
}
930906
});
931907
};

0 commit comments

Comments
 (0)