ProtoChart

Version: v0.5 beta

ProtoChart is a charting lib on top of Prototype.  This library is heavily motivated by excellent work done by:

Complete examples can be found at: http://www.deensoft.com/lab/protochart

Summary
Version: v0.5 beta
Description: Various options can be set.
{Object} data
{Object} data
{Object} colorSet
{Object} needle {Object} haystack
Description: sets the bar width for Bar Graph, you should enable autoScale property for bar graph
Description: Private function that goes through each graph data (series) and assigned the graph properties to it.
Description: processes graph data, setup xaxis and yaxis min and max points.
Description: constructs the main canvas for drawing.
Description: a container function that does a few interesting things.
{Object} axis {Object} axisOptions
{Object} axis {Object} axisOptions
{Object} axis {Object} axisOptions
Parameters: none
{Object} x
{Object} y
parameters: none
parameters: none
{Object} graphData
{Object} data {Object} offset
{Object} data
{Object} graphData
{Object} data {Object} radius {Object} fill
{Object} data {Object} offset {Object} radius
{Object} graphData
{Object} graphData
{Object} anchorX {Object} anchorY {Object} radius
{Object} graphData
{Object} graphData {Object} barDataRange
{Object} graphData
{Object} graphData {Object} data {Object} barWidth {Object} offset {Object} fill {Object} counter {Object} total {Object} barDataRange
{Object} graphData {Object} orgStrokeStyle {Object} left {Object} bottom {Object} top {Object} right {Object} drawLeft {Object} drawRight {Object} drawTop
{Object} graphData {Object} counter {Object} total {Object} barDataRange
inserts legend onto the graph.
event: {Object} ev
Internal function called when a selection on the graph is made.
Description: Internal function called to draw the selection made on the graph.
Helper class that manipulates colors using RGBA values.

Properties

options

Description: Various options can be set.  More details in description.

colors

{Array}pass in a array which contains strings of colors you want to use.  Default has 6 color set.

legend

{BOOL}show - if you want to show the legend.  Default is false
{integer}noColumns - Number of columns for the legend.  Default is 1
{function}labelFormatter - A function that returns a string.  The function is called with a string and is expected to return a string.  Default = null
{string}labelBoxBorderColor - border color for the little label boxes.  Default #CCC
{HTMLElem}container - an HTML id or HTML element where the legend should be rendered.  If left null means to put the legend on top of the Chart
{string}position - position for the legend on the Chart.  Default value ‘ne’
{integer}margin - default valud of 5
{string}backgroundColor - default to null (which means auto-detect)
{float}backgroundOpacity - leave it 0 to avoid background

xaxis (yaxis) options

{string}mode - default is null but you can pass a string “time” to indicate time series
{integer}min
{integer}max
{float}autoscaleMargin - in % to add if auto-setting min/max
{mixed}ticks - either [1, 3] or [[1, “a”], 3] or a function which gets axis info and returns ticks
{function}tickFormatter - A function that returns a string as a tick label.  Default is null
{float}tickDecimals
{integer}tickSize
{integer}minTickSize
{array}monthNames
{string}timeformat

Points / Lines / Bars options

{bool}show, default is false
{integer}radius: default is 3
{integer}lineWidth : default is 2
{bool}fill : default is true
{string}fillColor: default is #ffffff

Grid options

{string}color
{string}backgroundColor - defualt is null
{string}tickColor - default is #dddddd
{integer}labelMargin - should be in pixels default is 3
{integer}borderWidth - default 1
{bool}clickable - default null - pass in TRUE if you wish to monitor click events
{mixed}coloredAreas - default null - pass in mixed object eg.  {x1, x2}
{string}coloredAreasColor - default #f4f4f4
{bool}drawXAxis - default true
{bool}drawYAxis - default true

selection options

{string}mode : either “x”, “y” or “xy”
{string}color : string

Functions

setData

setData: function(data)

{Object} data

Description

Sets datasoruces properly then sets the Bar Width accordingly, then copies the default data options and then processes the graph data

Returns: none

parseData

parseData: function(data)

{Object} data

Return

{Object} result

Description

Takes the provided data object and converts it into generic data that we can understand.  User can pass in data in 3 different ways:

  • [d1, d2]
  • [{data: d1, label: “data1”}, {data: d2, label: “data2”}]
  • [d1, {data: d1, label: “data1”}]

This function parses these senarios and makes it readable

makeRandomColor

makeRandomColor: function(colorSet)

{Object} colorSet

Return

{Array} resultarray containing random colors

checkExisted

checkExisted: function(needle,
haystack)

{Object} needle {Object} haystack

return

{bool} existedtrue if it finds needle in the haystack

setBarWidth

setBarWidth: function()

Description: sets the bar width for Bar Graph, you should enable autoScale property for bar graph

copyGraphDataOptions

copyGraphDataOptions: function()

Description: Private function that goes through each graph data (series) and assigned the graph properties to it.

processGraphData

processGraphData: function()

Description: processes graph data, setup xaxis and yaxis min and max points.

constructCanvas

constructCanvas: function()

Description: constructs the main canvas for drawing.  It replicates the HTML elem (usually DIV) passed in via constructor.  If there is no height/width assigned to the HTML elem then we take a default size of 400px (width) and 300px (height)

setupGrid

setupGrid: function()

Description: a container function that does a few interesting things.

1. calls extendXRangeIfNeededByBar function which makes sure that our axis are expanded if needed

2. calls setRange function providing xaxis options which fixes the ranges according to data points

3. calls prepareTickGeneration function for xaxis which generates ticks according to options provided by user

4. calls setTicks function for xaxis that sets the ticks

similar sequence is called for y-axis.

At the end if this is a pie chart than we insert Labels (around the pie chart) via insertLabels and we also call insertLegend

setRange

setRange: function(axis,
axisOptions)

parameters

{Object} axis {Object} axisOptions

prepareTickGeneration

prepareTickGeneration: function(axis,
axisOptions)

Parameters

{Object} axis {Object} axisOptions

extendXRangeIfNeededByBar

extendXRangeIfNeededByBar: function()

setTicks

setTicks: function(axis,
axisOptions)

parameters

{Object} axis {Object} axisOptions

setSpacing

setSpacing: function()

Parameters: none

draw

draw: function()

translateHoz

translateHoz: function(x)

Paramters

{Object} x

Description: Given a value this function translate it to relative x coord on canvas

translateVert

translateVert: function(y)

parameters

{Object} y

Description: Given a value this function translate it to relative y coord on canvas

drawGrid

drawGrid: function()

parameters: none

description: draws the actual grid on the canvas

insertLabels

insertLabels: function()

parameters: none

description: inserts the label with proper spacing.  Both on X and Y axis

drawGraph

drawGraph: function(graphData)

Paramters

{Object} graphData

Description: given a graphData (series) this function calls a proper lower level method to draw it.

plotLine

plotLine: function(data,
offset)

parameters

{Object} data {Object} offset

description

Helper function that plots a line based on the data provided

plotLineArea

plotLineArea: function(data)

parameters

{Object} data

description

Helper functoin that plots a colored line graph.  This function takes the data nad then fill in the area on the graph properly

drawGraphLines

drawGraphLines: function(graphData)

parameters

{Object} graphData

description

Main function that daws the line graph.  This function is called if options lines property is set to show or no other type of graph is specified.  This function depends on plotLineArea and plotLine functions.

plotPoints

plotPoints: function(data,
radius,
fill)

parameters

{Object} data {Object} radius {Object} fill

description

Helper function that draws the point graph according to the data provided.  Size of each point is provided by radius variable and fill specifies if points are filled

plotPointShadows

plotPointShadows: function(data,
offset,
radius)

parameters

{Object} data {Object} offset {Object} radius

description

Helper function that draws the shadows for the points.

drawGraphPoints

drawGraphPoints: function(graphData)

paramters

{Object} graphData

description

Draws the point graph onto the canvas.  This function depends on helper functions plotPointShadows and plotPoints

preparePieData

preparePieData: function(graphData)

parameters

{Object} graphData

Description

Helper function that manipulates the given data stream so that it can be plotted as a Pie Chart

drawPieShadow

drawPieShadow: function(anchorX,
anchorY,
radius)

{Object} anchorX {Object} anchorY {Object} radius

description

Helper function that draws a shadow for the Pie Chart.  This just draws a circle with offset that simulates shadow.  We do not give each piece of the pie an individual shadow.

drawPieGraph

drawPieGraph: function(graphData)

parameters

{Object} graphData

description

Draws the actual pie chart.  This function depends on helper function drawPieShadow to draw the actual shadow

drawBarGraph

drawBarGraph: function(graphData,
barDataRange)

parameters

{Object} graphData {Object} barDataRange

description

Goes through each series in graphdata and passes it onto drawBarGraphs function

drawGraphBar

drawGraphBar: function(graphData)

parameters

{Object} graphData

description

This function is called when an individual series in GraphData is bar graph and plots it

plotBars

plotBars: function(graphData,
data,
barWidth,
offset,
fill,
counter,
total,
barDataRange)

parameters

{Object} graphData {Object} data {Object} barWidth {Object} offset {Object} fill {Object} counter {Object} total {Object} barDataRange

description

Helper function that draws the bar graph based on data.

plotShadowOutline

plotShadowOutline: function(graphData,
orgStrokeStyle,
left,
bottom,
top,
right,
drawLeft,
drawRight,
drawTop)

parameters

{Object} graphData {Object} orgStrokeStyle {Object} left {Object} bottom {Object} top {Object} right {Object} drawLeft {Object} drawRight {Object} drawTop

description

Helper function that draws a outline simulating shadow for bar chart

drawGraphBars

drawGraphBars: function(graphData,
counter,
total,
barDataRange)

parameters

{Object} graphData {Object} counter {Object} total {Object} barDataRange

description

Draws the actual bar graphs.  Calls plotBars to draw the individual bar

insertLegend

insertLegend: function()

description

inserts legend onto the graph.  legend: {show: true} must be set in options for for this to work.

onMouseMove

onMouseMove: function(ev)

parameters

event: {Object} ev

Description

Called whenever the mouse is moved on the graph.  This takes care of the mousetracking.  This event also fires <ProtoChart:mousemove> event, which gets current position of the mouse as a parameters.

onMouseDown

onMouseDown: function(e)

Parameters

Event{Object} e

Description

Called whenever the mouse is clicked.

onClick

onClick: function(e)

parameters

Event{Object} e Description: Handles the “click” event on the chart.  This function fires <ProtoChart:plotclick> event.  If <options.allowDataClick> is enabled then it also fires <ProtoChart:dataclick> event which gives you access to exact data point where user clicked.

triggerSelectedEvent

triggerSelectedEvent: function()

Description

Internal function called when a selection on the graph is made.  This function fires <ProtoChart:selected> event which has a parameter representing the selection { x1: {int}, y1: {int}, x2: {int}, y2: {int} }

setSelection

setSelection: function(area)

Parameters

Area{Object} area represented as a range like: {x1: 3, y1: 3, x2: 4, y2: 8}

Description

Sets the current graph selection to the provided range.  Calls drawSelection and triggerSelectedEvent functions internally.

drawSelection

drawSelection: function()

Description: Internal function called to draw the selection made on the graph.

clearHit

clearHit: function()

hit

hit: function(event,
mouse)

Parameters

event{Object} event object
mouse{Object} mouse object that is used to keep track of mouse movement

Description

If hit occurs this function will fire a ProtoChart:hit event.

extractColor

extractColor: function(element)

Parameters

elementHTML element or ID of an HTML element

Returns

color in string format

parseColor

parseColor: function(str)

Parameters

strcolor string in different formats

Returns

a Proto.Color Objectuse toString() function to retreive the color in rgba/rgb format

Proto. Color

Helper class that manipulates colors using RGBA values.

setData: function(data)
{Object} data
parseData: function(data)
{Object} data
makeRandomColor: function(colorSet)
{Object} colorSet
checkExisted: function(needle,
haystack)
{Object} needle {Object} haystack
setBarWidth: function()
Description: sets the bar width for Bar Graph, you should enable autoScale property for bar graph
copyGraphDataOptions: function()
Description: Private function that goes through each graph data (series) and assigned the graph properties to it.
processGraphData: function()
Description: processes graph data, setup xaxis and yaxis min and max points.
constructCanvas: function()
Description: constructs the main canvas for drawing.
setupGrid: function()
Description: a container function that does a few interesting things.
setRange: function(axis,
axisOptions)
{Object} axis {Object} axisOptions
prepareTickGeneration: function(axis,
axisOptions)
{Object} axis {Object} axisOptions
extendXRangeIfNeededByBar: function()
setTicks: function(axis,
axisOptions)
{Object} axis {Object} axisOptions
setSpacing: function()
Parameters: none
draw: function()
translateHoz: function(x)
{Object} x
translateVert: function(y)
{Object} y
drawGrid: function()
parameters: none
insertLabels: function()
parameters: none
drawGraph: function(graphData)
{Object} graphData
plotLine: function(data,
offset)
{Object} data {Object} offset
plotLineArea: function(data)
{Object} data
drawGraphLines: function(graphData)
{Object} graphData
plotPoints: function(data,
radius,
fill)
{Object} data {Object} radius {Object} fill
plotPointShadows: function(data,
offset,
radius)
{Object} data {Object} offset {Object} radius
drawGraphPoints: function(graphData)
{Object} graphData
preparePieData: function(graphData)
{Object} graphData
drawPieShadow: function(anchorX,
anchorY,
radius)
{Object} anchorX {Object} anchorY {Object} radius
drawPieGraph: function(graphData)
{Object} graphData
drawBarGraph: function(graphData,
barDataRange)
{Object} graphData {Object} barDataRange
drawGraphBar: function(graphData)
{Object} graphData
plotBars: function(graphData,
data,
barWidth,
offset,
fill,
counter,
total,
barDataRange)
{Object} graphData {Object} data {Object} barWidth {Object} offset {Object} fill {Object} counter {Object} total {Object} barDataRange
plotShadowOutline: function(graphData,
orgStrokeStyle,
left,
bottom,
top,
right,
drawLeft,
drawRight,
drawTop)
{Object} graphData {Object} orgStrokeStyle {Object} left {Object} bottom {Object} top {Object} right {Object} drawLeft {Object} drawRight {Object} drawTop
drawGraphBars: function(graphData,
counter,
total,
barDataRange)
{Object} graphData {Object} counter {Object} total {Object} barDataRange
insertLegend: function()
inserts legend onto the graph.
onMouseMove: function(ev)
event: {Object} ev
onMouseDown: function(e)
onClick: function(e)
triggerSelectedEvent: function()
Internal function called when a selection on the graph is made.
setSelection: function(area)
drawSelection: function()
Description: Internal function called to draw the selection made on the graph.
clearHit: function()
hit: function(event,
mouse)
extractColor: function(element)
parseColor: function(str)
Description: Various options can be set.