拉斐尔没有使用g.raphael-rails和raphael-rails gem定义

时间:2013-08-29 10:40:17

标签: ruby-on-rails-3.2 gem raphael graphael

我正在尝试为通过Rails生成的JSON数据生成示例条形图。

首先,我只是想看看我的Rails应用是否可以从example中提供的g.raphael github repo生成barchart.html。

我在我的Rails 3.2.11应用中使用raphael-railsg.raphael-rails gem。

目前,当我尝试在我的sample code浏览器中点击/ companies / company_division_stats时,Firebug会抛出以下错误: -

ReferenceError: Raphael is not defined in corresponding to the line of code :-
var r = Raphael("holder")

两个宝石的文档没有指定是否需要在我的application.js中包含任何js文件。

有趣的是,

当我尝试在浏览器中运行下面的示例纯HTML时,我也会遇到两个错误。

1

 ReferenceError: Raphael is not defined
 F.prototype = Raphael.g;

Reference g.bar.js(line 419)    

2。      TypeError: r.barchart is not a function

示例HTML: -

<!--<!doctype html>-->
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>g·Raphaël Static Bar Charts</title>
  <link rel="stylesheet" href="css/demo.css" type="text/css" media="screen" charset="utf-8">
  <link rel="stylesheet" href="css/demo-print.css" type="text/css" media="print" charset="utf-8">
  <!-- Throws same error when using the raphael and g.raphael gem-->
  <script src="https://raw.github.com/DmitryBaranovskiy/g.raphael/master/g.bar.js" type="text/javascript" charset="utf-8"></script>
  <script src="https://raw.github.com/DmitryBaranovskiy/g.raphael/master/raphael-min.js" type="text/javascript" charset="utf-8"></script>
  <script src="https://raw.github.com/DmitryBaranovskiy/g.raphael/master/g.raphael.js" type="text/javascript" charset="utf-8"></script>
  <script type="text/javascript" charset="utf-8">
      window.onload = function () {
          var r = Raphael("holder"),
                  data1 = [[55, 20, 13, 32, 5, 1, 2, 10], [10, 2, 1, 5, 32, 13, 20, 55], [12, 20, 30]],
                  data2 = [[55, 20, 13, 32, 5, 1, 2, 10], [10, 2, 1, 5, 32, 13, 20, 55], [12, 20, 30]],
                  data3 = [[55, 20, 13, 32, 5, 1, 2, 10], [10, 2, 1, 5, 32, 13, 20, 55], [12, 20, 30]],
                  txtattr = { font: "12px 'Fontin Sans', Fontin-Sans, sans-serif" };

          r.text(160, 10, "Single Series Chart").attr(txtattr);
//          r.text(480, 10, "Multiline Series Chart").attr(txtattr);
//          r.text(160, 250, "Multiple Series Stacked Chart").attr(txtattr);
//          r.text(480, 250, 'Multiline Series Stacked Vertical Chart. Type "round"').attr(txtattr);

          r.barchart(10, 10, 300, 220, [[55, 20, 13, 32, 5, 1, 2, 10]], 0, {type: "soft"});
//          r.barchart(330, 10, 300, 220, data1);
//          r.barchart(10, 250, 300, 220, data2, {stacked: true});
//          r.barchart(330, 250, 300, 220, data3, {stacked: true, type: "round"});
      };
  </script>
</head>
<body class="raphael" id="g.raphael.dmitry.baranovskiy.com">
<div id="holder"></div>
<p>
  Demo of <a href="http://g.raphaeljs.com/">g·Raphaël</a> JavaScript library.
</p>
</body>
</html>

我真的不确定自己到底做错了什么。关于我可能遗失的任何意见?。

谢谢。

2 个答案:

答案 0 :(得分:1)

得益于我的大四学生的意见,解决了这个问题。目前我只是将它们添加为app / assets / javascripts中的JS并在我的application.js中要求它们

我需要以正确的顺序定义js。

- &GT;订购样本html代码

  <script src="https://raw.github.com/DmitryBaranovskiy/g.raphael/master/raphael-min.js" type="text/javascript" charset="utf-8"></script>
  <script src="https://raw.github.com/DmitryBaranovskiy/g.raphael/master/g.raphael.js" type="text/javascript" charset="utf-8"></script>
  <script src="https://raw.github.com/DmitryBaranovskiy/g.raphael/master/g.bar.js" type="text/javascript" charset="utf-8"></script>
  • 在application.js中订购

    require raphael-min
    
    require g.raphael
    
    require g.bar
    

答案 1 :(得分:0)

对于使用Rails 4和graphael-rails gem的人:

require_tree声明之前添加到application.js:

...
//= require raphael/raphael.js
//= require g.raphael/g.raphael.js
//= require g.raphael/g.bar.js // ...and g.pie.js etc
//= require_tree .

这个宝石有助于内部链接到Rafaël和g.Rafaëlrepos,帮助他们更新。