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-series-Scatter'>/**
19 </span> * @class Ext.chart.series.Scatter
20 * @extends Ext.chart.series.Cartesian
22 * Creates a Scatter Chart. The scatter plot is useful when trying to display more than two variables in the same visualization.
23 * These variables can be mapped into x, y coordinates and also to an element's radius/size, color, etc.
24 * As with all other series, the Scatter Series must be appended in the *series* Chart array configuration. See the Chart
25 * documentation for more information on creating charts. A typical configuration object for the scatter could be:
27 * {@img Ext.chart.series.Scatter/Ext.chart.series.Scatter.png Ext.chart.series.Scatter chart series}
29 * var store = Ext.create('Ext.data.JsonStore', {
30 * fields: ['name', 'data1', 'data2', 'data3', 'data4', 'data5'],
32 * {'name':'metric one', 'data1':10, 'data2':12, 'data3':14, 'data4':8, 'data5':13},
33 * {'name':'metric two', 'data1':7, 'data2':8, 'data3':16, 'data4':10, 'data5':3},
34 * {'name':'metric three', 'data1':5, 'data2':2, 'data3':14, 'data4':12, 'data5':7},
35 * {'name':'metric four', 'data1':2, 'data2':14, 'data3':6, 'data4':1, 'data5':23},
36 * {'name':'metric five', 'data1':27, 'data2':38, 'data3':36, 'data4':13, 'data5':33}
40 * Ext.create('Ext.chart.Chart', {
41 * renderTo: Ext.getBody(),
50 * fields: ['data1', 'data2', 'data3'],
51 * title: 'Sample Values',
58 * title: 'Sample Metrics'
81 * In this configuration we add three different categories of scatter series. Each of them is bound to a different field of the same data store,
82 * `data1`, `data2` and `data3` respectively. All x-fields for the series must be the same field, in this case `name`.
83 * Each scatter series has a different styling configuration for markers, specified by the `markerConfig` object. Finally we set the left axis as
84 * axis to show the current values of the elements.
88 Ext.define('Ext.chart.series.Scatter', {
90 /* Begin Definitions */
92 extend: 'Ext.chart.series.Cartesian',
94 requires: ['Ext.chart.axis.Axis', 'Ext.chart.Shape', 'Ext.fx.Anim'],
99 alias: 'series.scatter',
101 <span id='Ext-chart-series-Scatter-cfg-markerConfig'> /**
102 </span> * @cfg {Object} markerConfig
103 * The display style for the scatter series markers.
106 <span id='Ext-chart-series-Scatter-cfg-style'> /**
107 </span> * @cfg {Object} style
108 * Append styling properties to this object for it to override theme properties.
111 constructor: function(config) {
112 this.callParent(arguments);
114 shadow = me.chart.shadow,
115 surface = me.chart.surface, i, l;
116 Ext.apply(me, config, {
120 "stroke-width": 6,
121 "stroke-opacity": 0.05,
122 stroke: 'rgb(0, 0, 0)'
124 "stroke-width": 4,
125 "stroke-opacity": 0.1,
126 stroke: 'rgb(0, 0, 0)'
128 "stroke-width": 2,
129 "stroke-opacity": 0.15,
130 stroke: 'rgb(0, 0, 0)'
133 me.group = surface.getGroup(me.seriesId);
135 for (i = 0, l = me.shadowAttributes.length; i < l; i++) {
136 me.shadowGroups.push(surface.getGroup(me.seriesId + '-shadows' + i));
141 // @private Get chart and data boundaries
142 getBounds: function() {
145 store = chart.substore || chart.store,
146 axes = [].concat(me.axis),
147 bbox, xScale, yScale, ln, minX, minY, maxX, maxY, i, axis, ends;
152 for (i = 0, ln = axes.length; i < ln; i++) {
153 axis = chart.axes.get(axes[i]);
155 ends = axis.calcEnds();
156 if (axis.position == 'top' || axis.position == 'bottom') {
166 // If a field was specified without a corresponding axis, create one to get bounds
167 if (me.xField && !Ext.isNumber(minX)) {
168 axis = Ext.create('Ext.chart.axis.Axis', {
170 fields: [].concat(me.xField)
175 if (me.yField && !Ext.isNumber(minY)) {
176 axis = Ext.create('Ext.chart.axis.Axis', {
178 fields: [].concat(me.yField)
186 maxX = store.getCount() - 1;
187 xScale = bbox.width / (store.getCount() - 1);
190 xScale = bbox.width / (maxX - minX);
195 maxY = store.getCount() - 1;
196 yScale = bbox.height / (store.getCount() - 1);
199 yScale = bbox.height / (maxY - minY);
211 // @private Build an array of paths for the chart
212 getPaths: function() {
215 enableShadows = chart.shadow,
216 store = chart.substore || chart.store,
218 bounds = me.bounds = me.getBounds(),
220 xScale = bounds.xScale,
221 yScale = bounds.yScale,
226 boxHeight = bbox.height,
227 items = me.items = [],
229 x, y, xValue, yValue, sprite;
231 store.each(function(record, i) {
232 xValue = record.get(me.xField);
233 yValue = record.get(me.yField);
234 //skip undefined values
235 if (typeof yValue == 'undefined' || (typeof yValue == 'string' && !yValue)) {
237 if (Ext.isDefined(Ext.global.console)) {
238 Ext.global.console.warn("[Ext.chart.series.Scatter] Skipping a store element with an undefined value at ", record, xValue, yValue);
244 if (typeof xValue == 'string' || typeof xValue == 'object') {
247 if (typeof yValue == 'string' || typeof yValue == 'object') {
250 x = boxX + (xValue - minX) * xScale;
251 y = boxY + boxHeight - (yValue - minY) * yScale;
259 value: [xValue, yValue],
264 // When resizing, reset before animating
265 if (chart.animate && chart.resizing) {
266 sprite = group.getAt(i);
268 me.resetPoint(sprite);
270 me.resetShadow(sprite);
278 // @private translate point to the center
279 resetPoint: function(sprite) {
280 var bbox = this.bbox;
281 sprite.setAttributes({
283 x: (bbox.x + bbox.width) / 2,
284 y: (bbox.y + bbox.height) / 2
289 // @private translate shadows of a sprite to the center
290 resetShadow: function(sprite) {
292 shadows = sprite.shadows,
293 shadowAttributes = me.shadowAttributes,
294 ln = me.shadowGroups.length,
297 for (i = 0; i < ln; i++) {
298 attr = Ext.apply({}, shadowAttributes[i]);
299 if (attr.translate) {
300 attr.translate.x += (bbox.x + bbox.width) / 2;
301 attr.translate.y += (bbox.y + bbox.height) / 2;
305 x: (bbox.x + bbox.width) / 2,
306 y: (bbox.y + bbox.height) / 2
309 shadows[i].setAttributes(attr, true);
313 // @private create a new point
314 createPoint: function(attr, type) {
320 return Ext.chart.Shape[type](chart.surface, Ext.apply({}, {
325 x: (bbox.x + bbox.width) / 2,
326 y: (bbox.y + bbox.height) / 2
331 // @private create a new set of shadows for a sprite
332 createShadow: function(sprite, endMarkerStyle, type) {
335 shadowGroups = me.shadowGroups,
336 shadowAttributes = me.shadowAttributes,
337 lnsh = shadowGroups.length,
339 i, shadow, shadows, attr;
341 sprite.shadows = shadows = [];
343 for (i = 0; i < lnsh; i++) {
344 attr = Ext.apply({}, shadowAttributes[i]);
345 if (attr.translate) {
346 attr.translate.x += (bbox.x + bbox.width) / 2;
347 attr.translate.y += (bbox.y + bbox.height) / 2;
352 x: (bbox.x + bbox.width) / 2,
353 y: (bbox.y + bbox.height) / 2
357 Ext.apply(attr, endMarkerStyle);
358 shadow = Ext.chart.Shape[type](chart.surface, Ext.apply({}, {
361 group: shadowGroups[i]
363 shadows.push(shadow);
367 <span id='Ext-chart-series-Scatter-method-drawSeries'> /**
368 </span> * Draws the series for the current chart.
370 drawSeries: function() {
373 store = chart.substore || chart.store,
375 enableShadows = chart.shadow,
376 shadowGroups = me.shadowGroups,
377 shadowAttributes = me.shadowAttributes,
378 lnsh = shadowGroups.length,
379 sprite, attrs, attr, ln, i, endMarkerStyle, shindex, type, shadows,
380 rendererAttributes, shadowAttribute;
382 endMarkerStyle = Ext.apply(me.markerStyle, me.markerConfig);
383 type = endMarkerStyle.type;
384 delete endMarkerStyle.type;
386 //if the store is empty then there's nothing to be rendered
387 if (!store || !store.getCount()) {
391 me.unHighlightItem();
392 me.cleanHighlights();
394 attrs = me.getPaths();
396 for (i = 0; i < ln; i++) {
398 sprite = group.getAt(i);
399 Ext.apply(attr, endMarkerStyle);
401 // Create a new sprite if needed (no height)
403 sprite = me.createPoint(attr, type);
405 me.createShadow(sprite, endMarkerStyle, type);
409 shadows = sprite.shadows;
411 rendererAttributes = me.renderer(sprite, store.getAt(i), { translate: attr }, i, store);
412 sprite._to = rendererAttributes;
413 me.onAnimate(sprite, {
414 to: rendererAttributes
417 for (shindex = 0; shindex < lnsh; shindex++) {
418 shadowAttribute = Ext.apply({}, shadowAttributes[shindex]);
419 rendererAttributes = me.renderer(shadows[shindex], store.getAt(i), Ext.apply({}, {
421 x: attr.x + (shadowAttribute.translate? shadowAttribute.translate.x : 0),
422 y: attr.y + (shadowAttribute.translate? shadowAttribute.translate.y : 0)
424 }, shadowAttribute), i, store);
425 me.onAnimate(shadows[shindex], { to: rendererAttributes });
429 rendererAttributes = me.renderer(sprite, store.getAt(i), Ext.apply({ translate: attr }, { hidden: false }), i, store);
430 sprite.setAttributes(rendererAttributes, true);
432 for (shindex = 0; shindex < lnsh; shindex++) {
433 shadowAttribute = shadowAttributes[shindex];
434 rendererAttributes = me.renderer(shadows[shindex], store.getAt(i), Ext.apply({
437 }, shadowAttribute), i, store);
438 shadows[shindex].setAttributes(rendererAttributes, true);
441 me.items[i].sprite = sprite;
444 // Hide unused sprites
445 ln = group.getCount();
446 for (i = attrs.length; i < ln; i++) {
447 group.getAt(i).hide(true);
453 // @private callback for when creating a label sprite.
454 onCreateLabel: function(storeItem, item, i, display) {
456 group = me.labelsGroup,
458 endLabelStyle = Ext.apply({}, config, me.seriesLabelStyle),
461 return me.chart.surface.add(Ext.apply({
465 y: bbox.y + bbox.height / 2
469 // @private callback for when placing a label sprite.
470 onPlaceLabel: function(label, storeItem, item, i, display, animate) {
473 resizing = chart.resizing,
475 format = config.renderer,
476 field = config.field,
480 radius = item.sprite.attr.radius,
481 bb, width, height, anim;
483 label.setAttributes({
484 text: format(storeItem.get(field)),
488 if (display == 'rotate') {
489 label.setAttributes({
490 'text-anchor': 'start',
497 //correct label position to fit into the box
498 bb = label.getBBox();
501 x = x < bbox.x? bbox.x : x;
502 x = (x + width > bbox.x + bbox.width)? (x - (x + width - bbox.x - bbox.width)) : x;
503 y = (y - height < bbox.y)? bbox.y + height : y;
505 } else if (display == 'under' || display == 'over') {
506 //TODO(nicolas): find out why width/height values in circle bounding boxes are undefined.
507 bb = item.sprite.getBBox();
508 bb.width = bb.width || (radius * 2);
509 bb.height = bb.height || (radius * 2);
510 y = y + (display == 'over'? -bb.height : bb.height);
511 //correct label position to fit into the box
512 bb = label.getBBox();
514 height = bb.height/2;
515 x = x - width < bbox.x ? bbox.x + width : x;
516 x = (x + width > bbox.x + bbox.width) ? (x - (x + width - bbox.x - bbox.width)) : x;
517 y = y - height < bbox.y? bbox.y + height : y;
518 y = (y + height > bbox.y + bbox.height) ? (y - (y + height - bbox.y - bbox.height)) : y;
521 if (!chart.animate) {
522 label.setAttributes({
530 anim = item.sprite.getActiveAnimation();
532 anim.on('afteranimate', function() {
533 label.setAttributes({
545 me.onAnimate(label, {
555 // @private callback for when placing a callout sprite.
556 onPlaceCallout: function(callout, storeItem, item, i, display, animate, index) {
559 surface = chart.surface,
560 resizing = chart.resizing,
561 config = me.callouts,
565 bbox = callout.label.getBBox(),
569 boxx, boxy, boxw, boxh,
570 p, clipRect = me.bbox,
574 normal = [Math.cos(Math.PI /4), -Math.sin(Math.PI /4)];
575 x = cur[0] + normal[0] * offsetFromViz;
576 y = cur[1] + normal[1] * offsetFromViz;
578 //box position and dimensions
579 boxx = x + (normal[0] > 0? 0 : -(bbox.width + 2 * offsetBox));
580 boxy = y - bbox.height /2 - offsetBox;
581 boxw = bbox.width + 2 * offsetBox;
582 boxh = bbox.height + 2 * offsetBox;
584 //now check if we're out of bounds and invert the normal vector correspondingly
585 //this may add new overlaps between labels (but labels won't be out of bounds).
586 if (boxx < clipRect[0] || (boxx + boxw) > (clipRect[0] + clipRect[2])) {
589 if (boxy < clipRect[1] || (boxy + boxh) > (clipRect[1] + clipRect[3])) {
594 x = cur[0] + normal[0] * offsetFromViz;
595 y = cur[1] + normal[1] * offsetFromViz;
597 //update box position and dimensions
598 boxx = x + (normal[0] > 0? 0 : -(bbox.width + 2 * offsetBox));
599 boxy = y - bbox.height /2 - offsetBox;
600 boxw = bbox.width + 2 * offsetBox;
601 boxh = bbox.height + 2 * offsetBox;
604 //set the line from the middle of the pie to the box.
605 me.onAnimate(callout.lines, {
607 path: ["M", cur[0], cur[1], "L", x, y, "Z"]
611 me.onAnimate(callout.box, {
620 me.onAnimate(callout.label, {
622 x: x + (normal[0] > 0? offsetBox : -(bbox.width + offsetBox)),
627 //set the line from the middle of the pie to the box.
628 callout.lines.setAttributes({
629 path: ["M", cur[0], cur[1], "L", x, y, "Z"]
632 callout.box.setAttributes({
639 callout.label.setAttributes({
640 x: x + (normal[0] > 0? offsetBox : -(bbox.width + offsetBox)),
645 callout[p].show(true);
649 // @private handles sprite animation for the series.
650 onAnimate: function(sprite, attr) {
652 return this.callParent(arguments);
655 isItemInPoint: function(x, y, item) {
660 function dist(point) {
661 var dx = abs(point[0] - x),
662 dy = abs(point[1] - y);
663 return Math.sqrt(dx * dx + dy * dy);
666 return (point[0] - tolerance <= x && point[0] + tolerance >= x &&
667 point[1] - tolerance <= y && point[1] + tolerance >= y);