This is the first of a series of postings demonstrating basic JavaScript implementations of charts and graphs.
Imalgam is creating
visualization tools to aid with projects under development, and some of the results will be shared here.
Below are sample renderings using the
LineGraph
JavaScript class. In the first case
(
Square Root),
the data source is a list of (x, y) values, and in the second case
(
Sine function)
the data source is a JavaScript
lambda function characterizing the values of y as a function of x.
The
LineGraph
class has two externally consumed methods, the
constructor
and
renderTo
. The
constructor
takes as
its argument the graph's configuration, described in detail below. The
renderTo
method takes as its argument a
canvas object in the DOM where the graph
is rendered. The
constructor
does not copy/clone the configuration object, so that any changes to the configuration object passed to the
constructor
will
affect the rendering if said change is made before calling
renderTo
.
title
[Optional] Title of the chart, displayed centered at the top of the chart.
legend
[Optional] Legend Object.
data
[Optional]
Array whose elements are two-entry Arrays representing points to be rendered. The two-entry arrays are of the form [x-value, y-value].
data
is optional, it ought not be present if
dataFunction
is provided.
dataFunction
[Optional] A function which when passed a x-value returns the associated y-value for the rendered graph. dataFunction
is optional, and overrides data
if it is also present.
min
[Optional] Minimum x-value of the graph, if dataFunction
is provided.
max
[Optional] Maximum x-value of the graph, if dataFunction
is provided.