Skip to content

Commit

Permalink
adding vignette, close #18, add hc_series (api complete)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbkunst committed Jan 29, 2016
1 parent e945246 commit 1318791
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 4 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

# RStudio files
.Rproj.user/

# produced vignettes
vignettes/*.html
vignettes/*.pdf
.Rproj.user

# Some htmls and rpubs
no_build/*.html
no_build/rsconnect/
inst/doc
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ License: MIT + file LICENSE
URL: http://jkunst.com/highcharter
BugReports: https://github.com/jbkunst/highcharter/issues
RoxygenNote: 5.0.1
Depends:
Depends:
R (>= 2.10)
Imports:
Imports:
htmlwidgets,
purrr,
rlist,
Expand All @@ -30,3 +30,5 @@ Imports:
quantmod,
methods,
tidyr
Vignettes:
Replicating Highcharts Demos
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export(hc_rangeSelector)
export(hc_rm_serie)
export(hc_rm_series)
export(hc_scrollbar)
export(hc_series)
export(hc_subtitle)
export(hc_theme)
export(hc_theme_chalk)
Expand Down
26 changes: 26 additions & 0 deletions R/api-hc.R
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,32 @@ hc_drilldown <- function(hc, ...) {

}

#' Series options from highchart objects
#'
#' @param hc A \code{highchart} \code{htmlwidget} object.
#' @param ... Arguments defined in \url{http://api.highcharts.com/highcharts#series}.
#'
#' @examples
#'
#' highchart() %>%
#' hc_series(
#' list(
#' name = "Tokyo",
#' data = c(7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6)
#' ),
#' list(
#' name = "London",
#' data = c(3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8)
#' )
#' )
#'
#' @export
hc_series <- function(hc, ...) {

.hc_opt(hc, "series", ...)

}

#' Adding and removing series from highchart objects
#'
#' @param hc A \code{highchart} \code{htmlwidget} object.
Expand Down
Binary file modified data/citytemp.rda
Binary file not shown.
Binary file modified data/favorite_bars.rda
Binary file not shown.
Binary file modified data/favorite_pies.rda
Binary file not shown.
Binary file modified data/unemployment.rda
Binary file not shown.
Binary file modified data/uscountygeojson.rda
Binary file not shown.
Binary file modified data/worldgeojson.rda
Binary file not shown.
1 change: 1 addition & 0 deletions highcharter.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ AutoAppendNewline: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageBuildArgs: --resave-data
PackageRoxygenize: rd,collate,namespace
32 changes: 32 additions & 0 deletions man/hc_series.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

185 changes: 185 additions & 0 deletions vignettes/replicating-highcharts-demos.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
---
title: "Replicating Highcharts Demos"
author: "Joshua Kunst"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Vignette Title}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

Let's repliate the [line-lables](http://www.highcharts.com/demo/line-labels) demo. If you click
*View Options* button you'll see the code to create that chart in a html file via javascript. Let's
take a look to the options which are:

```
{
chart: {
type: 'line'
},
title: {
text: 'Monthly Average Temperature'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: 'Temperature (C)'
}
},
plotOptions: {
line: {
dataLabels: {
enabled: true
},
enableMouseTracking: false
}
},
series: [{
name: 'Tokyo',
data: [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
}, {
name: 'London',
data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
}]
}
```

The previous code is a javascript object. It's like a dictionary, or if we want we can say is like a named
list. `highcharter` have the implementation of each option in highcharts like `chart`, `title`, etc. With
each of them you can add the requiered parameters to get the chart what you want. First of all we need
to create the `highchart` object and then add very option what we want.

```{r}
library("highcharter")
highchart() %>%
hc_chart(type = "line") %>%
hc_title(text = "Monthly Average Temperature") %>%
hc_subtitle(text = "Source: WorldClimate.com") %>%
hc_xAxis(categories = c('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) %>%
hc_yAxis(title = list(text = "Temperature (C)")) %>%
hc_plotOptions(line = list(
dataLabels = list(enabled = TRUE),
enableMouseTracking = FALSE)
) %>%
hc_series(
list(
name = "Tokyo",
data = c(7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6)
),
list(
name = "London",
data = c(3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8)
)
)
```

Now, let's try other example: [area stacked percent](http://www.highcharts.com/demo/area-stacked-percent). The
options used are:

```
{
chart: {
type: 'area'
},
title: {
text: 'Historic and Estimated Worldwide Population Distribution by Region'
},
subtitle: {
text: 'Source: Wikipedia.org'
},
xAxis: {
categories: ['1750', '1800', '1850', '1900', '1950', '1999', '2050'],
tickmarkPlacement: 'on',
title: {
enabled: false
}
},
yAxis: {
title: {
text: 'Percent'
}
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>:
<b>{point.percentage:.1f}%</b> ({point.y:,.0f} millions)<br/>',
shared: true
},
plotOptions: {
area: {
stacking: 'percent',
lineColor: '#ffffff',
lineWidth: 1,
marker: {
lineWidth: 1,
lineColor: '#ffffff'
}
}
},
series: [{
name: 'Asia',
data: [502, 635, 809, 947, 1402, 3634, 5268]
}, {
name: 'Africa',
data: [106, 107, 111, 133, 221, 767, 1766]
}, {
name: 'Europe',
data: [163, 203, 276, 408, 547, 729, 628]
}, {
name: 'America',
data: [18, 31, 54, 156, 339, 818, 1201]
}, {
name: 'Oceania',
data: [2, 2, 2, 6, 13, 30, 46]
}]
}
```

In this example we'll use the `hc_add_series()` to add every series one by one.

```{r}
hc <- highchart() %>%
hc_chart(type = "area") %>%
hc_title(text = "Historic and Estimated Worldwide Population Distribution by Region") %>%
hc_subtitle(text = "Source: Wikipedia.org") %>%
hc_xAxis(categories = c('1750', '1800', '1850', '1900', '1950', '1999', '2050'),
tickmarkPlacement = 'on',
title = list(enabled = FALSE)) %>%
hc_yAxis(title = list(text = "Percent")) %>%
hc_tooltip(pointFormat = '<span style="color:{series.color}">{series.name}</span>:
<b>{point.percentage:.1f}%</b> ({point.y:,.0f} millions)<br/>',
shared = TRUE) %>%
hc_plotOptions(area = list(
stacking = "percent",
lineColor = "#ffffff",
lineWidth = 1,
marker = list(
lineWidth = 1,
lineColor = "#ffffff"
))
) %>%
hc_add_series(name = "Asia", data = c(502, 635, 809, 947, 1402, 3634, 5268)) %>%
hc_add_series(name = "Africa", data = c(106, 107, 111, 133, 221, 767, 1766)) %>%
hc_add_series(name = "Europe", data = c(163, 203, 276, 408, 547, 729, 628)) %>%
hc_add_series(name = "America", data = c(18, 31, 54, 156, 339, 818, 1201)) %>%
hc_add_series(name = "Oceania", data = c(2, 2, 2, 6, 13, 30, 46))
hc
```

If you dont get what you hope to get you must check if you passed all the arguments correctly.
You can compare the javascript object with:

```{r}
library("jsonlite")
toJSON(hc$x$hc_opts, pretty = TRUE, auto_unbox = TRUE)
```

0 comments on commit 1318791

Please sign in to comment.