概述
本 Demo 展示了如何使用 BOXPLOTSPARKLINE 函数创建箱线图迷你图。Demo 包含两个工作表:第一个工作表展示不同地区 2014 年的销售数据,使用水平箱线图对比各地区销售分布;第二个工作表展示三种教学方法的学生成绩分布,使用垂直箱线图进行对比分析。Demo 还提供了交互式控制面板,允许用户动态调整比例范围、可接受范围和配色方案。
实现思路
初始化两个工作表,分别命名为 "Horizontal" 和 "Vertical"
在第一个工作表中加载地区销售数据,为每个地区设置水平箱线图公式
在第二个工作表中加载教学方法评分数据,为每种方法设置垂直箱线图公式
为控制面板的输入框添加默认值(比例范围 0-100,可接受范围 20-80)
监听"更改设置"按钮点击事件,根据当前工作表重新应用箱线图公式
代码解析
创建水平箱线图
这段代码为第 3-11 行的销售数据创建水平箱线图。BOXPLOTSPARKLINE 函数的参数依次为:
points:数据区域,使用 $C 和 $N 锁定列引用
boxPlotClass:箱线类型为 "5ns"(五数概括:最小值、Q1、中位数、Q3、最大值)
showAverage:true 表示显示平均值线
scaleStart 和 scaleEnd:比例范围 0-100
acceptableStart 和 acceptableEnd:可接受范围 20-80
colorScheme:箱线颜色为浅灰色 "#D3D3D3"
style:样式为 0(传统样式)
vertical:false 表示水平显示
创建垂直箱线图
这段代码为三种教学方法分别创建垂直箱线图。参数与水平箱线图类似,不同之处:
vertical 参数为 true,表示垂直显示
每个箱线图占用 20 行的高度(通过 addSpan 合并单元格)
动态更新箱线图设置
这段代码从控制面板读取用户输入的参数值,动态重新生成箱线图公式。用户可以调整比例范围、可接受范围和配色方案。
运行效果
在 "Horizontal" 工作表中,每行销售数据旁边显示一个水平箱线图,直观展示各地区 2014 年销售分布情况
在 "Vertical" 工作表中,三种教学方法的数据右侧显示三个垂直箱线图,便于对比不同教学方法的成绩分布
点击右侧控制面板的"更改设置"按钮,可以实时调整箱线图的比例范围、可接受范围和颜色
可接受范围(20-80)会在箱线图中以特殊样式显示,帮助识别超出预期范围的数据
API 参考
BOXPLOTSPARKLINE 函数
points:数据区域,例如 "A1:A4"
boxPlotClass:箱线类型,可选值:"5ns"(默认)、"7ns"、"tukey"、"bowley"、"sigma3"
showAverage:是否显示平均值线,默认 false
scaleStart:迷你图最小边界,默认为所有数据的最小值
scaleEnd:迷你图最大边界,默认为所有数据的最大值
acceptableStart:可接受范围起始值,默认无
acceptableEnd:可接受范围结束值,默认无
colorScheme:箱线颜色,默认 "#D2D2D2"
style:样式,0(传统样式,默认)或 1(Neo 样式)
vertical:是否垂直显示,默认 false
var spreadNS = GC.Spread.Sheets, spread;
window.onload = function () {
spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 2 });
initSpread(spread);
document.getElementById('btnChangeSetting').addEventListener('click',applyChanges);
};
function applyChanges() {
var sheet = spread.getActiveSheet();
if (spread.getActiveSheetIndex() == 0) {
for (var index = 3; index < 12; index++) {
sheet.setFormula(index, 14, '=BOXPLOTSPARKLINE($C' + (index + 1) + ':$N' + (index + 1) + ',"5ns",true,' +
document.getElementById('scaleStart').value + ',' +
document.getElementById('scaleEnd').value + ',' +
document.getElementById('acceptableStart').value + ',' +
document.getElementById('acceptableEnd').value + ',"' +
document.getElementById('colorScheme').value +
'",0,FALSE)');
}
} else if (spread.getActiveSheetIndex() == 1) {
sheet.setFormula(3, 6, '=BOXPLOTSPARKLINE(C4:C23,"5ns",true,' +
document.getElementById('scaleStart').value + ',' +
document.getElementById('scaleEnd').value + ',' +
document.getElementById('acceptableStart').value + ',' +
document.getElementById('acceptableEnd').value + ',"' +
document.getElementById('colorScheme').value +
'",0,TRUE)');
sheet.setFormula(3, 7, '=BOXPLOTSPARKLINE(D4:D23,"5ns",true,' +
document.getElementById('scaleStart').value + ',' +
document.getElementById('scaleEnd').value + ',' +
document.getElementById('acceptableStart').value + ',' +
document.getElementById('acceptableEnd').value + ',"' +
document.getElementById('colorScheme').value +
'",0,TRUE)');
sheet.setFormula(3, 8, '=BOXPLOTSPARKLINE(E4:E23,"5ns",true,' +
document.getElementById('scaleStart').value + ',' +
document.getElementById('scaleEnd').value + ',' +
document.getElementById('acceptableStart').value + ',' +
document.getElementById('acceptableEnd').value + ',"' +
document.getElementById('colorScheme').value +
'",0,TRUE)');
}
}
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, 14);
sheet.getCell(1, 1).value("The Company Sales in 2014")
.font("20px Arial").vAlign(spreadNS.VerticalAlign.center)
.foreColor("white").backColor("#999999");
sheet.getRange(2, 1, 1, 14).vAlign(spreadNS.VerticalAlign.center)
.font("bold 12px Arial")
.backColor("#efefef");
sheet.setValue(2, 1, "Region");
sheet.setValue(2, 14, "Actual Sales (mn)");
for (var i = 2; i < 14; i++) {
sheet.setValue(2, i, new Date(2014, i - 2, 1));
sheet.setColumnWidth(i, 40);
sheet.setFormatter(2, i, "mmm");
}
sheet.setArray(3, 1, [
["Alabama",68, 81, 21, 69, 39, 28, 63, 73, 80, 95, 46, 37],
["Alaska",37, 57, 99, 47, 95, 86, 44, 82, 89, 61, 45, 75],
["Arizona",61, 42, 97, 12, 95, 25, 29, 44, 64, 39, 59, 35],
["Idaho",16, 51, 45, 82, 72, 51, 100, 98, 92, 89, 44, 68],
["Indiana",26, 46,74, 58, 40,32,58, 75, 39, 58, 97, 39],
["Ohio",37, 68, 20, 35, 65, 87, 39, 25, 79, 15, 91, 42],
["Oklahoma",36, 43, 31, 63, 95, 26, 19, 88, 25, 78, 98, 53],
["Oregon",28, 71, 68, 48, 19, 56, 88, 54, 28, 25, 92, 75],
["Vermon",85, 25, 31, 34, 75, 82, 50, 27, 76, 72, 25, 100]]);
for (var index = 3; index < 12; index++) {
sheet.setFormula(index, 14, '=BOXPLOTSPARKLINE($C' + (index + 1) + ':$N' + (index + 1) + ',"5ns",true,0,100,20,80,"#D3D3D3",0,false)');
}
for (let r = 1; r < 12; r++) {
sheet.setRowHeight(r, 30);
}
sheet.setColumnWidth(14, 250);
sheet.resumePaint();
}
function initVerticalSparkline(sheet, name) {
sheet.suspendPaint();
sheet.name(name);
sheet.addSpan(1, 1, 1, 4);
sheet.getCell(1, 1).value("Scores by Teaching Method").vAlign(spreadNS.VerticalAlign.center)
.font("bold 14px Arial")
.foreColor("white").backColor("#999999");
sheet.getRange(2, 1, 1, 4).vAlign(spreadNS.VerticalAlign.center)
.font("bold 12px Arial")
.backColor("#efefef");
sheet.setValue(2, 1, "Student ID");
sheet.setValue(2, 2, "Method 1");
sheet.setValue(2, 3, "Method 2");
sheet.setValue(2, 4, "Method 3");
sheet.getCell(23, 6).value("Method 1").font("bold 10px Arial");
sheet.getCell(23, 7).value("Method 2").font("bold 10px Arial");
sheet.getCell(23, 8).value("Method 3").font("bold 10px Arial");
let score = [
{ id: 1, methodOne: 65, methodTwo: 60, methodThree: 98 },
{ id: 2, methodOne: 91, methodTwo: 70, methodThree: 99 },
{ id: 3, methodOne: 70, methodTwo: 86, methodThree: 92 },
{ id: 4, methodOne: 62, methodTwo: 62, methodThree: 78 },
{ id: 5, methodOne: 98, methodTwo: 79, methodThree: 71 },
{ id: 6, methodOne: 89, methodTwo: 99, methodThree: 68 },
{ id: 7, methodOne: 85, methodTwo: 100, methodThree: 88 },
{ id: 8, methodOne: 65, methodTwo: 78, methodThree: 74 },
{ id: 9, methodOne: 65, methodTwo: 66, methodThree: 85 },
{ id: 10, methodOne: 65, methodTwo: 84, methodThree: 80 },
{ id: 11, methodOne: 65, methodTwo: 98, methodThree: 79 },
{ id: 12, methodOne: 65, methodTwo: 87, methodThree: 50 },
{ id: 13, methodOne: 51, methodTwo: 68, methodThree: 64 },
{ id: 14, methodOne: 45, methodTwo: 78, methodThree: 73 },
{ id: 15, methodOne: 67, methodTwo: 81, methodThree: 88 },
{ id: 16, methodOne: 87, methodTwo: 83, methodThree: 85 },
{ id: 17, methodOne: 72, methodTwo: 84, methodThree: 84 },
{ id: 18, methodOne: 74, methodTwo: 82, methodThree: 86 },
{ id: 19, methodOne: 85, methodTwo: 45, methodThree: 92 },
{ id: 20, methodOne: 65, methodTwo: 60, methodThree: 69 }
];
for (let i = 0, len = score.length; i < len; i++) {
let student = score[i];
sheet.setValue(i + 3, 1, student.id);
sheet.setValue(i + 3, 2, student.methodOne);
sheet.setValue(i + 3, 3, student.methodTwo);
sheet.setValue(i + 3, 4, student.methodThree);
}
for(let index=0;index<4;index++)
{
sheet.setColumnWidth(index+1, 100);
}
//sparklines
sheet.addSpan(3, 6, 20, 1);
sheet.addSpan(3, 7, 20, 1);
sheet.addSpan(3, 8, 20, 1);
sheet.setFormula(3, 6, '=BOXPLOTSPARKLINE(C4:C23,"5ns",true,0,100,20,80,"#DDDDDD",0, true)');
sheet.setFormula(3, 7, '=BOXPLOTSPARKLINE(D4:D23,"5ns",true,0,100,20,80,"#DDDDDD",0, true)');
sheet.setFormula(3, 8, '=BOXPLOTSPARKLINE(E4:E23,"5ns",true,0,100,20,80,"#DDDDDD",0, true)');
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>设置:</b></label>
</div>
<hr>
<div class="option-row">
<label for="scaleStart">销售起始范围:</label>
<input type="text" style="margin: 0 20px 0 6px;" id="scaleStart" value="0" />
</div>
<div class="option-row">
<label for="scaleEnd">销售结束范围:</label>
<input type="text" style="margin: 0 20px 0 6px;" id="scaleEnd" value="100" />
</div>
<div class="option-row">
<label for="acceptableStart">预期销售起始范围:</label>
<input type="text" style="margin: 0 20px 0 6px;" id="acceptableStart" value="20" />
</div>
<div class="option-row">
<label for="acceptableEnd">预期销售结束范围:</label>
<input type="text" style="margin: 0 20px 0 6px;" id="acceptableEnd" value="80" />
</div>
<div class="option-row">
<label for="colorScheme">配色方案:</label>
<select id="colorScheme" style="margin: 0 20px 0 6px">
<option value="#DDDDDD" selected>浅灰色</option>
<option value="#BBBBBB">灰色</option>
<option value="#999999">深灰色</option>
<option value="#82BC00">绿色</option>
<option value="#000000">黑色</option>
<option value="#F7A711">橙色</option>
</select>
</div>
<hr>
<div class="option-row">
<div class="option">
<input id='btnChangeSetting' type='button' value='更改设置' />
</div>
</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;
}