Skip to content

Commit 5bd9cb6

Browse files
author
andrew.hurskiy
committed
Added majorTick configuration via options.
1 parent 7242f96 commit 5bd9cb6

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

docs/axes/styling.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,14 @@ The tick configuration is nested under the scale configuration in the `ticks` ke
3535
| `fontSize` | `Number` | `12` | Font size for the tick labels.
3636
| `fontStyle` | `String` | `'normal'` | Font style for the tick labels, follows CSS font-style options (i.e. normal, italic, oblique, initial, inherit).
3737
| `reverse` | `Boolean` | `false` | Reverses order of tick labels.
38+
39+
## Major Tick Configuration
40+
The majorTick configuration is nested under the scale configuration in the `majorTicks` key. It defines options for the major tick marks that are generated by the axis. Omitted options are onherited from `ticks` configuration.
41+
42+
| Name | Type | Default | Description
43+
| -----| ---- | --------| -----------
44+
| `callback` | `Function` | | Returns the string representation of the tick value as it should be displayed on the chart. See [callback](../axes/labelling.md#creating-custom-tick-formats).
45+
| `fontColor` | Color | `'#666'` | Font color for tick labels.
46+
| `fontFamily` | `String` | `"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"` | Font family for the tick labels, follows CSS font-family options.
47+
| `fontSize` | `Number` | `12` | Font size for the tick labels.
48+
| `fontStyle` | `String` | `'normal'` | Font style for the tick labels, follows CSS font-style options (i.e. normal, italic, oblique, initial, inherit).

src/core/core.scale.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ module.exports = function(Chart) {
487487
var context = me.ctx;
488488
var globalDefaults = Chart.defaults.global;
489489
var optionTicks = options.ticks;
490+
var optionMajorTicks = options.majorTicks ? options.majorTicks : optionTicks;
490491
var gridLines = options.gridLines;
491492
var scaleLabel = options.scaleLabel;
492493

@@ -503,7 +504,8 @@ module.exports = function(Chart) {
503504

504505
var tickFontColor = helpers.getValueOrDefault(optionTicks.fontColor, globalDefaults.defaultFontColor);
505506
var tickFont = parseFontOptions(optionTicks);
506-
var majorTickFont = helpers.fontString(tickFont.size, 'bold', tickFont.family);
507+
var majorTickFontColor = helpers.getValueOrDefault(optionMajorTicks.fontColor, globalDefaults.defaultFontColor);
508+
var majorTickFont = parseFontOptions(optionMajorTicks);
507509

508510
var tl = gridLines.drawTicks ? gridLines.tickMarkLength : 0;
509511

@@ -514,9 +516,6 @@ module.exports = function(Chart) {
514516
var cosRotation = Math.cos(labelRotationRadians);
515517
var longestRotatedLabel = me.longestLabelWidth * cosRotation;
516518

517-
// Make sure we draw text in the correct color and font
518-
context.fillStyle = tickFontColor;
519-
520519
var itemsToDraw = [];
521520

522521
if (isHorizontal) {
@@ -681,10 +680,12 @@ module.exports = function(Chart) {
681680
}
682681

683682
if (optionTicks.display) {
683+
// Make sure we draw text in the correct color and font
684684
context.save();
685685
context.translate(itemToDraw.labelX, itemToDraw.labelY);
686686
context.rotate(itemToDraw.rotation);
687-
context.font = itemToDraw.major ? majorTickFont : tickFont.font;
687+
context.font = itemToDraw.major ? majorTickFont.font : tickFont.font;
688+
context.fillStyle = itemToDraw.major ? majorTickFontColor : tickFontColor;
688689
context.textBaseline = itemToDraw.textBaseline;
689690
context.textAlign = itemToDraw.textAlign;
690691

src/scales/scale.time.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ module.exports = function(Chart) {
3636
},
3737
ticks: {
3838
autoSkip: false
39+
},
40+
majorTicks: {
41+
beginAtZero: false,
42+
minRotation: 0,
43+
maxRotation: 50,
44+
mirror: false,
45+
padding: 0,
46+
reverse: false,
47+
display: true,
48+
autoSkip: true,
49+
autoSkipPadding: 0,
50+
labelOffset: 0,
51+
callback: Chart.Ticks.formatters.values
3952
}
4053
};
4154

@@ -45,8 +58,17 @@ module.exports = function(Chart) {
4558
throw new Error('Chart.js - Moment.js could not be found! You must include it before Chart.js to use the time scale. Download at https://momentjs.com');
4659
}
4760

61+
this.mergeTicksOptions();
62+
4863
Chart.Scale.prototype.initialize.call(this);
4964
},
65+
mergeTicksOptions: function() {
66+
for (var key in this.options.ticks) {
67+
if (typeof this.options.majorTicks[key] === 'undefined') {
68+
this.options.majorTicks[key] = this.options.ticks[key];
69+
}
70+
}
71+
},
5072
determineDataLimits: function() {
5173
var me = this;
5274
var timeOpts = me.options.time;
@@ -186,16 +208,18 @@ module.exports = function(Chart) {
186208
var tickClone = tick.clone();
187209
var tickTimestamp = tick.valueOf();
188210
var major = false;
211+
var tickOpts;
189212
if (this.majorUnit && this.majorDisplayFormat && tickTimestamp === tickClone.startOf(this.majorUnit).valueOf()) {
190213
// format as senior unit
191214
formattedTick = tick.format(this.majorDisplayFormat);
215+
tickOpts = this.options.majorTicks;
192216
major = true;
193217
} else {
194218
// format as base unit
195219
formattedTick = tick.format(this.displayFormat);
220+
tickOpts = this.options.ticks;
196221
}
197222

198-
var tickOpts = this.options.ticks;
199223
var callback = helpers.getValueOrDefault(tickOpts.callback, tickOpts.userCallback);
200224

201225
if (callback) {

test/specs/scale.time.tests.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ describe('Time scale tests', function() {
9191
autoSkipPadding: 0,
9292
labelOffset: 0
9393
},
94+
majorTicks: {
95+
beginAtZero: false,
96+
minRotation: 0,
97+
maxRotation: 50,
98+
mirror: false,
99+
padding: 0,
100+
reverse: false,
101+
display: true,
102+
callback: defaultConfig.majorTicks.callback, // make this nicer, then check explicitly below,
103+
autoSkip: true,
104+
autoSkipPadding: 0,
105+
labelOffset: 0
106+
},
94107
time: {
95108
parser: false,
96109
format: false,

0 commit comments

Comments
 (0)