我建立一个垂直率的曲线图谷歌的指令后:https://developers.google.com/chart/interactive/docs/gallery/barchart#StackedBars
这是应该的样子:
我使用的是相同的data
作为谷歌的一个。
my_graph_controllers
@data = [
['Genre', 'Fantasy & Sci Fi', 'Romance', 'Mystery/Crime', 'General',
'Western', 'Literature', { role: 'annotation' } ],
['2010', 10, 24, 20, 32, 18, 5, ''],
['2020', 16, 22, 23, 30, 16, 9, ''],
['2030', 28, 19, 29, 30, 12, 13, '']
]
视图/ my_graph.html.erb
<head>
<%= javascript_include_tag "https://www.gstatic.com/charts/loader.js" %>
<%= javascript_include_tag 'application' %>
</head>
<div class="container">
<%= bar_chart @data, isStacked: true, isStacked: "percentage" %>
</div>
资产/ Java脚本/ application.js中
//= require rails-ujs
//= require_tree .
//= require Chart.bundle
//= require chartkick
//= require highcharts
的Gemfile
gem "chartkick"
因此,关键是要改变的数据位。
@data = [
{
name: "Fantasy & Sci Fi",
data: [["2010", 10], ["2020", 16], ["2030", 28]]
},
{
name: "Romance",
data: [["2010", 24], ["2020", 22], ["2030", 19]]
},
{
name: "Mystery/Crime",
data: [["2010", 20], ["2020", 23], ["2030", 29]]
}
]
这就像一个魅力则:
<%= bar_chart @data, stacked: true %>
我已经找到了一个老问题over here答案。
但是,如果你想要做的百分比堆栈,你需要使用库散了点。
<%= bar_chart @data, adapter: 'google', library: { isStacked: 'percent' } %>
附:请注意,它应该是真或为isStacked“百分比”。更多信息here。