-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup scales export for better import strategy (#5953)
Scales now export their class and associated defaults (`_defaults`), the registration being done globally in `src/chart.js`.
- Loading branch information
1 parent
26b7375
commit 87e44fa
Showing
10 changed files
with
1,524 additions
and
1,475 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,27 @@ | ||
'use strict'; | ||
|
||
var bar = require('./controller.bar'); | ||
var bubble = require('./controller.bubble'); | ||
var doughnut = require('./controller.doughnut'); | ||
var horizontalBar = require('./controller.horizontalBar'); | ||
var line = require('./controller.line'); | ||
var polarArea = require('./controller.polarArea'); | ||
var pie = require('./controller.pie'); | ||
var radar = require('./controller.radar'); | ||
var scatter = require('./controller.scatter'); | ||
|
||
// NOTE export a map in which the key represents the controller type, not | ||
// the class, and so must be CamelCase in order to be correctly retrieved | ||
// by the controller in core.controller.js (`controllers[meta.type]`). | ||
|
||
/* eslint-disable global-require */ | ||
module.exports = { | ||
bar: require('./controller.bar'), | ||
bubble: require('./controller.bubble'), | ||
doughnut: require('./controller.doughnut'), | ||
horizontalBar: require('./controller.horizontalBar'), | ||
line: require('./controller.line'), | ||
polarArea: require('./controller.polarArea'), | ||
pie: require('./controller.pie'), | ||
radar: require('./controller.radar'), | ||
scatter: require('./controller.scatter') | ||
bar: bar, | ||
bubble: bubble, | ||
doughnut: doughnut, | ||
horizontalBar: horizontalBar, | ||
line: line, | ||
polarArea: polarArea, | ||
pie: pie, | ||
radar: radar, | ||
scatter: scatter | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
var category = require('./scale.category'); | ||
var linear = require('./scale.linear'); | ||
var logarithmic = require('./scale.logarithmic'); | ||
var radialLinear = require('./scale.radialLinear'); | ||
var time = require('./scale.time'); | ||
|
||
module.exports = { | ||
category: category, | ||
linear: linear, | ||
logarithmic: logarithmic, | ||
radialLinear: radialLinear, | ||
time: time | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,134 @@ | ||
'use strict'; | ||
|
||
var Scale = require('../core/core.scale'); | ||
var scaleService = require('../core/core.scaleService'); | ||
|
||
module.exports = function() { | ||
|
||
// Default config for a category scale | ||
var defaultConfig = { | ||
position: 'bottom' | ||
}; | ||
|
||
var DatasetScale = Scale.extend({ | ||
/** | ||
* Internal function to get the correct labels. If data.xLabels or data.yLabels are defined, use those | ||
* else fall back to data.labels | ||
* @private | ||
*/ | ||
getLabels: function() { | ||
var data = this.chart.data; | ||
return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels; | ||
}, | ||
|
||
determineDataLimits: function() { | ||
var me = this; | ||
var labels = me.getLabels(); | ||
me.minIndex = 0; | ||
me.maxIndex = labels.length - 1; | ||
var findIndex; | ||
|
||
if (me.options.ticks.min !== undefined) { | ||
// user specified min value | ||
findIndex = labels.indexOf(me.options.ticks.min); | ||
me.minIndex = findIndex !== -1 ? findIndex : me.minIndex; | ||
} | ||
|
||
if (me.options.ticks.max !== undefined) { | ||
// user specified max value | ||
findIndex = labels.indexOf(me.options.ticks.max); | ||
me.maxIndex = findIndex !== -1 ? findIndex : me.maxIndex; | ||
} | ||
var defaultConfig = { | ||
position: 'bottom' | ||
}; | ||
|
||
me.min = labels[me.minIndex]; | ||
me.max = labels[me.maxIndex]; | ||
}, | ||
module.exports = Scale.extend({ | ||
/** | ||
* Internal function to get the correct labels. If data.xLabels or data.yLabels are defined, use those | ||
* else fall back to data.labels | ||
* @private | ||
*/ | ||
getLabels: function() { | ||
var data = this.chart.data; | ||
return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels; | ||
}, | ||
|
||
determineDataLimits: function() { | ||
var me = this; | ||
var labels = me.getLabels(); | ||
me.minIndex = 0; | ||
me.maxIndex = labels.length - 1; | ||
var findIndex; | ||
|
||
if (me.options.ticks.min !== undefined) { | ||
// user specified min value | ||
findIndex = labels.indexOf(me.options.ticks.min); | ||
me.minIndex = findIndex !== -1 ? findIndex : me.minIndex; | ||
} | ||
|
||
buildTicks: function() { | ||
var me = this; | ||
var labels = me.getLabels(); | ||
// If we are viewing some subset of labels, slice the original array | ||
me.ticks = (me.minIndex === 0 && me.maxIndex === labels.length - 1) ? labels : labels.slice(me.minIndex, me.maxIndex + 1); | ||
}, | ||
if (me.options.ticks.max !== undefined) { | ||
// user specified max value | ||
findIndex = labels.indexOf(me.options.ticks.max); | ||
me.maxIndex = findIndex !== -1 ? findIndex : me.maxIndex; | ||
} | ||
|
||
getLabelForIndex: function(index, datasetIndex) { | ||
var me = this; | ||
var data = me.chart.data; | ||
var isHorizontal = me.isHorizontal(); | ||
me.min = labels[me.minIndex]; | ||
me.max = labels[me.maxIndex]; | ||
}, | ||
|
||
if (data.yLabels && !isHorizontal) { | ||
return me.getRightValue(data.datasets[datasetIndex].data[index]); | ||
} | ||
return me.ticks[index - me.minIndex]; | ||
}, | ||
|
||
// Used to get data value locations. Value can either be an index or a numerical value | ||
getPixelForValue: function(value, index) { | ||
var me = this; | ||
var offset = me.options.offset; | ||
// 1 is added because we need the length but we have the indexes | ||
var offsetAmt = Math.max((me.maxIndex + 1 - me.minIndex - (offset ? 0 : 1)), 1); | ||
|
||
// If value is a data object, then index is the index in the data array, | ||
// not the index of the scale. We need to change that. | ||
var valueCategory; | ||
if (value !== undefined && value !== null) { | ||
valueCategory = me.isHorizontal() ? value.x : value.y; | ||
} | ||
if (valueCategory !== undefined || (value !== undefined && isNaN(index))) { | ||
var labels = me.getLabels(); | ||
value = valueCategory || value; | ||
var idx = labels.indexOf(value); | ||
index = idx !== -1 ? idx : index; | ||
} | ||
buildTicks: function() { | ||
var me = this; | ||
var labels = me.getLabels(); | ||
// If we are viewing some subset of labels, slice the original array | ||
me.ticks = (me.minIndex === 0 && me.maxIndex === labels.length - 1) ? labels : labels.slice(me.minIndex, me.maxIndex + 1); | ||
}, | ||
|
||
if (me.isHorizontal()) { | ||
var valueWidth = me.width / offsetAmt; | ||
var widthOffset = (valueWidth * (index - me.minIndex)); | ||
getLabelForIndex: function(index, datasetIndex) { | ||
var me = this; | ||
var data = me.chart.data; | ||
var isHorizontal = me.isHorizontal(); | ||
|
||
if (offset) { | ||
widthOffset += (valueWidth / 2); | ||
} | ||
if (data.yLabels && !isHorizontal) { | ||
return me.getRightValue(data.datasets[datasetIndex].data[index]); | ||
} | ||
return me.ticks[index - me.minIndex]; | ||
}, | ||
|
||
// Used to get data value locations. Value can either be an index or a numerical value | ||
getPixelForValue: function(value, index) { | ||
var me = this; | ||
var offset = me.options.offset; | ||
// 1 is added because we need the length but we have the indexes | ||
var offsetAmt = Math.max((me.maxIndex + 1 - me.minIndex - (offset ? 0 : 1)), 1); | ||
|
||
// If value is a data object, then index is the index in the data array, | ||
// not the index of the scale. We need to change that. | ||
var valueCategory; | ||
if (value !== undefined && value !== null) { | ||
valueCategory = me.isHorizontal() ? value.x : value.y; | ||
} | ||
if (valueCategory !== undefined || (value !== undefined && isNaN(index))) { | ||
var labels = me.getLabels(); | ||
value = valueCategory || value; | ||
var idx = labels.indexOf(value); | ||
index = idx !== -1 ? idx : index; | ||
} | ||
|
||
return me.left + widthOffset; | ||
} | ||
var valueHeight = me.height / offsetAmt; | ||
var heightOffset = (valueHeight * (index - me.minIndex)); | ||
if (me.isHorizontal()) { | ||
var valueWidth = me.width / offsetAmt; | ||
var widthOffset = (valueWidth * (index - me.minIndex)); | ||
|
||
if (offset) { | ||
heightOffset += (valueHeight / 2); | ||
widthOffset += (valueWidth / 2); | ||
} | ||
|
||
return me.top + heightOffset; | ||
}, | ||
getPixelForTick: function(index) { | ||
return this.getPixelForValue(this.ticks[index], index + this.minIndex, null); | ||
}, | ||
getValueForPixel: function(pixel) { | ||
var me = this; | ||
var offset = me.options.offset; | ||
var value; | ||
var offsetAmt = Math.max((me._ticks.length - (offset ? 0 : 1)), 1); | ||
var horz = me.isHorizontal(); | ||
var valueDimension = (horz ? me.width : me.height) / offsetAmt; | ||
|
||
pixel -= horz ? me.left : me.top; | ||
return me.left + widthOffset; | ||
} | ||
var valueHeight = me.height / offsetAmt; | ||
var heightOffset = (valueHeight * (index - me.minIndex)); | ||
|
||
if (offset) { | ||
pixel -= (valueDimension / 2); | ||
} | ||
if (offset) { | ||
heightOffset += (valueHeight / 2); | ||
} | ||
|
||
if (pixel <= 0) { | ||
value = 0; | ||
} else { | ||
value = Math.round(pixel / valueDimension); | ||
} | ||
return me.top + heightOffset; | ||
}, | ||
|
||
getPixelForTick: function(index) { | ||
return this.getPixelForValue(this.ticks[index], index + this.minIndex, null); | ||
}, | ||
|
||
return value + me.minIndex; | ||
}, | ||
getBasePixel: function() { | ||
return this.bottom; | ||
getValueForPixel: function(pixel) { | ||
var me = this; | ||
var offset = me.options.offset; | ||
var value; | ||
var offsetAmt = Math.max((me._ticks.length - (offset ? 0 : 1)), 1); | ||
var horz = me.isHorizontal(); | ||
var valueDimension = (horz ? me.width : me.height) / offsetAmt; | ||
|
||
pixel -= horz ? me.left : me.top; | ||
|
||
if (offset) { | ||
pixel -= (valueDimension / 2); | ||
} | ||
}); | ||
|
||
scaleService.registerScaleType('category', DatasetScale, defaultConfig); | ||
}; | ||
if (pixel <= 0) { | ||
value = 0; | ||
} else { | ||
value = Math.round(pixel / valueDimension); | ||
} | ||
|
||
return value + me.minIndex; | ||
}, | ||
|
||
getBasePixel: function() { | ||
return this.bottom; | ||
} | ||
}); | ||
|
||
// INTERNAL: static default options, registered in src/chart.js | ||
module.exports._defaults = defaultConfig; |
Oops, something went wrong.