-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathaxis_setting.R
182 lines (163 loc) · 8.07 KB
/
axis_setting.R
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#' @title x axis settings
#' @description Set x axis properties.
#' @param x chart object
#' @param orientation `character(1)`: axis orientation,
#' one of \Sexpr[stage=render, results=rd]{mschart:::choices_rd(mschart:::st_orientation)}.
#' @param crosses `character(1)`: specifies how the axis crosses the perpendicular axis,
#' one of \Sexpr[stage=render, results=rd]{mschart:::choices_rd(mschart:::st_crosses)}.
#' @param cross_between `character(1)`: specifies how the value axis crosses the category
#' axis between categories,
#' one of \Sexpr[stage=render, results=rd]{mschart:::choices_rd(mschart:::st_crossbetween)}.
#' @param major_tick_mark,minor_tick_mark `character(1)`: tick marks position,
#' one of \Sexpr[stage=render, results=rd]{mschart:::choices_rd(mschart:::st_tickmark)}.
#' @param tick_label_pos `character(1)`: ticks labels position,
#' one of \Sexpr[stage=render, results=rd]{mschart:::choices_rd(mschart:::st_ticklblpos)}.
#' @param display `logical(1)`: should the axis be displayed.
#' @param num_fmt `character(1)`: number formatting. It can be "General", "0.00", "#,##0",
#' "#,##0.00", "mm-dd-yy", "m/d/yy h:mm", etc.
#' @param rotation `integer(1)`: rotation angle. Value should be between `-360` and `360`.
#' @param limit_min `numeric(1)`: minimum value on the axis.
#' @param limit_max `numeric(1)`: maximum value on the axis.
#' @param position `numeric(1)`: position value that cross the other axis.
#' @param second_axis `logical(1)`: unused
#' @seealso \code{\link{chart_ax_y}}
#' @export
chart_ax_x <- function( x, orientation, crosses, cross_between,
major_tick_mark, minor_tick_mark,
tick_label_pos, display,
num_fmt, rotation,
limit_min, limit_max, position,
second_axis = FALSE ){
UseMethod("chart_ax_x")
}
#' @export
#' @title y axis settings
#' @description Set y axis properties.
#' @inheritParams chart_ax_x
#' @seealso \code{\link{chart_ax_x}}
chart_ax_y <- function( x, orientation, crosses, cross_between,
major_tick_mark, minor_tick_mark,
tick_label_pos, display,
num_fmt, rotation,
limit_min, limit_max, position,
second_axis = FALSE ){
UseMethod("chart_ax_y")
}
#' @export
#' @describeIn chart_ax_x chart_ax_x method for ms_chart objects
chart_ax_x.ms_chart <- function( x, orientation, crosses, cross_between,
major_tick_mark, minor_tick_mark,
tick_label_pos, display,
num_fmt, rotation,
limit_min, limit_max, position,
second_axis = FALSE ){
options <- list( orientation = ifelse(missing(orientation), x$x_axis$orientation, orientation),
axis_position = ifelse( second_axis, "r", "l" ),
crosses = ifelse(missing(crosses), x$x_axis$crosses, crosses),
cross_between = ifelse(missing(cross_between), x$x_axis$cross_between, cross_between),
major_tick_mark = ifelse(missing(major_tick_mark), x$x_axis$major_tick_mark, major_tick_mark),
minor_tick_mark = ifelse(missing(minor_tick_mark), x$x_axis$minor_tick_mark, minor_tick_mark),
tick_label_pos = ifelse(missing(tick_label_pos), x$x_axis$tick_label_pos, tick_label_pos),
delete = ifelse(missing(display), x$x_axis$delete, !display),
num_fmt = ifelse(missing(num_fmt), x$x_axis$num_fmt, num_fmt),
rotation = ifelse(missing(rotation), x$x_axis$rotation, rotation)
)
if( missing(limit_min) && !is.null(x$x_axis$limit_min) ){
options$limit_min <- x$x_axis$limit_min
} else if( !missing(limit_min) ){
options$limit_min <- limit_min
}
if( missing(limit_max) && !is.null(x$x_axis$limit_max) ){
options$limit_max <- x$x_axis$limit_max
} else if( !missing(limit_max) ){
options$limit_max <- limit_max
}
if( missing(position) && !is.null(x$x_axis$position) ){
options$position <- x$x_axis$position
} else if( !missing(position) ){
options$position <- position
}
x$x_axis <- do.call(axis_options, options)
x
}
#' @export
#' @describeIn chart_ax_y chart_ax_y method for ms_chart objects
chart_ax_y.ms_chart <- function( x, orientation, crosses, cross_between,
major_tick_mark, minor_tick_mark,
tick_label_pos, display,
num_fmt, rotation,
limit_min, limit_max, position,
second_axis = FALSE ){
options <- list( orientation = ifelse(missing(orientation), x$y_axis$orientation, orientation),
axis_position = ifelse( second_axis, "r", "l" ),
crosses = ifelse(missing(crosses), x$y_axis$crosses, crosses),
cross_between = ifelse(missing(cross_between), x$y_axis$cross_between, cross_between),
major_tick_mark = ifelse(missing(major_tick_mark), x$y_axis$major_tick_mark, major_tick_mark),
minor_tick_mark = ifelse(missing(minor_tick_mark), x$y_axis$minor_tick_mark, minor_tick_mark),
tick_label_pos = ifelse(missing(tick_label_pos), x$y_axis$tick_label_pos, tick_label_pos),
delete = ifelse(missing(display), x$y_axis$delete, !display),
num_fmt = ifelse(missing(num_fmt), x$y_axis$num_fmt, num_fmt),
rotation = ifelse(missing(rotation), x$y_axis$rotation, rotation)
)
if( missing(limit_min) && !is.null(x$y_axis$limit_min) ){
options$limit_min <- x$y_axis$limit_min
} else if( !missing(limit_min) ){
options$limit_min <- limit_min
}
if( missing(limit_max) && !is.null(x$y_axis$limit_max) ){
options$limit_max <- x$y_axis$limit_max
} else if( !missing(limit_max) ){
options$limit_max <- limit_max
}
if( missing(position) && !is.null(x$y_axis$position) ){
options$position <- x$y_axis$position
} else if( !missing(position) ){
options$position <- position
}
x$y_axis <- do.call(axis_options, options)
x
}
axis_options <- function( orientation = "minMax", axis_position = "b",
crosses = "autoZero", cross_between = "between",
major_tick_mark = "cross", minor_tick_mark = "none",
tick_label_pos = "nextTo", delete = FALSE, num_fmt = "General",
rotation = 0, limit_min = NULL, limit_max = NULL, position = NULL ){
if( !orientation %in% st_orientation ){
stop("orientation should be one of ", paste0(shQuote(st_orientation), collapse = ", " ))
}
if( !axis_position %in% st_axpos ){
stop("axis_position should be one of ", paste0(shQuote(st_axpos), collapse = ", " ))
}
if( !crosses %in% st_crosses ){
stop("crosses should be one of ", paste0(shQuote(st_crosses), collapse = ", " ))
}
if( !cross_between %in% st_crossbetween ){
stop("cross_between should be one of ", paste0(shQuote(st_crossbetween), collapse = ", " ))
}
if( !major_tick_mark %in% st_tickmark ){
stop("major_tick_mark should be one of ", paste0(shQuote(st_tickmark), collapse = ", " ))
}
if( !minor_tick_mark %in% st_tickmark ){
stop("minor_tick_mark should be one of ", paste0(shQuote(st_tickmark), collapse = ", " ))
}
if( !tick_label_pos %in% st_ticklblpos ){
stop("tick_label_pos should be one of ", paste0(shQuote(st_ticklblpos), collapse = ", " ))
}
out <- list(
orientation = orientation,
axis_position = axis_position,
crosses = crosses,
cross_between = cross_between,
delete = delete,
num_fmt = num_fmt,
major_tick_mark = major_tick_mark,
minor_tick_mark = minor_tick_mark,
tick_label_pos = tick_label_pos,
rotation = rotation,
limit_min = limit_min,
limit_max = limit_max,
position = position
)
class(out) <- "axis_options"
out
}