graph.js
Summary
No overview generated for 'graph.js'
function Graph()
{
this.title = 'untitled Graph';
this.width = '480px';
this.height = '360px';
this.bgColor = '#ffcc99';
this.fgColor = '#336600';
var rendererMethodPrefix = 'renderer_';
var labels = [];
var dataSets = [];
var min;
var max;
function isArray( what )
{
return (typeof(what)=='object' && what.constructor==Array);
}
function reset()
{
labels = [];
dataSets = [];
min = undefined;
max = undefined;
}
this.setLabels = function( _labels )
{
if( !isArray(_labels) )
return false;
reset();
labels = _labels;
return true;
}
this.getLabels = function()
{
return labels;
}
this.addDataSet = function( _dataId, _dataSet, _color )
{
if( !isArray(_dataSet) || _dataSet.length!=labels.length )
return false;
for( var i=0; i<_dataSet.length; i++ )
if( isNaN(_dataSet[i]) )
return false;
max = Math.max( max||-Infinity, Math.max.apply( Math, _dataSet ) );
min = Math.min( min||Infinity, Math.min.apply( Math, _dataSet ) );
dataSets.push( typeof(_color)=='string'?{id:_dataId, data:_dataSet, color:_color }:{id:_dataId, data:_dataSet } );
return true;
}
this.getDataSets = function()
{
return dataSets;
}
this.getDataRange = function()
{
if( dataSets.length )
return {minimum:min,maximum:max};
return null;
}
this.getDataSetColor = function( index )
{
if( isNaN(index) || index<0 || !dataSets[index] )
return false;
if( this.colorList==undefined )
{
this.colorList = [];
this.colorList[ -1 ] = { seed: 0x01337 }
}
if( !this.colorList[ index ] )
{
var currentSeed = this.colorList[ this.colorList.length-1 ].seed;
for( var i=this.colorList.length; i<=index; i++ )
{
if( !dataSets[i].color )
{
currentSeed *= 1337;
currentSeed = (currentSeed&0x0aaaa>>1)+(currentSeed&0x05555<<1);
var rnd255 = (currentSeed&255)/255;
this.colorList.push( {seed:currentSeed, color:'#'+ (''+( Math.round(this.colorObjBg.b+this.colorObjDelta.b*rnd255) + 256*Math.round(this.colorObjBg.g+this.colorObjDelta.g*rnd255) + 65536*Math.round(this.colorObjBg.r+this.colorObjDelta.r*rnd255) ).toString(16)).slice(-6) } );
}
else
this.colorList.push( {seed:currentSeed, color:dataSets[i].color } );
}
}
return this.colorList[index].color;
}
this.listStyles = function()
{
var renderers = [];
for( var key in this )
if( key.substr(0,rendererMethodPrefix.length)==rendererMethodPrefix && typeof(this[key])=='function' )
renderers.push( key.substr(rendererMethodPrefix.length) );
return renderers;
}
this.render = function( _style, _renderTarget )
{
if( typeof(_style)!='string' || !_style.length || !dataSets.length )
return false;
var graphRendererId = rendererMethodPrefix+_style;
if( typeof(this[graphRendererId])!='function' )
return false;
var SVG = createSVGElement
(
'svg',
{
width:this.width,
height:this.height,
xmlns:'http://www.w3.org/2000/svg',
viewBox:'0 0 '+ parseFloat(this.width) +' '+ parseFloat(this.height)
}
)
var c1 = parseInt( this.bgColor.substr( 1 ), 16 ),
c2 = parseInt( this.fgColor.substr( 1 ), 16 );
this.colorObjBg = { r:c1>>16, g:c1>>8&255, b:c1&255 };
this.colorObjDelta = { r:(c2>>16)-this.colorObjBg.r, g:(c2>>8&255)-this.colorObjBg.g, b:(c2&255)-this.colorObjBg.b };
var renderState = this[graphRendererId]( SVG );
delete this.colorList;
delete this.colorObjBg;
delete this.colorObjDelta;
if( renderState )
(_renderTarget||document.body).appendChild( SVG );
return renderState;
}
return this;
}
window.createSVGElement = function( nodeName, attributesList )
{
var svgElement = document.createElementNS( 'http://www.w3.org/2000/svg', nodeName );
if( attributesList!=undefined )
for( var key in attributesList )
if( typeof(key)=='string' )
svgElement.setAttribute( key, attributesList[key] );
return svgElement;
}
Documentation generated by
JSDoc on Wed Jun 14 11:01:46 2006