简体   繁体   English

Highcharts-如何在Rails 3中拆分多个图表

[英]Highcharts - How to split up multiple charts in Rails 3

I'm trying to draw two separate graphs on two separate views - one on the Project#Index page, and the other on the Project#Show page. 我试图在两个单独的视图上绘制两个单独的图形-一个在Project#Index页面上,另一个在Project#Show页面上。 I plan on having different graphs in similar locations for each model, so I've created folders in app/assets/javascripts/ that correspond to my models and contain all the Javascript files required for those graphs (I currently have one file for each graph). 我计划在每个模型的相似位置放置不同的图,因此我在app/assets/javascripts/了与我的模型相对应的文件夹,并包含这些图所需的所有Javascript文件(我目前每个图都有一个文件)。 I can get them to draw separately on their own, but when I have both files being served to the browser, It will only draw the one that is listed first in the HTML source code. 我可以让它们自己单独绘制,但是当我将两个文件都提供给浏览器时,它只会绘制HTML源代码中最先列出的文件。 I've made sure both graphs are rendering to a different div - but that doesn't seem to help. 我已确保两个图形都呈现到不同的div但这似乎无济于事。 When I take a look at the Javascript console in my web browser, there is an error: 当我在Web浏览器中查看Javascript控制台时,出现错误:

Uncaught Highcharts error #13: www.highcharts.com/errors/13 

Which means the graph is unable to find the div it has been told to render in. 这意味着图形无法找到被告知要渲染的div

Which makes sense, both graphs are being served to the page, but only one is able to find it's corresponding div . 这很有意义,两个图形都被提供给页面,但是只有一个能够找到它对应的div Being able to see one and not the other is partially explained by this, too - the graph that is listed first in the HTML source works, and the the error is Highcharts saying the second graph cannot find it's div . 这也可以部分解释能够看到一个而不看到另一个-在HTML源代码中首先列出的图形有效,并且错误是Highcharts表示第二个图形找不到div When I go to the second graph's page, Highcharts errors out on the first one, leaving the second one unrendered. 当我转到第二张图的页面时,Highcharts在第一张图上出错,而​​第二张图未呈现。

Is there a better way to organize this? 有没有更好的方式来组织这个? Should I be keeping all model-related Javascripts in a single file? 我应该将所有与模型相关的Javascript放在一个文件中吗? Am I referencing the div s wrong? 我引用div错吗?

So, after a little more research, and some help from a friend, I've come across a pretty suitable answer: 因此,在进行了更多研究之后,并在朋友的帮助下,我找到了一个非常合适的答案:

Originally, I had the following line in my app/assets/javascripts/application.js : 最初,我在app/assets/javascripts/application.js包含以下行:

//= require_tree .

Which would instruct the rails server to serve out all the files in that directory, including those that were in directories. 这将指示Rails服务器提供该目录中的所有文件,包括目录中的文件。

By changing this to 通过将其更改为

//= require_directory .

The rails server will now only serve out files that are directly located in that directory. Rails服务器现在将仅提供直接位于该目录中的文件。

This then allowed me to add individual javascript_include_tag s in each view, as such: 然后,这使我可以在每个视图中添加单个javascript_include_tag ,如下所示:

= javascript_include_tag "projects/show"

Now my graphs work, and the server doesn't serve out Javascripts unnecessarily! 现在,我的图形起作用了,并且服务器没有不必要地提供Javascript!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM