如何设置在X轴上显示条的位置:第一位置,第二位置等,而不使用“,”(数据:[,,,, 11504])?使用了echarts库,该代码具有完整的功能。请帮我怎么写。如何优化缩短代码。提前致谢!该代码来自echarts网站。
<!DOCTYPE html>
<html style="height: 100%">
<head>
<meta charset="utf-8">
</head>
<body style="height: 100%; margin: 0">
<div id="container" style="height: 100%"></div>
<script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts.min.js"></script>
<script type="text/javascript">
var dom = document.getElementById("container");
var myChart = echarts.init(dom);
var app = {};
option = null;
app.title = '';
option = {
legend: {
data: ['Audi 1/2017', 'Audi 2/2017', 'Audi 1/2018','VW 1/2017','VW 1/2018','Volvo 1/2017','Volvo 1/2017','Lam 1/2017','Lam 2/2017','Lam 1/2018','Lam 2/2018','Lam 3/2018'],
orient: 'vertical',
align: 'right',
top: 20,
right: 10
},
xAxis: [{
type: 'category',
position: "bottom",
data: ['2017','2018','2017','2018','2017','2018','2017','2018']
}],
yAxis: {
type: 'value',
boundaryGap: [0, 0.01]
},
series: [
{
name: 'Audi 1/2017',
type: 'bar',
stack: '2017',
barWidth:'70%',
barGap: '-100%',
data: [11203]
},
{
name: 'Audi 2/2017',
type: 'bar',
stack: '2017',
barWidth:'70%',
barGap: '-100%',
data: [12325]
},
{
name: 'Audi 1/2018',
type: 'bar',
stack: '2018',
barWidth:'70%',
barGap: '-100%',
data: [,12594]
},
{
name: 'Lam 1/2017',
type: 'bar',
stack: 'UK2017',
barWidth:'70%',
barGap: '-100%',
data: [,,,,11504]
},
{
name: 'Lam 2/2017',
type: 'bar',
stack: 'UK2017',
barWidth:'70%',
barGap: '-100%',
data: [,,,,11203]
},
{
name: 'Lam 1/2018',
type: 'bar',
stack: 'UK2018',
barWidth:'70%',
barGap: '-100%',
data: [,,,,,11594]
},
{
name: 'Lam 2/2018',
type: 'bar',
stack: 'UK2018',
barWidth:'70%',
barGap: '-100%',
data: [,,,,,15094]
},
{
name: 'Lam 3/2018',
type: 'bar',
stack: 'UK2018',
barWidth:'70%',
barGap: '-100%',
data: [,,,,,21594]
}
]
};
;
if (option && typeof option === "object") {
myChart.setOption(option, true);
}
</script>
</body>
</html>
最佳答案
您可以使用Array(length).fill(value, startIndex)
。
要创建[,,,,,21594]
,请输入:
Array(6).fill(21594, 5)
您也可以这样使用
Array(length).fill(value, startIndex, endIndexExclusive)
:Array(6).fill(21594, 2, 3)
它将返回
[empty × 2, 21594, empty × 3]
。您可以为此创建一个函数:
function getArray(index, value) {
return Array(index + 1).fill(value, index);
}
关于javascript - X轴上的钢筋位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51499906/