these functions add or update minicharts in a leaflet map at given coordinates: they can be bar charts, pie charts or polar charts where chartdata is encoded either by area or by radius.
addMinicharts(
map,
lng,
lat,
chartdata = 1,
time = NULL,
maxValues = NULL,
type = "auto",
fillColor = d3.schemeCategory10[1],
colorPalette = d3.schemeCategory10,
width = 30,
height = 30,
opacity = 1,
showLabels = FALSE,
labelText = NULL,
labelMinSize = 8,
labelMaxSize = 24,
labelStyle = NULL,
transitionTime = 750,
popup = popupArgs(),
layerId = NULL,
legend = TRUE,
legendPosition = "topright",
timeFormat = NULL,
initialTime = NULL,
onChange = NULL,
popupOptions = NULL
)updateMinicharts(
map,
layerId,
chartdata = NULL,
time = NULL,
maxValues = NULL,
type = NULL,
fillColor = NULL,
colorPalette = d3.schemeCategory10,
width = NULL,
height = NULL,
opacity = NULL,
showLabels = NULL,
labelText = NULL,
labelMinSize = NULL,
labelMaxSize = NULL,
labelStyle = NULL,
transitionTime = NULL,
popup = NULL,
legend = TRUE,
legendPosition = NULL,
timeFormat = NULL,
initialTime = NULL,
onChange = NULL,
popupOptions = NULL
)
removeMinicharts(map, layerId)
clearMinicharts(map)
A leaflet map object created with leaflet
.
Longitude where to place the charts.
Latitude where to place the charts.
A numeric matrix with number of rows equal to the number of
elements in lng
or lat
and number of column equal to the
number of variables to represent. If parameter time
is set, the
number of rows must be equal to the length of lng
times the number
of unique time steps in the data.
A vector with length equal to the number of rows in chartdata
and containing either numbers representing time indices or dates or
datetimes. Each unique value must appear as many times as the others. This
parameter can be used when one wants to represent the evolution of some
variables on a map.
maximal absolute values of the variables to represent.
It can be a vector with one value per column of chartdata
or a single
value. Using a single value enforces charts to use a unique scale for all
variables. If it is NULL
, the maximum value of chartdata
is used.
Type of chart. Possible values are "bar"
for bar charts,
"pie"
for pie charts, "polar-area"
and "polar-radius"
for polar area charts where the values are represented respectively by the
area or the radius of the slices. Finally it can be equal to "auto"
,
the default. In this case, if there is only one variable to represent, the
chart will be a single circle, else it is a barchart.
Used only if data contains only one column. It is the color used to fill the circles.
Color palette to use when chartdata
contains more than
one column.
maximal width of the created elements.
maximal height of the created elements.
Opacity of the chart.
Should values be displayed above chart elements.
character vector containing the text content of the charts.
Used only if chartdata
contains only one column.
Minimal height of labels in pixels. When there is not enough space for labels, they are hidden.
Maximal height of labels in pixels.
Character string containing CSS properties to apply to the labels.
Duration in milliseconds of the transitions when a property of a chart is updated.
Options that control popup generation.
An ID variable. It is mandatory when one wants to update some
chart with updateMinicharts
.
If TRUE and if data has column names, then a legend is automatically added to the map.
Where should legend be placed?
Character string used to format dates and times when
argument time
is a Date
, POSIXct
or POSIXlt
object. See strptime
for more information.
This argument can be used to set the initial time step
shown when the map is created. It is used only when argument time
is
set.
(For power users who know javascript) A character string containing javascript code that is executed each time a chart is updated. See the details section to understand why and how to use this parameter.
Change default popupOptions (ex : autoClose, maxHeight, closeButton ...)
See popupOptions
for more informations.
The modified leaflet map object. addMinicharts
add new minicharts to
the map. updateMinicharts
updates minicharts that have already been
added to the map. removeMinicharts
removes some specific charts from
the map and clearMinicharts
removes all charts from the map and
if necessary the legend that has been automatically created.
Since version 0.5, the parameter onChange
can be used to execute
some arbitrary javascript code each time a chart is updated (with
updateMinicharts()
or when time step changes). A typical use case
would be to change the color of a polygon added with
addPolygons
based on the data of the chart. It is even
possible to create an invisible chart and use it to manage the color and the
popup of a polygon. Here is a sample code that do that:
leaflet() %>% addTiles() %>% addPolygons(data = myPolygons, layerId = myPolygons$myIds) %>% addMinicharts( mydata$lon, mydata$lat, time = mydata$time fillColor = mydata$color, layerId = mydata$myIds, width = 0, height = 0, onChange = " var s = this._map.layerManager.getLayer("shape", this.layerId); s.bindPopup(popup); if (opts.fillColor) { d3.select(s._path) .transition() .duration(750) .attr("fill", opts.fillColor); }" )
The following objects are available when executing the javascript code:
The current minichart object. See https://rte-antares-rpackage.github.io/leaflet.minichart/-_L.Minichart_.html for more information.
The current options passed to the current minichart object.
Popup html.
The D3 module.
Here is a toy example
# NOT RUN {
require(leaflet)
mymap <- leaflet() %>% addTiles() %>% addMinicharts(0, 0, chartdata = 1:3, layerId = "c1")
mymap
mymap %>% updateMinicharts("c1", maxValues = 6)
mymap %>% updateMinicharts("c1", type="pie")
# popupOptions
mymap <- leaflet() %>% addTiles() %>%
addMinicharts(0, 0, chartdata = 1:3, layerId = "c1", popupOptions = list(closeButton = FALSE))
mymap
mymap %>% updateMinicharts("c1", maxValues = 6, popupOptions = list(closeButton = TRUE))
# }
Run the code above in your browser using DataLab