zuhao / plotrb

A plotting library in Ruby built on top of Vega and D3.
Other
42 stars 12 forks source link

Unable to draw a line #12

Closed translunar closed 10 years ago

translunar commented 11 years ago

I'm trying to create a line plot, and can't get the first line to show up. I suspect it's the names I'm picking for my data tables, but I could just be doing it wrong.

require 'plotrb'

mouse = {2000 => 1000, 2002 => 8000, 2003 => 23000, 2005 => 45000, 2006 => 52000, 2007 => 63000, 2008.4 => 160000}
human = {2004 => 800, 2008.4 => 1200}

mouse_ary = []
mouse.each_key do |year|
  mouse_ary << {year: year, q: mouse[year]}
end

human_ary = []
human.each_key do |year|
  human_ary << {year: year, q: human[year]}
end

data = pdata.name('mouse').values(mouse_ary)

xs = ordinal_scale.name('year').from('mouse.year').to_width
ys = linear_scale.name('q').from('mouse.q').nicely.to_height

mark = line_mark.from(data) do
  enter do
    x_start    { scale(xs).from('year') }
    width      { scale(xs).offset(-1).use_band }
    y_start    { scale(ys).from('q') }
    y_end      { scale(ys).value(0) }
  end

  update do
    stroke 'steelblue'
  end

  hover do
    fill 'red'
  end
end

vis = visualization.width(400).height(400) do
  padding top: 10, left: 30, bottom: 30, right: 10
  data data
  scales xs, ys
  marks mark
  axes x_axis.scale(xs), y_axis.scale(ys)
end

puts vis.generate_spec(:pretty)

The output has an entry "field": "data.year", which seems like it should probably be "field": "mouse.year". Am I wrong?

zuhao commented 11 years ago

It works fine here. Did you pull the latest update from my repo? I did quite a lot of bug fixing last night.

And by the way, data.year is the correct way recognized by Vega (yeah, it's a bit weird I know...), because it actually separates the data and the field. So foo.bar will be {data: 'foo', field: 'data.bar'} internally.

Give it a try here in the browser. I did just now, and I can see a nice line chart. :-)

translunar commented 11 years ago

Ahh, yes. I probably wasn't up to date. Let me go ahead and give you push access for SciRuby, too, so you can push to both repos. =)

Out of curiosity, how would I add a second line?

translunar commented 11 years ago

Okay, you have push access for sciruby/plotrb.

zuhao commented 11 years ago

Awesome!

A visualization can have multiple data, scales, and marks. So in order to add a second line, you simply define another mark object and include it in the visualization.