2 * @class Ext.chart.axis.Axis
3 * @extends Ext.chart.axis.Abstract
5 * Defines axis for charts. The axis position, type, style can be configured.
6 * The axes are defined in an axes array of configuration objects where the type,
7 * field, grid and other configuration options can be set. To know more about how
8 * to create a Chart please check the Chart class documentation. Here's an example for the axes part:
9 * An example of axis for a series (in this case for an area chart that has multiple layers of yFields) could be:
15 * fields: ['data1', 'data2', 'data3'],
16 * title: 'Number of Hits',
30 * title: 'Month of the Year',
39 * In this case we use a `Numeric` axis for displaying the values of the Area series and a `Category` axis for displaying the names of
40 * the store elements. The numeric axis is placed on the left of the screen, while the category axis is placed at the bottom of the chart.
41 * Both the category and numeric axes have `grid` set, which means that horizontal and vertical lines will cover the chart background. In the
42 * category axis the labels will be rotated so they can fit the space better.
44 Ext.define('Ext.chart.axis.Axis', {
46 /* Begin Definitions */
48 extend: 'Ext.chart.axis.Abstract',
50 alternateClassName: 'Ext.chart.Axis',
52 requires: ['Ext.draw.Draw'],
57 * @cfg {Number} majorTickSteps
58 * If `minimum` and `maximum` are specified it forces the number of major ticks to the specified value.
62 * @cfg {Number} minorTickSteps
63 * The number of small ticks between two major ticks. Default is zero.
66 //@private force min/max values from store
70 * @cfg {Number} dashSize
71 * The size of the dash marker. Default's 3.
76 * @cfg {String} position
77 * Where to set the axis. Available options are `left`, `bottom`, `right`, `top`. Default's `bottom`.
85 * @cfg {Number} length
86 * Offset axis position. Default's 0.
92 * Offset axis width. Default's 0.
96 majorTickSteps: false,
99 applyData: Ext.emptyFn,
101 // @private creates a structure with start, end and step points.
102 calcEnds: function() {
107 store = me.chart.substore || me.chart.store,
108 series = me.chart.series.items,
111 min = isNaN(me.minimum) ? Infinity : me.minimum,
112 max = isNaN(me.maximum) ? -Infinity : me.maximum,
113 prevMin = me.prevMin,
114 prevMax = me.prevMax,
119 i, l, values, rec, out;
121 //if one series is stacked I have to aggregate the values
123 for (i = 0, l = series.length; !aggregate && i < l; i++) {
124 aggregate = aggregate || series[i].stacked;
125 excludes = series[i].__excludes || excludes;
127 store.each(function(record) {
129 if (!isFinite(min)) {
132 for (values = [0, 0], i = 0; i < ln; i++) {
136 rec = record.get(fields[i]);
137 values[+(rec > 0)] += math.abs(rec);
139 max = mmax(max, -values[0], values[1]);
140 min = mmin(min, -values[0], values[1]);
143 for (i = 0; i < ln; i++) {
147 value = record.get(fields[i]);
148 max = mmax(max, value);
149 min = mmin(min, value);
153 if (!isFinite(max)) {
154 max = me.prevMax || 0;
156 if (!isFinite(min)) {
157 min = me.prevMin || 0;
159 //normalize min max for snapEnds.
160 if (min != max && (max != (max >> 0))) {
161 max = (max >> 0) + 1;
163 out = Ext.draw.Draw.snapEnds(min, max, me.majorTickSteps !== false ? (me.majorTickSteps +1) : me.steps);
166 if (me.forceMinMax) {
174 if (!isNaN(me.maximum)) {
175 //TODO(nico) users are responsible for their own minimum/maximum values set.
176 //Clipping should be added to remove lines in the chart which are below the axis.
179 if (!isNaN(me.minimum)) {
180 //TODO(nico) users are responsible for their own minimum/maximum values set.
181 //Clipping should be added to remove lines in the chart which are below the axis.
182 out.from = me.minimum;
185 //Adjust after adjusting minimum and maximum
186 out.step = (out.to - out.from) / (outto - outfrom) * out.step;
188 if (me.adjustMaximumByMajorUnit) {
191 if (me.adjustMinimumByMajorUnit) {
192 out.from -= out.step;
194 me.prevMin = min == max? 0 : min;
200 * Renders the axis into the screen and updates it's position.
202 drawAxis: function (init) {
207 gutterX = me.chart.maxGutter[0],
208 gutterY = me.chart.maxGutter[1],
209 dashSize = me.dashSize,
210 subDashesX = me.minorTickSteps || 0,
211 subDashesY = me.minorTickSteps || 0,
213 position = me.position,
216 stepCalcs = me.applyData(),
217 step = stepCalcs.step,
218 steps = stepCalcs.steps,
219 from = stepCalcs.from,
230 //If no steps are specified
231 //then don't draw the axis. This generally happens
232 //when an empty store.
233 if (me.hidden || isNaN(step) || (from == to)) {
237 me.from = stepCalcs.from;
238 me.to = stepCalcs.to;
239 if (position == 'left' || position == 'right') {
240 currentX = Math.floor(x) + 0.5;
241 path = ["M", currentX, y, "l", 0, -length];
242 trueLength = length - (gutterY * 2);
245 currentY = Math.floor(y) + 0.5;
246 path = ["M", x, currentY, "l", length, 0];
247 trueLength = length - (gutterX * 2);
250 delta = trueLength / (steps || 1);
251 dashesX = Math.max(subDashesX +1, 0);
252 dashesY = Math.max(subDashesY +1, 0);
253 if (me.type == 'Numeric') {
255 me.labels = [stepCalcs.from];
257 if (position == 'right' || position == 'left') {
258 currentY = y - gutterY;
259 currentX = x - ((position == 'left') * dashSize * 2);
260 while (currentY >= y - gutterY - trueLength) {
261 path.push("M", currentX, Math.floor(currentY) + 0.5, "l", dashSize * 2 + 1, 0);
262 if (currentY != y - gutterY) {
263 for (i = 1; i < dashesY; i++) {
264 path.push("M", currentX + dashSize, Math.floor(currentY + delta * i / dashesY) + 0.5, "l", dashSize + 1, 0);
267 inflections.push([ Math.floor(x), Math.floor(currentY) ]);
270 me.labels.push(me.labels[me.labels.length -1] + step);
276 if (Math.round(currentY + delta - (y - gutterY - trueLength))) {
277 path.push("M", currentX, Math.floor(y - length + gutterY) + 0.5, "l", dashSize * 2 + 1, 0);
278 for (i = 1; i < dashesY; i++) {
279 path.push("M", currentX + dashSize, Math.floor(y - length + gutterY + delta * i / dashesY) + 0.5, "l", dashSize + 1, 0);
281 inflections.push([ Math.floor(x), Math.floor(currentY) ]);
283 me.labels.push(me.labels[me.labels.length -1] + step);
287 currentX = x + gutterX;
288 currentY = y - ((position == 'top') * dashSize * 2);
289 while (currentX <= x + gutterX + trueLength) {
290 path.push("M", Math.floor(currentX) + 0.5, currentY, "l", 0, dashSize * 2 + 1);
291 if (currentX != x + gutterX) {
292 for (i = 1; i < dashesX; i++) {
293 path.push("M", Math.floor(currentX - delta * i / dashesX) + 0.5, currentY, "l", 0, dashSize + 1);
296 inflections.push([ Math.floor(currentX), Math.floor(y) ]);
299 me.labels.push(me.labels[me.labels.length -1] + step);
305 if (Math.round(currentX - delta - (x + gutterX + trueLength))) {
306 path.push("M", Math.floor(x + length - gutterX) + 0.5, currentY, "l", 0, dashSize * 2 + 1);
307 for (i = 1; i < dashesX; i++) {
308 path.push("M", Math.floor(x + length - gutterX - delta * i / dashesX) + 0.5, currentY, "l", 0, dashSize + 1);
310 inflections.push([ Math.floor(currentX), Math.floor(y) ]);
312 me.labels.push(me.labels[me.labels.length -1] + step);
317 me.axis = me.chart.surface.add(Ext.apply({
322 me.axis.setAttributes({
325 me.inflections = inflections;
326 if (!init && me.grid) {
329 me.axisBBox = me.axis.getBBox();
334 * Renders an horizontal and/or vertical grid into the Surface.
336 drawGrid: function() {
338 surface = me.chart.surface,
342 inflections = me.inflections,
343 ln = inflections.length - ((odd || even)? 0 : 1),
344 position = me.position,
345 gutter = me.chart.maxGutter,
346 width = me.width - 2,
350 path = [], styles, lineWidth, dlineWidth,
351 oddPath = [], evenPath = [];
353 if ((gutter[1] !== 0 && (position == 'left' || position == 'right')) ||
354 (gutter[0] !== 0 && (position == 'top' || position == 'bottom'))) {
358 for (; i < ln; i++) {
359 point = inflections[i];
360 prevPoint = inflections[i - 1];
362 path = (i % 2)? oddPath : evenPath;
363 styles = ((i % 2)? odd : even) || {};
364 lineWidth = (styles.lineWidth || styles['stroke-width'] || 0) / 2;
365 dlineWidth = 2 * lineWidth;
366 if (position == 'left') {
367 path.push("M", prevPoint[0] + 1 + lineWidth, prevPoint[1] + 0.5 - lineWidth,
368 "L", prevPoint[0] + 1 + width - lineWidth, prevPoint[1] + 0.5 - lineWidth,
369 "L", point[0] + 1 + width - lineWidth, point[1] + 0.5 + lineWidth,
370 "L", point[0] + 1 + lineWidth, point[1] + 0.5 + lineWidth, "Z");
372 else if (position == 'right') {
373 path.push("M", prevPoint[0] - lineWidth, prevPoint[1] + 0.5 - lineWidth,
374 "L", prevPoint[0] - width + lineWidth, prevPoint[1] + 0.5 - lineWidth,
375 "L", point[0] - width + lineWidth, point[1] + 0.5 + lineWidth,
376 "L", point[0] - lineWidth, point[1] + 0.5 + lineWidth, "Z");
378 else if (position == 'top') {
379 path.push("M", prevPoint[0] + 0.5 + lineWidth, prevPoint[1] + 1 + lineWidth,
380 "L", prevPoint[0] + 0.5 + lineWidth, prevPoint[1] + 1 + width - lineWidth,
381 "L", point[0] + 0.5 - lineWidth, point[1] + 1 + width - lineWidth,
382 "L", point[0] + 0.5 - lineWidth, point[1] + 1 + lineWidth, "Z");
385 path.push("M", prevPoint[0] + 0.5 + lineWidth, prevPoint[1] - lineWidth,
386 "L", prevPoint[0] + 0.5 + lineWidth, prevPoint[1] - width + lineWidth,
387 "L", point[0] + 0.5 - lineWidth, point[1] - width + lineWidth,
388 "L", point[0] + 0.5 - lineWidth, point[1] - lineWidth, "Z");
391 if (position == 'left') {
392 path = path.concat(["M", point[0] + 0.5, point[1] + 0.5, "l", width, 0]);
394 else if (position == 'right') {
395 path = path.concat(["M", point[0] - 0.5, point[1] + 0.5, "l", -width, 0]);
397 else if (position == 'top') {
398 path = path.concat(["M", point[0] + 0.5, point[1] + 0.5, "l", 0, width]);
401 path = path.concat(["M", point[0] + 0.5, point[1] - 0.5, "l", 0, -width]);
406 if (oddPath.length) {
407 if (!me.gridOdd && oddPath.length) {
408 me.gridOdd = surface.add({
413 me.gridOdd.setAttributes(Ext.apply({
416 }, odd || {}), true);
418 if (evenPath.length) {
420 me.gridEven = surface.add({
425 me.gridEven.setAttributes(Ext.apply({
428 }, even || {}), true);
434 me.gridLines = me.chart.surface.add({
437 "stroke-width": me.lineWidth || 1,
438 stroke: me.gridColor || '#ccc'
441 me.gridLines.setAttributes({
446 else if (me.gridLines) {
447 me.gridLines.hide(true);
453 getOrCreateLabel: function(i, text) {
455 labelGroup = me.labelGroup,
456 textLabel = labelGroup.getAt(i),
457 surface = me.chart.surface;
459 if (text != textLabel.attr.text) {
460 textLabel.setAttributes(Ext.apply({
463 textLabel._bbox = textLabel.getBBox();
467 textLabel = surface.add(Ext.apply({
474 surface.renderItem(textLabel);
475 textLabel._bbox = textLabel.getBBox();
477 //get untransformed bounding box
478 if (me.label.rotation) {
479 textLabel.setAttributes({
484 textLabel._ubbox = textLabel.getBBox();
485 textLabel.setAttributes(me.label, true);
487 textLabel._ubbox = textLabel._bbox;
492 rect2pointArray: function(sprite) {
493 var surface = this.chart.surface,
494 rect = surface.getBBox(sprite, true),
495 p1 = [rect.x, rect.y],
497 p2 = [rect.x + rect.width, rect.y],
499 p3 = [rect.x + rect.width, rect.y + rect.height],
501 p4 = [rect.x, rect.y + rect.height],
503 matrix = sprite.matrix;
504 //transform the points
505 p1[0] = matrix.x.apply(matrix, p1p);
506 p1[1] = matrix.y.apply(matrix, p1p);
508 p2[0] = matrix.x.apply(matrix, p2p);
509 p2[1] = matrix.y.apply(matrix, p2p);
511 p3[0] = matrix.x.apply(matrix, p3p);
512 p3[1] = matrix.y.apply(matrix, p3p);
514 p4[0] = matrix.x.apply(matrix, p4p);
515 p4[1] = matrix.y.apply(matrix, p4p);
516 return [p1, p2, p3, p4];
519 intersect: function(l1, l2) {
520 var r1 = this.rect2pointArray(l1),
521 r2 = this.rect2pointArray(l2);
522 return !!Ext.draw.Draw.intersect(r1, r2).length;
525 drawHorizontalLabels: function() {
527 labelConf = me.label,
530 axes = me.chart.axes,
531 position = me.position,
532 inflections = me.inflections,
533 ln = inflections.length,
535 labelGroup = me.labelGroup,
538 gutterY = me.chart.maxGutter[1],
539 ubbox, bbox, point, prevX, prevLabel,
541 textLabel, attr, textRight, text,
542 label, last, x, y, i, firstLabel;
545 //get a reference to the first text label dimensions
546 point = inflections[0];
547 firstLabel = me.getOrCreateLabel(0, me.label.renderer(labels[0]));
548 ratio = Math.abs(Math.sin(labelConf.rotate && (labelConf.rotate.degrees * Math.PI / 180) || 0)) >> 0;
550 for (i = 0; i < ln; i++) {
551 point = inflections[i];
552 text = me.label.renderer(labels[i]);
553 textLabel = me.getOrCreateLabel(i, text);
554 bbox = textLabel._bbox;
555 maxHeight = max(maxHeight, bbox.height + me.dashSize + me.label.padding);
556 x = floor(point[0] - (ratio? bbox.height : bbox.width) / 2);
557 if (me.chart.maxGutter[0] == 0) {
558 if (i == 0 && axes.findIndex('position', 'left') == -1) {
561 else if (i == last && axes.findIndex('position', 'right') == -1) {
562 x = point[0] - bbox.width;
565 if (position == 'top') {
566 y = point[1] - (me.dashSize * 2) - me.label.padding - (bbox.height / 2);
569 y = point[1] + (me.dashSize * 2) + me.label.padding + (bbox.height / 2);
572 textLabel.setAttributes({
578 // Skip label if there isn't available minimum space
579 if (i != 0 && (me.intersect(textLabel, prevLabel)
580 || me.intersect(textLabel, firstLabel))) {
581 textLabel.hide(true);
585 prevLabel = textLabel;
591 drawVerticalLabels: function() {
593 inflections = me.inflections,
594 position = me.position,
595 ln = inflections.length,
601 axes = me.chart.axes,
602 gutterY = me.chart.maxGutter[1],
603 ubbox, bbox, point, prevLabel,
605 textLabel, attr, textRight, text,
606 label, last, x, y, i;
609 for (i = 0; i < last; i++) {
610 point = inflections[i];
611 text = me.label.renderer(labels[i]);
612 textLabel = me.getOrCreateLabel(i, text);
613 bbox = textLabel._bbox;
615 maxWidth = max(maxWidth, bbox.width + me.dashSize + me.label.padding);
617 if (gutterY < bbox.height / 2) {
618 if (i == last - 1 && axes.findIndex('position', 'top') == -1) {
619 y = me.y - me.length + ceil(bbox.height / 2);
621 else if (i == 0 && axes.findIndex('position', 'bottom') == -1) {
622 y = me.y - floor(bbox.height / 2);
625 if (position == 'left') {
626 x = point[0] - bbox.width - me.dashSize - me.label.padding - 2;
629 x = point[0] + me.dashSize + me.label.padding + 2;
631 textLabel.setAttributes(Ext.apply({
636 // Skip label if there isn't available minimum space
637 if (i != 0 && me.intersect(textLabel, prevLabel)) {
638 textLabel.hide(true);
641 prevLabel = textLabel;
648 * Renders the labels in the axes.
650 drawLabel: function() {
652 position = me.position,
653 labelGroup = me.labelGroup,
654 inflections = me.inflections,
659 if (position == 'left' || position == 'right') {
660 maxWidth = me.drawVerticalLabels();
662 maxHeight = me.drawHorizontalLabels();
666 ln = labelGroup.getCount();
667 i = inflections.length;
668 for (; i < ln; i++) {
669 labelGroup.getAt(i).hide(true);
673 Ext.apply(me.bbox, me.axisBBox);
674 me.bbox.height = maxHeight;
675 me.bbox.width = maxWidth;
676 if (Ext.isString(me.title)) {
677 me.drawTitle(maxWidth, maxHeight);
681 // @private creates the elipsis for the text.
682 elipsis: function(sprite, text, desiredWidth, minWidth, center) {
686 if (desiredWidth < minWidth) {
690 while (text.length > 4) {
691 text = text.substr(0, text.length - 4) + "...";
692 sprite.setAttributes({
695 bbox = sprite.getBBox();
696 if (bbox.width < desiredWidth) {
697 if (typeof center == 'number') {
698 sprite.setAttributes({
699 x: Math.floor(center - (bbox.width / 2))
709 * Updates the {@link #title} of this axis.
710 * @param {String} title
712 setTitle: function(title) {
717 // @private draws the title for the axis.
718 drawTitle: function(maxWidth, maxHeight) {
720 position = me.position,
721 surface = me.chart.surface,
722 displaySprite = me.displaySprite,
724 rotate = (position == 'left' || position == 'right'),
730 displaySprite.setAttributes({text: title}, true);
738 displaySprite = me.displaySprite = surface.add(Ext.apply(base, me.axisTitleStyle, me.labelTitle));
739 surface.renderItem(displaySprite);
741 bbox = displaySprite.getBBox();
742 pad = me.dashSize + me.label.padding;
745 y -= ((me.length / 2) - (bbox.height / 2));
746 if (position == 'left') {
747 x -= (maxWidth + pad + (bbox.width / 2));
750 x += (maxWidth + pad + bbox.width - (bbox.width / 2));
752 me.bbox.width += bbox.width + 10;
755 x += (me.length / 2) - (bbox.width * 0.5);
756 if (position == 'top') {
757 y -= (maxHeight + pad + (bbox.height * 0.3));
760 y += (maxHeight + pad + (bbox.height * 0.8));
762 me.bbox.height += bbox.height + 10;
764 displaySprite.setAttributes({