简体   繁体   English

在Rails中获取Highcharts数据的正确方法是什么

[英]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. 我知道一些宝石可以帮助我,例如ChartkickLazyHighCharts ,但我想了解基本策略。

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>


Edit - get data from server 编辑-从服务器获取数据

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.

相关问题 从ajax获取数据的正确或最佳方法是什么? - What is the proper or best way to get data from ajax? ExpressJS + JWT。 获取身份验证数据的正确方法是什么? - ExpressJS + JWT. What's the proper way to get auth data? 可视化数据的正确方法是什么? - What is the proper way of visualizing data? 从highcharts图表中删除所有系列数据的正确方法? - Proper way to remove all series data from a highcharts chart? 在rails中创建JSON对象,然后使用jQuery / Ajax从操作中获取JSON的正确方法是什么? - What is the proper way to create a JSON object in rails, and then use jQuery/Ajax to get the JSON from an action? 从Relay Modern的内部存储中获取数据的正确方法是什么? - What is the proper way to get data out of Relay Modern's internal store? Rails 6 - 加载自定义 JS 文件的正确方法是什么? - Rails 6 - what is the proper way to load custom JS files? 在 StencilJS 中使用 HighCharts 的正确方法是什么? - How is the proper way to use HighCharts with StencilJS? 用Ember数据发出数据处理请求的正确方法是什么? - What is the proper way to make a data process request with Ember data? 什么是获得React价值的正确方法 <select>元件? - What is the proper way to get the value of a React <select> element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM