Ruby on rails的html绘制带有文本的矩形

时间:2016-01-30 04:23:28

标签: html ruby-on-rails html5 svg html5-canvas

这是我的目标,我有一堆数据,一个包含名称值的名称数组,以及相同的重量,高度,颜色。

所以在我的控制器功能中,我有以下内容 我的数据库名称人员

def display
  people= Person.all
end

在display.html.erb中:

<h1> Display Result</h1>
<div id='chart'>
    <% @people.each do |x| %>
        <svg width="<%=x.weight%>" height="<%=x.height%>">
            <rect width = "<%=x.weight%>" height="<%=x.height%>" style="fill:<%=x.color%>;stroke-width:3;stroke:black">
            <text fill="#ffffff",50,50><%= x.name %></text>
        </svg>
    <% end %>
</div>

enter image description here 想要有类似上面的东西,因为矩形随高度和重量而变化,重量是水平的,高度是垂直的。

已经可以绘制矩形,但需要将文本放在矩形内,任何想法如何将它放在矩形内? 任何想法如何解决它,提前感谢您的亲切帮助! enter image description here

1 个答案:

答案 0 :(得分:0)

使用rails连接来修复高度和宽度。

<h1> Display Result</h1>
<div id='chart'>
    <%= @people.each do |x| %>
    <svg width = "500" height = "500">
        <rect width="<%= x.weight %>" height="<%= x.height%>" style="fill:<%= x.color %>;stroke-width:3;stroke:black">
        <text fill="#ffffff">x.name</text>
    </svg>
    <% end %>
</div>
<%= link_to 'Back', {:controller => 'people', :action => 'index'}, id:'proceedss' %>