javascript - 使用javascript以动态方式为同一轴绘制多个数据标签
问题描述
我从后端 api 获取数据集,它将具有相同 x 和 y 轴的多个数据标签。我如何才能为“n”值实现这一点。
我尝试了 1,2 和 4,它正在工作。但不确定这是否是正确的方法,以及如何实现调整行大小和列大小的“n”值。
Highcharts.chart('container', {
chart: {
type: 'heatmap',
marginTop: 40,
marginBottom: 80,
plotBorderWidth: 1
},
title: {
text: 'Sales per employee per weekday'
},
xAxis: {
min: 0,
categories: ['Alexander', 'Marie', 'Maximilian', 'Sophia', 'Lukas', 'Maria', 'Leon', 'Anna', 'Tim', 'Laura']
},
yAxis: {
categories: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
title: null,
min: 0
},
colorAxis: {
min: 0,
minColor: '#FFFFFF',
maxColor: Highcharts.getOptions().colors[0]
},
legend: {
align: 'right',
layout: 'vertical',
margin: 0,
verticalAlign: 'top',
y: 25,
symbolHeight: 280
},
plotOptions: {
series: {
borderWidth: 1,
dataLabels: {
enabled: true,
color: '#000000'
},
states: {
inactive: {
opacity: 1
}
}
}
},
series: [{
name: 'Sales per employee',
data: [
[0, 1, 19],
[0, 2, 8],
[0, 3, 24],
[0, 4, 67],
[1, 0, 92],
[9, 4, 91]
]
}, {
rowsize: 0.5,
colsize: 0.5,
color: Highcharts.getOptions().colors[0],
data: [
[-0.25, 0.25, 70],
[0.25, 0.25, 60],
[-0.25, -0.25, 65],
[0.25, -0.25, 34]
]
}]
});
解决方案
我认为您的想法非常好,但是为了更容易对齐点,您只能创建列或行。
我在下面创建了一个函数,它为具有相同x
和y
值的点创建新系列,并将它们对齐在一行中。
function getSeries(data) {
var i = 0,
k,
dataLength,
movement,
index = 0,
colSize,
limitVal,
series = [{
data: []
}],
newSeries;
for (i; i < data.length; i++) {
if (
data[i + 1] &&
data[i][0] === data[i + 1][0] &&
data[i][1] === data[i + 1][1]
) {
if (newSeries) {
newSeries.data.push(data[i]);
} else {
newSeries = {
data: [data[i]]
}
}
} else {
if (newSeries) {
newSeries.data.push(data[i]);
dataLength = newSeries.data.length;
newSeries.colsize = colSize = 1 / newSeries.data.length;
movement = dataLength % 2 ? 0 : colSize / 2;
limitVal = colSize * Math.floor(dataLength / 2) - movement;
for (k = -limitVal; k <= limitVal; k += colSize) {
newSeries.data[index][0] += k;
index++;
}
series.push(newSeries);
index = 0;
newSeries = null;
} else {
series[0].data.push(data[i]);
}
}
}
return series
}
Highcharts.chart('container', {
...,
series: getSeries(data)
});
推荐阅读
- java - 除非强制唯一时间,否则带有 influxdb-java 的批处理点会被覆盖?
- javascript - 如何用 JavaScript 替换输入值中的对象值
- c - 在C中对结构列表进行排序
- python - 电子邮件验证 django rest 框架
- javascript - 按对象从数组对象分组到树对象Javascript
- javascript - Javascript 数组到 html div 标签 .aspx
- mongodb - 具有 NFS 持久卷配置的 Kubernetes MongoDB pod
- javascript - 根据名字和姓氏对角卡进行排序
- regex - 小写除一些使用 Nginix 的所有 url
- yii - 如何在查询中使用两个条件?