// 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 */ RGraph = window.RGraph || {isRGraph: true}; // Module pattern (function (win, doc, undefined) { var RG = RGraph, ua = navigator.userAgent, ma = Math; /** * This installs some event handlers * * Checking the RGraph.Annotate flag means the annotate code only runs once */ RG.Annotating_canvas_onmousedown = function (e) { if (e.button === 0) { e.target.__object__.Set('chart.mousedown', true); // Get the object from the canvas. Annotating must be enabled on the // last object defined var obj = e.target.__object__; // This starts the annotating "path" and set the color obj.context.beginPath(); obj.context.strokeStyle = obj.Get('chart.annotate.color'); obj.context.lineWidth = obj.Get('chart.annotate.linewidth'); var mouseXY = RG.getMouseXY(e); var mouseX = mouseXY[0]; var mouseY = mouseXY[1]; // Clear the annotation recording RG.Registry.Set('annotate.actions', [obj.Get('chart.annotate.color')]); // This sets the initial X/Y position obj.context.moveTo(mouseX, mouseY); RG.Registry.Set('annotate.last.coordinates', [mouseX,mouseY]); RG.Registry.Set('started.annotating', false); RG.Registry.Set('chart.annotating', obj); // Fire the onannotatebegin event. RG.FireCustomEvent(obj, 'onannotatebegin'); } return false; }; /** * This cancels annotating for ALL canvases */ RG.Annotating_window_onmouseup = function (e) { var obj = RG.Registry.Get('chart.annotating'); if (e.button != 0 || !obj) { return; } // This cancels annotating on ALL canvas tags on the page var tags = doc.getElementsByTagName('canvas'); for (var i=0; i 0 && win.localStorage) { var id = '__rgraph_annotations_' + e.target.id + '__'; var annotations = win.localStorage[id] ? win.localStorage[id] + '|' : ''; annotations += RG.Registry.Get('annotate.actions'); // Store the annotations information in HTML5 browser storage here win.localStorage[id] = annotations; } // Clear the recorded annotations RG.Registry.Set('annotate.actions', []); // Fire the annotate event RG.FireCustomEvent(obj, 'onannotateend'); }; /** * The canvas onmousemove function */ RGraph.Annotating_canvas_onmousemove = function (e) { var obj = e.target.__object__; var mouseXY = RGraph.getMouseXY(e); var mouseX = mouseXY[0]; var mouseY = mouseXY[1]; var lastXY = RG.Registry.Get('annotate.last.coordinates'); if (obj.Get('chart.mousedown')) { obj.context.beginPath(); if (!lastXY) { obj.context.moveTo(mouseX, mouseY) } else { obj.context.strokeStyle = obj.properties['chart.annotate.color']; obj.context.moveTo(lastXY[0], lastXY[1]); obj.context.lineTo(mouseX, mouseY); } RG.Registry.Set('annotate.actions', RG.Registry.Get('annotate.actions') + '|' + mouseX + ',' + mouseY); RG.Registry.Set('annotate.last.coordinates', [mouseX,mouseY]); RG.FireCustomEvent(obj, 'onannotate'); obj.context.stroke(); } }; /** * Shows the mini palette used for annotations * * @param object e The event object */ RG.ShowPalette = RG.Showpalette = function (e) { var isSafari = navigator.userAgent.indexOf('Safari') ? true : false; e = RG.FixEventObject(e); var canvas = e.target.parentNode.__canvas__; var context = canvas.getContext('2d'); var obj = canvas.__object__; var div = document.createElement('DIV'); var coords = RG.getMouseXY(e); div.__object__ = obj; // The graph object div.className = 'RGraph_palette'; div.style.position = 'absolute'; div.style.backgroundColor = 'white'; div.style.border = '1px solid black'; div.style.left = 0; div.style.top = 0; div.style.padding = '3px'; div.style.opacity = 0; div.style.boxShadow = 'rgba(96,96,96,0.5) 3px 3px 3px'; div.style.WebkitBoxShadow = 'rgba(96,96,96,0.5) 3px 3px 3px'; div.style.MozBoxShadow = 'rgba(96,96,96,0.5) 3px 3px 3px'; // MUST use named colors that are capitalised var colors = ['Black', 'Red', 'Yellow','Green','Orange', 'White', 'Magenta', 'Pink']; // Add the colors to the palette for (var i=0,len=colors.length; i document.body.offsetWidth) { div.style.left = (e.pageX - div.offsetWidth) + 'px'; } /** * Store the palette div in the registry */ RGraph.Registry.Set('palette', div); setTimeout(function () {RG.Registry.Get('palette').style.opacity = 0.2;}, 50); setTimeout(function () {RG.Registry.Get('palette').style.opacity = 0.4;}, 100); setTimeout(function () {RG.Registry.Get('palette').style.opacity = 0.6;}, 150); setTimeout(function () {RG.Registry.Get('palette').style.opacity = 0.8;}, 200); setTimeout(function () {RG.Registry.Get('palette').style.opacity = 1;}, 250); RGraph.HideContext(); window.onclick = function () { RG.hidePalette(); } // Should this be here? Yes. This function is being used as an event handler. e.stopPropagation(); return false; }; /** * Clears any annotation data from global storage * * @param object canvas The canvas tag object */ RG.clearAnnotations = RG.ClearAnnotations = function (canvas) { /** * For BC the argument can also be the ID of the canvas */ if (typeof canvas === 'string') { var id = canvas; canvas = doc.getElementById(id); } else { var id = canvas.id } var obj = canvas.__object__; if (win.localStorage && win.localStorage['__rgraph_annotations_' + id + '__'] && win.localStorage['__rgraph_annotations_' + id + '__'].length) { win.localStorage['__rgraph_annotations_' + id + '__'] = []; RGraph.FireCustomEvent(obj, 'onannotateclear'); } }; /** * Replays stored annotations * * @param object obj The graph object */ RG.replayAnnotations = RG.ReplayAnnotations = function (obj) { // Check for support if (!win.localStorage) { return; } var context = obj.context; var annotations = win.localStorage['__rgraph_annotations_' + obj.id + '__']; var i, len, move, coords; context.beginPath(); context.lineWidth = obj.Get('annotate.linewidth'); if (annotations && annotations.length) { annotations = annotations.split('|'); } else { return; } for (i=0,len=annotations.length; i