Rails,将 Ruby 变量放入 Google 日历的 Javascript 中

发布于 2025-01-08 05:14:32 字数 1136 浏览 4 评论 0原文

嗨,我想知道是否有人在谷歌图表之前玩过,对于懒惰的程序员来说,在任何网站上进行分析都很棒。我对 javascript 很陌生,事实上今天是我第一天使用它。所以我有一些来自谷歌的JavaScript,它可以生成这些图表来插入它为您提供的基本数据,在JavaScript中如下所示:

// Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('number', 'Velocity');
    data.addRows([
      ['Gary', 9],
      ['Hannes', 11],
      ['Sandra', 5],
      ['Rob', 3],

这都是在<<之间的。脚本>标签。现在我只是在玩它。但这里有静态数据就像船上的一个洞一样有用。我怎样才能用我制作的这个 haml 表附加数字:

%table.gap
  - test = sprint.work_by_developer
  %tr
    - test.each do |data|
      %td.column.developer_name
        = data.name
        \:
      %td.type_of_work
        Features:
        %span.indicator_color
          #{data.features} points
        %br/
        Chores: 
        %span.indicator_color
          = data.chores
        %br/
        bugs:
        %span.indicator_color
          = data.bugs

是否可以循环遍历 javascript 做这样的事情:

......
      [data.name, data.features],
      ])

这样,如果新人加入团队或功能数量动态变化,图表将进行调整即时的??

酷,提前致谢。这将是一个非常酷的功能。

hi i am wondering if anyone has played with google charts before its great for the lazy programmer to pump up his analytic's in any site. i am very new to javascript in fact today was my first day using it. so i have some javascript from google that generates these charts to insert the base data it supplies you with bare bones, in javascript like this:

// Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('number', 'Velocity');
    data.addRows([
      ['Gary', 9],
      ['Hannes', 11],
      ['Sandra', 5],
      ['Rob', 3],

this is all between < script > tags. now i am just playing with it. but having static data in here is about useful as a hole in a boat. how could i append the numbers with say this haml table i made:

%table.gap
  - test = sprint.work_by_developer
  %tr
    - test.each do |data|
      %td.column.developer_name
        = data.name
        \:
      %td.type_of_work
        Features:
        %span.indicator_color
          #{data.features} points
        %br/
        Chores: 
        %span.indicator_color
          = data.chores
        %br/
        bugs:
        %span.indicator_color
          = data.bugs

is it possible to loop through the javascript do something like this:

......
      [data.name, data.features],
      ])

so that if new people join in the team or the amount of features changes on the fly the chart will adjust in real time??

cool, thanks in advance. this would be a really cool feature.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一世旳自豪 2025-01-15 05:14:32

您基本上需要做的是进行字符串操作(通常是模板化),以使 ruby​​ 输出成为有效的 JavaScript 源文件,发送到客户端并使用 Google Charts 进行呈现。

您发出的 Javascript 必须经过 Ruby 处理才能工作。

如果您使用 Ruby on Rails,有一个很好的方法可以通过 respond_to 关键字来执行此操作,您可以在其中为不同格式 (json/xml/html/js) 指定不同的输出。

如果您使用 Rails,您可以简单地定义如下操作方法:

class HomeController < ApplicationController
  def Show
    @data = [["harry", 10], ["sue", 19]]

    respond_to do |format|
      format.js
    end
  end
end

然后您必须在 app/views/home 中有一个名为 show.js.erb 的文件,如下所示:

 var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('number', 'Velocity');
    <% @data.each do |d| %>
    data.addRow(['<%= @d[0] %>', <%= @d[1] %>]);
    <% end %>

然后继续在您想要此数据的位置,您只需插入

<script type="text/javascript" src="/home/show.js" />

然后,这将执行 home#show 操作(在请注意一个新请求)并带回数据到您的页面。

您可以简单地在视图中输出一个标签,其中包含当前请求填充的数据,就像处理任何其他视图内容一样(我只是无法告诉您如何在 HAML 中执行此操作)

What you basically need to do is do string manipulation (usually templating) to make ruby output a valid JavaScript source file to be sent over to the client and be rendered using Google Charts.

The Javascript you are emitting has to be processed by Ruby for it to work.

If you are using Ruby on Rails there is the excellent way to do that via the respond_to keyword where you can specify a different output for different formats (json/xml/html/js).

If you are using Rails you can simply define a action method like this:

class HomeController < ApplicationController
  def Show
    @data = [["harry", 10], ["sue", 19]]

    respond_to do |format|
      format.js
    end
  end
end

You then have to have a file called show.js.erb inside app/views/home that looks like this:

 var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('number', 'Velocity');
    <% @data.each do |d| %>
    data.addRow(['<%= @d[0] %>', <%= @d[1] %>]);
    <% end %>

You then go ahead an at the point where you want this data you simply insert your <script> tag to point to that home/show.js action:

<script type="text/javascript" src="/home/show.js" />

This will then execute the home#show action (in a new request mind you) and bring back the data into your page.

OR:

You could simply output a tag in your view with data filled from the current request like you would with any other view stuff (i just can't tell you how you do that in HAML)

权谋诡计 2025-01-15 05:14:32

现在,有一个 gem 可以方便地将变量传输到视图的 javascript:gon。
https://github.com/gazay/gon

Now, there's a gem to conveniently transfers your variable to view's javascript: gon.
https://github.com/gazay/gon

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文