findTimeFormat
(str)
Return the timeformat in d3.timeParse function
parameter | type | description |
---|---|---|
str |
string | string containing d3.timeParse(xxx) |
string
:
time format argument of d3.timeParse function
unique
(array, sorting, _sort)
Return unique values of an array (including if there are dates)
parameter | type | description |
---|---|---|
array |
Array | array |
sorting |
boolean
= true
|
whether to sort the array |
_sort |
function
= undefined
|
sorting function |
Array
:
of unique values of 'arr'
let uniqueArray = unique([1, 1, 2, 3 ])
uniqueArray
> [1, 2, 3]
getOptimalPrecision
(maxN)
Get optimal decimal precision for number formatting
parameter | type | description |
---|---|---|
maxN |
number | maximum number in the data |
string
:
optimal format for d3.format
updateDict
(dict, newDict)
Update a nested dictionary (stored as an Object)
parameter | type | description |
---|---|---|
dict |
Object | Object to update with values in newDict |
newDict |
Object | Object containing key:values to update dict with |
getDimensionText
(text, fontSize)
Retrieve the width and height of some text based on its font
parameter | type | description |
---|---|---|
text |
string | text to be measured |
fontSize |
string | fontSize of the text |
Object
:
Object with width and height of the text as if it was on the DOM
to_csv
(j, header)
Transform a JSON object into a csv
parameter | type | description |
---|---|---|
j |
Array<Object> | Array of objects, each object should has the same number of keys (and each key is going to be a column). Each element of the array is going to be a line. |
header |
Boolean
= true
|
If true, first line of the csv fine will be a header composed of the first object keys |
String
:
return a csv file as a string
splitString
(s, n, sep)
Split a string in substrings of length of approx. n characters and splitting on space only. The function will split the string in substring of minimum length 'n' on spaces inside the string.
parameter | type | description |
---|---|---|
s |
string | string to be split |
n |
number | length of bits to split string 's' |
sep |
string
= "|"
|
separator to be used to for splitting the string. The character should not be inside the string. |
Array
:
array of substrings
wideToLong
(wideData, pivotColumns, keyName, valueName, category, mapLongElement)
Transform a 'wide' array of Dict to 'long' array, pivoting on some 'pivot' columns (keys). The long format is a {id, key, value}, where id is one or multiple 'pivot' columns, 'key' are the non pivot columns of the original array and the value are the value of the corresponding keys. A category value can also be passed as a new 'column'. The long format becomes {id, key, value, category}. A mapping function can also be passed to be applied to each long element (for instance to parse a date)
parameter | type | description |
---|---|---|
wideData |
Array | data to pivot |
pivotColumns |
Array | list of keys on which to pivot from wide to long |
keyName |
string
= "field_id"
|
name of the key for variable name in the long format. |
valueName |
string
= "field_value"
|
name of the key for value in the long format |
category |
string?
= "undefined"
|
optional category key to be added in the long data |
mapLongElement |
function (Dict)
= undefined
|
optional function to be applied on each long element |
wideArray = [{'date':'2020-07-19','temperature':32,'pressure':1016},
{'date':'2020-07-20','temperature':25,'pressure':1020}];
longArray = wideToLong(wideArray, ['date']);
longArray = [{'date':'2020-07-19', 'field_id':'temperature','field_value':32},
{'date':'2020-07-19', 'field_id':'pressure','field_value':1016},
{'date':'2020-07-20', 'field_id':'temperature','field_value':25},
{'date':'2020-07-20', 'field_id':'pressure','field_value':1020}]
longToWide
(longData, index, column, value, keyName, valueName, mapLongElement)
Transform a 'long' array of Dict to 'wide' array, pivoting on some 'index' columns (keys).
parameter | type | description |
---|---|---|
longData |
Array | data to pivot |
index |
Array | column(s) to pivot on |
column |
any
= 'field_id'
|
|
value |
any
= 'field_value'
|
|
keyName |
string
= "field_id"
|
name of the key for variable name in the long format. |
valueName |
string
= "field_value"
|
name of the key for value in the long format |
mapLongElement |
function (Dict) | optional function to be applied on each long element |
longArray = [{'date':'2020-07-19', 'field_id':'temperature','field_value':32},
{'date':'2020-07-19', 'field_id':'pressure','field_value':1016},
{'date':'2020-07-20', 'field_id':'temperature','field_value':25},
{'date':'2020-07-20', 'field_id':'pressure','field_value':1020}]
longToWide(longArray, 'date', 'field_id', 'field_value');
wideArray = [{'date':'2020-07-19','temperature':32,'pressure':1016},
{'date':'2020-07-20','temperature':25,'pressure':1020}];
barycenterColor
(startColor, endColor, weight)
Compute the barycenter between two colors. Returned color will be startColor weight + endColor (1 - weight)
parameter | type | description |
---|---|---|
startColor |
string | starting color in rgb / hsl format |
endColor |
string | ending color in rgb / hsl format |
weight |
number | weight of the first color |
string
:
the barycenter color
getBackgroundColor
(el, defaultColor)
Return the background color of an element, if transparent returns the background color of the parent element. If no parent, then the defaultColor is returned.
parameter | type | description |
---|---|---|
el |
string | DOM element to retrieve bacground color from |
defaultColor |
string
= "rgb(255, 255, 255)"
|
color to be returned if no background color is found. |
groupBy
(array, key, colSum, colCount, colFirst)
This function mimics Python pandas function group by.
parameter | type | description |
---|---|---|
array |
Array | the array to group by |
key |
string | key to group by with |
colSum |
Array
= []
|
array of columns to agg by sum |
colCount |
Array
= []
|
array of columns to agg by count |
colFirst |
Array
= []
|
array of columns to keep first |
GrapherBase
(id, type, width, height)
Base class for Grapher linking the chart to the DOM element and creating the svg
parameter | type | description |
---|---|---|
id |
string | id of the DOM element |
type |
string | Chart type |
width |
number
= null
|
width of the DOM element (if not already setup in HTML or CSS) |
height |
number
= null
|
height of the DOM element (if not already setup in HTML or CSS) |
wipe
()
Wipe the graph elements created by draw without removing the core elements so that the graph can be drawn again with .draw()
downloadData
()
Download the data associated with the Grapher element
Chart
(id, options, width, height)
The Chart
object represents the graph.
You create a Chart
by specifying a container
(a DOM element)
that will contain the graph, and other options.
parameter | type | description |
---|---|---|
id |
string | of the DOM element |
options |
Object | Set of options for the graph. |
options.data |
Array<Object>
(default [] )
|
Array of JSON containing the data |
options.x | Object | Set of options for the x axis |
options.x.name |
string
(default x )
|
Name of the x-axis variable in options.data |
options.x.scale |
string
(default "scaleLinear" )
|
d3-scale, see d3-scale on Github |
options.x.parse |
(function | null)
(default null )
|
Function to parse the data (useful if x-axis is a date) |
options.x.tickFormat |
(function | null)
(default null )
|
Function to format the variable when displayed as tick or in tooltip |
options.x.label |
(string | null)
(default null )
|
Label for x-axis |
options.x.domain |
(Array<number> | null)
(default null )
|
Hardcode the x-axis bound. Default is to use min and max of x values in options.data |
options.x.nice |
Boolean
(default true )
|
Extends the domain so that it starts and ends on nice round values (applying d3.nice()), for further reference see d3-scale on Github |
options.x.padding | (number | null)? | Padding to be used between bars when x.scale is scaleBand. |
options.x.values | (Array<string> | null)? | List of values of x axis. Derived from the data if not provided. |
options.y | Object | Set of options for the y axis |
options.y.name |
string
(default x )
|
name of the y-axis variable in options.data |
options.y.scale |
string
(default "scaleLinear" )
|
d3-scale, see d3-scale on Github |
options.y.parse |
(function | null)
(default null )
|
function to parse the data (useful if y-axis is a date) |
options.y.tickFormat |
(function | null)
(default null )
|
function to format the variable when displayed as tick or in tooltip |
options.y.label |
(string | null)
(default null )
|
label for y-axis |
options.y.domain |
(Array<number> | null)
(default null )
|
Hardcode the y-axis bound. Default is to use min and max of y values in options.data |
options.y.nice |
Boolean
(default true )
|
Extends the domain so that it starts and ends on nice round values (applying d3.nice()), for further reference see d3-scale on Github |
options.category | Object | set of options for category |
options.category.name |
(string | null)
(default null )
|
name of the category variable in options.data |
options.category.parse |
function
(default (d=>d) )
|
function to parse (or format) the category variable when displayed in tooltip |
options.categories |
(Array<string> | Boolean)
(default false )
|
Hardcode the list of elements of the 'category' variable to select only data's elements belonging to this list. When no list is specified, the list of elements is derived from the data and all 'category' values found in the data are considered. |
options.type |
string
(default "line" )
|
type of the graph. Possible types are "line", "bar", "stacked-bar", "stacked-area", "dotted-line", "dot", "sparkline" |
options.style | Object | list of options for styling the elements of the graph |
options.style.colors | Array<string> | List of colors for the lines, bars, dots (not applicable for sparkline). Default is ["#1abb9b","#3497da","#9a59b5","#f0c30f","#e57e22","#e64c3c","#7f8b8c","#CC6666", "#9999CC", "#66CC99"] |
options.style.barWidth |
number
(default 0.8 )
|
Share of the x axis width that should be filled with bars. Setting to 0.8, lets 20% in space between bars. |
options.style.strokeWidth |
number
(default 3 )
|
stroke-width of the line. Default is 3px |
options.style.dotSize |
number
(default 4 )
|
dot-size of the dots. Default is 4px |
options.style.tooltipColor |
string
(default "#181818" )
|
Text color in the tooltip |
options.style.tooltipBackgroundColor |
string
(default "#ffffff" )
|
Background color of the tooltip |
options.style.tooltipOpacity |
string
(default "0.8" )
|
Opacity of the tooltip. Default is 0.8 (80%) |
options.style.tooltipLineColor |
string
(default "#000000" )
|
Color of the vertical line color |
options.style.beautifyClosestLine |
(string | function)
(default "widen" )
|
Beautify the closest line to current mouse cursor (only works for line or dotted-lines). Possible options are either "widen" (increase the stroke-width by 1), "color" (color the closest line, grayed the others) or you can pass a function that will be applied on every lines and takes four parameters: the element itself, whether the line is the closest, the path and the category. |
options.grid | Object | Options for the grid |
options.grid.x | Object | Options for grid on the x-axis |
options.grid.x.show |
Boolean
(default false )
|
if true, grid will be added (same frequency as ticks) |
options.grid.x.lines |
Array<{x: string, label: string, position: string, y: string, textAnchor: string, class: string}>
(default [] )
|
Array for vertical lines. For each line, you should at least specify the position on the x-axis (same format as x axis). You can add a label for which you can specify its position by choosing 'position' value among 'start', 'middle', 'end'. The label can also be more precisely positionned by specifying a 'y' and textAnchor value among 'start', 'middle', 'end'. You can also add a class with 'class'. |
options.grid.y | Object | Options for grid on the y-axis |
options.grid.y.show |
Boolean
(default true )
|
if true, grid will be added (same frequency as ticks) |
options.grid.y.lines |
Array<{y: string, label: string, position: string, x: string, textAnchor: string, class: string}>
(default [] )
|
Array for horizontal lines. For each line, you should at least specify the position on the y-axis (same format as y axis). You can add a label for which you can specify its position by choosing 'position' value among 'start', 'middle', 'end'. The label can also be more precisely positionned by specifying a 'x' and textAnchor value among 'start', 'middle', 'end'. You can also add a class with 'class'. |
options.legend | Object | Options for the legend |
options.legend.show |
Boolean
(default true )
|
show legend. If false, legend will not be displayed. |
options.legend.x |
number
(default 15 )
|
legend's x position in the svg |
options.legend.y | number | legend's y position in the svg. Default value equals the margin-top of the g element inside the svg. |
options.legend.interstice |
number
(default 25 )
|
space in px between the legend items |
options.legend.backgroundColor |
string
(default "#ffffff" )
|
legend's background color |
options.legend.opacity |
string
(default "0.9" )
|
opacity of the legend |
options.download | Object | options for downloading data associated with the graph |
options.filename |
string
(default "data_<elementId>_<date>.csv" )
|
filename for the csv linked to the data |
options.sparkline | Object | options for the sparkline object |
options.sparkline.range |
(Array<number> | null)
(default null )
|
range for the background's band displayed in the sparkline. If null, not the band is not displayed. |
options.sparkline.textLastPoint |
Boolean
(default true )
|
display the last point's value as adjacent text. |
options.sparkline.strokeWidth |
number
(default 1 )
|
stroke's width of the sparkline |
options.sparkline.lineColor |
string
(default "#000000" )
|
line's color of the sparkline |
options.sparkline.circleColor |
string
(default "#f00" )
|
circle's color for the last point of the sparkline |
options.sparkline.textColor |
string
(default "#f00" )
|
text's color of the last point value displayed next to the sparkline |
options.sparkline.textFontSize |
string
(default "85%" )
|
text's font size |
options.sparkline.textFontWeight |
string
(default "600" )
|
text's font weight |
options.sparkline.rangeFillColor |
string
(default "#ccc" )
|
range's band fill color |
options.sparkline.rangeFillColor |
string
(default "#ccc" )
|
range's band fill color |
options.advanced | Object | Advanced options |
options.advanced.additionalColumnsInData |
Array<string>
(default [] )
|
Grapher will only keep x, y and category when parsing the data. Passing columns names here will preserve them in the data. |
width |
number
= null
|
width of the DOM element (if not already setup in HTML or CSS) |
height |
number
= null
|
height of the DOM element (if not already setup in HTML or CSS) |
let myGraphExample1 = new Grapher('myGraphExample1',
{"data": data,
"x": {
"name": "date",
"scale": "scaleTime",
"parse": (d) => d3.timeParse('%Y-%m-%d')(d),
"tickFormat": d3.timeFormat("%d/%m/%y"),
"label": "Date"
},
"y": {
"name": "value",
"tickFormat": d3.format('.3s'),
"label": "Glucose"
},
"type": "dotted-line",
});
draw
(options)
Draw the Grapher object
parameter | type | description |
---|---|---|
options |
(Object | null) | you can pass an options Object to update the element's option. @link Grapher |
margin
(margin)
Get and sets the values of the inner margins
parameter | type | description |
---|---|---|
margin |
Object | |
margin.top |
string
(default 10 )
|
margin top |
margin.right |
(string | function)
(default (x)=>x<400?20:30 )
|
margin right depends of the width of the graph |
margin.bottom | string | margin bottom |
margin.left |
(string | function)
(default (x)=>x<400?40:60 )
|
margin left depends of the width of the graph |
Object
:
margin
(margin)
Get and sets the values of the inner margins
parameter | type | description |
---|---|---|
margin |
Object | |
margin.top |
string
(default 10 )
|
margin top |
margin.right |
(string | function)
(default (x)=>x<400?20:30 )
|
margin right depends of the width of the graph |
margin.bottom | string | margin bottom |
margin.left |
(string | function)
(default (x)=>x<400?40:60 )
|
margin left depends of the width of the graph |
Object
:
options
(options)
Gets and sets the option for Grapher
object.
Note that setter is called only by typing myGraph.options = {something};
Typing myGraph.options.data = something; do work but setter is not called and data is not parsed again, etc.
parameter | type | description |
---|---|---|
options |
Object | @link Grapher |
draw
(options)
Draw the Grapher object
parameter | type | description |
---|---|---|
options |
(Object | null) | you can pass an options Object to update the element's option. @link Grapher |
Donut
(id, options, displayText, size, type)
The Donut class is a children class of the Grapher class. This is still a work in progress. Currently the Donut is mainly a Gauge.
parameter | type | description |
---|---|---|
id |
string | id of the DOM element |
options |
Object | Set options for the Gauge |
options.value |
number
(default 50 )
|
Value to display |
options.foregroundColor | function | Function to plot a foreground color (function of the value). Default is d => #1abb9b. It's a function rather than a string so that it allows you to change the color depending on the value. |
options.backgroundColor | string | Background color of the donut. Default is the "weighted average" between the background color and foreground color with a 60% weight on the background color. |
options.innerRadius |
number
(default 0.4*size )
|
inner radius of the donut. Default is 40% of the size. |
options.outerRadius |
number
(default 0.5*size )
|
outer radius of the donut. Default is 50% of the size. |
options.startAngle |
number
(default -180 )
|
starting angle. Default is -180 (other values allows you to plot less than a "circle"). |
options.endAngle |
number
(default 180 )
|
ending angle. Default is 180 |
options.minValue |
number
(default 0 )
|
mininum value when the angle is -180, default is 0. |
options.maxValue |
number
(default 100 )
|
maximum value when the angle is 180, default is 100. |
options.displayValue | boolean? | display value inside the donut. |
options.fontSize | string | fontSize of the displayed value inside the donut. Default is the font size of the DOM element. |
options.cornerRadius |
number
(default 0 )
|
corner radius of the arc (see cornerRadius of d3.arc for more details) |
options.maxHeight |
number
(default size )
|
maximum height of the DOM element. Default is DOM element size. |
options.padAngle |
number
(default 0.03 )
|
pad angle of the arc (see padAngle of d3.arc for more details) |
displayText |
function | text to be displayed inside the donut. It's a function of the value. Default d => d (e.g. dispkay the value). |
size |
(number | null) | size of the gauge (for a gauge, width and height are the same). |
type |
string
= "donut"
|
type of the graph. |
let myArc = new g.Donut("myArc",
{"value": 30},
size=100);
myArc.draw();
_draw
()
Return a d3 scale for colors
Gauge
(id, options, size)
The Gauge is a child of Donut class with convenient defaults for plotting a gauge.
parameter | type | description |
---|---|---|
id |
string | id of the DOM element |
options |
Object | Set options for the Gauge |
size |
(number | null) | size of the gauge (for a gauge, width and height are the same). |
let myGauge = new g.Donut("myGauge",
{"value": 20},
size=100);
myGauge.draw();