2
* @license Highstock JS v1.3.10 (2014-03-10)
5
* @author Michael Nelson, Torstein Honsi.
7
* Feel free to use and modify this script.
8
* Highcharts license: www.highcharts.com/license.
12
/*global Effect, Class, Event, Element, $, $$, $A */
14
// Adapter interface between prototype and the Highcharts charting library
15
var HighchartsAdapter = (function () {
17
var hasEffect = typeof Effect !== 'undefined';
22
* Initialize the adapter. This is run once as Highcharts is first run.
23
* @param {Object} pathAnim The helper object to do animations across adapters.
25
init: function (pathAnim) {
28
* Animation for Highcharts SVG element wrappers only
29
* @param {Object} element
30
* @param {Object} attribute
32
* @param {Object} options
34
Effect.HighchartsTransition = Class.create(Effect.Base, {
35
initialize: function (element, attr, to, options) {
39
this.element = element;
41
from = element.attr ? element.attr(attr) : $(element).getStyle(attr);
43
// special treatment for paths
45
this.paths = pathAnim.init(
53
// fake values in order to read relative position as a float in update
58
opts = Object.extend((options || {}), {
66
HighchartsAdapter._extend(this.element);
67
// If this is the first animation on this object, create the _highcharts_animation helper that
68
// contain pointers to the animation objects.
69
if (!this.element._highchart_animation) {
70
this.element._highchart_animation = {};
73
// Store a reference to this animation instance.
74
this.element._highchart_animation[this.key] = this;
76
update: function (position) {
77
var paths = this.paths,
78
element = this.element,
82
position = pathAnim.step(paths[0], paths[1], position, this.toD);
85
if (element.attr) { // SVGElement
87
if (element.element) { // If not, it has been destroyed (#1405)
88
element.attr(this.options.attribute, position);
91
} else { // HTML, #409
93
obj[this.options.attribute] = position;
94
$(element).setStyle(obj);
99
// Delete the property that holds this animation now that it is finished.
100
// Both canceled animations and complete ones gets a 'finish' call.
101
if (this.element && this.element._highchart_animation) { // #1405
102
delete this.element._highchart_animation[this.key];
110
* Run a general method on the framework, following jQuery syntax
111
* @param {Object} el The HTML element
112
* @param {String} method Which method to run on the wrapped element
114
adapterRun: function (el, method) {
116
// This currently works for getting inner width and height. If adding
117
// more methods later, we need a conditional implementation for each.
118
return parseInt($(el).getStyle(method), 10);
123
* Downloads a script and executes a callback when done.
124
* @param {String} scriptLocation
125
* @param {Function} callback
127
getScript: function (scriptLocation, callback) {
128
var head = $$('head')[0]; // Returns an array, so pick the first element.
130
// Append a new 'script' element, set its type and src attributes, add a 'load' handler that calls the callback
131
head.appendChild(new Element('script', { type: 'text/javascript', src: scriptLocation}).observe('load', callback));
136
* Custom events in prototype needs to be namespaced. This method adds a namespace 'h:' in front of
137
* events that are not recognized as native.
139
addNS: function (eventName) {
140
var HTMLEvents = /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
141
MouseEvents = /^(?:click|mouse(?:down|up|over|move|out))$/;
142
return (HTMLEvents.test(eventName) || MouseEvents.test(eventName)) ?
147
// el needs an event to be attached. el is not necessarily a dom element
148
addEvent: function (el, event, fn) {
149
if (el.addEventListener || el.attachEvent) {
150
Event.observe($(el), HighchartsAdapter.addNS(event), fn);
153
HighchartsAdapter._extend(el);
154
el._highcharts_observe(event, fn);
158
// motion makes things pretty. use it if effects is loaded, if not... still get to the end result.
159
animate: function (el, params, options) {
164
options = options || {};
166
options.duration = (options.duration || 500) / 1000;
167
options.afterFinish = options.complete;
169
// animate wrappers and DOM elements
171
for (key in params) {
172
// The fx variable is seemingly thrown away here, but the Effect.setup will add itself to the _highcharts_animation object
173
// on the element itself so its not really lost.
174
fx = new Effect.HighchartsTransition($(el), key, params[key], options);
177
if (el.attr) { // #409 without effects
178
for (key in params) {
179
el.attr(key, params[key]);
182
if (options.complete) {
187
if (!el.attr) { // HTML element, #409
188
$(el).setStyle(params);
192
// this only occurs in higcharts 2.0+
193
stop: function (el) {
195
if (el._highcharts_extended && el._highchart_animation) {
196
for (key in el._highchart_animation) {
197
// Cancel the animation
198
// The 'finish' function in the Effect object will remove the reference
199
el._highchart_animation[key].cancel();
205
each: function (arr, fn) {
209
inArray: function (item, arr, from) {
210
return arr ? arr.indexOf(item, from) : -1;
214
* Get the cumulative offset relative to the top left of the page. This method, unlike its
215
* jQuery and MooTools counterpart, still suffers from issue #208 regarding the position
216
* of a chart within a fixed container.
218
offset: function (el) {
219
return $(el).cumulativeOffset();
222
// fire an event based on an event name (event) and an object (el).
223
// again, el may not be a dom element
224
fireEvent: function (el, event, eventArguments, defaultFunction) {
226
el.fire(HighchartsAdapter.addNS(event), eventArguments);
227
} else if (el._highcharts_extended) {
228
eventArguments = eventArguments || {};
229
el._highcharts_fire(event, eventArguments);
232
if (eventArguments && eventArguments.defaultPrevented) {
233
defaultFunction = null;
236
if (defaultFunction) {
237
defaultFunction(eventArguments);
241
removeEvent: function (el, event, handler) {
242
if ($(el).stopObserving) {
244
event = HighchartsAdapter.addNS(event);
246
$(el).stopObserving(event, handler);
247
} if (window === el) {
248
Event.stopObserving(el, event, handler);
250
HighchartsAdapter._extend(el);
251
el._highcharts_stop_observing(event, handler);
255
washMouseEvent: function (e) {
260
grep: function (arr, fn) {
261
return arr.findAll(fn);
265
map: function (arr, fn) {
269
// extend an object to handle highchart events (highchart objects, not svg elements).
270
// this is a very simple way of handling events but whatever, it works (i think)
271
_extend: function (object) {
272
if (!object._highcharts_extended) {
273
Object.extend(object, {
274
_highchart_events: {},
275
_highchart_animation: null,
276
_highcharts_extended: true,
277
_highcharts_observe: function (name, fn) {
278
this._highchart_events[name] = [this._highchart_events[name], fn].compact().flatten();
280
_highcharts_stop_observing: function (name, fn) {
283
this._highchart_events[name] = [this._highchart_events[name]].compact().flatten().without(fn);
285
delete this._highchart_events[name];
288
this._highchart_events = {};
291
_highcharts_fire: function (name, args) {
293
(this._highchart_events[name] || []).each(function (fn) {
294
// args is never null here
296
return; // "throw $break" wasn't working. i think because of the scope of 'this'.
299
// Attach a simple preventDefault function to skip default handler if called
300
args.preventDefault = function () {
301
args.defaultPrevented = true;
303
args.target = target;
305
// If the event handler return false, prevent the default handler from executing
306
if (fn.bind(this)(args) === false) {
307
args.preventDefault();