Skip to content

Commit 60799aa

Browse files
authored
Cleanup scales export for better import strategy (chartjs#5953)
Scales now export their class and associated defaults (`_defaults`), the registration being done globally in `src/chart.js`.
1 parent aef2749 commit 60799aa

File tree

10 files changed

+1524
-1475
lines changed

10 files changed

+1524
-1475
lines changed

src/chart.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ Chart.Tooltip = require('./core/core.tooltip');
2626

2727
require('./core/core.controller')(Chart);
2828

29-
require('./scales/scale.linearbase')(Chart);
30-
require('./scales/scale.category')(Chart);
31-
require('./scales/scale.linear')(Chart);
32-
require('./scales/scale.logarithmic')(Chart);
33-
require('./scales/scale.radialLinear')(Chart);
34-
require('./scales/scale.time')(Chart);
29+
// Register built-in scales
30+
var scales = require('./scales');
31+
Chart.helpers.each(scales, function(scale, type) {
32+
Chart.scaleService.registerScaleType(type, scale, scale._defaults);
33+
});
3534

3635
// Loading built-in plugins
3736
var plugins = require('./plugins');
@@ -105,6 +104,15 @@ Chart.canvasHelpers = Chart.helpers.canvas;
105104
*/
106105
Chart.layoutService = Chart.layouts;
107106

107+
/**
108+
* Provided for backward compatibility, not available anymore.
109+
* @namespace Chart.LinearScaleBase
110+
* @deprecated since version 2.8
111+
* @todo remove at version 3
112+
* @private
113+
*/
114+
Chart.LinearScaleBase = require('./scales/scale.linearbase');
115+
108116
/**
109117
* Provided for backward compatibility, instead we should create a new Chart
110118
* by setting the type in the config (`new Chart(id, {type: '{chart-type}'}`).

src/controllers/index.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
'use strict';
22

3+
var bar = require('./controller.bar');
4+
var bubble = require('./controller.bubble');
5+
var doughnut = require('./controller.doughnut');
6+
var horizontalBar = require('./controller.horizontalBar');
7+
var line = require('./controller.line');
8+
var polarArea = require('./controller.polarArea');
9+
var pie = require('./controller.pie');
10+
var radar = require('./controller.radar');
11+
var scatter = require('./controller.scatter');
12+
313
// NOTE export a map in which the key represents the controller type, not
414
// the class, and so must be CamelCase in order to be correctly retrieved
515
// by the controller in core.controller.js (`controllers[meta.type]`).
616

7-
/* eslint-disable global-require */
817
module.exports = {
9-
bar: require('./controller.bar'),
10-
bubble: require('./controller.bubble'),
11-
doughnut: require('./controller.doughnut'),
12-
horizontalBar: require('./controller.horizontalBar'),
13-
line: require('./controller.line'),
14-
polarArea: require('./controller.polarArea'),
15-
pie: require('./controller.pie'),
16-
radar: require('./controller.radar'),
17-
scatter: require('./controller.scatter')
18+
bar: bar,
19+
bubble: bubble,
20+
doughnut: doughnut,
21+
horizontalBar: horizontalBar,
22+
line: line,
23+
polarArea: polarArea,
24+
pie: pie,
25+
radar: radar,
26+
scatter: scatter
1827
};

src/scales/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
var category = require('./scale.category');
4+
var linear = require('./scale.linear');
5+
var logarithmic = require('./scale.logarithmic');
6+
var radialLinear = require('./scale.radialLinear');
7+
var time = require('./scale.time');
8+
9+
module.exports = {
10+
category: category,
11+
linear: linear,
12+
logarithmic: logarithmic,
13+
radialLinear: radialLinear,
14+
time: time
15+
};

src/scales/scale.category.js

Lines changed: 114 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,134 @@
11
'use strict';
22

33
var Scale = require('../core/core.scale');
4-
var scaleService = require('../core/core.scaleService');
5-
6-
module.exports = function() {
7-
8-
// Default config for a category scale
9-
var defaultConfig = {
10-
position: 'bottom'
11-
};
12-
13-
var DatasetScale = Scale.extend({
14-
/**
15-
* Internal function to get the correct labels. If data.xLabels or data.yLabels are defined, use those
16-
* else fall back to data.labels
17-
* @private
18-
*/
19-
getLabels: function() {
20-
var data = this.chart.data;
21-
return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels;
22-
},
23-
24-
determineDataLimits: function() {
25-
var me = this;
26-
var labels = me.getLabels();
27-
me.minIndex = 0;
28-
me.maxIndex = labels.length - 1;
29-
var findIndex;
30-
31-
if (me.options.ticks.min !== undefined) {
32-
// user specified min value
33-
findIndex = labels.indexOf(me.options.ticks.min);
34-
me.minIndex = findIndex !== -1 ? findIndex : me.minIndex;
35-
}
364

37-
if (me.options.ticks.max !== undefined) {
38-
// user specified max value
39-
findIndex = labels.indexOf(me.options.ticks.max);
40-
me.maxIndex = findIndex !== -1 ? findIndex : me.maxIndex;
41-
}
5+
var defaultConfig = {
6+
position: 'bottom'
7+
};
428

43-
me.min = labels[me.minIndex];
44-
me.max = labels[me.maxIndex];
45-
},
9+
module.exports = Scale.extend({
10+
/**
11+
* Internal function to get the correct labels. If data.xLabels or data.yLabels are defined, use those
12+
* else fall back to data.labels
13+
* @private
14+
*/
15+
getLabels: function() {
16+
var data = this.chart.data;
17+
return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels;
18+
},
19+
20+
determineDataLimits: function() {
21+
var me = this;
22+
var labels = me.getLabels();
23+
me.minIndex = 0;
24+
me.maxIndex = labels.length - 1;
25+
var findIndex;
26+
27+
if (me.options.ticks.min !== undefined) {
28+
// user specified min value
29+
findIndex = labels.indexOf(me.options.ticks.min);
30+
me.minIndex = findIndex !== -1 ? findIndex : me.minIndex;
31+
}
4632

47-
buildTicks: function() {
48-
var me = this;
49-
var labels = me.getLabels();
50-
// If we are viewing some subset of labels, slice the original array
51-
me.ticks = (me.minIndex === 0 && me.maxIndex === labels.length - 1) ? labels : labels.slice(me.minIndex, me.maxIndex + 1);
52-
},
33+
if (me.options.ticks.max !== undefined) {
34+
// user specified max value
35+
findIndex = labels.indexOf(me.options.ticks.max);
36+
me.maxIndex = findIndex !== -1 ? findIndex : me.maxIndex;
37+
}
5338

54-
getLabelForIndex: function(index, datasetIndex) {
55-
var me = this;
56-
var data = me.chart.data;
57-
var isHorizontal = me.isHorizontal();
39+
me.min = labels[me.minIndex];
40+
me.max = labels[me.maxIndex];
41+
},
5842

59-
if (data.yLabels && !isHorizontal) {
60-
return me.getRightValue(data.datasets[datasetIndex].data[index]);
61-
}
62-
return me.ticks[index - me.minIndex];
63-
},
64-
65-
// Used to get data value locations. Value can either be an index or a numerical value
66-
getPixelForValue: function(value, index) {
67-
var me = this;
68-
var offset = me.options.offset;
69-
// 1 is added because we need the length but we have the indexes
70-
var offsetAmt = Math.max((me.maxIndex + 1 - me.minIndex - (offset ? 0 : 1)), 1);
71-
72-
// If value is a data object, then index is the index in the data array,
73-
// not the index of the scale. We need to change that.
74-
var valueCategory;
75-
if (value !== undefined && value !== null) {
76-
valueCategory = me.isHorizontal() ? value.x : value.y;
77-
}
78-
if (valueCategory !== undefined || (value !== undefined && isNaN(index))) {
79-
var labels = me.getLabels();
80-
value = valueCategory || value;
81-
var idx = labels.indexOf(value);
82-
index = idx !== -1 ? idx : index;
83-
}
43+
buildTicks: function() {
44+
var me = this;
45+
var labels = me.getLabels();
46+
// If we are viewing some subset of labels, slice the original array
47+
me.ticks = (me.minIndex === 0 && me.maxIndex === labels.length - 1) ? labels : labels.slice(me.minIndex, me.maxIndex + 1);
48+
},
8449

85-
if (me.isHorizontal()) {
86-
var valueWidth = me.width / offsetAmt;
87-
var widthOffset = (valueWidth * (index - me.minIndex));
50+
getLabelForIndex: function(index, datasetIndex) {
51+
var me = this;
52+
var data = me.chart.data;
53+
var isHorizontal = me.isHorizontal();
8854

89-
if (offset) {
90-
widthOffset += (valueWidth / 2);
91-
}
55+
if (data.yLabels && !isHorizontal) {
56+
return me.getRightValue(data.datasets[datasetIndex].data[index]);
57+
}
58+
return me.ticks[index - me.minIndex];
59+
},
60+
61+
// Used to get data value locations. Value can either be an index or a numerical value
62+
getPixelForValue: function(value, index) {
63+
var me = this;
64+
var offset = me.options.offset;
65+
// 1 is added because we need the length but we have the indexes
66+
var offsetAmt = Math.max((me.maxIndex + 1 - me.minIndex - (offset ? 0 : 1)), 1);
67+
68+
// If value is a data object, then index is the index in the data array,
69+
// not the index of the scale. We need to change that.
70+
var valueCategory;
71+
if (value !== undefined && value !== null) {
72+
valueCategory = me.isHorizontal() ? value.x : value.y;
73+
}
74+
if (valueCategory !== undefined || (value !== undefined && isNaN(index))) {
75+
var labels = me.getLabels();
76+
value = valueCategory || value;
77+
var idx = labels.indexOf(value);
78+
index = idx !== -1 ? idx : index;
79+
}
9280

93-
return me.left + widthOffset;
94-
}
95-
var valueHeight = me.height / offsetAmt;
96-
var heightOffset = (valueHeight * (index - me.minIndex));
81+
if (me.isHorizontal()) {
82+
var valueWidth = me.width / offsetAmt;
83+
var widthOffset = (valueWidth * (index - me.minIndex));
9784

9885
if (offset) {
99-
heightOffset += (valueHeight / 2);
86+
widthOffset += (valueWidth / 2);
10087
}
10188

102-
return me.top + heightOffset;
103-
},
104-
getPixelForTick: function(index) {
105-
return this.getPixelForValue(this.ticks[index], index + this.minIndex, null);
106-
},
107-
getValueForPixel: function(pixel) {
108-
var me = this;
109-
var offset = me.options.offset;
110-
var value;
111-
var offsetAmt = Math.max((me._ticks.length - (offset ? 0 : 1)), 1);
112-
var horz = me.isHorizontal();
113-
var valueDimension = (horz ? me.width : me.height) / offsetAmt;
114-
115-
pixel -= horz ? me.left : me.top;
89+
return me.left + widthOffset;
90+
}
91+
var valueHeight = me.height / offsetAmt;
92+
var heightOffset = (valueHeight * (index - me.minIndex));
11693

117-
if (offset) {
118-
pixel -= (valueDimension / 2);
119-
}
94+
if (offset) {
95+
heightOffset += (valueHeight / 2);
96+
}
12097

121-
if (pixel <= 0) {
122-
value = 0;
123-
} else {
124-
value = Math.round(pixel / valueDimension);
125-
}
98+
return me.top + heightOffset;
99+
},
100+
101+
getPixelForTick: function(index) {
102+
return this.getPixelForValue(this.ticks[index], index + this.minIndex, null);
103+
},
126104

127-
return value + me.minIndex;
128-
},
129-
getBasePixel: function() {
130-
return this.bottom;
105+
getValueForPixel: function(pixel) {
106+
var me = this;
107+
var offset = me.options.offset;
108+
var value;
109+
var offsetAmt = Math.max((me._ticks.length - (offset ? 0 : 1)), 1);
110+
var horz = me.isHorizontal();
111+
var valueDimension = (horz ? me.width : me.height) / offsetAmt;
112+
113+
pixel -= horz ? me.left : me.top;
114+
115+
if (offset) {
116+
pixel -= (valueDimension / 2);
131117
}
132-
});
133118

134-
scaleService.registerScaleType('category', DatasetScale, defaultConfig);
135-
};
119+
if (pixel <= 0) {
120+
value = 0;
121+
} else {
122+
value = Math.round(pixel / valueDimension);
123+
}
124+
125+
return value + me.minIndex;
126+
},
127+
128+
getBasePixel: function() {
129+
return this.bottom;
130+
}
131+
});
132+
133+
// INTERNAL: static default options, registered in src/chart.js
134+
module.exports._defaults = defaultConfig;

0 commit comments

Comments
 (0)