如何在 Rails 应用程序中实现 Highcharts 柱形细分图表
How to implement Highcharts column-drilldown chart in Rails application?
我正在尝试使用lazy_high_charts在我的 Rails 应用程序中实现 Highcharts 柱形向下钻取图表。我想显示从我的数据库中提取并存储在四个数组(区域、区域分数、部门和部门分数)中的数据。我在将 JS 从 highchart 站点上列出的示例 (JSFiddle) 转换为 ruby 时遇到问题。我无法找到有关在 ruby 中创建柱形钻取图表的任何资源。有关如何将钻取图表集成到我的 ruby 应用程序中的任何帮助将不胜感激。
我已经包含了Highcharts演示页面上显示的示例JavaScript和我的控制器方法,该方法用数据填充四个数组并构建Highchart。
Highcharts 柱形细分图表示例 (Javascript)
$(function () {
Highcharts.data({
csv: document.getElementById('tsv').innerHTML,
itemDelimiter: ''t',
parsed: function (columns) {
var brands = {},
brandsData = [],
versions = {},
drilldownSeries = [];
// Parse percentage strings
columns[1] = $.map(columns[1], function (value) {
if (value.indexOf('%') === value.length - 1) {
value = parseFloat(value);
}
return value;
});
$.each(columns[0], function (i, name) {
var brand,
version;
if (i > 0) {
// Remove special edition notes
name = name.split(' -')[0];
// Split into brand and version
version = name.match(/([0-9]+['.0-9x]*)/);
if (version) {
version = version[0];
}
brand = name.replace(version, '');
// Create the main data
if (!brands[brand]) {
brands[brand] = columns[1][i];
} else {
brands[brand] += columns[1][i];
}
// Create the version data
if (version !== null) {
if (!versions[brand]) {
versions[brand] = [];
}
versions[brand].push(['v' + version, columns[1][i]]);
}
}
});
$.each(brands, function (name, y) {
brandsData.push({
name: name,
y: y,
drilldown: versions[name] ? name : null
});
});
$.each(versions, function (key, value) {
drilldownSeries.push({
name: key,
id: key,
data: value
});
});
// Create the chart
$('#container').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Browser market shares. November, 2013'
},
subtitle: {
text: 'Click the columns to view versions. Source: netmarketshare.com.'
},
xAxis: {
type: 'category'
},
yAxis: {
title: {
text: 'Total percent market share'
}
},
legend: {
enabled: false
},
plotOptions: {
series: {
borderWidth: 0,
dataLabels: {
enabled: true,
format: '{point.y:.1f}%'
}
}
},
tooltip: {
headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
},
series: [{
name: 'Brands',
colorByPoint: true,
data: brandsData
}],
drilldown: {
series: drilldownSeries
}
})
}
});
});
我的控制器:
def generateOrgBreakdownReport
# First, query the database for the data you need for the report
@jsonanswerBRKD = queryDatabaseForOrgProgressReport()
# Second, process and combine data as needed for the report
@areaBreakdown, @deptBreakdown, @employBreakdown = computeAreaAndDeptPrepareScore(@jsonanswerBRKD)
# Third, you'll need to put the processed data into a format
# Highcharts will understand for the data series it uses
# for the graph.
#THESE ARRAYS HOLD THE NAMES AND SCORES OF AREAS AND DEPARTMENTS
@deptScores, @departments, @areaScores, @areas = cycleThroughProcessedDataAndCreateHighChartsDataSetsBreakdown(@areaBreakdown, @deptBreakdown, @employBreakdown)
# Last, we put the newly made data sets for Highcharts to work its magic.
#DONT KNOW HOW TO IMPLEMENT DRILLDOWN FOR RUBY
@orgBreakdown = LazyHighCharts::HighChart.new('column') do |f|
f.chart( type: 'column' )
f.xAxis(
title: { text: "Areas" },
type: 'category'
)
f.yAxis(
title: { text: "Preparedness Score (%)"},
)
f.series(
name: "Department Score",
colorByPoint: true,
data: @deptScores
)
f.series(
name: "Area Score",
data: @areaScores
)
f.title(
text: "Organizational Breakdown"
)
f.options[:xAxis][:categories] = @areas
f.drilldown({:series=>{
name:"Dept. Score",
data: @deptScore
}
})
end
end
谢谢马 特
使用过 Lazy Highcharts,但假设它镜像了 JavaScript API 中的 JSON,您需要按名称添加子系列,例如
f.series(
name: "Department Score",
colorByPoint: true,
data: @deptScores,
drilldown: "subdept" #add this
)
然后,您需要添加明细数据,如果 Lazy Highcharts 支持它,它可能看起来像这样:
f.drilldown(
series: {
id: "subdept",
data: [
["One", 1],
["Two", 2],
["Three", 3]
]
}
)
请参阅此基本向下钻取小提琴,以了解生成的 Javascript 应该是什么样子。
要在 Rails 中向下钻取,您必须确保在 JavaScript 清单文件(application.js)中包含向下钻取模块。
我还必须下载该文件,因为它不是我的高图表模块目录。您可以在此处找到该文件:http://code.highcharts.com/modules/drilldown.js
将其添加到应用程序.js:
//= require highcharts/modules/drilldown
在 Rails 之外,您可以包含如下向下钻取模块:
<script src="http://code.highcharts.com/modules/drilldown.js"></script>
- 在Web应用程序中使用Highcharts javascript
- 在Highcharts中,我们可以通过任何方式在渲染图表之前获得plotWidth和plotHeight
- 州和城市选择框类似于国家细分页面
- HighCharts长标题文本在某些元素上重叠
- Highcharts colorsByPoint与系列中的线性渐变一起使用时不起作用
- Highcharts-如何在自定义格式化程序中获得默认的y轴标签格式化程序
- Highcharts仪表未显示
- Highcharts-图表外的图例定位
- Highcharts动态烛台
- 在highcharts.js中向点弹出窗口动态添加文本
- Highcharts 3d调整zindex错误
- 如何在一页上显示多个Highcharts图表
- Highcharts可以从服务器加载数据,但不能更新
- 如何使用webview在React Native中使用Highcharts
- Highcharts热图上的多个轴
- Highcharts-显示noData覆盖和x轴标签
- HighCharts:3D柱形图在选择时更改边框颜色
- 如何使用队列呈现多个HighCharts以防止加载时间过长
- Highcharts动态更改标签
- 如何在 Rails 应用程序中实现 Highcharts 柱形细分图表