// o---------------------------------------------------------------------------------o // | This file is part of the RGraph package - you can learn more at: | // | | // | https://www.rgraph.net/license.html | // | | // | RGraph is dual-licensed under the Open Source GPL license. That means that it's | // | free to use and there are no restrictions on what you can use RGraph for! | // | If the GPL license does not suit you however, then there's an inexpensive | // | commercial license option available. See the URL above for more details. | // o---------------------------------------------------------------------------------o RGraph = window.RGraph || {isrgraph:true,isRGraph: true,rgraph:true}; // Module pattern (function (win, doc, undefined) { var ua = navigator.userAgent; // // This installs some event handlers // // Checking the RGraph.annotate flag means the annotate code only runs once // RGraph.annotating_canvas_onmousedown = function (e) { if (e.button === 0) { e.target.__object__.set('mousedown', true); // Get the object from the canvas. Annotating must be enabled on the // last object defined var obj = e.target.__object__, properties = obj.properties // This starts the annotating "path" and set the color obj.context.beginPath(); obj.context.strokeStyle = obj.get('annotatableColor'); obj.context.lineWidth = obj.get('annotatableLinewidth'); var mouseXY = RGraph.getMouseXY(e), mouseX = mouseXY[0], mouseY = mouseXY[1] // Allow for the Bar chart 3D if (obj.type === 'bar' && properties.variant === '3d') { var adjustment = properties.variantThreedAngle * mouseXY[0]; mouseY -= adjustment; } // Clear the annotation recording RGraph.Registry.set('annotatable.actions', [obj.get('annotatableColor')]); // This sets the initial X/Y position obj.context.moveTo(mouseX, mouseY); RGraph.Registry.set('annotatable.last.coordinates', [mouseX,mouseY]); RGraph.Registry.set('started.annotating', false); RGraph.Registry.set('annotating', obj); // Fire the onannotatebegin event. RGraph.fireCustomEvent(obj, 'onannotatebegin'); } return false; }; // // This cancels annotating for ALL canvases // RGraph.annotating_window_onmouseup = function (e) { var obj = RGraph.Registry.get('annotating'); var win = window; 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 += RGraph.Registry.get('annotatable.actions'); // Store the annotations information in HTML5 browser storage here win.localStorage[id] = annotations; } // Clear the recorded annotations RGraph.Registry.set('annotatable.actions', []); // Fire the annotate event RGraph.fireCustomEvent(obj, 'onannotateend'); }; // // The canvas onmousemove function // RGraph.annotating_canvas_onmousemove = function (e) { var obj = e.target.__object__; var properties = obj.properties; var mouseXY = RGraph.getMouseXY(e); var mouseX = mouseXY[0]; var mouseY = mouseXY[1]; var lastXY = RGraph.Registry.get('annotatable.last.coordinates'); if (obj.get('mousedown')) { // Allow for the Bar chart 3D if (obj.type === 'bar' && properties.variant === '3d') { var adjustment = properties.variantThreedAngle * mouseXY[0]; mouseY -= adjustment; } obj.context.beginPath(); if (!lastXY) { obj.context.moveTo(mouseX, mouseY) } else { obj.context.strokeStyle = obj.properties.annotatableColor; obj.context.moveTo(lastXY[0], lastXY[1]); obj.context.lineTo(mouseX, mouseY); } RGraph.Registry.set('annotatable.actions', RGraph.Registry.get('annotatable.actions') + '|' + mouseX + ',' + mouseY); RGraph.Registry.set('annotatable.last.coordinates', [mouseX,mouseY]); RGraph.fireCustomEvent(obj, 'onannotate'); obj.context.stroke(); } }; // // Shows the mini palette used for annotations // // @param object e The event object // RGraph.showPalette = function (e) { var isSafari = navigator.userAgent.indexOf('Safari') ? true : false; var canvas = e.target.parentNode.__canvas__, context = canvas.getContext('2d'), obj = canvas.__object__, div = document.createElement('DIV'), coords = RGraph.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 #999'; div.style.left = 0; div.style.top = 0; div.style.padding = '3px'; div.style.paddingLeft = '5px'; div.style.opacity = 0; div.style.boxShadow = 'rgba(48,48,48,0.25) 1px 1px 3px'; div.style.WebkitBoxShadow = 'rgba(48,48,48,0.25) 1px 1px 3px'; div.style.MozBoxShadow = 'rgba(48,48,48,0.25) 1px 1px 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 () {div.style.opacity = 0.2;}, 50); setTimeout(function () {div.style.opacity = 0.4;}, 100); setTimeout(function () {div.style.opacity = 0.6;}, 150); setTimeout(function () {div.style.opacity = 0.8;}, 200); setTimeout(function () {div.style.opacity = 1;}, 250); RGraph.hideContext(); window.onclick = function () { RGraph.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 // RGraph.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 // RGraph.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; // Save the state of the canvas context.save(); context.beginPath(); context.lineWidth = obj.get('annotatableLinewidth'); if (annotations && annotations.length) { annotations = annotations.split('|'); } else { return; } // Set the stroke color context.strokeStyle = annotations[i] || 'black'; for (i=0,len=annotations.length; i