Permalink
Browse files

Merge remote branch 'origin/master'

  • Loading branch information...
2 parents 54537ff + 72e512b commit 416fef5d477f1908dde0954d24f12e2ec79a7e89 highslide-software committed Aug 5, 2011
Showing with 224 additions and 61 deletions.
  1. +63 −23 js/highcharts.src.js
  2. +7 −22 js/parts/Chart.js
  3. +1 −1 js/parts/Series.js
  4. +55 −0 js/parts/Utilities.js
  5. +98 −15 test/unit/UtilitiesTest.js
View
@@ -477,6 +477,61 @@ ChartCounters.prototype = {
};
/**
+ * Utility method extracted from Tooltip code that places a tooltip in a chart without spilling over
+ * and not covering the point it self.
+ */
+function placeBox(boxWidth, boxHeight, outerLeft, outerTop, outerWidth, outerHeight, point) {
+ // keep the box within the chart area
+ var x = point.x - boxWidth + outerLeft - 25,
+ y = point.y - boxHeight + outerTop + 10;
+
+ // it is too far to the left, adjust it
+ if (x < 7) {
+ x = outerLeft + point.x + 15;
+ }
+
+ // Test to see if the tooltip is to far to the right,
+ // if it is, move it back to be inside and then up to not cover the point.
+ if ((x + boxWidth) > (outerLeft + outerWidth)) {
+ x -= (x + boxWidth) - (outerLeft + outerWidth);
+ y -= boxHeight;
+ }
+
+ if (y < 5) {
+ y = 5; // above
+ } else if (y + boxHeight > outerHeight) {
+ y = outerHeight - boxHeight - 5; // below
+ y = outerHeight - boxHeight - 5; // below
+ }
+
+ return {x: x, y: y};
+}
+
+/**
+ * Utility method that sorts an object array and keeping the order of equal items.
+ * ECMA script standard does not specify the behaviour when items are equal.
+ */
+function stableSort(arr, sortFunction) {
+ var length = arr.length,
+ i;
+
+ // Add index to each item
+ for (i = 0; i < length; i++) {
+ arr[i].ss_i = i; // stable sort index
+ }
+
+ arr.sort(function (a, b) {
+ var sortValue = sortFunction(a, b);
+ return sortValue === 0 ? a.ss_i - b.ss_i : sortValue;
+ });
+
+ // Remove index from items
+ for (i = 0; i < length; i++) {
+ delete arr[i].ss_i; // stable sort index
+ }
+}
+
+/**
* Set the global animation to either a given value, or fall back to the
* given chart's animation option
* @param {Object} animation
@@ -5912,7 +5967,8 @@ function Chart(options, callback) {
pointConfig = [],
tooltipPos = point.tooltipPos,
formatter = options.formatter || defaultFormatter,
- hoverPoints = chart.hoverPoints;
+ hoverPoints = chart.hoverPoints,
+ placedTooltipPoint;
// shared tooltip, array is sent over
if (shared) {
@@ -5993,27 +6049,11 @@ function Chart(options, callback) {
height: boxHeight,
stroke: options.borderColor || point.color || currentSeries.color || '#606060'
});
-
- // keep the box within the chart area
- boxX = x - boxWidth + plotLeft - 25;
- boxY = y - boxHeight + plotTop + 10;
-
- // it is too far to the left, adjust it
- if (boxX < 7) {
- boxX = plotLeft + x + 15;
- }
-
-
- if (boxY < 5) {
- boxY = 5; // above
- } else if (boxY + boxHeight > chartHeight) {
- boxY = chartHeight - boxHeight - 5; // below
- }
-
+
+ placedTooltipPoint = placeBox(boxWidth, boxHeight, plotLeft, plotTop, plotWidth, plotHeight, {x: x, y: y});
+
// do the move
- move(mathRound(boxX - boxOffLeft), mathRound(boxY - boxOffLeft));
-
-
+ move(mathRound(placedTooltipPoint.x - boxOffLeft), mathRound(placedTooltipPoint.y - boxOffLeft));
}
@@ -6939,7 +6979,7 @@ function Chart(options, callback) {
});
// sort by legendIndex
- allItems.sort(function (a, b) {
+ stableSort(allItems, function (a, b) {
return (a.options.legendIndex || 0) - (b.options.legendIndex || 0);
});
@@ -8649,7 +8689,7 @@ Series.prototype = {
i;
// sort the data points
- data.sort(function (a, b) {
+ stableSort(data, function (a, b) {
return (a.x - b.x);
});
View
@@ -2010,7 +2010,8 @@ function Chart(options, callback) {
pointConfig = [],
tooltipPos = point.tooltipPos,
formatter = options.formatter || defaultFormatter,
- hoverPoints = chart.hoverPoints;
+ hoverPoints = chart.hoverPoints,
+ placedTooltipPoint;
// shared tooltip, array is sent over
if (shared) {
@@ -2091,27 +2092,11 @@ function Chart(options, callback) {
height: boxHeight,
stroke: options.borderColor || point.color || currentSeries.color || '#606060'
});
-
- // keep the box within the chart area
- boxX = x - boxWidth + plotLeft - 25;
- boxY = y - boxHeight + plotTop + 10;
-
- // it is too far to the left, adjust it
- if (boxX < 7) {
- boxX = plotLeft + x + 15;
- }
-
-
- if (boxY < 5) {
- boxY = 5; // above
- } else if (boxY + boxHeight > chartHeight) {
- boxY = chartHeight - boxHeight - 5; // below
- }
-
+
+ placedTooltipPoint = placeBox(boxWidth, boxHeight, plotLeft, plotTop, plotWidth, plotHeight, {x: x, y: y});
+
// do the move
- move(mathRound(boxX - boxOffLeft), mathRound(boxY - boxOffLeft));
-
-
+ move(mathRound(placedTooltipPoint.x - boxOffLeft), mathRound(placedTooltipPoint.y - boxOffLeft));
}
@@ -3037,7 +3022,7 @@ function Chart(options, callback) {
});
// sort by legendIndex
- allItems.sort(function (a, b) {
+ stableSort(allItems, function (a, b) {
return (a.options.legendIndex || 0) - (b.options.legendIndex || 0);
});
View
@@ -475,7 +475,7 @@ Series.prototype = {
i;
// sort the data points
- data.sort(function (a, b) {
+ stableSort(data, function (a, b) {
return (a.x - b.x);
});
View
@@ -360,3 +360,58 @@ ChartCounters.prototype = {
}
}
};
+
+/**
+ * Utility method extracted from Tooltip code that places a tooltip in a chart without spilling over
+ * and not covering the point it self.
+ */
+function placeBox(boxWidth, boxHeight, outerLeft, outerTop, outerWidth, outerHeight, point) {
+ // keep the box within the chart area
+ var x = point.x - boxWidth + outerLeft - 25,
+ y = point.y - boxHeight + outerTop + 10;
+
+ // it is too far to the left, adjust it
+ if (x < 7) {
+ x = outerLeft + point.x + 15;
+ }
+
+ // Test to see if the tooltip is to far to the right,
+ // if it is, move it back to be inside and then up to not cover the point.
+ if ((x + boxWidth) > (outerLeft + outerWidth)) {
+ x -= (x + boxWidth) - (outerLeft + outerWidth);
+ y -= boxHeight;
+ }
+
+ if (y < 5) {
+ y = 5; // above
+ } else if (y + boxHeight > outerHeight) {
+ y = outerHeight - boxHeight - 5; // below
+ y = outerHeight - boxHeight - 5; // below
+ }
+
+ return {x: x, y: y};
+}
+
+/**
+ * Utility method that sorts an object array and keeping the order of equal items.
+ * ECMA script standard does not specify the behaviour when items are equal.
+ */
+function stableSort(arr, sortFunction) {
+ var length = arr.length,
+ i;
+
+ // Add index to each item
+ for (i = 0; i < length; i++) {
+ arr[i].ss_i = i; // stable sort index
+ }
+
+ arr.sort(function (a, b) {
+ var sortValue = sortFunction(a, b);
+ return sortValue === 0 ? a.ss_i - b.ss_i : sortValue;
+ });
+
+ // Remove index from items
+ for (i = 0; i < length; i++) {
+ delete arr[i].ss_i; // stable sort index
+ }
+}
Oops, something went wrong.

0 comments on commit 416fef5

Please sign in to comment.