VisualsAplenty.js Documentation
Getting Started
Welcome to the world of VisualsAplenty.js! We'll get you started in a pinch, but
first we have to make sure you're good to go.
Please ensure that you have Font Awesome
v5.15.4+ included in your webpage to make use of VisualsAplenty.js's full functionality.
Now, let's let's walk through how to create instances of the objects that
VisualsAplenty.js provides.
// in your HTML file, include the tag: <script>defer src="/path/to/VisualsAplenty.js"></script>
// sample bar graph
// here is a sample array of objects containing the data to be displayed
const tvData = [
{
Show: "LOST",
Count: 145
},
{
Show: "Sherlock",
Count: 99,
}
]
const tvTableDiv = document.querySelector('.tvTableDiv'); // get the parent element you want the graph to be displayed in
// create the BarGraph instance, resulting in the BarGraph to be drawn within the parent element specified
const favTVShow = new BarGraph("Favourite TV Show", tvData, "Count", "Show", tvTableDiv);
//------------------------------------------------------------------------------------------------------------------------------------------------------
// sample scatter plot
const winLoseData = [
{
Name: "Toronto Maple Leafs",
Wins: 50,
Losses: 80
},
{
Name: "Toronto Raptors",
Wins: 30,
Losses: 90
}
]
const numWinLoseDiv = document.querySelector('.numWinLoseDiv');
// create the ScatterPlot instance, resulting in the ScatterPlot to be drawn within the parent element specified
const winLose = new ScatterPlot("Toronto Team 2021 Win Rate", winLoseData, "Wins", "Losses", numWinLoseDiv, 'Name');
winLose.setLegendVisibility(true); // ScatterPlot by default has no legend, but we can enable it
//------------------------------------------------------------------------------------------------------------------------------------------------------
// sample pie chart
const birdWatchData = [
{
Count: 50,
Name: "Sparrow"
},
{
Count: 30,
Name: "Falcon"
}
]
const pieChartDiv = document.querySelector('.pieChartDiv');
// create the PieChart instance, resulting in the PieChart to be drawn within the parent element specified
const pieChartExample = new PieChart("Bird Watching Data 2021", birdWatchData, "Count", "Name", pieChartDiv);
Now, go out and create!
Legend Class
new Legend(parentElement)
Constructor function for Legend.
Parameters:
Name |
Type |
Description |
parentElement |
HTMLElement
|
the parent element for this Legend |
Methods
setVisibility(isVisible) → {boolean}
Change whether the legend should be visible.
Parameters:
Name |
Type |
Description |
isVisible |
boolean
|
optional (otherwise, simply toggle setting) boolean indicating whether
display or not
|
Returns:
boolean: whether the legend is now visible or not
updateLegend(data, labelProperty, colours, height) → {Array}
Delete the legend and redraw it based on the data provided.
Parameters:
Name |
Type |
Description |
data |
Object
|
the data to be drawn, with elements that have property labelProperty
|
labelProperty |
string
|
a property in all elements of data |
colours |
Array
|
an array of colour code strings |
height |
number
|
the pixel height of the parent element |
Returns:
Array the array of colours for the new legend
BarGraph Class
new BarGraph(title, data, vertAxis, horAxis, parentElement)
Constructor for BarGraph
Parameters:
Name |
Type |
Description |
title |
string
|
the title of the graph |
data |
object
|
array of objects containing the data to be displayed; data[vertAxis] and
data[horAxis] must be numerical
|
vertAxis |
string
|
string representing the attribute in all elements of data to be represented
vertically
|
horAxis |
string
|
string representing the attribute in all elements of data to be represented
horizontally
|
parentElement |
HTMLElement
|
element that is to be the parent element of the graph |
Methods
addData(dataToAdd)
Add data provided in dataToAdd to the BarGraph.
NOTE: this data will be appended to the data array provided in the constructor.
Parameters:
Name |
Type |
Description |
dataToAdd |
array
|
an array of objects with keys corresponding to horAxis and verAxis |
changeTableDimensions(width, height) → {boolean}
Change the dimensions (in pixels) of the table.
Parameters:
Name |
Type |
Description |
width |
number
|
numerical pixel value > 0 or null if current width is to be maintained |
height |
number
|
numerical pixel value > 0 or null if current height is to be maintained |
Returns:
boolean whether width and/or height were changed successfully
drawData()
Draw the bars of the graph based on the data in this.data
removeData(dataToRemove)
Remove the data specified in dataToRemove from the graph.
NOTE: this data will be removed from the data array provided in the constructor.
Parameters:
Name |
Type |
Description |
dataToRemove |
array
|
an array of objects with keys corresponding to horAxis and verAxis |
setMaxValueHonour(enable) → {boolean}
Change whether the bar(s) in the graph with the maximum value should have celebration effects and redraw in
accordance.
Parameters:
Name |
Type |
Description |
enable |
boolean
|
optional (otherwise, simply toggle setting) boolean indicating whether to
honour
maximum value bar or not
|
Returns:
boolean whether the maximum value bar is to be honoured or not
sortData(state) → {boolean}
Sort the data displayed in the BarGraph and re-draw the bars.
Parameters:
Name |
Type |
Default |
Description |
state |
number
|
1 |
1 if to be sorted ascending, 0 if no sorting is to occur (or to revert
sorting), -1
if to be sorted descending
|
Returns:
boolean whether the operation was successful
updateScale() → {number}
Update the scale of the graph based on the dataset and tableHeight
Returns:
number maximum value of the data
ScatterPlot Class
new ScatterPlot(title, data, vertAxis, horAxis, parentElement, legendKey)
Constructor for ScatterPlot
Parameters:
Name |
Type |
Description |
title |
string
|
the title of the graph |
data |
object
|
array of objects containing the data to be displayed;
data[vertAxis] and data[horAxis] must be numerical
|
vertAxis |
string
|
string representing the attribute in all elements of data to be
represented vertically
|
horAxis |
string
|
string representing the attribute in all elements of data to be
represented horizontally
|
parentElement |
HTMLElement
|
element that is to be the parent element of the graph |
legendKey |
string
|
optional - string representing the attribute in all elements of
data describing context
|
Methods
changeTableDimensions(width, height) → {boolean}
Change the dimensions (in pixels) of the table.
Parameters:
Name |
Type |
Description |
width |
number
|
numerical pixel value > 0 or null if current width is to be
maintained
|
height |
number
|
numerical pixel value > 0 or null if current height is to be
maintained
|
Returns:
boolean whether width and/or height were changed successfully
drawData()
Draw the data (dots) to the graph and update scale.
updateDotSize(newSize) → {boolean}
Change the size of the data dots in the ScatterPlot.
Parameters:
Name |
Type |
Description |
newSize |
number
|
the size (> 0) in pixels to change the dot size to |
Returns:
boolean whether the input was valid and change was successful
updateScale()
Re-process the scales and display the new version.
PieChart Class
new PieChart(title, data, valueKey, labelKey, parentElement)
Constructor for PieChart.
Parameters:
Name |
Type |
Description |
title |
string
|
the title of the pie chart |
data |
object
|
array of objects containing the data to be displayed;
data[valueKey] must contain the numerical data and data[labelKey] a corresponding string
|
valueKey |
string
|
string representing the attribute in all elements of data |
labelKey |
string
|
string representing the attribute in all elements of data |
parentElement |
HTMLElement
|
element that is to be the parent element of the graph |
Methods
addData(dataToAdd)
Add data provided in dataToAdd to the graph.
NOTE: this data will be appended to the data array provided in the constructor.
Parameters:
Name |
Type |
Description |
dataToAdd |
array
|
an array of objects with keys corresponding to horAxis and verAxis |
changeTableDimensions(diameter) → {boolean}
Change the dimensions (in pixels) of the PieChart.
Parameters:
Name |
Type |
Description |
diameter |
number
|
numerical pixel value > 0 |
Returns:
boolean whether diameter was changed successfully and PieChart re-drawn
clearColours()
Clear any custom colours for PieChart that have been set and re-draw pie slices if there have been
colours set.
drawData()
Draw the data (a.k.a. pie slices) to the graph alongside (if enabled) an updated legend.
removeData(dataToRemove)
Remove the data specified in dataToRemove from the PieChart.
NOTE: this data will be removed from the data array provided in the constructor.
Parameters:
Name |
Type |
Description |
dataToRemove |
array
|
an array of objects with keys corresponding to horAxis and verAxis |
setColours(colours) → {boolean}
Set the colours of the pie slices to values of the array of colour value strings.
Parameters:
Name |
Type |
Description |
colours |
|
array with length this.data.length containing colour value strings
|
Returns:
boolean whether the colour change was successful
setLegendVisibility(isVisible) → {boolean}
Change whether the legend should be visible.
Parameters:
Name |
Type |
Description |
isVisible |
boolean
|
optional (otherwise, simply toggle setting) boolean indicating whether
display or not
|
Returns:
boolean whether the legend is now visible or not
sortData(state) → {boolean}
Sort the data displayed in the PieChart and re-draw the pie slices.
Parameters:
Name |
Type |
Default |
Description |
state |
number
|
1 |
1 if to be sorted ascending clockwise, 0 if no sorting is to occur (or
to revert sorting), -1 if to be sorted descending clockwise
|
Returns:
boolean whether the operation was successful
updateLegend()
Update and draw the legend according to the dataset
Note: Pie Slice colours are not updated (unless drawData has been called)
generateColours(num) Function
generateColours(num) → {array}
Generate an array of length num of colour values 'rgb(r,g,b)' as strings.
For all colour values to be unique, num <= 768.
Parameters:
Name |
Type |
Description |
num |
number
|
the integer number of colours to be returned |
Returns:
array colours array of length num of strings with colour values