使用延迟高图表在一个页面上显示多个图形

时间:2015-06-05 15:43:11

标签: ruby-on-rails ruby-on-rails-3 highcharts lazy-high-charts

我目前正在rails应用程序上编写一个ruby,我正在使用lazy high charts gem来显示一些图表。但是,我在一个页面上显示两个图表时遇到问题。看起来后一个图形会覆盖第一个图形,因此第二个图形是唯一显示的图形。如果单独放置,则显示两个图形。如何在一个页面上显示两个图形?

这就是我控制器上的内容

@performance_chart = LazyHighCharts::HighChart.new('graph') do |f|
  f.title(:text => "Team Performance")
  f.xAxis(:categories => @x_axis)

  f.series(:name => @team.name, :yAxis => 0, :data => @performance)
  f.series(:name => "Top Scores for " + @team.division.name.capitalize[0...-1] + " Division", :yAxis => 0, :data => @top_scores)
  f.series(:name => "Averaged Scores for "+ @team.division.name.capitalize[0...-1] + " Division", :yAxis => 0, :data => @average_scores)
  f.yAxis [
    {:title => {:text => "Quiz Scores", :margin => 70} }
  ]
  f.chart({:defaultSeriesType=>"line"})
end 

#bar graph for individual team members 
@member_chart = LazyHighCharts::HighChart.new('column') do |f|
  f.title(:text => "Population vs GDP For 5 Big Countries [2009]")
  f.xAxis(:categories => ["United States", "Japan", "China", "Germany", "France"])
  f.series(:name => "GDP in Billions", :yAxis => 0, :data => [14119, 5068, 4985, 3339, 2656])
  f.series(:name => "Population in Millions", :yAxis => 1, :data => [310, 127, 1340, 81, 65])

  f.yAxis [
    {:title => {:text => "GDP in Billions", :margin => 70} },
    {:title => {:text => "Population in Millions"}, :opposite => true},
  ]

  f.legend(:align => 'right', :verticalAlign => 'top', :y => 75, :x => -50, :layout => 'vertical',)
  f.chart({:defaultSeriesType=>"column"})
end

这就是我的观点

<div = class"row">
    <div class="card">
        <%= high_chart("some_id", @performance_chart) %>
    </div>
</div>  

<div class="row">
    <div class="card">
        <%= high_chart("some_id", @member_chart) %> 
    </div>
</div>

1 个答案:

答案 0 :(得分:2)

我发现HighChart.new()接受了三个参数,其中一个参数用于创建div标签。

 def high_chart(placeholder, object, &block)
  object.html_options.merge!({:id => placeholder})
  object.options[:chart][:renderTo] = placeholder
  high_graph(placeholder, object, &block).concat(content_tag("div", "", object.html_options))
end

所以我所要做的就是更改我从控制器调用的一个显示图形的id的名称。如果没有,它会找到具有相同ID的div标签(在我的情况下是我创建的第一个图表)并替换之前显示的图表。为了解决这个问题,我改变了

<%= high_chart("some_id", @member_chart) %> 

<%= high_chart("some_other_id", @member_chart) %>