ramnathv / rCharts

Interactive JS Charts from R
http://rcharts.io
Other
1.19k stars 654 forks source link

custom tooltips on barcharts #170

Closed twjacobs closed 11 years ago

twjacobs commented 11 years ago

I'm trying to use the example code posted for issue 152 on custom tooltips (closed), but I cannot get a barchart working using that code. My dataframe looks like:

  Grade Count
1    01 40272
2    02 36902
3    03  1288
4    04   992
5    05   517
6     K   334
7    KA  8236
8    KP  6704

I want "Grade" to be the categories.

I thought the following code might work, but it does not. By the way, no tooltips in this yet, just trying to get the barchart to show using this method first. I'll add a column with the tooltip next.

h <- Highcharts$new()
h$chart(type = "bar")
h$series(data = toJSONArray2(myDataFrame, json = F), name = "Counts by Grade")
h$xAxis(categores = cData$Grade)
h
pssguy commented 11 years ago

you spelt categories incorrectly. maybe that is it

also you seem to have 2 dfs cData and myDataFrame

twjacobs commented 11 years ago

Thanks, Those are embarrassing mistakes in the post. The misspelling was also in my code, but the dataframe names in my code were consistent (just a post error). Changing "categores" to "categories" in the code did result in correctly displaying the x axis, but no bars are displaying.

ramnathv commented 11 years ago

Can you correct your mistakes and post the correct code, so that we can take a look and help?

twjacobs commented 11 years ago

The correct code is:

h <- Highcharts$new()
h$chart(type = "bar")
h$series(data = toJSONArray2(cData, json = F), name = "Counts by Grade")
h$xAxis(categories = cData$Grade)
h

I found that if I define a y value in cData, I get the chart: cData$y <- cData$Count

and if I also define an x value that can be interpreted as numeric, I also get a chart: cData$x <- c(1:8)

however, if I define an x value that cannot be interpreted as numeric, I don't get a chart: cData$x <- cData$Grade

Can you comment on the rules for x and y in Highcharts? I'll wait for your response and then mark this as closed.

Thanks

ramnathv commented 11 years ago

I think you are looking for a column chart. Here is how you can do it using hPlot, which is a convenience function that wraps a lot of functionality underneath.

cData  <- read.table(textConnection("
  no Grade Count
  1 01 40272
  2 02 36902
  3 03 1288
  4 04 992
  5 05 517
  6 K 334
  7 KA 8236
  8 KP 6704"), sep = '', header = T
)

a <- hPlot(Count ~ Grade, data = cData, type = 'column', group = 'Grade')
twjacobs commented 11 years ago

Thanks ramnathv, I actually started with that approach, without the group since the different colors are not needed. But I want to add custom tooltips, and I understood from closed post #152 that it isn't possible with hplot. That post was only closed a week ago so I thought the limitation for custom tooltips is still there?

ramnathv commented 11 years ago

Got it. I will wait for @reinholdsson to respond to this, since he has developed the Highcharts bindings for rCharts and is best suited to answer your question.

reinholdsson commented 11 years ago

@twjacobs Here is an example that might help: http://rcharts.io/viewer/?5910699.

I haven't figured out the best way to deal with categorical data yet, but here is one solution to your example:

a <- rCharts:::Highcharts$new()
a$series(data = toJSONArray2(cData[, c("Grade", "Count")], json = F, names = F))
a$chart(type = "column")
a$xAxis(type = "category")
a

I'll let you know if I manage to figure out a better solution.

twjacobs commented 11 years ago

thanks reinholdsson, Again, as with ramnathv, this solution works to produce a column plot (or a bar plot if type is changed to "bar" in line 2), but since there are no names in the JSON data (i.e., toJSONArray2(..., names = F) I won't be able to use custom tooltips. I tried your solution without the names = F, but the bar chart was not produced.

The full code example (with tooltips) that works is:

cData  <- read.table(textConnection("
  Grade Count Tip
  01 40272 Tip1
  02 36902 Tip2
  03 1288 Tip3
  04 992 Tip4
  05 517 Tip5
  K 334 Tip6
  KA 8236 Tip7
  KP 6704 Tip8"), sep = '', header = T
)
cData$y <- cData$Count

h <- Highcharts$new()
h$chart(type = "bar")
h$series(data = toJSONArray2(cData, json = F), name = "Counts by Grade")
h$xAxis(type = "category", categories = cData$Grade, title=(list(text="Grade")))
h$yAxis(list(title=(list(text="Number of Assessments"))))
h$tooltip(useHTML = T, formatter = "#! function() { return this.point.Tip; } !#")
h
ramnathv commented 11 years ago

Glad that it worked out.

theoryno3 commented 11 years ago

+1!

On Mon, Jul 22, 2013 at 5:36 AM, Ramnath Vaidyanathan < notifications@github.com> wrote:

Glad that it worked out.

— Reply to this email directly or view it on GitHubhttps://github.com/ramnathv/rCharts/issues/170#issuecomment-21340911 .

royfrancis commented 8 years ago

In the above example, the tooltip displays only variable 'Tip'. How would I go about displaying two variables for example, 'y-axis-value' and 'Tip' one below the other?

reinholdsson commented 8 years ago

@royfrancis you could add it to the formatter, e.g:

h$tooltip(
  useHTML = T,
  formatter = "#! function() { return this.point.Tip + '<br>' + this.point.Grade; } !#"
)
royfrancis commented 8 years ago

Thanks! :+1:

royfrancis commented 8 years ago

I am creating dynamic column plots in a shiny app. I find that these two approaches works overall but tooltips don't work.

#manual names=F
h$series(data = toJSONArray2(cData, json = F,names=F), name = "Counts by Grade")
#hPlot wrapper names=F
h <- rCharts::hPlot(x = "Grade", y = "y", data = cData, type = c("bar"))

To make tooltips work, I used this:

#manual names=T
h$series(data = toJSONArray2(cData, json = F,names=T), name = "Counts by Grade")

Now, the tooltips work fine but this creates too many other issues. Multiple plots don't always load, click and drag gets stuck, sorting column from shiny widget doesn't work etc. So it seems like names=T makes tooltip to work but messes up a lot of other stuff. I have tried it outside of shiny environment and it seems to be ok.