小编Sté*_*ane的帖子

当鼠标悬停统计时,Chartjs会显示标签和单位

当我的鼠标指针悬停在图表上时,是否可以使用标签和单位?目前只有这个数字.

对于下面的示例,我想表明:

  • 58%标签1
  • 0%Label2
  • 0%Label3
  • 0%Label4
  • 0%Label5

在此输入图像描述

我的选项如下:

var options = {
      //Boolean - Show a backdrop to the scale label
      scaleShowLabelBackdrop : true,
      //Boolean - Whether to show labels on the scale
      scaleShowLabels : true,
      // Boolean - Whether the scale should begin at zero
      scaleBeginAtZero : true,
      scaleLabel : "<%%= Number(value) + ' %'%>",
      legendTemplate: "<ul class=\"<%%=name.toLowerCase()%>-legend\"><%% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%%=datasets[i].strokeColor%>\"></span><%%if(datasets[i].label){%><%%=datasets[i].label%> <strong><%%=datasets[i].value%></strong><%%}%></li><%%}%></ul>",
      tooltipTemplate: "<%%= value %> Label"
    }
Run Code Online (Sandbox Code Playgroud)

使用scaleLabel选项,我在Y轴上显示%,但在悬停弹出窗口中没有显示...

javascript jquery label ruby-on-rails chart.js

3
推荐指数
2
解决办法
2万
查看次数

Ruby关键字参数语法解释

如果我们有这样create_widget定义的方法:

def create_widget(size, properties)
  puts properties.class #=> Hash
  puts properties[:id] #=> table22
end
Run Code Online (Sandbox Code Playgroud)

有什么区别:

create_widget(6, {:id => "table22", :class => "Cart"})
create_widget(6, :id => "table22", :class => "Cart")
create_widget(6, id: "table22", class: "Cart")
Run Code Online (Sandbox Code Playgroud)

在任何情况下,第二个参数都是a Hash,并呈现相同的结果.

ruby hash syntactic-sugar keyword-argument

2
推荐指数
1
解决办法
149
查看次数

Fixnum的未定义方法“介于”

我有一个网址:

xxxx/xxx?status=2
Run Code Online (Sandbox Code Playgroud)

:status从URL 获得了参数的值,将其转换为整数,并尝试对其使用between方法:

params[:status].to_i.between.(0, 3)
Run Code Online (Sandbox Code Playgroud)

但我得到如下错误:

undefined method `between' for 1:Fixnum
Run Code Online (Sandbox Code Playgroud)

我检查了班级祖先:

params[:status].to_i.class.ancestors
# => [Fixnum, JSON::Ext::Generator::GeneratorMethods::Fixnum, Integer, Numeric, Comparable, Object, ActiveSupport::Dependencies::Loadable, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]
Run Code Online (Sandbox Code Playgroud)

我可以看到Comparable,所以我不明白为什么我不能这样做。

ruby ruby-on-rails

1
推荐指数
1
解决办法
1356
查看次数

Rails多个group_by到数组

这是我想要排序的模型.

我有一个User人可以有多个Post,每个人都有很多Statistic.

总结一下这就是我所拥有的:

class User < ActiveRecord::Base
  has_many :posts
  has_many :statistics
end

class Post < ActiveRecord::Base
  belongs_to :user
  has_many :statistics
end

class Statistic < ActiveRecord::Base
  belongs_to :post
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

我最终想要的是两张桌子(一个牢房=一天):

  1. 一个表从日期分组的用户获取所有统计信息,我已设法获得它
  2. 第二个有点复杂.我想要同样的东西,但按帖子ID分组

对于第一个,我使用以下方法完成了:

# Group by day all statistics from one user
stats_by_date = current_user.statistics.group_by { |s| s.created_at.to_date }
# Transform the hash into an Array (one cell = one day)
@overall_statistics = stats_by_date.values
Run Code Online (Sandbox Code Playgroud)

对于第二个,我成功地将我的结果分组为Post ID和days但我不知道如何将其转换为可利用的Array,它表示每个Post ID的一个表,并且在每个表中为一天的一个单元格:

# Group statistics by posts
stats_by_post …
Run Code Online (Sandbox Code Playgroud)

arrays hash group-by ruby-on-rails associations

1
推荐指数
1
解决办法
1426
查看次数