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-majorTickSteps'> /**
74 </span> * @cfg {Number} majorTickSteps
75 * If `minimum` and `maximum` are specified it forces the number of major ticks to the specified value.
78 <span id='Ext-chart-axis-Axis-cfg-minorTickSteps'> /**
79 </span> * @cfg {Number} minorTickSteps
80 * The number of small ticks between two major ticks. Default is zero.
83 //@private force min/max values from store
86 <span id='Ext-chart-axis-Axis-cfg-dashSize'> /**
87 </span> * @cfg {Number} dashSize
88 * The size of the dash marker. Default's 3.
92 <span id='Ext-chart-axis-Axis-cfg-position'> /**
93 </span> * @cfg {String} position
94 * Where to set the axis. Available options are `left`, `bottom`, `right`, `top`. Default's `bottom`.
101 <span id='Ext-chart-axis-Axis-cfg-length'> /**
102 </span> * @cfg {Number} length
103 * Offset axis position. Default's 0.
107 <span id='Ext-chart-axis-Axis-cfg-width'> /**
108 </span> * @cfg {Number} width
109 * Offset axis width. Default's 0.
113 majorTickSteps: false,
116 applyData: Ext.emptyFn,
118 // @private creates a structure with start, end and step points.
119 calcEnds: function() {
124 store = me.chart.substore || me.chart.store,
125 series = me.chart.series.items,
128 min = isNaN(me.minimum) ? Infinity : me.minimum,
129 max = isNaN(me.maximum) ? -Infinity : me.maximum,
130 prevMin = me.prevMin,
131 prevMax = me.prevMax,
136 i, l, values, rec, out;
138 //if one series is stacked I have to aggregate the values
140 for (i = 0, l = series.length; !aggregate && i < l; i++) {
141 aggregate = aggregate || series[i].stacked;
142 excludes = series[i].__excludes || excludes;
144 store.each(function(record) {
146 if (!isFinite(min)) {
149 for (values = [0, 0], i = 0; i < ln; i++) {
153 rec = record.get(fields[i]);
154 values[+(rec > 0)] += math.abs(rec);
156 max = mmax(max, -values[0], values[1]);
157 min = mmin(min, -values[0], values[1]);
160 for (i = 0; i < ln; i++) {
164 value = record.get(fields[i]);
165 max = mmax(max, value);
166 min = mmin(min, value);
170 if (!isFinite(max)) {
171 max = me.prevMax || 0;
173 if (!isFinite(min)) {
174 min = me.prevMin || 0;
176 //normalize min max for snapEnds.
177 if (min != max && (max != (max >> 0))) {
178 max = (max >> 0) + 1;
180 out = Ext.draw.Draw.snapEnds(min, max, me.majorTickSteps !== false ? (me.majorTickSteps +1) : me.steps);
183 if (me.forceMinMax) {
191 if (!isNaN(me.maximum)) {
192 //TODO(nico) users are responsible for their own minimum/maximum values set.
193 //Clipping should be added to remove lines in the chart which are below the axis.
196 if (!isNaN(me.minimum)) {
197 //TODO(nico) users are responsible for their own minimum/maximum values set.
198 //Clipping should be added to remove lines in the chart which are below the axis.
199 out.from = me.minimum;
202 //Adjust after adjusting minimum and maximum
203 out.step = (out.to - out.from) / (outto - outfrom) * out.step;
205 if (me.adjustMaximumByMajorUnit) {
208 if (me.adjustMinimumByMajorUnit) {
209 out.from -= out.step;
211 me.prevMin = min == max? 0 : min;
216 <span id='Ext-chart-axis-Axis-method-drawAxis'> /**
217 </span> * Renders the axis into the screen and updates it's position.
219 drawAxis: function (init) {
224 gutterX = me.chart.maxGutter[0],
225 gutterY = me.chart.maxGutter[1],
226 dashSize = me.dashSize,
227 subDashesX = me.minorTickSteps || 0,
228 subDashesY = me.minorTickSteps || 0,
230 position = me.position,
233 stepCalcs = me.applyData(),
234 step = stepCalcs.step,
235 steps = stepCalcs.steps,
236 from = stepCalcs.from,
247 //If no steps are specified
248 //then don't draw the axis. This generally happens
249 //when an empty store.
250 if (me.hidden || isNaN(step) || (from == to)) {
254 me.from = stepCalcs.from;
255 me.to = stepCalcs.to;
256 if (position == 'left' || position == 'right') {
257 currentX = Math.floor(x) + 0.5;
258 path = ["M", currentX, y, "l", 0, -length];
259 trueLength = length - (gutterY * 2);
262 currentY = Math.floor(y) + 0.5;
263 path = ["M", x, currentY, "l", length, 0];
264 trueLength = length - (gutterX * 2);
267 delta = trueLength / (steps || 1);
268 dashesX = Math.max(subDashesX +1, 0);
269 dashesY = Math.max(subDashesY +1, 0);
270 if (me.type == 'Numeric') {
272 me.labels = [stepCalcs.from];
274 if (position == 'right' || position == 'left') {
275 currentY = y - gutterY;
276 currentX = x - ((position == 'left') * dashSize * 2);
277 while (currentY >= y - gutterY - trueLength) {
278 path.push("M", currentX, Math.floor(currentY) + 0.5, "l", dashSize * 2 + 1, 0);
279 if (currentY != y - gutterY) {
280 for (i = 1; i < dashesY; i++) {
281 path.push("M", currentX + dashSize, Math.floor(currentY + delta * i / dashesY) + 0.5, "l", dashSize + 1, 0);
284 inflections.push([ Math.floor(x), Math.floor(currentY) ]);
287 me.labels.push(me.labels[me.labels.length -1] + step);
293 if (Math.round(currentY + delta - (y - gutterY - trueLength))) {
294 path.push("M", currentX, Math.floor(y - length + gutterY) + 0.5, "l", dashSize * 2 + 1, 0);
295 for (i = 1; i < dashesY; i++) {
296 path.push("M", currentX + dashSize, Math.floor(y - length + gutterY + delta * i / dashesY) + 0.5, "l", dashSize + 1, 0);
298 inflections.push([ Math.floor(x), Math.floor(currentY) ]);
300 me.labels.push(me.labels[me.labels.length -1] + step);
304 currentX = x + gutterX;
305 currentY = y - ((position == 'top') * dashSize * 2);
306 while (currentX <= x + gutterX + trueLength) {
307 path.push("M", Math.floor(currentX) + 0.5, currentY, "l", 0, dashSize * 2 + 1);
308 if (currentX != x + gutterX) {
309 for (i = 1; i < dashesX; i++) {
310 path.push("M", Math.floor(currentX - delta * i / dashesX) + 0.5, currentY, "l", 0, dashSize + 1);
313 inflections.push([ Math.floor(currentX), Math.floor(y) ]);
316 me.labels.push(me.labels[me.labels.length -1] + step);
322 if (Math.round(currentX - delta - (x + gutterX + trueLength))) {
323 path.push("M", Math.floor(x + length - gutterX) + 0.5, currentY, "l", 0, dashSize * 2 + 1);
324 for (i = 1; i < dashesX; i++) {
325 path.push("M", Math.floor(x + length - gutterX - delta * i / dashesX) + 0.5, currentY, "l", 0, dashSize + 1);
327 inflections.push([ Math.floor(currentX), Math.floor(y) ]);
329 me.labels.push(me.labels[me.labels.length -1] + step);
334 me.axis = me.chart.surface.add(Ext.apply({
339 me.axis.setAttributes({
342 me.inflections = inflections;
343 if (!init && me.grid) {
346 me.axisBBox = me.axis.getBBox();
350 <span id='Ext-chart-axis-Axis-method-drawGrid'> /**
351 </span> * Renders an horizontal and/or vertical grid into the Surface.
353 drawGrid: function() {
355 surface = me.chart.surface,
359 inflections = me.inflections,
360 ln = inflections.length - ((odd || even)? 0 : 1),
361 position = me.position,
362 gutter = me.chart.maxGutter,
363 width = me.width - 2,
367 path = [], styles, lineWidth, dlineWidth,
368 oddPath = [], evenPath = [];
370 if ((gutter[1] !== 0 && (position == 'left' || position == 'right')) ||
371 (gutter[0] !== 0 && (position == 'top' || position == 'bottom'))) {
375 for (; i < ln; i++) {
376 point = inflections[i];
377 prevPoint = inflections[i - 1];
379 path = (i % 2)? oddPath : evenPath;
380 styles = ((i % 2)? odd : even) || {};
381 lineWidth = (styles.lineWidth || styles['stroke-width'] || 0) / 2;
382 dlineWidth = 2 * lineWidth;
383 if (position == 'left') {
384 path.push("M", prevPoint[0] + 1 + lineWidth, prevPoint[1] + 0.5 - lineWidth,
385 "L", prevPoint[0] + 1 + width - lineWidth, prevPoint[1] + 0.5 - lineWidth,
386 "L", point[0] + 1 + width - lineWidth, point[1] + 0.5 + lineWidth,
387 "L", point[0] + 1 + lineWidth, point[1] + 0.5 + lineWidth, "Z");
389 else if (position == 'right') {
390 path.push("M", prevPoint[0] - lineWidth, prevPoint[1] + 0.5 - lineWidth,
391 "L", prevPoint[0] - width + lineWidth, prevPoint[1] + 0.5 - lineWidth,
392 "L", point[0] - width + lineWidth, point[1] + 0.5 + lineWidth,
393 "L", point[0] - lineWidth, point[1] + 0.5 + lineWidth, "Z");
395 else if (position == 'top') {
396 path.push("M", prevPoint[0] + 0.5 + lineWidth, prevPoint[1] + 1 + lineWidth,
397 "L", prevPoint[0] + 0.5 + lineWidth, prevPoint[1] + 1 + width - lineWidth,
398 "L", point[0] + 0.5 - lineWidth, point[1] + 1 + width - lineWidth,
399 "L", point[0] + 0.5 - lineWidth, point[1] + 1 + lineWidth, "Z");
402 path.push("M", prevPoint[0] + 0.5 + lineWidth, prevPoint[1] - lineWidth,
403 "L", prevPoint[0] + 0.5 + lineWidth, prevPoint[1] - width + lineWidth,
404 "L", point[0] + 0.5 - lineWidth, point[1] - width + lineWidth,
405 "L", point[0] + 0.5 - lineWidth, point[1] - lineWidth, "Z");
408 if (position == 'left') {
409 path = path.concat(["M", point[0] + 0.5, point[1] + 0.5, "l", width, 0]);
411 else if (position == 'right') {
412 path = path.concat(["M", point[0] - 0.5, point[1] + 0.5, "l", -width, 0]);
414 else if (position == 'top') {
415 path = path.concat(["M", point[0] + 0.5, point[1] + 0.5, "l", 0, width]);
418 path = path.concat(["M", point[0] + 0.5, point[1] - 0.5, "l", 0, -width]);
423 if (oddPath.length) {
424 if (!me.gridOdd && oddPath.length) {
425 me.gridOdd = surface.add({
430 me.gridOdd.setAttributes(Ext.apply({
433 }, odd || {}), true);
435 if (evenPath.length) {
437 me.gridEven = surface.add({
442 me.gridEven.setAttributes(Ext.apply({
445 }, even || {}), true);
451 me.gridLines = me.chart.surface.add({
454 "stroke-width": me.lineWidth || 1,
455 stroke: me.gridColor || '#ccc'
458 me.gridLines.setAttributes({
463 else if (me.gridLines) {
464 me.gridLines.hide(true);
470 getOrCreateLabel: function(i, text) {
472 labelGroup = me.labelGroup,
473 textLabel = labelGroup.getAt(i),
474 surface = me.chart.surface;
476 if (text != textLabel.attr.text) {
477 textLabel.setAttributes(Ext.apply({
480 textLabel._bbox = textLabel.getBBox();
484 textLabel = surface.add(Ext.apply({
491 surface.renderItem(textLabel);
492 textLabel._bbox = textLabel.getBBox();
494 //get untransformed bounding box
495 if (me.label.rotation) {
496 textLabel.setAttributes({
501 textLabel._ubbox = textLabel.getBBox();
502 textLabel.setAttributes(me.label, true);
504 textLabel._ubbox = textLabel._bbox;
509 rect2pointArray: function(sprite) {
510 var surface = this.chart.surface,
511 rect = surface.getBBox(sprite, true),
512 p1 = [rect.x, rect.y],
514 p2 = [rect.x + rect.width, rect.y],
516 p3 = [rect.x + rect.width, rect.y + rect.height],
518 p4 = [rect.x, rect.y + rect.height],
520 matrix = sprite.matrix;
521 //transform the points
522 p1[0] = matrix.x.apply(matrix, p1p);
523 p1[1] = matrix.y.apply(matrix, p1p);
525 p2[0] = matrix.x.apply(matrix, p2p);
526 p2[1] = matrix.y.apply(matrix, p2p);
528 p3[0] = matrix.x.apply(matrix, p3p);
529 p3[1] = matrix.y.apply(matrix, p3p);
531 p4[0] = matrix.x.apply(matrix, p4p);
532 p4[1] = matrix.y.apply(matrix, p4p);
533 return [p1, p2, p3, p4];
536 intersect: function(l1, l2) {
537 var r1 = this.rect2pointArray(l1),
538 r2 = this.rect2pointArray(l2);
539 return !!Ext.draw.Draw.intersect(r1, r2).length;
542 drawHorizontalLabels: function() {
544 labelConf = me.label,
547 axes = me.chart.axes,
548 position = me.position,
549 inflections = me.inflections,
550 ln = inflections.length,
552 labelGroup = me.labelGroup,
555 gutterY = me.chart.maxGutter[1],
556 ubbox, bbox, point, prevX, prevLabel,
558 textLabel, attr, textRight, text,
559 label, last, x, y, i, firstLabel;
562 //get a reference to the first text label dimensions
563 point = inflections[0];
564 firstLabel = me.getOrCreateLabel(0, me.label.renderer(labels[0]));
565 ratio = Math.abs(Math.sin(labelConf.rotate && (labelConf.rotate.degrees * Math.PI / 180) || 0)) >> 0;
567 for (i = 0; i < ln; i++) {
568 point = inflections[i];
569 text = me.label.renderer(labels[i]);
570 textLabel = me.getOrCreateLabel(i, text);
571 bbox = textLabel._bbox;
572 maxHeight = max(maxHeight, bbox.height + me.dashSize + me.label.padding);
573 x = floor(point[0] - (ratio? bbox.height : bbox.width) / 2);
574 if (me.chart.maxGutter[0] == 0) {
575 if (i == 0 && axes.findIndex('position', 'left') == -1) {
578 else if (i == last && axes.findIndex('position', 'right') == -1) {
579 x = point[0] - bbox.width;
582 if (position == 'top') {
583 y = point[1] - (me.dashSize * 2) - me.label.padding - (bbox.height / 2);
586 y = point[1] + (me.dashSize * 2) + me.label.padding + (bbox.height / 2);
589 textLabel.setAttributes({
595 // Skip label if there isn't available minimum space
596 if (i != 0 && (me.intersect(textLabel, prevLabel)
597 || me.intersect(textLabel, firstLabel))) {
598 textLabel.hide(true);
602 prevLabel = textLabel;
608 drawVerticalLabels: function() {
610 inflections = me.inflections,
611 position = me.position,
612 ln = inflections.length,
618 axes = me.chart.axes,
619 gutterY = me.chart.maxGutter[1],
620 ubbox, bbox, point, prevLabel,
622 textLabel, attr, textRight, text,
623 label, last, x, y, i;
626 for (i = 0; i < last; i++) {
627 point = inflections[i];
628 text = me.label.renderer(labels[i]);
629 textLabel = me.getOrCreateLabel(i, text);
630 bbox = textLabel._bbox;
632 maxWidth = max(maxWidth, bbox.width + me.dashSize + me.label.padding);
634 if (gutterY < bbox.height / 2) {
635 if (i == last - 1 && axes.findIndex('position', 'top') == -1) {
636 y = me.y - me.length + ceil(bbox.height / 2);
638 else if (i == 0 && axes.findIndex('position', 'bottom') == -1) {
639 y = me.y - floor(bbox.height / 2);
642 if (position == 'left') {
643 x = point[0] - bbox.width - me.dashSize - me.label.padding - 2;
646 x = point[0] + me.dashSize + me.label.padding + 2;
648 textLabel.setAttributes(Ext.apply({
653 // Skip label if there isn't available minimum space
654 if (i != 0 && me.intersect(textLabel, prevLabel)) {
655 textLabel.hide(true);
658 prevLabel = textLabel;
664 <span id='Ext-chart-axis-Axis-method-drawLabel'> /**
665 </span> * Renders the labels in the axes.
667 drawLabel: function() {
669 position = me.position,
670 labelGroup = me.labelGroup,
671 inflections = me.inflections,
676 if (position == 'left' || position == 'right') {
677 maxWidth = me.drawVerticalLabels();
679 maxHeight = me.drawHorizontalLabels();
683 ln = labelGroup.getCount();
684 i = inflections.length;
685 for (; i < ln; i++) {
686 labelGroup.getAt(i).hide(true);
690 Ext.apply(me.bbox, me.axisBBox);
691 me.bbox.height = maxHeight;
692 me.bbox.width = maxWidth;
693 if (Ext.isString(me.title)) {
694 me.drawTitle(maxWidth, maxHeight);
698 // @private creates the elipsis for the text.
699 elipsis: function(sprite, text, desiredWidth, minWidth, center) {
703 if (desiredWidth < minWidth) {
707 while (text.length > 4) {
708 text = text.substr(0, text.length - 4) + "...";
709 sprite.setAttributes({
712 bbox = sprite.getBBox();
713 if (bbox.width < desiredWidth) {
714 if (typeof center == 'number') {
715 sprite.setAttributes({
716 x: Math.floor(center - (bbox.width / 2))
725 <span id='Ext-chart-axis-Axis-method-setTitle'> /**
726 </span> * Updates the {@link #title} of this axis.
727 * @param {String} title
729 setTitle: function(title) {
734 // @private draws the title for the axis.
735 drawTitle: function(maxWidth, maxHeight) {
737 position = me.position,
738 surface = me.chart.surface,
739 displaySprite = me.displaySprite,
741 rotate = (position == 'left' || position == 'right'),
747 displaySprite.setAttributes({text: title}, true);
755 displaySprite = me.displaySprite = surface.add(Ext.apply(base, me.axisTitleStyle, me.labelTitle));
756 surface.renderItem(displaySprite);
758 bbox = displaySprite.getBBox();
759 pad = me.dashSize + me.label.padding;
762 y -= ((me.length / 2) - (bbox.height / 2));
763 if (position == 'left') {
764 x -= (maxWidth + pad + (bbox.width / 2));
767 x += (maxWidth + pad + bbox.width - (bbox.width / 2));
769 me.bbox.width += bbox.width + 10;
772 x += (me.length / 2) - (bbox.width * 0.5);
773 if (position == 'top') {
774 y -= (maxHeight + pad + (bbox.height * 0.3));
777 y += (maxHeight + pad + (bbox.height * 0.8));
779 me.bbox.height += bbox.height + 10;
781 displaySprite.setAttributes({