-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphPlay.R
113 lines (89 loc) · 4.08 KB
/
graphPlay.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
library(dygraphs)
library(xts)
library(RColorBrewer)
add_shades = function(df,dy,clr) {
#df = donationPlan
wks = unique(ceiling(df$Week/2))*2
for (i in wks) {
dy = dyShading(dy, from=df$Start[i], to=df$End[i]+1, color=clr)
}
return(dy)
}
# CSS ---------------------------------------------------------------------
write('
.dygraph-axis-label {
}
.dygraph-ylabel {
color: #8DA0CB;
}
.dygraph-y2label {
color: #66C2A5;
}
.dygraph-legend {
font-size: 12px !important;
left: 80px !important;
top: 20px !important;
background: transparent !important;
}
.dygraph-legend span {
color: #66C2A5;
}
.dygraph-legend span + br + span {
color: #8DA0CB;
}
.dygraph-rangesel-zoomhandle{
visibility: hidden !important;
}
' , 'www/dygraph.css')
# Variables ---------------------------------------------------------------
marathonPlan = getPlan(TRUE)
checkDate = Sys.Date()+35
colors = brewer.pal(8, 'Set2')
graph.df = merge(dailyPlan, donationActual, by = 'Dates', all.x=TRUE )
graph.df = merge(graph.df, milesActual, by='Dates',all.x=TRUE)
graph.df$Miles = ifelse(is.na(graph.df$Miles),0,graph.df$Miles)
graph.df$TotalActualMiles = cumsum(graph.df$Miles)
graph.df$TotalActualMiles = ifelse(graph.df$Dates>=checkDate,NA,graph.df$TotalActualMiles)
graph.df$TotalMiles = ifelse(graph.df$Dates>=checkDate,graph.df$TotalMiles,NA)
graph.df$TotalDonations = ifelse(graph.df$Dates>=checkDate,graph.df$TotalDonations,NA)
graph.df$actualDonations = ifelse(graph.df$Dates>=checkDate,NA,graph.df$actualDonations)
graph.df = graph.df[,-which(names(graph.df)=='Week' | names(graph.df)=='Miles')]
diff = data.frame(
diff.date = checkDate
, min.miles = min(graph.df$TotalMiles, na.rm = TRUE)
, max.miles = max(graph.df$TotalActualMiles, na.rm = TRUE)
, min.donation = min(graph.df$TotalDonations, na.rm = TRUE)
, max.donation = max(graph.df$actualDonations, na.rm = TRUE)
, diff.miles = min(graph.df$TotalMiles, na.rm = TRUE) - max(graph.df$TotalActualMiles, na.rm = TRUE)
, diff.donation = min(graph.df$TotalDonations, na.rm = TRUE) - max(graph.df$actualDonations, na.rm = TRUE) )
names(graph.df) = c('Dates','Target Miles','Target Donations', 'Donations Collected', 'Miles Ran')
#xts.dona = dailyPlan[,c('Start','totalDonations','weeklyDonations')]
#graph.df$Dates = format(graph.df$Dates, '%b. %d')
xts.dona = xts(graph.df[,-1], order.by=graph.df[,1])
#index(xts.dona) = index(xts.dona)-0
# Draw Graph --------------------------------------------------------------
dy = dygraph(xts.dona) %>%
dyRangeSelector(dateWindow = c(checkDate-4,checkDate+4)
, retainDateWindow=TRUE, height = 40,fillColor=colors[1], strokeColor=colors[3] ) %>%
dySeries("Target Miles", axis = 'y2', color=colors[1], strokePattern = 'dotdash') %>%
dySeries("Miles Ran", axis = 'y2', stepPlot=TRUE, color=colors[1]) %>%
dySeries("Donations Collected", axis = 'y', stepPlot=TRUE, color=colors[3]) %>%
dySeries("Target Donations", axis = 'y', color=colors[3], strokePattern = 'dotdash') %>%
dyAxis(name="y", label = "Donations", axisLineColor='transparent'
, axisLabelColor=colors[3], axisLabelWidth = 60
, valueFormatter = 'function(d){return "$" + d.toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, ",");}'
, axisLabelFormatter = 'function(d){return "$" + d.toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, ",");}'
) %>%
dyAxis("y2", label = "Miles", axisLineColor='transparent', axisLabelColor=colors[1], axisLabelWidth = 50 ) %>%
dyAxis("x", axisLineWidth=0, axisLineColor='transparent', axisLabelColor=colors[8] ) %>%
dyLegend(show='always', width=200, labelsSeparateLines = TRUE) %>%
dyHighlight(highlightCircleSize = 5,
highlightSeriesBackgroundAlpha = 1,
hideOnMouseOut = FALSE) %>%
dyEvent(checkDate, label='Today', color=colors[8]) %>%
dyCSS('www/dygraph.css') %>%
dyOptions(axisLineWidth = 0, digitsAfterDecimal=0
, drawGrid=FALSE, mobileDisableYTouch=TRUE
, strokeWidth=3, axisLabelFontSize = 14, fillAlpha = 1.0)
dy = add_shades(donationPlan,dy,paste0('#',paste0(as.hexmode(col2rgb('grey98')),collapse = '')))
#dy