plotters
Browser support
Chrome, Safari, Firefox, Opera, IE9+
Installation
npm install plotters or jspm install npm:plotters
Usage
This is a full stock chart:
It has necessary functionalities for a stock chart. It contains a CANVAS plotter, timeline axis, number axis, timeline zoom,
chart grid, etc. The CANVAS plotter is the key of a chart. Plotters
class is such a component.
This example contains a simple chart implementation which can generate canvas graphs similiar to the first screenshot.
- This example is based on the demo, you can run the demo to see how it works.
- If you like you can read
demo.js
directly, the code is clean.
An example (pretty long) :
/** * Load needed modules. */ var $ = Arrays = Dates = DateIndex = Plotters = ; /* * A simple Chart. * We wrap plotter functionality in a Class called Chart. * * 1. plotterStyle() method will create a plotter instance and set plotting style. * 2. load() method will load data and draw graphs on CANVAS. */ var { this_parent = domParent; this; }; Chartprototype = (?:\?(\w+))?$/, _buildUI: function () { var container = ; this_canvas = ; this_canvas0width = 360; this_canvas0height = 280; this_header = ; this_xAxis = ; this_yAxis = ; } { return this_plotter; } { return PlottersclassNameStylestyle; } { var match = this_regexPlotterStyle; if match === null throw "IllegalArgumentException: plotterStyle is not supported: " + plotterStyle; var className = match1 style = match2; /** * Create plotter instance. */ this_plotter = className; if typeof style === 'string' && style !== '' /** * Set plotter style. */ this_plotterstylethis; else style = ''; this_plotter; // Set CANVAS object. if typeof hdrText === 'undefined' hdrText = className; if stylelength > 0 hdrText += ': ' + style; this_headertexthdrText; return this; } { this_plottercolorthis_colorscolor; // Set plotter color. return this; } { var dates = min = Infinity max = -Infinity len = datalength; for var i = 0; i < len; i++ var row = datai; dates; for var j = 1; j < rowlength; j++ var val = rowj; if min > val min = val; if max < val max = val; return dates: dates min: min max: max ; } { var len = datalength info = this plotter = this_plotter; this_xAxis; // Set dates info. this_yAxis; // Set value info. /** * Preparation. */ plotter; for var i = 0; i < len; i++ var row = datai; /** * put() method add data point to plotter instance. */ plotter; /** * Plotter instance draw CANVAS by calling close() method. * this._xAxis, this._yAxis both need to implement getPosition() method, * Plotter instance will call each one's getPosition() to convert date and * value to corresponding x and y coordinates on the canvas. */ plotter; } ; /** * A simplified DateAxis class. */ var { this_index = ; }; DateAxisprototype = { /** * Index dates. */ this_index; this_lowIdx = this_index; this_highIdx = this_index; } /** * Get a date's relative position. */ { var idx = this_index; if idx < 0 throw "IllegalArgumentException: date is not found."; return idx - this_lowIdx / this_highIdx - this_lowIdx ; } ; /** * A simplified NumberAxis class. */ var { this_min = null; this_max = null; this_range = null; }; NumberAxisprototype = { this_min = min; this_max = max; this_range = this_max - this_min; } /** * Get relative position of a value. */ { if typeof number !== 'number' || throw "IllegalArgumentException: number must be a Number."; return number - this_min / this_range; } ; /** * You can ignore this object, its purpose is to generate a series of fake data. */ var util = { return date + i * DatesmillisPerDay; } /** * Generate an array of date and values according to given value range, * base date, and number of data points. */ { var priceGen = this baseDate = Dates data = ; for var i = 0; i < numOfPoints; i++ var date = this price = row = date price; if isOHLC var close = price + Math - 05; row; row; row; data; return data; } { var range = maxPrice - minPrice / 40; // 5% variation var variation = range / 5; // point by point variation: 0.25% var barAt = maxPrice + minPrice / 2; // start in the middle. var inc = 0; var cnt = 0; return { if --cnt <= 0 cnt = Math; inc = Math * range - range / 2; if barAt < minPrice + range && inc < 0 || barAt > maxPrice - range && inc > 0 inc = -inc; barAt += inc; return util; }; } { return Math / 100; } ; /************************************************* ********* Draw charts. **************************************************/ var playGround = ; var { playGround; // I am lazy. ; }; ; var { var chart = playGround /** * Generate an array of data which contains 20 data-points, * value ranges between 10 and 20. */ data = util; /** * The format of generated `data` will be something like: * * [ [Date, 10], [Date, 13.5], [Date, 16.7], 0.9.19. ] */ chart // Plotter Line. ; chart = playGround; data = util; chart // Plotter Mountain. ; chart = playGround; data = util; chart; var plotter = chart; plotter; // Plotter MOUNTAIN with base value setting to 15. chart ; chart = playGround; data = util; chart // Dashed Line ; var plotter = chart; plotter /** * Draw dashed line starts from '2016-05-07' */ ; chart; chart = playGround; data = util; chart // Bar. ; chart = playGround; data = util; chart // Stock OHLC. ; chart = playGround; data = util; chart ; chart = playGround; data = util; chart ; }; ;
Demo
- Clone this repo.
- Run npm install ( You can skip this step if you have a globally installed jspm.)
- Run jspm install.
- Run live-server ( live-server is very useful, but if you have other server tools you don't have to use it.)
- Open demo.html in a browser.