Skip to content

Commit

Permalink
Modified charths arguments
Browse files Browse the repository at this point in the history
 - it now takes a data.frame and the name of the columns to plot,
 - also started work on polarArea charts but it does not work
  • Loading branch information
Tutuchan committed Dec 16, 2015
1 parent d56ed09 commit 98234ae
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 16 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: chartjs
Type: Package
Title: An Implementation of the Chart.js Library
Version: 0.1.0.9012
Date: 2015-12-13
Version: 0.1.0.9013
Date: 2015-12-16
Authors@R: person("Pierre", "Formont", email = "pierre.formont@gmail.com",
role = c("aut", "cre"))
Description: An implementation of the Chart.js visualization library.
Expand All @@ -12,6 +12,7 @@ Suggests:
testthat,
knitr
Imports:
dplyr,
htmltools,
htmlwidgets,
magrittr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(cjsBar)
export(cjsDoughnut)
export(cjsLine)
export(cjsPie)
export(cjsPolar)
export(cjsRadar)
export(cjsTitle)
export(cjsTooltips)
Expand Down
10 changes: 6 additions & 4 deletions R/basechart.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#' @keywords internal

baseChart <- function(chartjs, type){
if(!type %in% c("bar", "line", "pie", "polararea", "radar", "scatter")) stop("incorrect type")
if(!type %in% c("bar", "line", "pie", "polarArea", "radar", "scatter")) stop("incorrect type")
if (type %in% c("pie", "polarArea")) chartjs$x$data <- chartjs$x$data[[1]]

# Initialize colours
cjsColours <- chartjs$x$colours
vecColors <- baseColors()
Expand All @@ -19,8 +21,8 @@ baseChart <- function(chartjs, type){
lColors <- length(vecColors)

# Create the datasets part
# Pie charts behave differently
datasets <- if (type == "pie"){
# Pie and polar charts behave differently
datasets <- if (type %in% c("pie", "polarArea")){
len <- length(chartjs$x$data)
listColors <- lapply(colorTypes, function(colorType) if (!colorType %in% names(cjsColours)){
switch(colorType,
Expand All @@ -32,7 +34,7 @@ baseChart <- function(chartjs, type){
} else cjsColours[[colorType]])
names(listColors) <- colorTypes
list(c(list(data = unname(chartjs$x$data)),
listColors[!sapply(listColors, is.null)]))
listColors[!sapply(listColors, is.null)]))
} else {
lapply(seq_along(chartjs$x$data), function(i) {
listColors <- lapply(colorTypes, function(colorType) if (!colorType %in% names(cjsColours)){
Expand Down
19 changes: 15 additions & 4 deletions R/chartjs.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#'
#' See the documentation of Chart.js at \url{http://www.chartjs.org/docs}.
#'
#' @param data a named list or a vector of numerics, each element is a dataset to be plotted,
#' @param data a data.frame,
#' @param ... the columns of the data.frame to plot,
#' @param labels a vector of numerics or characters : the labels on the x-axis,
#' @param dataLabels a vector of characters : the names of the data series,
#' defaults to the names of \code{data}
Expand All @@ -16,7 +17,17 @@
#' @import htmltools
#'
#' @export
chartjs <- function(data, labels = NULL, dataLabels = NULL, width = NULL, height = NULL, chartOptions = NULL) {
chartjs <- function(data, ..., labels = NULL, dataLabels = NULL, width = NULL, height = NULL, chartOptions = NULL) {

# Handle labels
if (is.null(labels)){
if (is.null(row.names(data))) row.names(data) <- paste0("row", 1:nrow(data))
labels <- row.names(data)
}
# Select the correct rows
data <- data %>%
dplyr::select(...) %>%
as.list

len <- length(data)
# Base colours allow for 6 datasets to be plotted
Expand All @@ -34,14 +45,14 @@ chartjs <- function(data, labels = NULL, dataLabels = NULL, width = NULL, height
baseOptions <- list(responsive = TRUE)
chartOptions <- mergeLists(baseOptions, chartOptions)

# forward data using x
# Forward data using x
x = list(labels = labels,
data = data,
dataLabels = dataLabels,
options = chartOptions,
showLegend = FALSE)

# create widget
# Create widget
htmlwidgets::createWidget(
name = 'chartjs',
x = x,
Expand Down
6 changes: 6 additions & 0 deletions R/charts.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ cjsDoughnut <- function(chartjs, cutout = 50){
cjsPie(chartjs = chartjs)
}

#' @rdname charts
#' @export
cjsPolar <- function(chartjs){
baseChart(chartjs, "polarArea")
}

#' @rdname charts
#' @export
cjsRadar <- function(chartjs){
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Build Status](https://travis-ci.org/Tutuchan/chartjs.svg)](https://travis-ci.org/Tutuchan/chartjs)

# chartjs

This is an implementation of the [Chart.js](http://www.chartjs.org/) library in R using the [htmlwidgets](https://github.com/ramnathv/htmlwidgets) framework.
Expand Down
9 changes: 5 additions & 4 deletions inst/examples/example.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
library(chartjs)

df <- list(mpg = mtcars$mpg, qsec = mtcars$qsec)
chartjs(df, labels = row.names(mtcars)) %>%
chartjs(mtcars, mpg, qsec, labels = row.names(mtcars)) %>%
cjsBar %>%
cjsTooltips(titleFontSize = 20)


chartjs(df, labels = row.names(mtcars)) %>%
chartjs(mtcars, mpg, qsec, labels = row.names(mtcars)) %>%
cjsRadar

chartjs(data = df$mpg[c(1:6)], labels = row.names(mtcars)[c(1:6)]) %>%
cjsDoughnut(cutout = 80)
mt <- mtcars[1:6,]
chartjs(mt, hp, labels = row.names(mt)) %>%
cjsPie
6 changes: 4 additions & 2 deletions man/chartjs.Rd

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

3 changes: 3 additions & 0 deletions man/charts.Rd

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

0 comments on commit 98234ae

Please sign in to comment.