概述
本 Demo 展示了如何使用 VARISPARKLINE 函数创建方差函数迷你图,用于可视化销售数据中预算与实际值的差异百分比。Demo 创建了一个销售报表,通过方差迷你图直观展示每个月的实际销售额与预算之间的偏差程度。
实现思路
创建表格并填充销售数据,包含月份、预算和实际销售额
使用公式计算每个月的方差(实际值减预算值)
使用 VARISPARKLINE 函数创建方差迷你图,显示方差百分比
设置数值格式化,提升数据可读性
代码解析
创建表格并设置数据
创建一个 13 行 5 列的表格,包含表头和 12 个月的销售数据。
计算方差和方差百分比
D 列公式:计算方差(实际值 - 预算值)
E 列公式:使用 VARISPARKLINE 创建方差迷你图
ROUND((D/B),2):计算方差百分比并四舍五入到 2 位小数
0:参照线位置为 0(表示无偏差的基准线)
省略 mini、maxi、mark 参数,使用默认值
0.2:刻度单位为 0.2
TRUE:显示图例说明
设置单元格格式
设置数值格式和右对齐,提升数据可读性。
运行效果
表格展示 12 个月的销售数据,包括预算、实际值和方差
Variance 列显示方差金额(美元格式)
Variance % 列使用迷你图可视化方差百分比,条形长度表示偏差大小,颜色区分正负偏差
参照线位于 0 点,方便判断偏差方向
API 参考
VARISPARKLINE 函数
主要参数:
variance:条的长度,表示方差值
reference:参照线位置,默认为 0
tickunit:刻度单位
legend:是否显示图例说明
colorPositive:正方差的填充颜色,默认为 "green"
colorNegative:负方差的填充颜色,默认为 "red"
vertical:是否垂直显示,默认为 false(水平显示)
window.onload = function () {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 2 });
initSpread(spread);
};
function initSpread(spread) {
spread.suspendPaint();
var sheet = spread.getActiveSheet();
let spreadNS = GC.Spread.Sheets;
var table1 = sheet.tables.add("table1", 0, 0, 13, 5, spreadNS.Tables.TableThemes.medium4);
table1.rowFilter().filterButtonVisible(false);
sheet.setArray(0, 0, [
["Sales", "Budget", "Actual", "Variance", "Variance %"],
["Jan",305554 ,336431],
["Feb",280311 ,370582 ],
["Mar",309347 ,322689 ],
["Apr",502688 ,386790 ],
["May",351267 ,363122 ],
["Jun",351922 ,314589 ],
["Jul",364770 ,342515 ],
["Aug",373980 ,317154 ],
["Sep",397717 ,360083 ],
["Oct",275849 ,370909 ],
["Nov",426031 ,376791 ],
["Dec",267710 ,398198 ]
]);
for (let i = 2; i < 14; i++) {
sheet.setFormula((i-1),3,'=(C'+ i +'-B'+ i+')');
sheet.setFormula((i-1),4,'=VARISPARKLINE(ROUND((D'+ i+')/(B'+i+'),2),0,,,,0.2,TRUE)');
}
//some cell formatting
sheet.getRange(1, 1, 12, 3)
.hAlign(GC.Spread.Sheets.HorizontalAlign.right)
.formatter("$#,000;$-#,000");
sheet.getRange(1, 4, 12, 1)
.hAlign(GC.Spread.Sheets.HorizontalAlign.right)
.formatter("0%");
sheet.setColumnWidth(1, 120);
sheet.setColumnWidth(2, 120);
sheet.setColumnWidth(3, 120);
sheet.setColumnWidth(4, 200);
spread.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" style="width:100%;height:100%"></div>
</div>
</body>
</html>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}