addMinicharts: Add or update charts on a leaflet map

Description Usage Arguments Details Value Examples

View source: R/add_minicharts.R

Description

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.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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)

Arguments

map

A leaflet map object created with leaflet.

lng

Longitude where to place the charts.

lat

Latitude where to place the charts.

chartdata

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.

time

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.

maxValues

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

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.

fillColor

Used only if data contains only one column. It is the color used to fill the circles.

colorPalette

Color palette to use when chartdata contains more than one column.

width

maximal width of the created elements.

height

maximal height of the created elements.

opacity

Opacity of the chart.

showLabels

Should values be displayed above chart elements.

labelText

character vector containing the text content of the charts. Used only if chartdata contains only one column.

labelMinSize

Minimal height of labels in pixels. When there is not enough space for labels, they are hidden.

labelMaxSize

Maximal height of labels in pixels.

labelStyle

Character string containing CSS properties to apply to the labels.

transitionTime

Duration in milliseconds of the transitions when a property of a chart is updated.

popup

Options that control popup generation.

layerId

An ID variable. It is mandatory when one wants to update some chart with updateMinicharts.

legend

If TRUE and if data has column names, then a legend is automatically added to the map.

legendPosition

Where should legend be placed?

timeFormat

Character string used to format dates and times when argument time is a Date, POSIXct or POSIXlt object. See strptime for more information.

initialTime

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.

onChange

(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.

popupOptions

Change default popupOptions (ex : autoClose, maxHeight, closeButton ...) See popupOptions for more informations.

Details

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
  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:

this

The current minichart object. See https://rte-antares-rpackage.github.io/leaflet.minichart/-_L.Minichart_.html for more information.

opts

The current options passed to the current minichart object.

popup

Popup html.

d3

The D3 module.

Here is a toy example

Value

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.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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))

leaflet.minicharts documentation built on May 11, 2021, 5:07 p.m.