SymbolixAU / mapdeck
1
#' Clear Legend
2
#'
3
#' Clears the legend for a given layer_id
4
#'
5
#' @param map_id the id of the map you want to clear the legend from.
6
#' @param layer_id single value specifying an id for the layer. Use this value to
7
#' distinguish between shape layers of the same type. Layers with the same id are likely
8
#' to conflict and not plot correctly
9
#'
10
#' @export
11
clear_legend <- function( map_id, layer_id ) {
12 0
	invoke_method( map_id, "md_clear_legend", layer_id );
13
}
14

15

16
#' Mapdeck Legend
17
#'
18
#' Constructs legend elements into the correct JSON format for plotting on the map
19
#'
20
#' @param legend_elements vector of legend elements (made from \link{legend_element})
21
#'
22
#' @examples
23
#' l1 <- legend_element(
24
#' 	variables = c("a","b")
25
#' 	, colours = c("#00FF00","#FF0000")
26
#' 	, colour_type = "fill"
27
#' 	, variable_type = "category"
28
#' 	, title = "my title"
29
#' )
30
#'
31
#' mapdeck_legend(l1)
32
#'
33
#' @seealso \link{legend_element}
34
#'
35
#' @export
36 1
mapdeck_legend <- function(legend_elements) UseMethod("mapdeck_legend")
37

38
#' @export
39 1
mapdeck_legend.mapdeck_legend <- function( legend_elements ) jsonify::to_json(legend_elements)
40

41
#' @export
42
mapdeck_legend.default <- function( legend_elements ) {
43 1
	stop("mapdeck - mapdeck_legend will only work on objects created with legend_element")
44
}
45

46
#' Legend Element
47
#'
48
#' Creates a mapdeck legend element for when you want to manually specify a legend (using \link{mapdeck_legend})
49
#'
50
#' @param variables variables assigned to colours
51
#' @param colours vector of hex colours assigned to variables
52
#' @param colour_type one of "fill" or "stroke"
53
#' @param variable_type one of category (discrete) or gradient (continuous)
54
#' @param title string used as the legend title
55
#' @param css string of css to control appearance.
56
#'
57
#' @seealso \link{mapdeck_legend}
58
#'
59
#' @examples
60
#'
61
#' l1 <- legend_element(
62
#' 	variables = c("a","b")
63
#' 	, colours = c("#00FF00","#FF0000")
64
#' 	, colour_type = "fill"
65
#' 	, variable_type = "category"
66
#' 	, title = "my title"
67
#' )
68
#'
69
#' @export
70
legend_element <- function(
71
	variables,
72
	colours,
73
	colour_type = c("fill", "stroke"),
74
	variable_type = c("category", "gradient"),
75
	title = "",
76
	css = ""
77
	) {
78

79 1
	if( length( colours ) != length( variables ) ) {
80 0
		stop("mapdeck - colours and variables should be the same length")
81
	}
82

83 1
	colour_type <- legend_colour_type( colour_type )
84

85 1
	l <- list(
86 1
		colour = colours
87 1
		, variable = variables
88 1
		, colourType = colour_type
89 1
		, type = variable_type
90 1
		, title = title
91 1
		, css = css
92
	)
93

94 1
	l <- list( l )
95 1
	names(l) <- colour_type
96 1
	attr(l, "class") <- c("mapdeck_legend", attr(l, "class"))
97 1
	return(l)
98
}
99

100

101
legend_colour_type <- function( colour_type ) {
102 1
	switch(
103 1
		colour_type
104 1
		, "fill" = "fill_colour"
105 1
		, "stroke" = "stroke_colour"
106
	)
107
}
108

109
aggregation_legend <- function( legend, legend_options ) {
110

111 1
	if( is.null( legend_options ) ) {
112 1
		legend_options <- list(
113 1
			css = ""
114 1
			, title = "value"
115 1
			, digits = 2
116
		)
117
	}
118 1
	if( is.null( legend_options[["css"]] ) ) {
119 0
		legend_options[["css"]] <- ""
120
	}
121

122 1
	if( is.null( legend_options[["title"]] ) ) {
123 0
		legend_options[["title"]] <- "value"
124
	}
125

126 1
	if( is.null( legend_options[["digits"]] ) ) {
127 0
		legend_options[["digits"]] <- 2
128
	}
129

130 1
	legend <- list(
131 1
		legend = legend
132 1
		, css = legend_options[["css"]]
133 1
		, title = legend_options[["title"]]
134 1
		, digits = legend_options[["digits"]]
135
	)
136

137 1
	return( legend )
138
}
139

140

Read our documentation on viewing source code .

Loading