// version: 2017-11-25 /** * o--------------------------------------------------------------------------------o * | This file is part of the RGraph package - you can learn more at: | * | | * | http://www.rgraph.net | * | | * | RGraph is licensed under the Open Source MIT license. That means that it's | * | totally free to use and there are no restrictions on what you can do with it! | * o--------------------------------------------------------------------------------o */ RGraph = window.RGraph || {isRGraph: true}; RGraph.HTML = RGraph.HTML || {}; // Module pattern (function (win, doc, undefined) { var RG = RGraph, ua = navigator.userAgent, ma = Math; /** * Draws the graph key (used by various graphs) * * @param object obj The graph object * @param array key An array of the texts to be listed in the key * @param colors An array of the colors to be used */ RG.drawKey = RG.DrawKey = function (obj, key, colors) { if (!key) { return; } var ca = obj.canvas, co = obj.context, prop = obj.properties, // Key positioned in the gutter keypos = prop['chart.key.position'], textsize = prop['chart.text.size'], key_non_null = [], colors_non_null = []; co.lineWidth = 1; co.beginPath(); /** * Change the older chart.key.vpos to chart.key.position.y */ if (typeof(prop['chart.key.vpos']) == 'number') { obj.Set('chart.key.position.y', prop['chart.key.vpos'] * prop['chart.gutter.top']); } /** * Account for null values in the key */ for (var i=0; i=0; i--) { var j = Number(i) + 1; /** * Draw the blob of color */ // An array element, string if (typeof prop['chart.key.color.shape'] === 'object' && typeof prop['chart.key.color.shape'][i] === 'string') { var blob_shape = prop['chart.key.color.shape'][i]; // An array element, function } else if (typeof prop['chart.key.color.shape'] === 'object' && typeof prop['chart.key.color.shape'][i] === 'function') { var blob_shape = prop['chart.key.color.shape'][i]; // No array - just a string } else if (typeof prop['chart.key.color.shape'] === 'string') { var blob_shape = prop['chart.key.color.shape']; // No array - just a function } else if (typeof prop['chart.key.color.shape'] === 'function') { var blob_shape = prop['chart.key.color.shape']; // Unknown } else { var blob_shape = 'rect'; } if (blob_shape == 'circle') { co.beginPath(); co.fillStyle = colors[i]; co.arc(hpos + 5 + (blob_size / 2), vpos + (5 * j) + (text_size * j) - text_size + (blob_size / 2), blob_size / 2, 0, 6.26, 0); co.fill(); } else if (blob_shape == 'line') { co.beginPath(); co.strokeStyle = colors[i]; co.moveTo(hpos + 5, vpos + (5 * j) + (text_size * j) - text_size + (blob_size / 2)); co.lineTo(hpos + blob_size + 5, vpos + (5 * j) + (text_size * j) - text_size + (blob_size / 2)); co.stroke(); } else if (blob_shape == 'triangle') { co.beginPath(); co.strokeStyle = colors[i]; co.moveTo(hpos + 5, vpos + (5 * j) + (text_size * j) - text_size + blob_size); co.lineTo(hpos + (blob_size / 2) + 5, vpos + (5 * j) + (text_size * j) - text_size ); co.lineTo(hpos + blob_size + 5, vpos + (5 * j) + (text_size * j) - text_size + blob_size); co.closePath(); co.fillStyle = colors[i]; co.fill(); } else if (typeof blob_shape === 'function') { blob_shape({ object: obj, color: colors[i], x: hpos + 5, y: vpos + (5 * j) + (text_size * j) - text_size, width: text_size, height: text_size + 1 }); } else { co.fillStyle = colors[i]; co.fillRect( hpos + 5, vpos + (5 * j) + (text_size * j) - text_size, text_size, text_size + 1 ); } /////////////////////////////////////////////////////////////////////////////////////////////////////////// co.beginPath(); co.fillStyle = typeof text_color == 'object' ? text_color[i] : text_color; ret = RG.Text2(obj, { 'font': text_font, 'size': text_size, 'bold': text_bold, 'italic': text_italic, 'x': hpos + blob_size + 5 + 5, 'y': vpos + (5 * j) + (text_size * j) + 3, 'text': key[i], 'accessible': !obj.properties['chart.key.interactive'] }); obj.coords.key[i] = [ ret.x, ret.y, ret.width, ret.height, key[i], colors[i], obj ]; } co.fill(); } /** * This does the actual drawing of the key when it's in the gutter * * @param object obj The graph object * @param array key The key items to draw * @param array colors An aray of colors that the key will use */ function DrawKey_gutter (obj, key, colors) { var text_size = typeof(prop['chart.key.text.size']) == 'number' ? prop['chart.key.text.size'] : prop['chart.text.size'], text_bold = prop['chart.key.text.bold'], text_italic = prop['chart.key.text.italic'], text_font = prop['chart.key.text.font'] || prop['chart.key.font'] || prop['chart.text.font'], text_color = prop['chart.key.text.color'], gutterLeft = obj.gutterLeft, gutterRight = obj.gutterRight, gutterTop = obj.gutterTop, gutterBottom = obj.gutterBottom, hpos = ((ca.width - gutterLeft - gutterRight) / 2) + obj.gutterLeft, vpos = gutterTop - text_size - 5, title = prop['chart.title'], blob_size = text_size, // The blob of color hmargin = 8, // This is the size of the gaps between the blob of color and the text vmargin = 4, // This is the vertical margin of the key fillstyle = prop['chart.key.background'], strokestyle = '#999', length = 0; if (!obj.coords) obj.coords = {}; obj.coords.key = []; // Need to work out the length of the key first co.font = (obj.properties['chart.key.text.italic'] ? 'italic ' : '') + (obj.properties['chart.key.text.bold'] ? 'bold ' : '') + text_size + 'pt ' + text_font; for (i=0; i'; /** * Add the individual key elements */ for (var i=0; i ' + (prop.links && prop.links[i] ? '' : '') + '' + prop.labels[i] + '' + (prop.links && prop.links[i] ? '' : '') + ''; } div.innerHTML += (str + ''); // Return the TABLE object that is the HTML key return doc.getElementById('rgraph_key_' + uid); }; // End module pattern })(window, document);