// version: 2014-08-16 /** * o--------------------------------------------------------------------------------o * | This file is part of the RGraph package - you can learn more at: | * | | * | http://www.rgraph.net | * | | * | This package is licensed under the Creative Commons BY-NC license. That means | * | that for non-commercial purposes it's free to use and for business use there's | * | a 99 GBP per-company fee to pay. You can read the full license here: | * | | * | http://www.rgraph.net/license | * o--------------------------------------------------------------------------------o */ /** * Initialise the various objects */ RGraph = window.RGraph || {isRGraph: true}; // Module pattern (function (win, doc, undefined) { var RG = RGraph, ua = navigator.userAgent, ma = Math; /** * This is the window click event listener. It redraws all canvas tags on the page. */ RGraph.installWindowMousedownListener = RGraph.InstallWindowMousedownListener = function (obj) { if (!RGraph.window_mousedown_event_listener) { RGraph.window_mousedown_event_listener = function (e) { /** * For firefox add the window.event object */ if (navigator.userAgent.indexOf('Firefox') >= 0) win.event = e; e = RGraph.FixEventObject(e); if (RGraph.HideTooltip && RGraph.Registry.Get('chart.tooltip')) { RGraph.clear(RGraph.Registry.Get('chart.tooltip').__canvas__); RGraph.redraw(); RGraph.hideTooltip(); } }; win.addEventListener('mousedown', RGraph.window_mousedown_event_listener, false); } }; /** * This is the window click event listener. It redraws all canvas tags on the page. */ RGraph.installWindowMouseupListener = RGraph.InstallWindowMouseupListener = function (obj) { if (!RGraph.window_mouseup_event_listener) { RGraph.window_mouseup_event_listener = function (e) { /** * For firefox add the window.event object */ if (navigator.userAgent.indexOf('Firefox') >= 0) win.event = e; e = RGraph.FixEventObject(e); /** * Stop any annotating that may be going on */ if (RGraph.Annotating_window_onmouseup) { RGraph.Annotating_window_onmouseup(e); return; } /** * End adjusting */ if (RGraph.Registry.Get('chart.adjusting') || RGraph.Registry.Get('chart.adjusting.gantt')) { var obj = RGraph.Registry.Get('chart.adjusting'); // If it's a line chart update the data_arr variable if (obj && obj.type === 'line') { obj.data_arr = RGraph.arrayLinearize(obj.data); } RGraph.FireCustomEvent(RGraph.Registry.Get('chart.adjusting'), 'onadjustend'); } RGraph.Registry.Set('chart.adjusting', null); RGraph.Registry.Set('chart.adjusting.shape', null); RGraph.Registry.Set('chart.adjusting.gantt', null); // ============================================== // Finally, redraw the chart // ============================================== var tags = document.getElementsByTagName('canvas'); for (var i=0; i= 0) window.event = e; e = RGraph.FixEventObject(e); // ************************************************************************* // Tooltips // ************************************************************************* // This causes things at the edge of the chart area - eg line chart hotspots - not to fire because the // cursor is out of the chart area var objects = RGraph.ObjectRegistry.getObjectsByXY(e); //var objects = RGraph.ObjectRegistry.getObjectsByCanvasID(e.target.id); if (objects) { for (var i=0,len=objects.length; i= 0) window.event = e; e = RGraph.FixEventObject(e); /** * Go through all the objects and check them to see if anything needs doing */ var objects = RGraph.ObjectRegistry.getObjectsByXY(e); //var objects = RGraph.ObjectRegistry.getObjectsByCanvasID(e.target.id); if (objects && objects.length > 0) { for (var i=0,len=objects.length; i= 0) window.event = e; e = RGraph.FixEventObject(e); /** * Annotating */ if (e.target.__object__ && e.target.__object__.Get('chart.annotatable') && RGraph.Annotating_canvas_onmousedown) { RGraph.Annotating_canvas_onmousedown(e); return; } var obj = RGraph.ObjectRegistry.getObjectByXY(e); if (obj) { var id = obj.id; /************************************************************* * Handle adjusting for all object types *************************************************************/ if (obj && obj.isRGraph && obj.Get('chart.adjustable')) { /** * Check the cursor is in the correct area */ var obj = RGraph.ObjectRegistry.getObjectByXY(e); if (obj && obj.isRGraph) { // If applicable, get the appropriate shape and store it in the registry switch (obj.type) { case 'bar': var shape = obj.getShapeByX(e); break; case 'gantt': var shape = obj.getShape(e); if (shape) { var mouseXY = RGraph.getMouseXY(e); RGraph.Registry.Set('chart.adjusting.gantt', { 'index': shape['index'], 'object': obj, 'mousex': mouseXY[0], 'mousey': mouseXY[1], 'event_start': obj.data[shape['index']][0], 'event_duration': obj.data[shape['index']][1], 'mode': (mouseXY[0] > (shape['x'] + shape['width'] - 5) ? 'resize' : 'move'), 'shape': shape }); } break; case 'line': var shape = obj.getShape(e); break; default: var shape = null; } RGraph.Registry.Set('chart.adjusting.shape', shape); // Fire the onadjustbegin event RGraph.FireCustomEvent(obj, 'onadjustbegin'); RGraph.Registry.Set('chart.adjusting', obj); // Liberally redraw the canvas RGraph.Clear(obj.canvas); RGraph.Redraw(); // Call the mousemove event listener so that the canvas is adjusted even though the mouse isn't moved obj.canvas.rgraph_mousemove_event_listener(e); } } RGraph.Clear(obj.canvas); RGraph.Redraw(); } }; obj.canvas.addEventListener('mousedown', obj.canvas.rgraph_mousedown_event_listener, false); } }; /** * This is the canvas click event listener. Used by the pseudo event listener * * @param object obj The chart object */ RGraph.installCanvasClickListener = RGraph.InstallCanvasClickListener = function (obj) { if (!obj.canvas.rgraph_click_event_listener) { obj.canvas.rgraph_click_event_listener = function (e) { /** * For firefox add the window.event object */ if (navigator.userAgent.indexOf('Firefox') >= 0) window.event = e; e = RGraph.FixEventObject(e); var objects = RGraph.ObjectRegistry.getObjectsByXY(e); for (var i=0,len=objects.length; i obj.coords.key[j][0] && mouseX < (obj.coords.key[j][0] + obj.coords.key[j][2]) && mouseY > obj.coords.key[j][1] && mouseY < (obj.coords.key[j][1] + obj.coords.key[j][3])) { var pointer = true; } } } } /** * It can be specified in the user mousemove event - remember it can now be specified in THREE ways */ if (!RGraph.is_null(shape) && !RGraph.is_null(obj)) { if (!RGraph.is_null(obj.Get('chart.events.mousemove')) && typeof(obj.Get('chart.events.mousemove')) == 'function') { var str = (obj.Get('chart.events.mousemove')).toString(); if (str.match(/pointer/) && str.match(/cursor/) && str.match(/style/)) { var pointer = true; } } if (!RGraph.is_null(obj.onmousemove) && typeof(obj.onmousemove) == 'function') { var str = (obj.onmousemove).toString(); if (str.match(/pointer/) && str.match(/cursor/) && str.match(/style/)) { var pointer = true; } } var index = shape['object'].type == 'scatter' ? shape['index_adjusted'] : shape['index']; if (!RGraph.is_null(obj['$' + index]) && typeof(obj['$' + index].onmousemove) == 'function') { var str = (obj['$' + index].onmousemove).toString(); if (str.match(/pointer/) && str.match(/cursor/) && str.match(/style/)) { var pointer = true; } } } /** * Is the chart resizable? Go through all the objects again */ var objects = RGraph.ObjectRegistry.objects.byCanvasID; for (var i=0,len=objects.length; i (e.target.width - 32) && mouseY > (e.target.height - 16)) { pointer = true; } if (pointer) { e.target.style.cursor = 'pointer'; } else if (e.target.style.cursor == 'pointer') { e.target.style.cursor = 'default'; } else { e.target.style.cursor = null; } // ========================================================================= // Resize cursor - check mouseis in bottom left corner and if it is change it // ========================================================================= if (resizable && mouseX >= (e.target.width - 15) && mouseY >= (e.target.height - 15)) { e.target.style.cursor = 'move'; } else if (e.target.style.cursor === 'move') { e.target.style.cursor = 'default'; } // ========================================================================= // Interactive key // ========================================================================= if (typeof mouse_over_key == 'boolean' && mouse_over_key) { e.target.style.cursor = 'pointer'; } // ========================================================================= // Gantt chart adjusting // ========================================================================= //if (obj && obj.type == 'gantt' && obj.Get('chart.adjustable')) { // if (obj.getShape && obj.getShape(e)) { // e.target.style.cursor = 'ew-resize'; // } else { // e.target.style.cursor = 'default'; // } //} else if (!obj || !obj.type) { // e.target.style.cursor = cursor; //} // ========================================================================= // Line chart adjusting // ========================================================================= if (obj && obj.type == 'line' && obj.Get('chart.adjustable')) { if (obj.getShape && obj.getShape(e)) { e.target.style.cursor = 'ns-resize'; } else { e.target.style.cursor = 'default'; } } // ========================================================================= // Annotatable // ========================================================================= if (e.target.__object__ && e.target.__object__.Get('chart.annotatable')) { e.target.style.cursor = 'crosshair'; } // ========================================================================= // Drawing API link // ========================================================================= if (obj && obj.type === 'drawing.text' && shape && typeof obj.Get('link') === 'string') { e.target.style.cursor = 'pointer'; } }; /** * This function handles the tooltip text being a string, function * * @param mixed tooltip This could be a string or a function. If it's a function it's called and * the return value is used as the tooltip text * @param numbr idx The index of the tooltip. */ RGraph.parseTooltipText = function (tooltips, idx) { // No tooltips if (!tooltips) { return null; } // Get the tooltip text if (typeof tooltips == 'function') { var text = tooltips(idx); // A single tooltip. Only supported by the Scatter chart } else if (typeof tooltips == 'string') { var text = tooltips; } else if (typeof tooltips == 'object' && typeof tooltips[idx] == 'function') { var text = tooltips[idx](idx); } else if (typeof tooltips[idx] == 'string' && tooltips[idx]) { var text = tooltips[idx]; } else { var text = ''; } if (text == 'undefined') { text = ''; } else if (text == 'null') { text = ''; } // Conditional in case the tooltip file isn't included return RGraph.getTooltipTextFromDIV ? RGraph.getTooltipTextFromDIV(text) : text; }; /** * Draw crosshairs if enabled * * @param object obj The graph object (from which we can get the context and canvas as required) */ RGraph.drawCrosshairs = RGraph.DrawCrosshairs = function (e, obj) { var e = RGraph.FixEventObject(e); var width = obj.canvas.width; var height = obj.canvas.height; var mouseXY = RGraph.getMouseXY(e); var x = mouseXY[0]; var y = mouseXY[1]; var gutterLeft = obj.gutterLeft; var gutterRight = obj.gutterRight; var gutterTop = obj.gutterTop; var gutterBottom = obj.gutterBottom; var Mathround = Math.round; var prop = obj.properties; var co = obj.context; var ca = obj.canvas; RGraph.RedrawCanvas(ca); if ( x >= gutterLeft && y >= gutterTop && x <= (width - gutterRight) && y <= (height - gutterBottom) ) { var linewidth = prop['chart.crosshairs.linewidth'] ? prop['chart.crosshairs.linewidth'] : 1; co.lineWidth = linewidth ? linewidth : 1; co.beginPath(); co.strokeStyle = prop['chart.crosshairs.color']; /** * The chart.crosshairs.snap option */ if (prop['chart.crosshairs.snap']) { // Linear search for the closest point var point = null; var dist = null; var len = null; if (obj.type == 'line') { for (var i=0; i ' + xCoord + '
' + prop['chart.crosshairs.coords.labels.y'] + ': ' + yCoord; obj.canvas.addEventListener('mouseout', RGraph.HideCrosshairCoords, false); ca.__crosshairs_labels__ = div; ca.__crosshairs_x__ = xCoord; ca.__crosshairs_y__ = yCoord; } else if (prop['chart.crosshairs.coords']) { alert('[RGRAPH] Showing crosshair coordinates is only supported on the Scatter chart'); } /** * Fire the oncrosshairs custom event */ RGraph.FireCustomEvent(obj, 'oncrosshairs'); } else { RGraph.HideCrosshairCoords(); } }; // End module pattern })(window, document);