/* * Chartina Canvas Charting Library * * Copyright (c) 2010 Cantina Consulting * http://cantina.co/ * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ // Chartina Canvas Charting Library // ----- // // The initial goals for Chartina are: // // * Use the HTML5 canvas element for all drawing // * Target mobile browsers (for now, we're talking Mobile Safari on iOS devices) // * Limited to 2D drawing // * Support a limited number of chart types // * No external library dependencies // * Easy to extend via standard Canvas API // Ok, let's set up a namespace for Chartina so nobody can mess with our // stuff. var Chartina = (function() { var drawing_context, control_context, zoomer_context, base, drawing_origin = {x: 0, y: 0}; // Private Interface // ----- // Transforms the default canvas coordinate system to set the origin at // bottom left and the positive x,y values to right and up. var setCoordinateSystem = function (context) { context.setTransform(1, 0, 0, -1, 0, context.canvas.height); } var setDrawingOrigin = function (x, y) { drawing_origin.x = x; drawing_origin.y = y; } var drawGridLines = function (spacing, style, width, context, direction) { context.save(); context.strokeStyle = style; context.lineWidth = width; var limit = direction === "vertical" ? context.canvas.width : context.canvas.height; context.beginPath(); for (var i = 0; i <= limit; i = i + spacing) { if (direction === "vertical") { context.moveTo(i, 0); context.lineTo(i, context.canvas.height); } else { context.moveTo(0, i); context.lineTo(context.canvas.width, i); } } context.stroke(); context.closePath(); context.restore(); } var drawVerticalGridlines = function (spacing, style, width, context) { drawGridLines(spacing, style, width, context, "vertical"); } var drawHorizontalGridlines = function (spacing, style, width, context) { drawGridLines(spacing, style, width, context, "horizontal"); } var drawAxisX = function (verticalPosition, style, width, leftPadding, rightPadding, context) { context.save(); context.strokeStyle = style; context.lineWidth = width; context.lineCap = "square"; context.beginPath(); context.moveTo(leftPadding, verticalPosition); context.lineTo(context.canvas.width - rightPadding, verticalPosition); context.stroke(); context.closePath(); context.restore(); } var drawAxisY = function (horizontalPosition, style, width, topPadding, bottomPadding, context) { context.save(); context.strokeStyle = style; context.lineWidth = width; context.lineCap = "square"; context.beginPath(); context.moveTo(horizontalPosition, topPadding); context.lineTo(horizontalPosition, context.canvas.height - bottomPadding); context.stroke(); context.closePath(); context.restore(); } var drawBackground = function (style, context) { context.save(); context.fillStyle = style; context.fillRect(0, 0, context.canvas.width, context.canvas.height); context.restore(); } var clearChart = function(context) { context.clearRect(0, 0, context.canvas.width, context.canvas.height); } // ### Line Charts // // Simplistic implementation for line charts. This will need some work and // probably a complete overhaul. The goal is to get something that works. // // Made some assumptions to keep this simple: // // * The data series will be scaled to fit within the space of the chart // // This stuff needs to get extracted and place somewhere else; once we get // an extension mechanism set up, that can happen. var drawLineChart = function(data, xScale, style, width, context) { // Assume that all charts will use the drawing origin var o = drawing_origin; context.save(); context.strokeStyle = style; context.lineWidth = width; context.beginPath(); context.moveTo(drawing_origin.x, data[0]); for (var i = 1; i < data.length ;i++) { context.lineTo(xScale * i, data[i]); } context.stroke(); context.closePath(); context.restore(); } // Public Interface // ----- // Chartina use a descriptive interface to create charts. Pass an element // reference and a properties object to `Chartina.create()` and Chartina // will handle the drawing. // // For example, you can create a simple line chart like this: // // Chartina.chart('#placeholder', { // canvasHeight: 120, // canvasWidth: 200, // lineCharts: [{ // data: [100, 5, 1, 10, 13, 20, 12, 23, 34], // style: "orange" // }, // { // data: [20, 25, 35, 30, 40, 70, 10, 12.5, 12.9], // style: "rgba(255,10,20,0.5)", // width: 2 // }] // }); var c = { chart: function (element, properties) { properties = properties || {}; base = document.querySelector(element); c.destroy(); base.style.position = "relative"; base.style.width = properties.canvasWidth ? properties.canvasWidth : base.style.width; base.style.height = properties.canvasWidth ? properties.canvasWidth : base.style.height; // create the control surface var control = document.createElement('canvas'); control.setAttribute('id', 'chartina_control'); control.width = properties.canvasWidth ? properties.canvasWidth : control.width; control.height = properties.canvasHeight ? properties.canvasHeight : control.height; control.style.position = "absolute"; control.style.top = 0; control.style.left = 0; // create the main drawing surface var drawing = document.createElement('canvas'); drawing.setAttribute('id', 'chartina_drawing'); drawing.width = properties.canvasWidth ? properties.canvasWidth : drawing.width; drawing.height = properties.canvasHeight ? properties.canvasHeight : drawing.height; // create the zoomer surface, if required if (properties.zoomer) { var zoomer = document.createElement('canvas'); zoomer.setAttribute('id', 'chartina_zoomer'); zoomer.width = properties.canvasWidth ? properties.canvasWidth : drawing.width; zoomer.height = properties.zoomer.height ? properties.zoomer.height : 20; zoomer.style.position = "absolute"; zoomer.style.left = 0; zoomer.style.top = drawing.height + (properties.zoomer.offset || 10) + "px"; } drawing_context = drawing.getContext('2d'); control_context = control.getContext('2d'); if (zoomer) { zoomer_context = zoomer.getContext('2d'); } setCoordinateSystem(drawing_context); // Set the drawing origin as desired. This sets the point in the // local coordinate space that should be used as origin for all chart // drawing. if (properties.drawingOrigin) { setDrawingOrigin(properties.drawingOrigin.x, properties.drawingOrigin.y); } if (properties.backgroundStyle) { drawBackground(properties.backgroundStyle, drawing_context); } // Draw the background grid, if desired. if (properties.grid || properties.vertGrid) { drawVerticalGridlines(properties.vertGridSpacing || properties.gridSpacing || 10, properties.vertGridStyle || properties.gridStyle || "gray", properties.vertGridWidth || properties.gridWidth || 0.25, drawing_context) } if (properties.grid || properties.horzGrid) { drawHorizontalGridlines(properties.horzGridSpacing || properties.gridSpacing || 10, properties.horzGridStyle || properties.gridStyle || "gray", properties.horzGridWidth || properties.gridWidth || 0.25, drawing_context) } if (properties.axisX) { axisProperties = properties.axisX; drawAxisX(axisProperties.position || 0, axisProperties.style || 'black', axisProperties.width || 1 , axisProperties.padding || axisProperties.leftPadding || 0, axisProperties.padding || axisProperties.rightPadding || 0, drawing_context); } if (properties.axisY) { axisProperties = properties.axisY; drawAxisY(axisProperties.position || 0, axisProperties.style || 'black', axisProperties.width || 1 , axisProperties.padding || axisProperties.topPadding || 0, axisProperties.padding || axisProperties.bottomPadding || 0, drawing_context); } if (properties.lineCharts) { for (var i = 0; i < properties.lineCharts.length ; i++) { var lc = properties.lineCharts[i]; drawLineChart(lc.data, lc.scale || 10, lc.style || 'green', lc.width || 1, drawing_context); } } // Bind event listeners to the control layers events if (properties.controlEvents) { var ce = properties.controlEvents; for (i in ce) { control.addEventListener(i, ce[i]); } } // Attach the various canvas to the base element. base.appendChild(drawing); base.appendChild(control); if (zoomer) { base.appendChild(zoomer); } // Chartina exposes references to its child canvases. This gives // clients a simple way to extend the drawing capabilities of the // library using standard canvas code. return {drawingLayer: drawing, controlLayer: control, zoomerLayer: zoomer}; }, destroy: function(chartina) { if (base.hasChildNodes()) { base.removeChild(drawing_context.canvas); base.removeChild(control_context.canvas); if (zoomer_context) { base.removeChild(zoomer_context.canvas); } } } } return c; })();