你可以通过子弹函数创建一个子弹函数迷你图,例如 =BULLETSPARKLINE(measure, target, maxi, good?, bad?, forecast?, tickunit?, colorScheme?, vertical?).
这个函数有以下这些参数:
measure: 类型为数字或者引用,表示测量条的长度,例如 5 或者 "A1"。
target: 类型为数值或者引用,表示目标线的位置,例如 7 或者 "A2"。
maxi: 类型为数值或者引用,表示这个迷你图中的最大值,例如 10 或者 "A3"。
good: (可选参数)类型为数值或者引用,表示好值条的长度,例如 3 或者 "A4";默认值是0.
bad: (可选参数)类型为数值或者引用,表示坏值条的长度,例如 1 或者 "A5";默认值是0。
forecast: (可选参数)类型为数值或者引用,表示预测值线的长度,例如 8 或者 "A6";默认值是0。
tickunit: (可选参数)类型为数值或者引用,表示刻度单位,例如 1 或者 "A7";默认值是0。
colorScheme: (可选参数)类型为颜色字符串,表示主体颜色的字符串,主体颜色会生成一组颜色来展示这个迷你图。默认值是 "#A0A0A0"。
vertical: (可选参数)类型为布尔值,表示是否让箱线函数迷你图垂直显示;默认值是false,也就是默认是水平显示。
measureColor: (可选参数) 类型为颜色字符串,表示测量条的颜色。
targetColor: (可选参数) 类型为颜色字符串,表示目标线的颜色。
maxiColor: (可选参数) 类型为颜色字符串,表示最大值的颜色。
goodColor: (可选参数) 类型为颜色字符串,表示好值条的颜色。
badColor: (可选参数) 类型为颜色字符串,表示坏值条的颜色。
forecastColor: (可选参数) 类型为颜色字符串,表示预测值线的颜色。
allowMeasureOverMaxi: (可选参数) 类型为布尔值,表示测量条是否能够超过最大区域,默认值为false。
barSize: (可选参数) 类型为数值,表示线形宽度对于单元格高宽的百分比. (数值需大于0且小于等于1)
var spreadNS = GC.Spread.Sheets, spread;
window.onload = function () {
spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 2 });
initSpread(spread);
document.getElementById('colorScheme').addEventListener('change',applyChanges);
document.getElementById('measureColor').addEventListener('change',applyChanges);
document.getElementById('targetColor').addEventListener('change',applyChanges);
document.getElementById('maxiColor').addEventListener('change',applyChanges);
document.getElementById('goodColor').addEventListener('change',applyChanges);
document.getElementById('badColor').addEventListener('change',applyChanges);
document.getElementById('forecastColor').addEventListener('change',applyChanges);
document.getElementById('allowMeasureOverMaxi').addEventListener('change',applyChanges);
document.getElementById('barSize').addEventListener('change',applyChanges);
};
function applyChanges() {
var sheet = spread.getActiveSheet();
if (spread.getActiveSheetIndex() == 0) {
for (var index = 3; index < 9; index++) {
sheet.setFormula(index, 1, '=BULLETSPARKLINE($E' + (index + 1) + ','+
'$F' + (index + 1) +','+
'$G' + (index + 1) + ','+
'$H' + (index + 1) + ','+
'$I' + (index + 1) + ','+
'$D' + (index + 1) + ',1,"' +
document.getElementById('colorScheme').value + '",false,"'+
document.getElementById('measureColor').value + '","'+
document.getElementById('targetColor').value + '","'+
document.getElementById('maxiColor').value + '","'+
document.getElementById('goodColor').value + '","'+
document.getElementById('badColor').value + '","'+
document.getElementById('forecastColor').value +'",'+
document.getElementById('allowMeasureOverMaxi').checked +','+
document.getElementById('barSize').value );
}
} else if (spread.getActiveSheetIndex() == 1) {
for (var index = 2; index < 8; index++) {
var columnChar = String.fromCharCode(65 + index);
sheet.setFormula(9, index, '=BULLETSPARKLINE($' + columnChar + '5,'+
'$' + columnChar +'6,'+
'$' + columnChar +'7,'+
'$' + columnChar + '8,'+
'$' + columnChar + '9,'+
'$' + columnChar + '4,1,"' +
document.getElementById('colorScheme').value + '",true,"'+
document.getElementById('measureColor').value + '","'+
document.getElementById('targetColor').value + '","'+
document.getElementById('maxiColor').value + '","'+
document.getElementById('goodColor').value + '","'+
document.getElementById('badColor').value + '","'+
document.getElementById('forecastColor').value +'",'+
document.getElementById('allowMeasureOverMaxi').checked +','+
document.getElementById('barSize').value );
}
}
}
function initSpread(spread) {
spread.options.newTabVisible = false;
initHorizontalSparkline(spread.sheets[0], "Horizontal");
initVerticalSparkline(spread.sheets[1], "Vertical");
};
function initHorizontalSparkline(sheet, name) {
sheet.suspendPaint();
sheet.name(name);
sheet.addSpan(1, 1, 1, 8);
sheet.getCell(1, 1).value("Revenue By Region (million $)")
.font("20px Calibri").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center)
.foreColor("white").backColor("#82bc00");
sheet.getRange(2, 1, 1, 8).vAlign(spreadNS.VerticalAlign.center)
.font("bold 16px Calibri")
.backColor("#F4F8EB");
sheet.getRange(3, 1, 6, 7).font("20px").vAlign(spreadNS.VerticalAlign.center);
sheet.getRange(2, 1, 7, 8).setBorder(new GC.Spread.Sheets.LineBorder("Black", GC.Spread.Sheets.LineStyle.thin), {
all: true
});
sheet.getRange(3, 1, 6, 8).setBorder(new GC.Spread.Sheets.LineBorder("Black", GC.Spread.Sheets.LineStyle.dotted), {
inside: true
});
sheet.setArray(2, 2, [
['Region', 'Forecast', 'Actual', 'Target', 'Max', 'Optimist F.','Pessimist F.',' '],
['North America', 80, 75, 70, 95, 65, 50],
['South America', 79, 62, 80, 95, 75, 50],
['Europe', 95, 100, 100, 120, 90, 80],
['Azia', 80, 95, 80, 90, 70, 55],
['Australia', 35, 20, 40,50, 35, 15],
['Africa', 34, 40, 30,50, 20, 15]
]);
for (var index = 3; index < 9; index++) {
sheet.setFormula(index, 1, '=BULLETSPARKLINE($E' + (index + 1) + ','+
'$F' + (index + 1) +','+
'$G' + (index + 1) + ','+
'$H' + (index + 1) + ','+
'$I' + (index + 1) + ','+
'$D' + (index + 1) + ',1,"#FFFFFF",false,"#000000","#F7A711","#DDDDDD","#BBBBBB","#999999","#FFFFFF",TRUE,0.7');
}
sheet.setRowHeight(1, 30);
for (var i = 3; i < 9; i++) {
sheet.setRowHeight(i, 40);
}
for (var j = 2; j < 9; j++) {
sheet.setColumnWidth(j, 100);
}
sheet.setColumnWidth(1, 250);
sheet.resumePaint();
}
function initVerticalSparkline(sheet, name) {
sheet.suspendPaint();
sheet.name(name);
sheet.addSpan(1, 1, 1, 7);
sheet.getCell(1, 1).value("Revenue By Region (million $)")
.font("20px Calibri").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center)
.foreColor("white").backColor("#82bc00");
sheet.getRange(2, 1, 7, 1).vAlign(spreadNS.VerticalAlign.center)
.font("bold 16px Calibri")
.backColor("#F4F8EB");
sheet.getRange(2, 2, 7, 6).font("20px").vAlign(spreadNS.VerticalAlign.center);
sheet.getRange(2, 1, 7, 7).setBorder(new GC.Spread.Sheets.LineBorder("Black", GC.Spread.Sheets.LineStyle.thin), {
all: true
});
sheet.getRange(2, 2, 7, 6).setBorder(new GC.Spread.Sheets.LineBorder("Black", GC.Spread.Sheets.LineStyle.dotted), {
inside: true
});
sheet.setArray(2, 1, [
['Region', 'North America', 'South America', 'Europe', 'Azia', 'Australia','Africa'],
['Forecast', 80, 79, 95, 80, 35, 34],
['Actual', 75, 62, 100, 95, 20, 40],
['Target',70, 80, 100, 80, 40, 30],
['Max',95, 95, 120, 90, 50, 50],
['Optimist F.', 65, 75, 90,70, 35, 20],
['Pessimist F.', 50, 50, 80,55, 15, 15]
]);
for (var index = 2; index < 8; index++) {
var columnChar = String.fromCharCode(65 + index);
sheet.setFormula(9, index, '=BULLETSPARKLINE($' + columnChar + '5,'+
'$' + columnChar +'6,'+
'$' + columnChar +'7,'+
'$' + columnChar + '8,'+
'$' + columnChar + '9,'+
'$' + columnChar + '4,1,"#FFFFFF",TRUE,"#000000","#F7A711","#DDDDDD","#BBBBBB","#999999","#FFFFFF",TRUE,0.7');
}
sheet.setRowHeight(1, 30);
sheet.setRowHeight(9, 200);
for (var j = 1; j < 8; j++) {
sheet.setColumnWidth(j, 100);
}
sheet.resumePaint();
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta name="spreadjs culture" content="zh-cn" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js"
type="text/javascript"></script>
<script
src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.min.js"
type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script>
<script src="app.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="sample-tutorial">
<div id="ss" class="sample-spreadsheets"> </div>
<div class="options-container">
<div>
<label><b>Settings:</b></label>
</div>
<hr>
<div class="option-row">
<label for="colorScheme">Color Scheme:</label>
<select id="colorScheme" style="margin: 0 20px 0 6px">
<option value="#FFFFFF" selected>White</option>
<option value="#000000">Black</option>
<option value="#F7A711">Orange</option>
<option value="#DDDDDD">LightGrey</option>
<option value="#BBBBBB">Grey</option>
<option value="#999999">DarkGrey</option>
<option value="#82BC00">Green</option>
</select>
</div>
<div class="option-row">
<label for="measureColor">Measure Color:</label>
<select id="measureColor" style="margin: 0 20px 0 6px">
<option value="#FFFFFF">White</option>
<option value="#000000" selected>Black</option>
<option value="#F7A711">Orange</option>
<option value="#DDDDDD">LightGrey</option>
<option value="#BBBBBB">Grey</option>
<option value="#999999">DarkGrey</option>
<option value="#82BC00">Green</option>
</select>
</div>
<div class="option-row">
<label for="targetColor">Target Color:</label>
<select id="targetColor" style="margin: 0 20px 0 6px">
<option value="#FFFFFF">White</option>
<option value="#000000">Black</option>
<option value="#F7A711" selected>Orange</option>
<option value="#DDDDDD">LightGrey</option>
<option value="#BBBBBB">Grey</option>
<option value="#999999">DarkGrey</option>
<option value="#82BC00">Green</option>
</select>
</div>
<div class="option-row">
<label for="maxiColor">Maximum Color:</label>
<select id="maxiColor" style="margin: 0 20px 0 6px">
<option value="#FFFFFF">White</option>
<option value="#000000">Black</option>
<option value="#F7A711">Orange</option>
<option value="#DDDDDD" selected>LightGrey</option>
<option value="#BBBBBB">Grey</option>
<option value="#999999">DarkGrey</option>
<option value="#82BC00">Green</option>
</select>
</div>
<div class="option-row">
<label for="goodColor">Optimist Color:</label>
<select id="goodColor" style="margin: 0 20px 0 6px">
<option value="#FFFFFF">White</option>
<option value="#000000">Black</option>
<option value="#F7A711">Orange</option>
<option value="#DDDDDD">LightGrey</option>
<option value="#BBBBBB" selected>Grey</option>
<option value="#999999">DarkGrey</option>
<option value="#82BC00">Green</option>
</select>
</div>
<div class="option-row">
<label for="badColor">Pessimist Color:</label>
<select id="badColor" style="margin: 0 20px 0 6px">
<option value="#FFFFFF">White</option>
<option value="#000000">Black</option>
<option value="#F7A711">Orange</option>
<option value="#DDDDDD">LightGrey</option>
<option value="#BBBBBB">Grey</option>
<option value="#999999" selected>DarkGrey</option>
<option value="#82BC00">Green</option>
</select>
</div>
<div class="option-row">
<label for="forecastColor">Forecast Color:</label>
<select id="forecastColor" style="margin: 0 20px 0 6px">
<option value="#FFFFFF" selected>White</option>
<option value="#000000">Black</option>
<option value="#F7A711">Orange</option>
<option value="#DDDDDD">LightGrey</option>
<option value="#BBBBBB">Grey</option>
<option value="#999999">DarkGrey</option>
<option value="#82BC00">Green</option>
</select>
</div>
<div style="padding-top: 16px">
<input type="checkbox" id="allowMeasureOverMaxi" checked="checked" />
<label for="allowMeasureOverMaxi">Allow Measure Over Max</label>
</div>
<div class="option-row">
<label for="barSize">Bar Size:</label>
<input type="text" style="margin: 0 20px 0 6px;width:120px;" id="barSize" value="0.7" />
</div>
</div>
</div>
</body>
</html>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: calc(100% - 280px);
height: 100%;
overflow: hidden;
float: left;
}
.options-container {
float: right;
width: 280px;
padding: 12px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
}
.option-row {
font-size: 14px;
padding: 5px;
margin-top: 10px;
}
label {
margin-bottom: 6px;
}
input {
padding: 4px 6px;
}
input[type=button] {
margin-top: 6px;
display: block;
}
input[type=text] {
margin-top: 6px;
margin-bottom: 6px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}