datastorm-open / rAmCharts

API for Amcharts
48 stars 17 forks source link

[Question] Example of Tornado Chart? #4

Closed happyshows closed 8 years ago

happyshows commented 8 years ago

Hi,

Could you provide a minimal example of the below chart? Thanks. http://www.amcharts.com/demos/stacked-bar-chart-with-negative-values/

Djaiff commented 8 years ago

Hi,

There was a problem with the class 'Label' so you will need to update your version to the current "dev" one.

library(pipeR) ; library(data.table)

  # prepare data
  n <- 15
  (male <- round(sort(runif(n)), 1))
  (female <- -round(sort(runif(n)), 1))
  (category <- factor(paste("cat.", 1:15)))
  dataProvider <- data.table(male, female, category)
  labelFunction1 <- htmlwidgets::JS('function(item) {',
                                    'return Math.abs(item.values.value);',
                                    '}')
  balloonFunction <- htmlwidgets::JS('function(item) {',
                                     'return item.category + \': \' + Math.abs(item.values.value) + \'%\';',
                                     '}')
  labelFunction2 <- htmlwidgets::JS('function(value) {',
                                    'return Math.abs(value) + \'%\';',
                                    '}')

  valueAxis_obj <- valueAxis(gridAlpha = 0, ignoreAxisWidth = TRUE, id = "axis1",
                             labelFunction = labelFunction2)

  # draw chart
  pipeline(
    amSerialChart(startDuration = 0, rotate = TRUE, marginBottom = 50,
                  categoryField = "category"),
    addGraph(fillAlphas = .8, lineAlpha = .2, type = "column", valueField = "male",
             title = "Male", clustered = FALSE, labelFunction = labelFunction1,
             balloonFunction = balloonFunction),
    addGraph(fillAlphas = .8, lineAlpha = .2, type = "column", valueField = "female",
             title = "Female", clustered = FALSE, labelFunction = labelFunction1,
             balloonFunction = balloonFunction),
    setCategoryAxis(gridPosition = "start", gridAlpha = .2, axisAlpha = 0),
    setBalloon(fixedPosition = TRUE),
    setChartCursor(valueBalloonEnabled = FALSE, cursorAlpha = .05, fullWidth = TRUE),
    addLabel(text = "Male", x = "28%", y = "97%", bold = TRUE, align = "middle"),
    addLabel(text = "Male", x = "75%", y = "97%", bold = TRUE, align = "middle"),
    addGuide(value = 0, lineAlpha = .2, valueAxis = "axis1"),
    setDataProvider(dataProvider = dataProvider)
  )

Please let me know if it doesn't work. Also, don't hesitate to send an issue if you get a problem !

happyshows commented 8 years ago

I'm getting the following error. Also should it be male <- round(sort(runif(n)), 1) instead of (male <- round(sort(runif(n)), 1))?

Error in checkAtAssignment("Label", "x", "character") : assignment of an object of class “character” is not valid for @‘x’ in an object of class “Label”; is(value, "numeric") is not TRUE

Djaiff commented 8 years ago

It is an error of your package version in the class "Label". However I fixed this (thanks to your question) in the most recent version on GitHub, so I think you have just to update the package.

if (!require(devtools)) {
  install.packages("devtools")
} else {}

devtools::install_github("dataKnowledge/rAmCharts")

Let me know if it doesn't work after that.

happyshows commented 8 years ago

cool thanks.