gpt4 book ai didi

javascript - Highcharts xAxis 标签格式化程序回调数据为空

转载 作者:行者123 更新时间:2023-12-03 07:01:27 25 4
gpt4 key购买 nike

我是 Highcharts 新手,需要使用 xAxis.labels.formatter 函数来生成标签。问题在于,当格式化程序函数运行时,数据为空。如果我单击图例,则会加载数据并正确创建标签。我尝试使用 Charts.events.load 调用 labelFormatter 函数,但仍然没有成功。我基本上需要在加载系列数据并渲染图表后生成标签。

    chart: {
type: 'line',
backgroundColor: '#eee',
events: {
load: function () {
console.log(this);
this.xAxis[0].labelFormatter();
}
}
},
xAxis: {
categories: [],
labels: {
formatter: formatVehicleConversionLabels,
useHTML: true
}
},
...

function formatVehicleConversionLabels() {
//console.log(this);
var month = this.value;
var totalConnectedVehiclesValue = null;
var notificationsValue = null;
var downloadsValue = null;
var installationsValue = null;
for(var i = 0; i < this.chart.series.length; i++) {
var currentSeries = this.chart.series[i];
if(currentSeries.name === "Total Connected Vehicles") {
if(currentSeries.data.length > 0) {
for(var j = 0; j < currentSeries.data.length; j++) {
var currentData = currentSeries.data[j];
if(currentData.category === month) {
totalConnectedVehiclesValue = currentData.y;
}
}
}
}
if(currentSeries.name === "Notifications") {
if(currentSeries.data.length > 0) {
for(var j = 0; j < currentSeries.data.length; j++) {
var currentData = currentSeries.data[j];
if(currentData.category === month) {
notificationsValue = currentData.y;
}
}
}
}
if(currentSeries.name === "Downloads") {
if(currentSeries.data.length > 0) {
for(var j = 0; j < currentSeries.data.length; j++) {
var currentData = currentSeries.data[j];
if(currentData.category === month) {
downloadsValue = currentData.y;
}
}
}
}
if(currentSeries.name === "Installations") {
if(currentSeries.data.length > 0) {
for(var j = 0; j < currentSeries.data.length; j++) {
var currentData = currentSeries.data[j];
if(currentData.category === month) {
installationsValue = currentData.y;
}
}
}
}
}
return '<table class="x-axis"><tr><th>' + this.value + '</th></tr>' +
'<tr><td>' + totalConnectedVehiclesValue + '</td></tr>' +
'<tr><td>' + notificationsValue + '</td></tr>' +
'<tr><td>' + downloadsValue + '</td></tr>' +
'<tr><td>' + installationsValue + '</td></tr></table';
}

//修复

通过向选项属性添加一个数组并将其用作标签格式化程序函数的数据,已修复此问题。

options:{
labelData: [],
...
angular.forEach(vm.downloadAndInstallationRateChartConfig.options.xAxis.categories, function(d, i) {
vm.downloadAndInstallationRateChartConfig.options.labelData.push({
col: d,
successfulDownloads: successfulDownloadsData[i],
successfulInstallations: successfulInstallationsData[i]
});
});

function formatDownloadAndInstallationRateLabels() {
var labelData = null;
for(var i = 0; i < this.chart.options.labelData.length; i++) {
if(this.chart.options.labelData[i].col === this.value) {
labelData = this.chart.options.labelData[i];
}
}
return '<span style="margin-bottom: 20px; display: inline-block; font-size: 12px;">'+this.value+'</span><br />'+
'<span style="margin-top: 1px; display: inline-block; font-size: 12px;">'+labelData.successfulDownloads+'</span><br />'+
'<span style="margin-top: 1px; display: inline-block; font-size: 12px;">'+labelData.successfulInstallations+'</span>';
}

最佳答案

修复方法是将标签数据的数组添加到选项属性中,请参见上文。

关于javascript - Highcharts xAxis 标签格式化程序回调数据为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37040329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com