javascript - Highcharts:如何按类别对柱形图进行分组
问题描述
我有 6 列和 3 个类别,我无法在类别上创建列组。我可以从系列中调用类别吗?
我的目标是将 MACD BUY 和 MACD SELL 并排分组在 MACD 类别中,SMA 7/35 BUY 和 SMA 7/35 SELL 在 SMA 7/35 类别下,等等。你得到了漂移。
抱歉以下格式,我无法使片段功能正常工作。这是 JSFiddle:http: //jsfiddle.net/worb5gyz/
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/data.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="Chart" style="width:100%; height:400px; margin-bottom: 5%;">
document.addEventListener('DOMContentLoaded', function () {
const chart = Highcharts.chart('Chart', {
chart: {
type: 'column'
},
credits: {
enabled: false
},
xAxis: {
categories: [
'MACD',
'SMA 7/35',
'SMA 50/200'
]},
yAxis: {
min: 0,
title: {
text: 'Signal'
}
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [
{type:'column',name: 'MACD BUY', data: [7] },
{type:'column',name: 'MACD SELL', data: [6 ] },
{type:'column',name: 'SMA 7/35 BUY', data: [13] },
{type:'column',name: 'SMA 7/35 SELL', data: [4] },
{type:'column',name: 'SMA 50/200 BUY', data: [6] },
{type:'column',name: 'SMA 50/200 SELL', data: [1]}
]
});
});
编辑:在进行更多搜索后尝试了不同的设置,但不起作用。
series: [{
"data": [{"name": 'MACD BUY', x:0, data: [<?php echo json_encode($macd_M_BUY);?>] }] },
{"data": [{"name": 'MACD SELL', x:0, data: [<?php echo json_encode($macd_M_SELL);?>] }] },
{"data": [{"name": 'SMA 7/35 BUY', x:1, data: [<?php echo json_encode($sma_s_M_BUY);?>] }] },
{"data": [{"name": 'SMA 7/35 SELL', x:1, data: [<?php echo json_encode($sma_s_M_SELL);?>] }] },
{"data": [{"name": 'SMA 50/200 BUY', x:2, data: [<?php echo json_encode($sma_l_M_BUY);?>] }] },
{"data": [{"name": 'SMA 50/200 SELL', x:2, data: [<?php echo json_encode($sma_l_M_SELL);?>] }] }
]
```
解决方案
为系列配置中的每个数据点添加一个 x 位置:
series: [{
type: 'column',
name: 'MACD BUY',
data: [{x: 0, y: 7}]
},
{
type: 'column',
name: 'MACD SELL',
data: [{x: 0, y: 6}]
},
{
type: 'column',
name: 'SMA 7/35 BUY',
data: [{x: 1, y: 13}]
},
{
type: 'column',
name: 'SMA 7/35 SELL',
data: [{x: 1, y: 4}]
},
{
type: 'column',
name: 'SMA 50/200 BUY',
data: [{x: 2, y: 6}]
},
{
type: 'column',
name: 'SMA 50/200 SELL',
data: [{x: 2, y: 1}]
}
]
演示:http: //jsfiddle.net/BlackLabel/qhv4xoLf/
推荐阅读
- python - 如何使用python验证给定的URL是域还是子域或确切的页面?
- regex - 正则表达式提取文本和第一个 > 字符之间的所有内容
- javascript - 使用 for...in 循环时,数组索引不响应严格相等
- python - Python异步写入数据库
- java - Spring Boot App:.war 包和 Docker 构建后无法访问 html 文件
- docker - 在 ubuntu 中回显 $USER 打印空白
- php - 如何使用 PHP 从 Mysql 中获取的数据中创建不定数量的 CSS div
- flutter - 当底部导航栏存在时,appbar 与通知栏重叠
- splunk - 带变量的 Splunk Cloud 搜索查询不返回结果
- google-sheets - 从其他 2 个表创建百分比表 - Google Query