横向条状/竖向柱状迷你图

SpreadJS 提供横向条状(HBARSPARKLINE)和竖向柱状(VBARSPARKLINE)两种迷你图函数,用于在单元格内展示数据的相对大小。这两种迷你图适合快速对比多个数值的大小关系,常用于满意度调查、进度跟踪、绩效评估等数据可视化场景。

概述 本 Demo 展示了如何使用 HBARSPARKLINE(横向条状迷你图)和 VBARSPARKLINE(竖向柱状迷你图)函数在单元格内可视化数据。Demo 包含两个工作表:Horizontal 工作表展示客户服务满意度数据,使用横向条状图;Vertical 工作表展示 Sprint 进度数据,使用竖向柱状图。通过 LET 函数结合条件判断,实现根据数值大小动态设置条形颜色的效果。 实现思路 创建包含两个工作表的 Workbook,分别用于横向和竖向迷你图展示 为每个工作表设置表格数据(渠道满意度数据和员工进度数据) 使用 LET 函数定义颜色变量,根据数据值返回不同的颜色值 通过 HBARSPARKLINE 和 VBARSPARKLINE 函数创建迷你图,传入动态颜色参数 设置单元格格式、行高列宽和样式,优化显示效果 代码解析 创建横向条状迷你图 这段代码定义了一个公式生成函数。使用 LET 函数创建变量 ref 和 color:ref 引用单元格数值,color 通过嵌套 IF 函数根据数值范围返回不同的颜色值(满意度 >= 80% 显示深色,>= 60% 显示较深色,依此类推)。最后调用 HBARSPARKLINE 函数创建横向条状图。 遍历数据行,为每行的第 3 列设置迷你图公式,引用第 2 列的满意度数据。 创建竖向柱状迷你图 竖向柱状迷你图使用 VBARSPARKLINE 函数,除 value 和 colorScheme 参数外,还设置了 axisVisible=true 显示轴线,barWidth=0.4 设置柱宽度为单元格宽度的 40%。 设置单元格格式 为百分比数据列设置格式化器 "0%" 并左对齐,确保数值以百分比形式显示。 运行效果 Horizontal 工作表:显示不同客服渠道的满意度数据,每个渠道右侧显示横向条状迷你图,颜色从深到浅表示满意度从高到低 Vertical 工作表:显示 8 位员工的 Sprint 进度,每个员工下方显示竖向柱状迷你图,数值超过 100% 时顶部显示箭头标记 迷你图颜色根据数值自动变化:>= 80% 显示深蓝色,>= 60% 显示蓝色,>= 40% 显示绿色,>= 20% 显示浅绿色,>= 0% 显示浅黄绿色,负值显示红色 API 参考 HBARSPARKLINE 函数 value:数字或单元格引用,表示条的长度。值大于 100% 或小于 -100% 时显示箭头 colorScheme:可选,颜色字符串,表示条的填充色,默认值为 "gray" axisVisible:可选,布尔值,表示轴是否可见,默认值为 true barHeight:可选,数值,表示相对于单元格高度的百分比,值需大于 0 且小于等于 1 正值时条状图从单元格左侧开始,负值时从右侧开始。 VBARSPARKLINE 函数 value:数字或单元格引用,表示柱的高度。值大于 100% 或小于 -100% 时显示箭头 colorScheme:可选,颜色字符串,表示柱的填充色,默认值为 "gray" axisVisible:可选,布尔值,表示轴是否可见,默认值为 true barWidth:可选,数值,表示相对于单元格宽度的百分比,值需大于 0 且小于等于 1 正值时柱状图从单元格底部开始,负值时从顶部开始。 LET 函数 name1:必需,第一个变量名称,必须以字母开头 name_value1:必需,分配给 name1 的值 calculation_or_name2:必需,使用 LET 函数中名称的计算表达式,或第二个变量名称 LET 函数允许在公式中声明变量并赋值,提升公式可读性和性能,避免重复计算相同表达式。
var spreadNS = GC.Spread.Sheets; window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 2 }); initSpread(spread); }; 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.setArray(1,0,[["Channel","Satisfaction %", "Diagram"], ["Email",0.92], ["Social Networks",0.79], ["Forums/Message Boards",0.65], ["Phone",0.58], ["Text Message",0.55], ["Online Chat",0.39], ["Self-Serve Knowledge Base",0.28], ["On-Page Support Widget",0.19]]); for (var i = 2; i < 10; i++) { sheet.setRowHeight(i, 30); sheet.setFormula(i, 2, this.getFormula("B"+(i+1))); } sheet.addSpan(0, 0, 1, 3); sheet.getCell(0, 0).value("Customer Service Satisfaction, by Channel").font("17px Arial").backColor("#E8E8E8").vAlign(GC.Spread.Sheets.VerticalAlign.center); sheet.getRange(1, 0, 1, 3).font("bold 13px Arial").vAlign(GC.Spread.Sheets.VerticalAlign.center) .setBorder(new GC.Spread.Sheets.LineBorder("black", GC.Spread.Sheets.LineStyle.thin), { bottom: true }); sheet.getRange(2,1,8,1).formatter("0%").hAlign(GC.Spread.Sheets.HorizontalAlign.left); sheet.setRowHeight(0, 35); sheet.setRowHeight(1, 35); sheet.setColumnWidth(0, 250); sheet.setColumnWidth(1, 120); sheet.setColumnWidth(2, 250); sheet.resumePaint(); } function getFormula(range) { return "=LET(ref, "+range+", color, IF(ref >= 0.8, \"#092834\", IF(ref>=0.6, \"#347B98\", IF(ref >0.4, \"#66B032\", IF(ref >= 0.2, \"#B2D732\", IF(ref >= 0, \"#F0F7D4\", \"red\"))))), HBARSPARKLINE(ref, color))"; } function getVBarFormula(range) { return "=LET(ref, "+range+", color, IF(ref >= 0.8, \"#092834\", IF(ref>=0.6, \"#347B98\", IF(ref >0.4, \"#66B032\", IF(ref >= 0.2, \"#B2D732\", IF(ref >= 0, \"#F0F7D4\", \"red\"))))), VBARSPARKLINE(ref, color, true, 0.4))"; } function initVerticalSparkline(sheet, name) { sheet.suspendPaint(); sheet.name(name); sheet.setArray(1,0,[ ["Name","Employee 1","Employee 2","Employee 3","Employee 4","Employee 5","Employee 6","Employee 7","Employee 8"], ["Progress",1.1,0.79,0.59,0.55,0.37,0.19,0.35,0.60] ]); for (var i = 0; i < 8; i++) { var columnChar = String.fromCharCode(66 + i); sheet.setColumnWidth(i+1, 80); sheet.setFormula(3, i+1, this.getVBarFormula(columnChar + "3")); } sheet.addSpan(0, 0, 1, 9); sheet.getCell(0, 0).value("Sprint 3 Progress").font("17px Arial").backColor("#E8E8E8").vAlign(GC.Spread.Sheets.VerticalAlign.center); sheet.getRange(1, 0, 2, 1).font("bold 13px Arial").vAlign(GC.Spread.Sheets.VerticalAlign.center) .setBorder(new GC.Spread.Sheets.LineBorder("black", GC.Spread.Sheets.LineStyle.thin), { right: true }); sheet.getRange(2,1,1,8).formatter("0%").hAlign(GC.Spread.Sheets.HorizontalAlign.left); sheet.setRowHeight(0, 35); sheet.setRowHeight(1, 35); sheet.setRowHeight(3, 220); sheet.setColumnWidth(0, 120); 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" 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; }