帕累托函数迷你图

帕累托函数迷你图通过可视化方式突出显示最重要的因素,帮助用户快速识别关键问题。Demo 以员工迟到原因为例,展示了水平和垂直两种显示方式,并支持自定义目标线、高亮项和标签样式。

概述 本 Demo 展示了帕累托函数迷你图的使用方法,以员工迟到原因为例,演示了水平和垂直两种布局方式。用户可以通过右侧面板实时调整标签类型、高亮项、目标线位置和颜色等参数,直观地看到帕累托分析结果。 实现思路 创建两个工作表,分别展示水平和垂直布局的帕累托迷你图 准备迟到原因统计数据,包括原因、案例数、颜色和柱子大小 使用 PARETOSPARKLINE 公式为每个数据点创建对应的迷你图单元格 监听面板控件的 change 事件,动态更新公式参数 通过公式引用实现实时交互效果 代码解析 创建水平帕累托迷你图 这段代码为每一行数据设置帕累托迷你图公式。关键点: points 参数使用 $B$3:$B$8 绝对引用,所有单元格共享同一组数据 pointIndex 使用 ROW()-2 动态计算当前数据点的索引(从第 2 行开始) vertical 参数设为 false,表示水平显示 其他参数从 HTML 控件动态获取 响应参数变化 当用户修改标签类型、高亮项或目标线参数时,触发 applyChanges 函数重新设置公式。 运行效果 打开 Demo 后,默认显示水平布局的工作表,展示员工迟到原因的帕累托分析 切换到 "Vertical" 工作表,可以看到垂直布局的帕累托图 在右侧面板中调整参数,迷你图会实时更新: 标签类型:可选择无、累计百分比或单一百分比 高亮项:选择要高亮显示的数据项(红色标记) 目标1和目标2:设置两条参考线的位置(0-1 之间) 目标线颜色:自定义两条目标线的显示颜色 通过分析可以快速识别:交通、育儿和公共交通是造成 80% 员工迟到的主要原因 API 参考 PARETOSPARKLINE 函数 points:数据区域,如 "B2:B7" pointIndex:数据点索引(从 1 开始),可以是数字或单元格引用 colorRange:颜色区域,指定每个柱子的颜色 target、target2:目标线位置(0-1 之间) highlightPosition:高亮项索引,该数据点显示为红色 label:标签类型,0=不显示,1=累计百分比,2=单一百分比 vertical:是否垂直显示,true=垂直,false=水平 targetColor、target2Color:目标线颜色 labelColor:标签前景色 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("labelType").addEventListener('change',applyChanges); document.getElementById("highlightPosition").addEventListener('change',applyChanges); document.getElementById("target1").addEventListener('change',applyChanges); document.getElementById("target2").addEventListener('change',applyChanges); document.getElementById("color1").addEventListener('change',applyChanges); document.getElementById("color2").addEventListener('change',applyChanges); }; function applyChanges() { var sheet = spread.getActiveSheet(); var labelType = parseInt(document.getElementById("labelType").value, 10), highlightPosition = parseInt(document.getElementById("highlightPosition").value, 10); if (!isNaN(labelType) && !isNaN(highlightPosition)) { var formulaPart1, formulaPart2; if (spread.getActiveSheetIndex() == 0) { var formula = '=PARETOSPARKLINE($B$3:$B$8,ROW()-2,$C$3:$C$8,'+ document.getElementById('target1').value + ',' + document.getElementById('target2').value +',' + highlightPosition + ','+ labelType +',false,"'+ document.getElementById('color1').value + '","' + document.getElementById('color2').value +'",$C$3:$C$8,$D$3:$D$8)'; for (var i = 2; i <= 7; i++) { sheet.setFormula(i, 4, formula); } } else { var formulaVertical = '=PARETOSPARKLINE($B$3:$G$3,COLUMN()-1,$B$4:$G$4,'+ document.getElementById('target1').value + ',' + document.getElementById('target2').value +',' + highlightPosition + ','+ labelType +',true,"'+ document.getElementById('color1').value + '","' + document.getElementById('color2').value +'",$B$4:$G$4,$B$5:$G$5)'; for (var index = 1; index < 7; index++) { sheet.setFormula(5, index, formulaVertical); } } } } 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, [ ["Reason","Cases","Color","Bar Size","Diagram"], ["Traffic",20,"#CCCCCC",0.1], ["Child care",15,"#82BC00",0.3], ["Public transportation",13,"#F7A711",0.5], ["Weather",6,"#00C2D6",0.7], ["Overslept",4,"#FFE7BA",0.8], ["Emergency",1,"#000000",1] ]); sheet.setValue(10, 0, '*Result: By the sparkline above can draw a conclusion that the reasons for 80% of the employees be late are "traffic", "child care" and "public transportation".'); for (var index = 2; index < 8; index++) { sheet.setFormula(index, 4, '=PARETOSPARKLINE($B$3:$B$8,ROW()-2,$C$3:$C$8,'+ document.getElementById('target1').value + ',' + document.getElementById('target2').value +',0,2,false,"'+ document.getElementById('color1').value + '","' + document.getElementById('color2').value +'",$C$3:$C$8,$D$3:$D$8)'); } //styling for(var i = 1; i<4; i++) { sheet.setColumnWidth(i, 80); } sheet.setColumnWidth(4, 340); sheet.setColumnWidth(0, 140); sheet.setRowHeight(0, 35); sheet.addSpan(0, 0, 1, 5); sheet.getCell(0, 0).value("Late Arrivals by Reported Cause") .font("17px Arial") .vAlign(spreadNS.VerticalAlign.center) .backColor("gray") .foreColor("white"); sheet.getRange(2,0,6,4).hAlign(spreadNS.HorizontalAlign.left); sheet.getRange(1, 0, 1, 5) .font("bold 13px Arial") .setBorder(new spreadNS.LineBorder("black", spreadNS.LineStyle.thin), { bottom: true }); sheet.resumePaint(); } function initVerticalSparkline(sheet, name) { sheet.suspendPaint(); sheet.name(name); sheet.setArray(1, 0, [ ["Reason","Traffic","Child care","Public transportation","Weather","Overslept","Emergency"], ["Cases",20,15,13,5,4,1], ["Color","#CCCCCC","#82BC00","#F7A711","#00C2D6","#FFE7BA","#000000"], ["Bar Size",0.1,0.3,0.5,0.7,0.8,1], ["Diagram"] ]); sheet.setValue(8, 0, '*Result: By the sparkline above can draw a conclusion that the reasons for 80% of the employees be late are "traffic", "child care" and "public transportation".'); for (var index = 1; index < 7; index++) { sheet.setFormula(5, index, '=PARETOSPARKLINE($B$3:$G$3,COLUMN()-1,$B$4:$G$4,'+ document.getElementById('target1').value + ',' + document.getElementById('target2').value +',0,2,true,"'+ document.getElementById('color1').value + '","' + document.getElementById('color2').value +'",$B$4:$G$4,$B$5:$G$5)'); } //styling for(var i = 0; i<7; i++) { sheet.setColumnWidth(i, 120); } sheet.setColumnWidth(3, 150); sheet.setRowHeight(5, 160); sheet.setRowHeight(0, 35); sheet.addSpan(0, 0, 1, 7); sheet.getCell(0, 0).value("Late Arrivals by Reported Cause") .font("17px Arial") .vAlign(spreadNS.VerticalAlign.center) .backColor("gray") .foreColor("white"); sheet.getRange(1, 0, 5, 1) .font("bold 13px Arial") .setBorder(new spreadNS.LineBorder("black", spreadNS.LineStyle.thin), { right: true }); sheet.getRange(1,0,4,7).hAlign(spreadNS.HorizontalAlign.left); 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 class="option-row"> <label><b>迷你图设置:</b></label> </div> <hr> <div class="option-row"> <label for="labelType">标签类型:</label> <select id="labelType"> <option value="0">无</option> <option value="1">累计百分比</option> <option value="2" selected>单一百分比</option> </select> </div> <div class="option-row"> <label for="highlightPosition">高亮项:</label> <select id="highlightPosition"> <option value="0" selected>无</option> <option value="1">第1项</option> <option value="2">第2项</option> <option value="3">第3项</option> <option value="4">第4项</option> <option value="5">第5项</option> <option value="6">第6项</option> </select> </div> <div class="option-row"> <label for="target1">目标1:</label> <input type="text" style="margin: 0 20px 0 6px;width:120px;" id="target1" value="0.5" /> </div> <div class="option-row"> <label for="target2">目标2:</label> <input type="text" style="margin: 0 20px 0 6px;width:120px;" id="target2" value="0.8" /> </div> <div class="option-row"> <label for="color1">目标1颜色:</label> <select id="color1" style="margin: 0 20px 0 6px"> <option value="#BBBBBB" selected>灰色</option> <option value="#82BC00">绿色</option> <option value="#000000">黑色</option> <option value="#F7A711">橙色</option> </select> </div> <div class="option-row"> <label for="color2">目标2颜色:</label> <select id="color2" style="margin: 0 20px 0 6px"> <option value="#BBBBBB">灰色</option> <option value="#82BC00">绿色</option> <option value="#000000">黑色</option> <option value="#F7A711" selected>橙色</option> </select> </div> </div> </div> </body> </html>
.sample { position: relative; height: 100%; overflow: auto; } .sample::after { display: block; content: ""; clear: both; } .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; } .option-group { margin-bottom: 6px; } input, select { padding: 4px 6px; box-sizing: border-box; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }