|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | 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 |
| - } |
36 | 4 |
|
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 | +}; |
42 | 8 |
|
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 | + } |
46 | 32 |
|
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 | + } |
53 | 38 |
|
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 | + }, |
58 | 42 |
|
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 | + }, |
84 | 49 |
|
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(); |
88 | 54 |
|
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 | + } |
92 | 80 |
|
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)); |
97 | 84 |
|
98 | 85 | if (offset) {
|
99 |
| - heightOffset += (valueHeight / 2); |
| 86 | + widthOffset += (valueWidth / 2); |
100 | 87 | }
|
101 | 88 |
|
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)); |
116 | 93 |
|
117 |
| - if (offset) { |
118 |
| - pixel -= (valueDimension / 2); |
119 |
| - } |
| 94 | + if (offset) { |
| 95 | + heightOffset += (valueHeight / 2); |
| 96 | + } |
120 | 97 |
|
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 | + }, |
126 | 104 |
|
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); |
131 | 117 | }
|
132 |
| - }); |
133 | 118 |
|
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