4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-chart-axis-Axis'>/**
19 </span> * @class Ext.chart.axis.Axis
20 * @extends Ext.chart.axis.Abstract
22 * Defines axis for charts. The axis position, type, style can be configured.
23 * The axes are defined in an axes array of configuration objects where the type,
24 * field, grid and other configuration options can be set. To know more about how
25 * to create a Chart please check the Chart class documentation. Here's an example for the axes part:
26 * An example of axis for a series (in this case for an area chart that has multiple layers of yFields) could be:
32 * fields: ['data1', 'data2', 'data3'],
33 * title: 'Number of Hits',
47 * title: 'Month of the Year',
56 * 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
57 * 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.
58 * Both the category and numeric axes have `grid` set, which means that horizontal and vertical lines will cover the chart background. In the
59 * category axis the labels will be rotated so they can fit the space better.
61 Ext.define('Ext.chart.axis.Axis', {
63 /* Begin Definitions */
65 extend: 'Ext.chart.axis.Abstract',
67 alternateClassName: 'Ext.chart.Axis',
69 requires: ['Ext.draw.Draw'],
73 <span id='Ext-chart-axis-Axis-cfg-grid'> /**
74 </span> * @cfg {Boolean | Object} grid
75 * The grid configuration enables you to set a background grid for an axis.
76 * If set to *true* on a vertical axis, vertical lines will be drawn.
77 * If set to *true* on a horizontal axis, horizontal lines will be drawn.
78 * If both are set, a proper grid with horizontal and vertical lines will be drawn.
80 * You can set specific options for the grid configuration for odd and/or even lines/rows.
81 * Since the rows being drawn are rectangle sprites, you can set to an odd or even property
82 * all styles that apply to {@link Ext.draw.Sprite}. For more information on all the style
83 * properties you can set please take a look at {@link Ext.draw.Sprite}. Some useful style properties are `opacity`, `fill`, `stroke`, `stroke-width`, etc.
85 * The possible values for a grid option are then *true*, *false*, or an object with `{ odd, even }` properties
86 * where each property contains a sprite style descriptor object that is defined in {@link Ext.draw.Sprite}.
94 * fields: ['data1', 'data2', 'data3'],
95 * title: 'Number of Hits',
106 * position: 'bottom',
108 * title: 'Month of the Year',
114 <span id='Ext-chart-axis-Axis-cfg-majorTickSteps'> /**
115 </span> * @cfg {Number} majorTickSteps
116 * If `minimum` and `maximum` are specified it forces the number of major ticks to the specified value.
119 <span id='Ext-chart-axis-Axis-cfg-minorTickSteps'> /**
120 </span> * @cfg {Number} minorTickSteps
121 * The number of small ticks between two major ticks. Default is zero.
124 //@private force min/max values from store
127 <span id='Ext-chart-axis-Axis-cfg-dashSize'> /**
128 </span> * @cfg {Number} dashSize
129 * The size of the dash marker. Default's 3.
133 <span id='Ext-chart-axis-Axis-cfg-position'> /**
134 </span> * @cfg {String} position
135 * Where to set the axis. Available options are `left`, `bottom`, `right`, `top`. Default's `bottom`.
142 <span id='Ext-chart-axis-Axis-cfg-length'> /**
143 </span> * @cfg {Number} length
144 * Offset axis position. Default's 0.
148 <span id='Ext-chart-axis-Axis-cfg-width'> /**
149 </span> * @cfg {Number} width
150 * Offset axis width. Default's 0.
154 majorTickSteps: false,
157 applyData: Ext.emptyFn,
159 // @private creates a structure with start, end and step points.
160 calcEnds: function() {
165 store = me.chart.substore || me.chart.store,
166 series = me.chart.series.items,
169 min = isNaN(me.minimum) ? Infinity : me.minimum,
170 max = isNaN(me.maximum) ? -Infinity : me.maximum,
171 prevMin = me.prevMin,
172 prevMax = me.prevMax,
177 i, l, values, rec, out;
179 //if one series is stacked I have to aggregate the values
181 for (i = 0, l = series.length; !aggregate && i < l; i++) {
182 aggregate = aggregate || series[i].stacked;
183 excludes = series[i].__excludes || excludes;
185 store.each(function(record) {
187 if (!isFinite(min)) {
190 for (values = [0, 0], i = 0; i < ln; i++) {
194 rec = record.get(fields[i]);
195 values[+(rec > 0)] += math.abs(rec);
197 max = mmax(max, -values[0], values[1]);
198 min = mmin(min, -values[0], values[1]);
201 for (i = 0; i < ln; i++) {
205 value = record.get(fields[i]);
206 max = mmax(max, value);
207 min = mmin(min, value);
211 if (!isFinite(max)) {
212 max = me.prevMax || 0;
214 if (!isFinite(min)) {
215 min = me.prevMin || 0;
217 //normalize min max for snapEnds.
218 if (min != max && (max != (max >> 0))) {
219 max = (max >> 0) + 1;
221 out = Ext.draw.Draw.snapEnds(min, max, me.majorTickSteps !== false ? (me.majorTickSteps +1) : me.steps);
224 if (me.forceMinMax) {
232 if (!isNaN(me.maximum)) {
233 //TODO(nico) users are responsible for their own minimum/maximum values set.
234 //Clipping should be added to remove lines in the chart which are below the axis.
237 if (!isNaN(me.minimum)) {
238 //TODO(nico) users are responsible for their own minimum/maximum values set.
239 //Clipping should be added to remove lines in the chart which are below the axis.
240 out.from = me.minimum;
243 //Adjust after adjusting minimum and maximum
244 out.step = (out.to - out.from) / (outto - outfrom) * out.step;
246 if (me.adjustMaximumByMajorUnit) {
249 if (me.adjustMinimumByMajorUnit) {
250 out.from -= out.step;
252 me.prevMin = min == max? 0 : min;
257 <span id='Ext-chart-axis-Axis-method-drawAxis'> /**
258 </span> * Renders the axis into the screen and updates it's position.
260 drawAxis: function (init) {
265 gutterX = me.chart.maxGutter[0],
266 gutterY = me.chart.maxGutter[1],
267 dashSize = me.dashSize,
268 subDashesX = me.minorTickSteps || 0,
269 subDashesY = me.minorTickSteps || 0,
271 position = me.position,
274 stepCalcs = me.applyData(),
275 step = stepCalcs.step,
276 steps = stepCalcs.steps,
277 from = stepCalcs.from,
288 //If no steps are specified
289 //then don't draw the axis. This generally happens
290 //when an empty store.
291 if (me.hidden || isNaN(step) || (from == to)) {
295 me.from = stepCalcs.from;
296 me.to = stepCalcs.to;
297 if (position == 'left' || position == 'right') {
298 currentX = Math.floor(x) + 0.5;
299 path = ["M", currentX, y, "l", 0, -length];
300 trueLength = length - (gutterY * 2);
303 currentY = Math.floor(y) + 0.5;
304 path = ["M", x, currentY, "l", length, 0];
305 trueLength = length - (gutterX * 2);
308 delta = trueLength / (steps || 1);
309 dashesX = Math.max(subDashesX +1, 0);
310 dashesY = Math.max(subDashesY +1, 0);
311 if (me.type == 'Numeric') {
313 me.labels = [stepCalcs.from];
315 if (position == 'right' || position == 'left') {
316 currentY = y - gutterY;
317 currentX = x - ((position == 'left') * dashSize * 2);
318 while (currentY >= y - gutterY - trueLength) {
319 path.push("M", currentX, Math.floor(currentY) + 0.5, "l", dashSize * 2 + 1, 0);
320 if (currentY != y - gutterY) {
321 for (i = 1; i < dashesY; i++) {
322 path.push("M", currentX + dashSize, Math.floor(currentY + delta * i / dashesY) + 0.5, "l", dashSize + 1, 0);
325 inflections.push([ Math.floor(x), Math.floor(currentY) ]);
328 me.labels.push(me.labels[me.labels.length -1] + step);
334 if (Math.round(currentY + delta - (y - gutterY - trueLength))) {
335 path.push("M", currentX, Math.floor(y - length + gutterY) + 0.5, "l", dashSize * 2 + 1, 0);
336 for (i = 1; i < dashesY; i++) {
337 path.push("M", currentX + dashSize, Math.floor(y - length + gutterY + delta * i / dashesY) + 0.5, "l", dashSize + 1, 0);
339 inflections.push([ Math.floor(x), Math.floor(currentY) ]);
341 me.labels.push(me.labels[me.labels.length -1] + step);
345 currentX = x + gutterX;
346 currentY = y - ((position == 'top') * dashSize * 2);
347 while (currentX <= x + gutterX + trueLength) {
348 path.push("M", Math.floor(currentX) + 0.5, currentY, "l", 0, dashSize * 2 + 1);
349 if (currentX != x + gutterX) {
350 for (i = 1; i < dashesX; i++) {
351 path.push("M", Math.floor(currentX - delta * i / dashesX) + 0.5, currentY, "l", 0, dashSize + 1);
354 inflections.push([ Math.floor(currentX), Math.floor(y) ]);
357 me.labels.push(me.labels[me.labels.length -1] + step);
363 if (Math.round(currentX - delta - (x + gutterX + trueLength))) {
364 path.push("M", Math.floor(x + length - gutterX) + 0.5, currentY, "l", 0, dashSize * 2 + 1);
365 for (i = 1; i < dashesX; i++) {
366 path.push("M", Math.floor(x + length - gutterX - delta * i / dashesX) + 0.5, currentY, "l", 0, dashSize + 1);
368 inflections.push([ Math.floor(currentX), Math.floor(y) ]);
370 me.labels.push(me.labels[me.labels.length -1] + step);
375 me.axis = me.chart.surface.add(Ext.apply({
380 me.axis.setAttributes({
383 me.inflections = inflections;
384 if (!init && me.grid) {
387 me.axisBBox = me.axis.getBBox();
391 <span id='Ext-chart-axis-Axis-method-drawGrid'> /**
392 </span> * Renders an horizontal and/or vertical grid into the Surface.
394 drawGrid: function() {
396 surface = me.chart.surface,
400 inflections = me.inflections,
401 ln = inflections.length - ((odd || even)? 0 : 1),
402 position = me.position,
403 gutter = me.chart.maxGutter,
404 width = me.width - 2,
408 path = [], styles, lineWidth, dlineWidth,
409 oddPath = [], evenPath = [];
411 if ((gutter[1] !== 0 && (position == 'left' || position == 'right')) ||
412 (gutter[0] !== 0 && (position == 'top' || position == 'bottom'))) {
416 for (; i < ln; i++) {
417 point = inflections[i];
418 prevPoint = inflections[i - 1];
420 path = (i % 2)? oddPath : evenPath;
421 styles = ((i % 2)? odd : even) || {};
422 lineWidth = (styles.lineWidth || styles['stroke-width'] || 0) / 2;
423 dlineWidth = 2 * lineWidth;
424 if (position == 'left') {
425 path.push("M", prevPoint[0] + 1 + lineWidth, prevPoint[1] + 0.5 - lineWidth,
426 "L", prevPoint[0] + 1 + width - lineWidth, prevPoint[1] + 0.5 - lineWidth,
427 "L", point[0] + 1 + width - lineWidth, point[1] + 0.5 + lineWidth,
428 "L", point[0] + 1 + lineWidth, point[1] + 0.5 + lineWidth, "Z");
430 else if (position == 'right') {
431 path.push("M", prevPoint[0] - lineWidth, prevPoint[1] + 0.5 - lineWidth,
432 "L", prevPoint[0] - width + lineWidth, prevPoint[1] + 0.5 - lineWidth,
433 "L", point[0] - width + lineWidth, point[1] + 0.5 + lineWidth,
434 "L", point[0] - lineWidth, point[1] + 0.5 + lineWidth, "Z");
436 else if (position == 'top') {
437 path.push("M", prevPoint[0] + 0.5 + lineWidth, prevPoint[1] + 1 + lineWidth,
438 "L", prevPoint[0] + 0.5 + lineWidth, prevPoint[1] + 1 + width - lineWidth,
439 "L", point[0] + 0.5 - lineWidth, point[1] + 1 + width - lineWidth,
440 "L", point[0] + 0.5 - lineWidth, point[1] + 1 + lineWidth, "Z");
443 path.push("M", prevPoint[0] + 0.5 + lineWidth, prevPoint[1] - lineWidth,
444 "L", prevPoint[0] + 0.5 + lineWidth, prevPoint[1] - width + lineWidth,
445 "L", point[0] + 0.5 - lineWidth, point[1] - width + lineWidth,
446 "L", point[0] + 0.5 - lineWidth, point[1] - lineWidth, "Z");
449 if (position == 'left') {
450 path = path.concat(["M", point[0] + 0.5, point[1] + 0.5, "l", width, 0]);
452 else if (position == 'right') {
453 path = path.concat(["M", point[0] - 0.5, point[1] + 0.5, "l", -width, 0]);
455 else if (position == 'top') {
456 path = path.concat(["M", point[0] + 0.5, point[1] + 0.5, "l", 0, width]);
459 path = path.concat(["M", point[0] + 0.5, point[1] - 0.5, "l", 0, -width]);
464 if (oddPath.length) {
465 if (!me.gridOdd && oddPath.length) {
466 me.gridOdd = surface.add({
471 me.gridOdd.setAttributes(Ext.apply({
474 }, odd || {}), true);
476 if (evenPath.length) {
478 me.gridEven = surface.add({
483 me.gridEven.setAttributes(Ext.apply({
486 }, even || {}), true);
492 me.gridLines = me.chart.surface.add({
495 "stroke-width": me.lineWidth || 1,
496 stroke: me.gridColor || '#ccc'
499 me.gridLines.setAttributes({
504 else if (me.gridLines) {
505 me.gridLines.hide(true);
511 getOrCreateLabel: function(i, text) {
513 labelGroup = me.labelGroup,
514 textLabel = labelGroup.getAt(i),
515 surface = me.chart.surface;
517 if (text != textLabel.attr.text) {
518 textLabel.setAttributes(Ext.apply({
521 textLabel._bbox = textLabel.getBBox();
525 textLabel = surface.add(Ext.apply({
532 surface.renderItem(textLabel);
533 textLabel._bbox = textLabel.getBBox();
535 //get untransformed bounding box
536 if (me.label.rotation) {
537 textLabel.setAttributes({
542 textLabel._ubbox = textLabel.getBBox();
543 textLabel.setAttributes(me.label, true);
545 textLabel._ubbox = textLabel._bbox;
550 rect2pointArray: function(sprite) {
551 var surface = this.chart.surface,
552 rect = surface.getBBox(sprite, true),
553 p1 = [rect.x, rect.y],
555 p2 = [rect.x + rect.width, rect.y],
557 p3 = [rect.x + rect.width, rect.y + rect.height],
559 p4 = [rect.x, rect.y + rect.height],
561 matrix = sprite.matrix;
562 //transform the points
563 p1[0] = matrix.x.apply(matrix, p1p);
564 p1[1] = matrix.y.apply(matrix, p1p);
566 p2[0] = matrix.x.apply(matrix, p2p);
567 p2[1] = matrix.y.apply(matrix, p2p);
569 p3[0] = matrix.x.apply(matrix, p3p);
570 p3[1] = matrix.y.apply(matrix, p3p);
572 p4[0] = matrix.x.apply(matrix, p4p);
573 p4[1] = matrix.y.apply(matrix, p4p);
574 return [p1, p2, p3, p4];
577 intersect: function(l1, l2) {
578 var r1 = this.rect2pointArray(l1),
579 r2 = this.rect2pointArray(l2);
580 return !!Ext.draw.Draw.intersect(r1, r2).length;
583 drawHorizontalLabels: function() {
585 labelConf = me.label,
588 axes = me.chart.axes,
589 position = me.position,
590 inflections = me.inflections,
591 ln = inflections.length,
593 labelGroup = me.labelGroup,
596 gutterY = me.chart.maxGutter[1],
597 ubbox, bbox, point, prevX, prevLabel,
599 textLabel, attr, textRight, text,
600 label, last, x, y, i, firstLabel;
603 //get a reference to the first text label dimensions
604 point = inflections[0];
605 firstLabel = me.getOrCreateLabel(0, me.label.renderer(labels[0]));
606 ratio = Math.abs(Math.sin(labelConf.rotate && (labelConf.rotate.degrees * Math.PI / 180) || 0)) >> 0;
608 for (i = 0; i < ln; i++) {
609 point = inflections[i];
610 text = me.label.renderer(labels[i]);
611 textLabel = me.getOrCreateLabel(i, text);
612 bbox = textLabel._bbox;
613 maxHeight = max(maxHeight, bbox.height + me.dashSize + me.label.padding);
614 x = floor(point[0] - (ratio? bbox.height : bbox.width) / 2);
615 if (me.chart.maxGutter[0] == 0) {
616 if (i == 0 && axes.findIndex('position', 'left') == -1) {
619 else if (i == last && axes.findIndex('position', 'right') == -1) {
620 x = point[0] - bbox.width;
623 if (position == 'top') {
624 y = point[1] - (me.dashSize * 2) - me.label.padding - (bbox.height / 2);
627 y = point[1] + (me.dashSize * 2) + me.label.padding + (bbox.height / 2);
630 textLabel.setAttributes({
636 // Skip label if there isn't available minimum space
637 if (i != 0 && (me.intersect(textLabel, prevLabel)
638 || me.intersect(textLabel, firstLabel))) {
639 textLabel.hide(true);
643 prevLabel = textLabel;
649 drawVerticalLabels: function() {
651 inflections = me.inflections,
652 position = me.position,
653 ln = inflections.length,
659 axes = me.chart.axes,
660 gutterY = me.chart.maxGutter[1],
661 ubbox, bbox, point, prevLabel,
663 textLabel, attr, textRight, text,
664 label, last, x, y, i;
667 for (i = 0; i < last; i++) {
668 point = inflections[i];
669 text = me.label.renderer(labels[i]);
670 textLabel = me.getOrCreateLabel(i, text);
671 bbox = textLabel._bbox;
673 maxWidth = max(maxWidth, bbox.width + me.dashSize + me.label.padding);
675 if (gutterY < bbox.height / 2) {
676 if (i == last - 1 && axes.findIndex('position', 'top') == -1) {
677 y = me.y - me.length + ceil(bbox.height / 2);
679 else if (i == 0 && axes.findIndex('position', 'bottom') == -1) {
680 y = me.y - floor(bbox.height / 2);
683 if (position == 'left') {
684 x = point[0] - bbox.width - me.dashSize - me.label.padding - 2;
687 x = point[0] + me.dashSize + me.label.padding + 2;
689 textLabel.setAttributes(Ext.apply({
694 // Skip label if there isn't available minimum space
695 if (i != 0 && me.intersect(textLabel, prevLabel)) {
696 textLabel.hide(true);
699 prevLabel = textLabel;
705 <span id='Ext-chart-axis-Axis-method-drawLabel'> /**
706 </span> * Renders the labels in the axes.
708 drawLabel: function() {
710 position = me.position,
711 labelGroup = me.labelGroup,
712 inflections = me.inflections,
717 if (position == 'left' || position == 'right') {
718 maxWidth = me.drawVerticalLabels();
720 maxHeight = me.drawHorizontalLabels();
724 ln = labelGroup.getCount();
725 i = inflections.length;
726 for (; i < ln; i++) {
727 labelGroup.getAt(i).hide(true);
731 Ext.apply(me.bbox, me.axisBBox);
732 me.bbox.height = maxHeight;
733 me.bbox.width = maxWidth;
734 if (Ext.isString(me.title)) {
735 me.drawTitle(maxWidth, maxHeight);
739 // @private creates the elipsis for the text.
740 elipsis: function(sprite, text, desiredWidth, minWidth, center) {
744 if (desiredWidth < minWidth) {
748 while (text.length > 4) {
749 text = text.substr(0, text.length - 4) + "...";
750 sprite.setAttributes({
753 bbox = sprite.getBBox();
754 if (bbox.width < desiredWidth) {
755 if (typeof center == 'number') {
756 sprite.setAttributes({
757 x: Math.floor(center - (bbox.width / 2))
766 <span id='Ext-chart-axis-Axis-method-setTitle'> /**
767 </span> * Updates the {@link #title} of this axis.
768 * @param {String} title
770 setTitle: function(title) {
775 // @private draws the title for the axis.
776 drawTitle: function(maxWidth, maxHeight) {
778 position = me.position,
779 surface = me.chart.surface,
780 displaySprite = me.displaySprite,
782 rotate = (position == 'left' || position == 'right'),
788 displaySprite.setAttributes({text: title}, true);
796 displaySprite = me.displaySprite = surface.add(Ext.apply(base, me.axisTitleStyle, me.labelTitle));
797 surface.renderItem(displaySprite);
799 bbox = displaySprite.getBBox();
800 pad = me.dashSize + me.label.padding;
803 y -= ((me.length / 2) - (bbox.height / 2));
804 if (position == 'left') {
805 x -= (maxWidth + pad + (bbox.width / 2));
808 x += (maxWidth + pad + bbox.width - (bbox.width / 2));
810 me.bbox.width += bbox.width + 10;
813 x += (me.length / 2) - (bbox.width * 0.5);
814 if (position == 'top') {
815 y -= (maxHeight + pad + (bbox.height * 0.3));
818 y += (maxHeight + pad + (bbox.height * 0.8));
820 me.bbox.height += bbox.height + 10;
822 displaySprite.setAttributes({