Giter Club home page Giter Club logo

c3's Introduction

c3

DOI R-CMD-check codecov CRAN_Status_Badge

The c3 package is a wrapper, or htmlwidget, for the C3 javascript charting library by Masayuki Tanaka. You will find this package useful if you are wanting to create a chart using R and embedding it in a Rmarkdown document or Shiny App.

The C3 library is very versatile and includes a lot of options. Currently this package wraps most of the C3 options object.

Installation

You probably already guessed this bit.

install.packages('c3')

# OR

devtools::install_github("mrjoh3/c3")

Basic Usage

The c3 package is intended to be as simple and lightweight as possible. As a starting point the data input must be a data.frame or tibble with several options.

Where no options are supplied a simple line plot is produced by default. Where no x-axis is defined the plots are sequential. Date x-axis can be parsed with not additional setting if in the format %Y-%m-%d (ie โ€˜2014-01-01โ€™)

library(c3)

data <- data.frame(a = abs(rnorm(20) * 10),
                   b = abs(rnorm(20) * 10),
                   date = seq(as.Date("2014-01-01"), by = "month", length.out = 20))

c3(data)

Documentation

For more detailed documentation and methods see http://mrjoh3.github.io/c3/.

c3's People

Contributors

carlganz avatar mrjoh3 avatar t-morrison avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

c3's Issues

c3 charts behave inconsistently in shiny

Disclaimer: I'm unable to produce 'reproducible' code because of the following reasons:

  • In one R shiny app c3_pie works but c3_gauge does not
  • In another R shiny app c3_gauge works but c3_pie does not
  • In another R shiny app neither of these work

Definition: "not working": svg is created and rendered in the browser, but it is blank

The input types for charts are:

  • c3_pie: data frame with 1 row and several columns
  • c3_gauge: data frame with 1 row and 1 column

Summary of attempts, successes and failures:

The input to charts is a reactiveValues object that updates based on user's inputs. In both cases I'm using c3::c3Output(...) and defined a suitable output$... <- c3::renderC3({ ... }). To be safe I avoided using %>% and returned c3_pie() or c3_gauge object. I saved (cached) the data frame used for plotting, read it in R and plotted (c3_gauge or c3_pie) - it worked as expected. I also observed that replacing the dynamic data with static data (Eg: data.frame(x=10, y=10)) makes the plot appear in R shiny. I experimented a bit more and found that there is no clear relationship between the type of data (reactive or static) and the behavior of the R shiny output.

Is there a universal fix for this problem?

Adjust c3 axis labels with css

The last date on the x-axis of my c3 chart is cut off.

Screen Shot 2021-01-11 at 8 28 39 PM

Since space is tight I wish to re-align the date rather than create more space for the date to print.

Using Chrome Inspector I can manually adjust the text-anchor: middle to text-anchor: end

and that seems to work.

Screen Shot 2021-01-11 at 8 22 12 PM

However, I do not understand how to do this with css.

Here's my R Script and related css though I don't think that R is relevant to the issue here.

R Script

library(shiny)
library(c3)

data <- data.frame(a = abs(rnorm(20) * 10),
                   b = abs(rnorm(20) * 10),
                   date = seq(as.Date("2014-01-01"), by = "month", length.out = 20))


ui <- fluidPage(
  
  includeCSS("c3.css"),
  
  c3Output("chart",  height = "100%")
)

server <- function(input, output, session) {
  output$chart <- renderC3({
    data %>%
      c3(x = 'date') %>%
      tickAxis('x', count = 5, format = '%Y-%m-%d') 
  })
}

shinyApp(ui, server)

css

.c3-line {
  stroke-width: 4px !important; 
}

.c3-axis-x-label {
    text-anchor: end; 
}

c3_bar produces a barchart with no gaps between bars

firstly, many thanks for a great package! trying to use c3_bar with the code below, which produces a bar chart with three bars in one group, with no gaps between bars. how may I code it to produce the bars in three separate groups. thanks!

library(c3)

data.frame(a=1, b=3, c=2) %>%
  c3() %>%
  c3_bar()

c3 height issue

Hi,

When some c3 visualisation is inserted into a shiny page, the height parameter does not seem to work.

I am not sure if its related to this issue but it is slightly difficult to use it right now in large Shiny Applications.

Here is a working minimal example:

library(shiny)
library(shinydashboard)

data <- data.frame(a = c(1,2,3,2), b = c(2,3,1,5))

shinyApp(
ui = dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
tags$head(tags$style("#plot1{height:100vh !important;}")),
tags$head(tags$style("#plot2{height:100vh !important;}")),
fluidRow(
column(6,c3Output("plot1", "100%", "100%")),
column(6,plotOutput("plot2", "100%", "100%"))

)

        )

),
server = function(input, output) {

output$plot1 <- renderC3({
data %>%
  c3()
})

output$plot2 <- renderPlot({
  plot(data)
})

}
)

Unexpected behaviour for JS events

Hi,

It looks like this javascript issue is replicated in the latest c3 library available on CRAN.

c3js/c3#2354

In particular, tooltips and mouse events do not trigger with Pie charts - including the default settings.

Once some class is modified in CSS, it works immediately.

Here are two reproducible examples that are supposed to result in the same chart (with hover-events!):

library(shiny) library(c3) ui <- fluidPage( titlePanel("C3 Test"), mainPanel( tags$style(".c3-event-rects { display: none; }"), c3Output("plot1") ) ) server <- function(input, output) { output$plot1 <- renderC3({ data.frame(sugar = 20, fat = 45, salt = 10) %>% c3() %>% c3_pie() }) } shinyApp(ui = ui, server = server)

data.frame(sugar = 20, fat = 45, salt = 10) %>% c3() %>% c3_pie()

access to chart object after chart is generated

Hi,
first of all thanks for packing this library into htmlwidgets.
I created chart in shiny, however I would like to update it dynamically.
I'm thinking about executing custom JS code from Shiny, however I cannot access object of chart to run some of its methods.
Do you provide any update methods or access to chart's objects?
Can you share some docs about it, if it's available?
Thanks again!

c3 produces a blank plot when the dplyr tibble classes exist.

iris_tibble <- iris %>%
    as_tibble()

iris_tibble %>%
  c3(x = 'Sepal_Length', 
     y = 'Sepal_Width', 
     group = 'Species') %>% 
  c3_line('line')

This results in a blank plot for me using RStudio 1.1.453 and R3.5.

If iris is plotted directly:

iris %>%
  c3(x = 'Sepal_Length', 
     y = 'Sepal_Width', 
     group = 'Species') %>% 
  c3_line('line')

then it works beautifully.

(This really is a beautiful plotting package).

display year or full date x-axis

Here is my code and I do not see any documentation on how to do this:

library(c3)
data <- data.frame(structure(
  list(
    Time = structure(c(6939, 7304, 7669, 8034, 8400,
                       8765), class = "Date"),
    `Access Vector Required-ADJACENT_NETWORK-HIGH` = c(0,
                                                       0, 0, 0, 0, 0),
    `Access Vector Required-ADJACENT_NETWORK-LOW` = c(0,
                                                      0, 0, 0, 0, 0),
    `Access Vector Required-ADJACENT_NETWORK-MEDIUM` = c(0,
                                                         0, 0, 0, 0, 0),
    `Access Vector Required-LOCAL-HIGH` = c(0, 0,
                                            0, 1, 0, 0),
    `Access Vector Required-LOCAL-LOW` = c(0, 2, 8,
                                           8, 6, 8),
    `Access Vector Required-LOCAL-MEDIUM` = c(0, 0, 0,
                                              0, 0, 0),
    `Access Vector Required-NETWORK-HIGH` = c(0, 0, 0,
                                              0, 0, 0),
    `Access Vector Required-NETWORK-LOW` = c(2, 1, 3, 6,
                                             7, 5),
    `Access Vector Required-NETWORK-MEDIUM` = c(0, 0, 0, 0,
                                                0, 0)
  ),
  row.names = c(NA, 6L),
  class = "data.frame"
))

data %>%
  c3(x = 'Time')

I want it to display 1988-12-31 or just 1988

Mapping series to secondary y-axis

I'm trying to create a chart where one series (a) is mapped to the primary y-axis as a line chart and another series on a different scale (b) is mapped to the secondary y-axis as an area chart. There is an exampl e that illustrates this kind of dual y-axis mapping in c3.js, but am am not sure how to translate this example for use with the c3 package in R. I've been able to implement something like this with ggplot2 using the following MWE:

library(tidyverse)

# generate MWE data
data <- data.frame(a = abs(runif(20)),
                  date = seq(as.Date(Sys.Date()), by = "day", length.out = 20)) %>% 
  mutate(b = rev(a)*100)

# working ggplot2 implementation
data %>% 
  ggplot(aes(x = date)) +
  geom_area(aes(y = b / 100), alpha = 0.2) +
  geom_line(aes(y = a)) +
  scale_y_continuous(name = 'a', sec.axis = sec_axis(~ . * 100, name = 'b'), limits = c(0, 1))

However, I'm struggling to recreate something similar using c3:

library(c3)
# non-working c3 attempt 
data %>% 
  c3(x = 'date', y = 'a', y2 = 'b') %>% 
  c3_mixedGeom(types = list(a = 'line', b = 'area')) %>% 
  grid('y', show =  T) %>% 
  xAxis(type = 'timeseries', tick = list(format = "%Y-%m-%d")) %>% 
  yAxis(min = 0, label = list(text = 'a')) %>% 
  y2Axis(show = T, label = list(text = 'b')) 

Is this currently possible with c3? If so, where am I going wrong? Thanks!

Something like c3_click event for capturing click on c3 pie charts

Hi,

I'm unable to find any function which can capture event_data just like plotly does for scatter plots (though it doesn't provide it for pie charts, maybe if you can then people would be using more of c3. You never know!).

So, basically I want to show which part/region of the pie I selected to show metadata about it into a datatable or something like that sort.

I would be really glad if you could provide me with some documentation because I checked most of the part online and couldn't find a better solution than asking the author directly.

Thanks and Regards,
Nimesh Sajjan Bumb

How to add Onclick event to chart?

I want to add onclick event to chart in Shiny App. I`v read this article with c3 examples and try to do some simple app with print in console when clicked on chart. But in my case, onclick I have this error in console. Could you, please, say what I do wrong?

    library(c3)
    library(shiny)

    shinyApp(
        ui = fluidPage(
            c3Output("c3test")
        ),
        server = function(input, output, session) {
          output$c3test <- renderC3({
            pie.chart <- data.frame(sugar=20,fat=45,salt=10) %>% 
              c3(onclick="function(d,element) {
                 console.log(d.id);
          }") %>% 
                    c3_donut()
        })
        }
    )

defined x axis breaks with tibble input

when a tibble is used witha defined x axis the check on the data type breaks

data <- tibble(a = abs(rnorm(20) * 10),
                      b = abs(rnorm(20) * 10),
                      date = seq(as.Date("2014-01-01"), by = "month", length.out = 20))

c3(data, x = 'date')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.