1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | 1 1 1 1 73 73 73 73 73 73 73 34 23 11 73 73 73 73 73 73 1 77 77 1 76 6 70 2 77 1 73 73 73 73 5 68 2 66 73 1 8 8 8 8 1 78 78 1 77 77 3 74 35 39 32 77 78 2 78 1 1 73 73 1 73 1 73 73 73 42 31 6 6 6 5 25 25 73 1 74 74 1 1 1 23 23 23 23 23 1 75 75 75 2 5 68 | 'use strict'; require('../globals'); var util = require('../util'), setter = util.setter, getter = util.getter, time = require('./time'); var axis = module.exports = {}; axis.def = function(name, encoding, layout, stats, opt) { var isCol = name == COL, isRow = name == ROW, type = isCol ? 'x' : isRow ? 'y' : name; var def = { type: type, scale: name, properties: {}, layer: encoding.encDef(name).axis.layer }; def = axis.orient(def, encoding, name, stats); // Add axis label custom scale (for bin / time) def = axis.labels.scale(def, encoding, name); def = axis.labels.format(def, encoding, name, stats); def = axis.labels.angle(def, encoding, name); // for x-axis, set ticks for Q or rotate scale for ordinal scale if (name == X) { if ((encoding.isDimension(X) || encoding.isType(X, T)) && !('angle' in getter(def, ['properties', 'labels']))) { // TODO(kanitw): Jul 19, 2015 - #506 add condition for rotation def = axis.labels.rotate(def); } else { // Q def.ticks = encoding.encDef(name).axis.ticks; } } // TitleOffset depends on labels rotation def.titleOffset = axis.titleOffset(encoding, layout, name); //def.offset is used in axis.grid if(isRow) def.offset = axis.titleOffset(encoding, layout, Y) + 20; // FIXME(kanitw): Jul 19, 2015 - offset for column when x is put on top def = axis.grid(def, encoding, name, layout); def = axis.title(def, encoding, name, layout, opt); if (isRow || isCol) def = axis.hideTicks(def); return def; }; axis.orient = function(def, encoding, name, stats) { var orient = encoding.encDef(name).axis.orient; if (orient) { def.orient = orient; } else if (name === COL) { def.orient = 'top'; } // x-axis for long y - put on top else if (name === X && encoding.has(Y) && encoding.isOrdinalScale(Y) && encoding.cardinality(Y, stats) > 30) { def.orient = 'top'; } return def; }; axis.grid = function(def, encoding, name, layout) { var cellPadding = layout.cellPadding, isCol = name == COL, isRow = name == ROW; Eif (encoding.axis(name).grid) { def.grid = true; if (isCol) { // set grid property -- put the lines on the right the cell def.properties.grid = { x: { offset: layout.cellWidth * (1+ cellPadding/2.0), band: true, // default value(s) -- vega doesn't do recursive merge scale: 'col' }, y: { value: -layout.cellHeight * (cellPadding/2), }, stroke: { value: encoding.config('cellGridColor') }, opacity: { value: encoding.config('cellGridOpacity') } }; } else if (isRow) { // set grid property -- put the lines on the top def.properties.grid = { y: { offset: -layout.cellHeight * (cellPadding/2), band: true, // default value(s) -- vega doesn't do recursive merge scale: 'row' }, x: { value: def.offset }, x2: { field: {group: 'mark.group.width'}, offset: def.offset + (layout.cellWidth * 0.05), // default value(s) -- vega doesn't do recursive merge mult: 1 }, stroke: { value: encoding.config('cellGridColor') }, opacity: { value: encoding.config('cellGridOpacity') } }; } else { def.properties.grid = { stroke: { value: encoding.config('gridColor') }, opacity: { value: encoding.config('gridOpacity') } }; } } return def; }; axis.hideTicks = function(def) { def.properties.ticks = {opacity: {value: 0}}; def.properties.majorTicks = {opacity: {value: 0}}; def.properties.axis = {opacity: {value: 0}}; return def; }; axis.title = function (def, encoding, name, layout) { var ax = encoding.encDef(name).axis; if (ax.title) { def.title = ax.title; } else { // if not defined, automatically determine axis title from field def var fieldTitle = encoding.fieldTitle(name), maxLength; if (ax.titleMaxLength) { maxLength = ax.titleMaxLength; } else if (name===X) { maxLength = layout.cellWidth / encoding.config('characterWidth'); } else if (name === Y) { maxLength = layout.cellHeight / encoding.config('characterWidth'); } def.title = maxLength ? util.truncate(fieldTitle, maxLength) : fieldTitle; } if (name === ROW) { def.properties.title = { angle: {value: 0}, align: {value: 'right'}, baseline: {value: 'middle'}, dy: {value: (-layout.height/2) -20} }; } return def; }; axis.labels = {}; /** add custom label for time type and bin */ axis.labels.scale = function(def, encoding, name) { // time var timeUnit = encoding.encDef(name).timeUnit; if (encoding.isType(name, T) && timeUnit && (time.hasScale(timeUnit))) { setter(def, ['properties','labels','text','scale'], 'time-'+ timeUnit); } // FIXME bin return def; }; /** * Determine number format or truncate if maxLabel length is presented. */ axis.labels.format = function (def, encoding, name, stats) { var fieldStats = stats[encoding.encDef(name).name]; Iif (encoding.axis(name).format) { def.format = encoding.axis(name).format; } else if (encoding.isType(name, Q) || fieldStats.type === 'number') { def.format = encoding.numberFormat(fieldStats); } else if (encoding.isType(name, T)) { var timeUnit = encoding.encDef(name).timeUnit; Iif (!timeUnit) { def.format = encoding.config('timeFormat'); } else if (timeUnit === 'year') { def.format = 'd'; } } else Eif (encoding.isTypes(name, [N, O]) && encoding.axis(name).maxLabelLength) { setter(def, ['properties','labels','text','template'], '{{ datum.data | truncate:' + encoding.axis(name).maxLabelLength + '}}' ); } return def; }; axis.labels.angle = function(def, encoding, name) { var angle = encoding.axis(name).labelAngle; if (typeof angle === 'undefined') return def; setter(def, ['properties', 'labels', 'angle', 'value'], angle); return def; }; axis.labels.rotate = function(def) { var align = def.orient ==='top' ? 'left' : 'right'; setter(def, ['properties','labels', 'angle', 'value'], 270); setter(def, ['properties','labels', 'align', 'value'], align); setter(def, ['properties','labels', 'baseline', 'value'], 'middle'); return def; }; axis.titleOffset = function (encoding, layout, name) { // return specified value if specified var value = encoding.axis(name).titleOffset; Iif (value) return value; switch (name) { //FIXME make this adjustable case ROW: return 0; case COL: return 35; } return getter(layout, [name, 'axisTitleOffset']); }; |