-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(Trade) Arrows with Optional Annotations #157
Open
przmv
wants to merge
14
commits into
rstudio:master
Choose a base branch
from
przmv:arrow
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8df1457
Adding plugin helper functions
903fb5b
Initial commit
5c0106f
Remove console.log calls
4aadd3b
Remove redundant tooltip property
007a19f
Load arrow popup styles from external CSS file
1e3a67f
Show popup on point with arrow select
7da1ba6
Better data sample
fc03683
Merge remote-tracking branch 'upstream/master' into arrow
f93ba4c
Change CSS classes names
7745e65
Formatting and code style
c226ec6
Popup initialization refactoring
165f047
Correct angles for directions
50fa46e
Popup positions depend on arrows directions and graph bounds
25b6549
Better disableZoom attribute handling
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#' dygraph trade arrow | ||
#' | ||
#' Add an arrow to a dygraph | ||
#' | ||
#' @param dygraph Dygraph to add arrow to | ||
#' @param seriesName Name of series within data set. | ||
#' @param x Either numeric or date/time for the event, depending on the format of the | ||
#' x-axis of the dygraph. (For date/time must be a \code{POSIXct} object or another | ||
#' object convertible to \code{POSIXct} via \code{as.POSIXct}) | ||
#' @param label Label for event. Defaults to blank | ||
#' @param direction Direction of arrow (up or down) | ||
#' @param fillColor Fill color of arrow. This can be of the form "#AABBCC" or | ||
#' "rgb(255,100,200)" or "yellow". Defaults to white. | ||
#' @param strokeColor Stroke color of arrow. This can be of the form "#AABBCC" or | ||
#' "rgb(255,100,200)" or "yellow". Defaults to black. | ||
#' | ||
#' @return A dygraph with the specified trade arrow. | ||
#' | ||
#' @examples | ||
#' library(dygraphs) | ||
#' dygraph(presidents, main = "Quarterly Presidential Approval Ratings") %>% | ||
#' dyArrow("1950-6-30", "Korea", direction = "down", fillColor = "red") %>% | ||
#' dyArrow("1965-2-09", "Vietnam", direction = "down", fillColor = "red") | ||
#' | ||
#' dygraph(presidents, main = "Quarterly Presidential Approval Ratings") %>% | ||
#' dyArrow(c("1950-6-30", "1965-2-09"), | ||
#' c("Korea", "Vietnam"), | ||
#' direction = "down", | ||
#' fillColor = "red") | ||
#' | ||
#' @export | ||
dyArrow <- function(dygraph, | ||
x, | ||
text = NULL, | ||
tooltip = NULL, | ||
direction = c("up", "down", "left", "right", "ne", "se", "sw", "nw"), | ||
fillColor = "white", | ||
strokeColor = "black", | ||
series = NULL) { | ||
|
||
# create arrows | ||
if (!is.null(text) && length(x) != length(text)) { | ||
stop("Length of 'x' and 'text' does not match") | ||
} | ||
if (!is.null(tooltip) && length(x) != length(tooltip)) { | ||
stop("Length of 'x' and 'tooltip' does not match") | ||
} | ||
|
||
# validate series if specified | ||
if (!is.null(series) && ! series %in% dygraph$x$attrs$labels) { | ||
stop("The specified series was not found. Valid series names are: ", | ||
paste(dygraph$x$attrs$labels[-1], collapse = ", ")) | ||
} | ||
|
||
# default the series if necessary | ||
if (is.null(series)) | ||
series <- dygraph$x$attrs$labels[length(dygraph$x$attrs$labels)] | ||
|
||
fixedtz <- ifelse(is.null(dygraph$x$fixedtz), FALSE, dygraph$x$fixedtz) | ||
scale <- dygraph$x$scale | ||
|
||
direction <- match.arg(direction) | ||
arrows <- lapply(seq_along(x), | ||
function(i) { | ||
if (is.null(text)) | ||
arrowText <- NULL | ||
else | ||
arrowText <- text[i] | ||
if (is.null(tooltip)) | ||
arrowTooltip <- NULL | ||
else | ||
arrowTooltip <- tooltip[i] | ||
list(xval = ifelse(dygraph$x$format == "date", | ||
asISO8601Time(x[i]), x[i]), | ||
text = arrowText, | ||
tooltip = arrowTooltip, | ||
direction = direction, | ||
fillColor = fillColor, | ||
strokeColor = strokeColor, | ||
series = series, | ||
fixedtz = fixedtz, | ||
scale = scale) | ||
}) | ||
|
||
pluginName <- "Arrow" | ||
if (dyHasPlugin(dygraph, pluginName)) { | ||
dygraph <- dySetPluginOptions(dygraph, | ||
pluginName, | ||
append(dyGetPluginOptions(dygraph, pluginName), | ||
arrows)) | ||
} else { | ||
dygraph <- dyPlugin(dygraph = dygraph, | ||
name = pluginName, | ||
path = system.file("plugins/arrow.js", | ||
package = "dygraphs"), | ||
options = arrows) | ||
} | ||
|
||
# return dygraph | ||
dygraph | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
title: (Trade) Arrows with Optional Annotations | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
library(dygraphs) | ||
``` | ||
|
||
```{r} | ||
dygraph(presidents, main = "Quarterly Presidential Approval Ratings") %>% | ||
dyArrow("1950-7-1", | ||
"Korea", | ||
direction = "se", | ||
fillColor = "#d9534f") %>% | ||
dyArrow("1965-1-1", | ||
"Vietnam", | ||
direction = "sw", | ||
fillColor = "#5cb85c") | ||
``` | ||
|
||
```{r} | ||
library(quantmod) | ||
|
||
getSymbols("SPY", from = "2016-12-01", auto.assign=TRUE) | ||
|
||
difference <- SPY[, "SPY.Open"] - SPY[, "SPY.Close"] | ||
decreasing <- which(difference < 0) | ||
increasing <- which(difference > 0) | ||
|
||
dyData <- SPY[, "SPY.Close"] | ||
|
||
dygraph(dyData) %>% | ||
dyArrow(index(dyData[increasing]), | ||
as.vector(dyData[increasing]), | ||
direction = "up", | ||
fillColor = "green") %>% | ||
dyArrow(index(dyData[decreasing]), | ||
as.vector(dyData[decreasing]), | ||
direction = "down", | ||
fillColor = "red") | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This behavior could just be made part of
dyPlugin
(i.e. if an a plugin name that already exists in the dygraph is added then we append rather than overwrite options). Would there be any downsides to this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I'm going to make a separate pull request introducing changes in
dyPlugin
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR added #158