[英]What is the proper way to get the data for Highcharts in Rails
My Rails website display a simple table about name and age of students. My Rails网站显示有关学生姓名和年龄的简单表格。
name age
Lily 25
Tom 27
Chris 19
...
So I have @names = Student.pluck(:name)
, @ages = Student.pluck(:age)
. 所以我有@names = Student.pluck(:name)
, @ages = Student.pluck(:age)
。 Now I would like to generate a line chart by using Highcharts: 现在,我想使用Highcharts生成折线图:
HTML: <div id='students-chart'></div>
HTML: <div id='students-chart'></div>
JavaScript: JavaScript的:
$(function() {
Highcharts.chart('students_chart', {
...
};
};
Now I should provide the name and age to the chart as the xAxis and yAxis. 现在,我应该为图表提供名称和年龄,分别为xAxis和yAxis。 The simplest way is to include the JavaScript in the html.erb
file and provide the data by <%= @names %>
and <%= @ages %>
. 最简单的方法是将JavaScript包含在html.erb
文件中,并通过<%= @names %>
和<%= @ages %>
提供数据。 However, it's not recommended, and I want to put the JavaScript code in the assets/javascripts/students.js
file. 但是,不建议这样做,我想将JavaScript代码放在assets/javascripts/students.js
文件中。
A very common way to fetch the data in the JavaScript file is using the Ajax, however, my data is already in the page so I don't want to add an extra action in the controller to send the data. 在JavaScript文件中获取数据的一种非常常见的方法是使用Ajax,但是,我的数据已经在页面中了,所以我不想在控制器中添加额外的操作来发送数据。
So what's the best practice to get the data for the Highcharts? 那么获取Highcharts数据的最佳实践是什么? data-
attribute? data-
属性?
No front-end frameworks in the project, only jQuery
. 项目中没有前端框架,只有jQuery
。 I know some gems could help me like Chartkick
or LazyHighCharts
, but I would like to know the basic strategy. 我知道一些宝石可以帮助我,例如Chartkick
或LazyHighCharts
,但我想了解基本策略。
This is one way to show the chart, just jQuery getting data from the controller. 这是显示图表的一种方法,只是jQuery从控制器获取数据。
In controller fetch the data, adjust and convert to json
. 在控制器中获取数据,调整并转换为json
。 Customise respect to on your models. 自定义对模型的尊重。 Here is an example with an array of hashes (data are passed as arrays): 这是一个带有哈希数组的示例(数据作为数组传递):
@series = [ {name: 'Lily', data: [25]}, {name: 'Tom', data: [27]}, {name: 'Chris', data: [19]} ].to_json
For example, if your User
model includes the age column, you can adjust like this: 例如,如果您的User
模型包括年龄列,则可以进行如下调整:
@series = User.all.map{ |user| {name: user.name, data: [user.age]} }.to_json
In view (customise as you will), passing the variable here: 在视图中(您可以自定义),在此处传递变量:
<div id='students_chart'></div>
<script>
$(function () {
var myChart = Highcharts.chart('students_chart', {
chart: {
type: 'column'
},
title: {
text: 'User ages'
},
xAxis: {
categories: ['Users']
},
yAxis: {
title: {
text: 'Age'
}
},
series: <%= raw @series %>
});
});
</script>
Instead of sending data to view, render as json (no need to add e new action): 无需发送数据即可查看,而是呈现为json(无需添加新操作):
respond_to do |format| format.html format.json { render json: @series } end
Then place the javascript in a file and get json data using jQuery.getJSON()
: 然后将javascript放在文件中,并使用jQuery.getJSON()
获取json数据:
$.getJSON(window.location.href, function(json) { var highChartData = json; console.log(json) var myChart = Highcharts.chart('students_chart', { chart: { type: 'column' }, title: { text: 'User ages' }, xAxis: { categories: ['Users'] }, yAxis: { title: { text: 'Age' } }, series: highChartData }); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.