我在Highcharts中创建了一个列图。我想在系列数据全为零或零时显示一组默认的y轴标签,但是当系列数据包含某个值时,y轴应该有自动生成的标签。请帮忙。当系列数据为空时,应使用json更改生成标签。
function renderGraph(graphdata, isAnim) {
var graphObj = JSON.parse(graphdata);
Highcharts.setOptions({
lang : {
numericSymbols : ["K", "M", "G", "T", "P", "E"]
}
});
var change = {
0 : '$0K',
2 : '$2K',
4 : '$4K',
6 : '$6K',
8 : '$8K'
};
var xAxisLegends = graphObj.bottomLegends;
var seriesData = graphObj.seriesData;
var xAxisLegends = graphObj.bottomLegends;
//['Q2, 16', 'Q3, 16', 'Q4, 16', 'Q1, 17'];
var columnColors = ["#69C3DB", "#3a8a9f"];
var seriesData = graphObj.seriesData;
/*[{
name : 'Budget',
showInLegend : false,
data : [2, 4, 6, 8]
}, {
name : 'Utilisation',
showInLegend : false,
data : [1, 2, 3, 4]
}];*/
// variables which have diff values according to OS
var chartProperties = {};
// properties to assign to Charts's object
var graphHeight = 0;
// height of chart
var graphWidth = 0;
//Width of the column
var pointWidth;
// Separating the graph dimensions & styling properties as per OS name & version
if (graphObj.osname == "iphone") {
chartProperties = {
type : 'column',
renderTo : 'container'
};
xAxisProp = {
gridLineWidth : 0,
categories : xAxisLegends,
crosshair : true
};
yAxisProp = {
min : 0,
gridLineWidth : 0,
tickInterval : 1,
title : {
text : ' '
},
labels : {
formatter : function() {
var value = this.axis.defaultLabelFormatter.call(this);
return '$' + (this.value === 0 ? '0K' : value); }
}
};
pointWidth = 5;
} else if (graphObj.osname == "android") {
chartProperties = {
type : 'column',
plotBackgroundColor : null,
plotBackgroundImage : null,
plotBorderWidth : 0,
plotShadow : false,
height : 450,
marginTop : 100,
marginLeft : 120
},
xAxisProp = {
categories : xAxisLegends,
width : 800,
tickmarkPlacement : 'on',
labels : {
y : 40,
style : {
color : '#333333',
fontSize : '25',
fontFamily : 'Metropolis-Light',
opacity : '.6'
},
}
};
yAxisProp = {
gridLineWidth : 0,
min : 0,
tickAmount : 5,
offset : 60,
title : {
text : ''
},
labels : {
align : 'left',
style : {
color : '#333333',
fontSize : '28',
fontFamily : 'Metropolis-Light',
opacity : '.5'
},
formatter : function() {
var value = this.axis.defaultLabelFormatter.call(this);
return '$' + (this.value === 0 ? '0K' : value);
}
},
};
pointWidth = 10;
if (parseInt(graphObj.osversion) >= 500 && parseInt(graphObj.osversion) <= 600) {
graphHeight = 600;
} else {
graphHeight = 630;
}
}
$(function() {
new Highcharts.chart('container', {
chart : chartProperties,
credits : {
enabled : false
},
tooltip : {
enabled : false
},
exporting : {
enabled : false
},
title : {
text : ''
},
xAxis : xAxisProp,
yAxis : yAxisProp,
plotOptions : {
column : {
pointPadding : 0.2,
borderWidth : 0,
groupPadding : 0.38,
pointWidth : pointWidth
}
},
colors : columnColors,
series : seriesData
});
});
}
答案 0 :(得分:0)
此代码检查是否存在y
值与0
不同的点,并对y轴应用自定义配置:
load: function() {
var nonZeroPointPresent = false,
series,
yAxisOptions;
// check if there're non zero points
for (var i = 0; i < this.series.length; i++) {
if (nonZeroPointPresent) {
break;
}
if (this.series[i].points.find((p) => p.y)) {
nonZeroPointPresent = true;
}
}
// change the configuration of the axis
if (nonZeroPointPresent) {
yAxisOptions = {
categories: null,
min: null,
max: null
}
} else {
yAxisOptions = {
categories: ['a', 'b'],
min: 0,
max: 1
}
}
this.yAxis[0].update(yAxisOptions);
}
现场演示: http://jsfiddle.net/BlackLabel/ay0Ltw9v/
如果您注释掉data: [1, 2]
行,则会使用自定义配置。