SciRuby / plotrb

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

Can't make grouped bar work #16

Open translunar opened 10 years ago

translunar commented 10 years ago

I think there are still some problems with the group mark.

In particular, to get things to the right height in grouped_bar, I have to include a mult in several that shouldn't need it.

Another big problem is that it prints exactly the same data in each group — and the data appears to be incorrect.

The text is also wrong, and I can't see why that would be.

require 'plotrb'

data = pdata.name('table').values(
    [
      {category: :A, position: 0, value: 0.1},
      {category: :A, position: 1, value: 0.6},
      {category: :A, position: 2, value: 0.9},
      {category: :A, position: 3, value: 0.4},
      {category: :B, position: 0, value: 0.7},
      {category: :B, position: 1, value: 0.2},
      {category: :B, position: 2, value: 1.1},
      {category: :B, position: 3, value: 0.8},
      {category: :C, position: 0, value: 0.6},
      {category: :C, position: 1, value: 0.1},
      {category: :C, position: 2, value: 0.2},
      {category: :C, position: 3, value: 0.7}
    ]
)

category = pdata.name('category').source('table').transform [facet_transform.keys(:category)]

xs = linear_scale.name('val').from('table.value').nicely.to_width
gs = ordinal_scale.name('cat').from('category.key').padding(0.2).to_height
cs = ordinal_scale.name('color').to_colors
ys = ordinal_scale.name('pos').from('table.position').to_height

rm = rect_mark.from(data) do
  enter do
    y_start { scale(ys).mult(0.25).field(:position) }
    height  { scale(ys).mult(0.25).use_band }
    x_start { scale(xs).field(:value) }
    x_end   { scale(xs).value(0) }
    fill    { scale(cs).field(:position) }
  end
end

tm = text_mark.from(data) do
  enter do
    y       { scale(ys).mult(0.25).field(:position) }
    dy      { scale(ys).mult(0.25).use_band }
    x       { scale(xs).field(:value).offset(-4) }
    text    { scale(xs).field(:value) }
    fill :white
    align :right
    baseline :middle
  end
end

mark = group_mark.from(category) do
  scales gs
  axes y_axis.scale(gs).tick_size(0).tick_padding(8)
  marks rm, tm
  enter do
    y      { scale(gs).field(:key) }
    height { scale(gs).use_band }
  end
end

vis = visualization.width(300).height(240) do
  data data, category
  scales gs, cs, xs, ys
  marks mark
  axes x_axis.scale(xs), y_axis.scale(ys)
end

puts vis.generate_spec(:pretty)