New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleanup scales export for better import strategy #5953
Merged
simonbrunel
merged 7 commits into
chartjs:master
from
simonbrunel:chore/importable-scales
Jan 5, 2019
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ced3811
Cleanup scales export for better import strategy
simonbrunel 1a7a36d
Remove the exported function argument
simonbrunel 93850b6
Better scale export (class with static `_defaults`)
simonbrunel 8d5fe00
Remove useless comment
simonbrunel 74e49ef
Fix huge comment indentation
simonbrunel f3e3dcc
Better scales and controllers index.js export
simonbrunel 8243721
Deprecate `Chart.LinearScaleBase`
simonbrunel File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
/* eslint-disable global-require */ | ||
simonbrunel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
module.exports = { | ||
category: require('./scale.category'), | ||
linear: require('./scale.linear'), | ||
logarithmic: require('./scale.logarithmic'), | ||
radialLinear: require('./scale.radialLinear'), | ||
time: require('./scale.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,135 @@ | ||
'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]; | ||
}, | ||
var CategoryScale = 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); | ||
} | ||
|
||
if (pixel <= 0) { | ||
value = 0; | ||
} else { | ||
value = Math.round(pixel / valueDimension); | ||
} | ||
}); | ||
|
||
scaleService.registerScaleType('category', DatasetScale, defaultConfig); | ||
return value + me.minIndex; | ||
}, | ||
|
||
getBasePixel: function() { | ||
return this.bottom; | ||
} | ||
}); | ||
|
||
module.exports = function(scales) { | ||
simonbrunel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
scales.registerScaleType('category', CategoryScale, defaultConfig); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a comment to remove this at v3 since it should be internal only?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done