// for debugging purpose only var c = function(){ console.log.apply(console, arguments); }; // the main class! var Graph = function(container, width, height) { var context = null; var contextType = 'none'; var data; var __this__ = this; var __defaultoptions__ = {color:'#000', size:1, background:'rgba(0,0,0,0.5)', align:'center'}; var __options__ = {}; var __methods__ = {canvas:{}, raphael:{}}; var cache = []; __methods__.canvas.size = function(width, height) { context.canvas.width = width; context.canvas.height = height; __options__.width = width; __options__.height = height; }; __methods__.canvas.get = function() { return __options__; } __methods__.canvas.set = function(options) { options.color && (context.strokeStyle = options.color); options.color && (context.fillStyle = options.color); // options.size && }; __methods__.canvas.reset = function() { }; __methods__.canvas.line = function(x1, y1, x2, y2, options) { options || (options = {}); context.lineCap = 'round'; context.lineWidth = getSize(options.size || 1); context.strokeStyle = options.color || __options__.color; context.beginPath(); context.moveTo(getX(x1), getY(y1)); context.lineTo(getX(x2), getY(y2)); context.stroke(); }; __methods__.canvas.rect = function(x1, y1, x2, y2, options) { options || (options = {}); context.lineCap = 'round'; context.lineWidth = getSize(options.size || __options__.size); context.fillStyle = options.color || __options__.color; context.fillRect( getX(x1), getY(y1), getX(x2)-getX(x1), getY(y2)-getY(y1) ); return __this__; }; __methods__.canvas.text = function(x, y, text, options) { options = options || {}; var size = 16 * (options.size || __options__.size); if (scales && scales.size) { size *= scales.size; } size = Math.round(10 * size) / 10; context.font = size + 'px sans-serif'; context.fillStyle = options.color || __options__.color; context.textAlign = options.align || __options__.align; context.textAlign = options.align || __options__.align; context.textBaseline = 'middle'; context.fillText(text, getX(x), getY(y)); return __this__; }; __methods__.canvas.fill = function(background) { context.fillStyle = background || __options__.background; context.fillStyle = 'white'; context.fillRect(0, 0, __options__.width, __options__.height); }; __methods__.canvas.clear = function() { var emptyImage = context.createImageData(context.canvas.width, context.canvas.height); context.putImageData(emptyImage, 0, 0); }; this.size = function(width, height) { __options__.width = width; __options__.height = height; __options__.size = Math.sqrt(__options__.width * __options__.height) / 800; // container.style.width = width + 'px'; // container.style.height = height + 'px'; __this__._size(width, height); __this__.draw(); return __this__; }; this.draw = function() { // clear the canvas __this__._clear(); // redraw what is in the cache for (var i=0; i b) return 1; return 0; }); } // __datasets__.list = datasets; return __this__; }; this.axisX = function(label, grads) { var extrema = plottingData.extrema; var valuesX = __datasets__.values[0]; var valuesY = __datasets__.values[1]; // main components __this__.line( scales[0].min, valuesY[0], scales[0].max, valuesY[0], {size:1, color:'#000'} ); __this__.text( scales[0].max - .0125*scales[0].span, valuesY[0] + .025*scales[1].span, label, {align:'right', size:1, color:'#000'} ); // graduations for (var i=0; i0) ? (average+std) : (average-std); __this__.line( i + 1, 0, i + 1, y, options ); __this__.line( i + .9, y, i + 1.1, y, options ); } // X axis this.line(0, min, __datasets__.list.length + .5, min, {size:1, color:'rgba(0,0,0,.75)'}); // Y axis var values = __datasets__.values[1]; var grad = Math.log(max - min); grad /= Math.log(10); grad = Math.floor(grad); grad = Math.pow(10, grad); __this__.axisY(labels[1], [grad, .1*grad], { Xmin: 0, Xmax: __datasets__.list.length + .5, Ymin: min, Ymax: max, }); }; this.viewLine = function(labels, options) { // compute the scales scales = []; for (var i=0; i<__datasets__.dimensions; i++) { var values = __datasets__.values[i]; if (__datasets__.types[i] == 'number') { var min = values[0]; var max = values[values.length - 1]; var span = max - min; scales.push({ type: 'numeric', min: min - .1 * span, max: max + .1 * span, span: 1.2 * span }); } else { var positions = {}; for (var j=0; j