0

I am interested in plotting JVM GC events using thin vertical bars using nvd3 and rCharts. The example data is like this. The time taken is in seconds.

Event         Date         TimeTakenforEvent

FullGC        1/1/2014        10
GCType1       1/1/2014        30
GCType2       1/1/2014        60
GCType3       1/1/2014        20
GCType4       1/1/2014        70

The events will be dynamically added to a R data frame as and when they happen. The chart is cumulative but every event type will cause a bar of a certain color to be plotted. So, for example, Full GC events will plot blue bars. Date is plotted on x-axis and the time taken is plotted on the y-axis.

I know how to use R and add data to a data frame. So this is a question about the rChart plotting function.

I think the solution could be something like this

p2$chart(color = "#! function(d){ return d.y > 12 ? 'red':'blue'} !#")
p2$chart(color = "#! d3.selectAll('rect.nv-bar').style('fill', function(d, i){return d.y > 12 ? 'red':'blue;}  !#")

Can someone guide me ?

Update: I have determined that this example code works.

p2 = nPlot(x = "Var1", y = "Freq", data = bpf3, type = "discreteBarChart")
p2$chart(color = "#! function(d){ return d.Freq > 12.0 ? 'red':'blue'} !#")

But the 'Event' is what determines color in my case.

John Paul
  • 12,196
  • 6
  • 55
  • 75
Mohan Radhakrishnan
  • 3,002
  • 5
  • 28
  • 42

1 Answers1

0

I made some assumptions, and I think I understand the result you would like to achieve. Let me know if something like this works. It uses d3's d3.scale.ordinal to provide a translation from an event type to a color. See this d3 scale tutorial if you want additional details. Let me know how this works for you.

  require(rCharts)

  bpf3 <- data.frame(
    Var1 = rep(1:20),
    Freq = round(runif(20,1,20)),
    Event = unlist(lapply(1:5,function(x){rep(LETTERS[x],4)}))
  )

  p2 = nPlot(x = "Var1", y = "Freq", data = bpf3, type = "discreteBarChart")
  p2$chart(
    color = "#! function(d){
      var ourColorScale = d3.scale.ordinal().domain(['A','B','C','D']).range(['red','blue','green','purple']);
      return ourColorScale(d.Event);
    }!#")
  p2
timelyportfolio
  • 6,479
  • 30
  • 33
  • That works. I appended the color and accessed it like this but your function seems to be the standard. `p2$chart(color = "#! function(d, x){ var color = d.Var1; return color.split(':')[1];} !#")` – Mohan Radhakrishnan Jul 03 '14 at 06:16
  • I am not sure I understand. Are you good with this or looking for something else? I'll be happy to show alternate solutions. – timelyportfolio Jul 07 '14 at 19:17