body {
background-color: #CCCCCC;
}
canvas {
background-color: #FFFFFF;
}
JavaScript
/**
* Base graph class, handles containment of data points and the overarching
* interface of all graph classes.
*
* @class
* @param {Number} width
* @param {Number} height
* @param {*} [initialDataSource]
*/
function Graph(width, height, initialDataSource) {
this.setDataSource(initialDataSource);
this.initialiseCanvas(width, height);
}
/**
* Initialises the canvas element and stores it's context object. It will also
* set the initial width and height.
*
* @param {Number} width
* @param {Number} height
*/
Graph.prototype.initialiseCanvas = function (width, height) {
this._canvas = document.createElement('canvas');
this._context = this._canvas.getContext('2d');
this.setSize(width, height);
};
/**
* Updates the current size of the graph.
*
* @param {Number} width
* @param {Number} height
*/
Graph.prototype.setSize = function (width, height) {
this._canvas.width = this._width = width;
this._canvas.height = this._height = height;
};
/**
* Fetches the actual canvas DOM node. This can be used to place the canvas
* within your page.
*
* @return {HTMLElement}
*/
Graph.prototype.getCanvasElement = function () {
return this._canvas;
};
/**
* Updates the current data source. The values contained within are used to
* render the actual graph.
*
* @param {*} dataSource
*/
Graph.prototype.setDataSource = function (dataSource) {
this._dataSource = dataSource;
};
/**
* Renders the current data source onto the canvas.
*/
Graph.prototype.renderGraph = function () {
this.clearCanvasElement();
this.drawDataSourceOntoCanvasElement();
};
/**
* Clears the current canvas state.
*/
Graph.prototype.clearCanvasElement = function () {
this._context.clearRect(0, 0, this._width, this._height);
};
/**
* Draws the current data source onto the canvas.
*
* @abstract
*/
Graph.prototype.drawDataSourceOntoCanvasElement = function () {};
/**
* Line graph, used for plotting a value over time.
*
* @class
* @augments Graph
*/
function...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.