Chartkick gem,饼图和多个系列中的限制组

时间:2015-06-02 16:26:57

标签: sql ruby-on-rails activerecord charts highcharts

我的分组记录存在小问题。图表代码:

<%= pie_chart CountChatLine.group(:channel).count %>

问题是,我在数据库中有多个:channels,图表如下所示: enter image description here

我能以某种方式只拿N顶:channels并将其余部分加到others或其他东西吗?或者每个:channel添加少于N%的其他人?

3 个答案:

答案 0 :(得分:1)

chartkick gem(我刚刚开始使用)将绘制您提供的任何数据。这取决于您提供的数据是什么样的。

是的,您可以通过聚合来绝对减少饼图中的切片数量,但是,您需要自己这样做。

您可以在模型中编写一个方法来总结这个方法并且调用是这样的:

def self.summarized_channel_info
   {code to get info and convert it into format you really want}
end
CountChatLine模型中的

方法:

select id, name, date from table where date > now()

希望有所帮助。这就是我所做的。

答案 1 :(得分:0)

一些沉重的思考和@Agazoom的帮助,我已经做到了这一点。 代码:

  @top_count = 5
  @size = CountChatLine.group(:channel).count.count-@top_count 
  @data_top = CountChatLine.group(:channel).order("count_all").reverse_order.limit(@top_count).count 
  @data_other = CountChatLine.group(:channel).order("count_all").limit(@size).count 
  @data_other_final = {"others" => 0} 
  @data_other.each { |name, count| @data_other_final = {"others" => @data_other_final["others"]+count }} 
  @sum_all_data = @data_top.reverse_merge!(@data_other_final)

IDK,如果有更好的方法。如果有,请发布。但就目前而言,它有效:)

答案 2 :(得分:0)

如果我们可以让数据库来完成工作并返回最重要的结果,那将是很好的,但是类似的事情应该可以工作:

<%= pie_chart CountChatLine.group(:channel).count.sort_by {|k,v| v}.reverse[0..4] %>

[0..4]将显示前5大结果。 [0..9]将是前10名。

它正在从数据库返回结果 然后按值排序(从低到高) 然后颠倒排序(从高到低) 然后仅取得前[0..4]个结果。