如何设置在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>
答案 0 :(得分:1)
您可以使用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);
}