概述
这个 demo 在 GanttSheet 里展示进度线、当前日期和状态日期,并能通过右侧面板即时调整样式。
实现思路
先从服务端接口读取甘特任务数据并绑定到 GanttSheet。
再设置项目当前日期和状态日期,让进度线有明确参照。
用户点击应用按钮后,把面板里的线型、颜色、点样式和标签样式回写到 progressLine。
代码解析
这一段直接把面板值写进 progressLine,所以样式修改后会立即反映到 Gantt 图上。
运行效果
左侧 Gantt 视图会显示任务进度和依赖关系。
当前日期线和状态日期线会用不同颜色区分。
进度线的显示、路径和点样式都可以即时切换。
/*REPLACE_MARKER*/
/*DO NOT DELETE THESE COMMENTS*/
var ganttSheet;
var GanttGridlineType = GC.Spread.Sheets.GanttSheet.GanttGridlineType;
var lineTypeMap = {};
lineTypeMap.thin = GanttGridlineType.thin;
lineTypeMap.dashed = GanttGridlineType.dashed;
lineTypeMap.dotted = GanttGridlineType.dotted;
lineTypeMap.dashDot = GanttGridlineType.dashDot;
lineTypeMap.empty = GanttGridlineType.empty;
window.onload = function() {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 0 });
initSpread(spread);
initSplitView(spread);
};
function initSpread(spread) {
spread.suspendPaint();
initGanttSheet(spread);
spread.resumePaint();
}
function initGanttSheet(spread) {
var tableName = "Gantt_Mode";
var baseApiUrl = getBaseApiUrl();
var apiUrl = baseApiUrl + "/" + tableName;
var dataManager = spread.dataManager();
var myTable = dataManager.addTable("myTable", {
batch: true,
remote: {
read: {
url: apiUrl
},
batch: {
url: apiUrl + "Collection"
}
},
schema: {
hierarchy: {
type: "Parent",
column: "parentId"
},
columns: {
id: { isPrimaryKey: true },
taskNumber: { dataType: "rowOrder" }
}
}
});
ganttSheet = spread.addSheetTab(0, "GanttSheet1", GC.Spread.Sheets.SheetType.ganttSheet);
var view = myTable.addView("myView1", [
{ value: "taskNumber", caption: "编号", width: 60 },
{ value: "name", caption: "任务名称", width: 200 },
{ value: "duration", caption: "工期", width: 90 },
{ value: "predecessors", caption: "前置任务", width: 120 },
{ value: "complete", caption: "完成百分比", width: 100 }
]);
view.fetch().then(function() {
return ganttSheet.bindGanttView(view);
}).then(function() {
initDefaultProjectDates();
applyFixedGridlines();
applyProgressLine();
});
initSidePanel();
}
function initDefaultProjectDates() {
var projectStart = ganttSheet.project.startDate;
var currentDate = addDays(projectStart, 4);
var statusDate = addDays(projectStart, 7);
ganttSheet.project.currentDate = currentDate;
ganttSheet.project.statusDate = statusDate;
document.getElementById("current-date").value = formatInputDate(currentDate);
document.getElementById("status-date").value = formatInputDate(statusDate);
}
function applyFixedGridlines() {
ganttSheet.gridlines.currentDate = {
lineColor: "#4A8F40",
lineType: GanttGridlineType.thin
};
ganttSheet.gridlines.statusDate = {
lineColor: "#5B9BD5",
lineType: GanttGridlineType.dashDot
};
}
function initSidePanel() {
document.getElementById("apply-progress-line").addEventListener("click", function() {
applyProjectDates();
applyFixedGridlines();
applyProgressLine();
});
}
function applyProjectDates() {
var currentDateValue = document.getElementById("current-date").value;
ganttSheet.project.currentDate = parseInputDate(currentDateValue);
var statusDateValue = document.getElementById("status-date").value;
ganttSheet.project.statusDate = parseInputDate(statusDateValue);
}
function applyProgressLine() {
ganttSheet.suspendPaint();
ganttSheet.progressLine = {
display: document.getElementById("display").checked,
referenceDate: document.getElementById("reference-date").value,
pathType: {
returnToReferenceDate: document.getElementById("return-to-reference-date").checked,
pointConnectionType: document.getElementById("point-connection-type").value
},
lineStyle: {
lineType: lineTypeMap[document.getElementById("line-type").value],
lineColor: document.getElementById("line-color").value
},
pointStyle: {
shape: document.getElementById("point-shape").value,
color: document.getElementById("point-color").value
},
dateLabelStyle: {
display: document.getElementById("label-display").checked,
format: document.getElementById("label-format").value,
font: document.getElementById("label-font").value
}
};
ganttSheet.resumePaint();
}
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
function formatInputDate(date) {
if (!date) {
return "";
}
var year = date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + date.getDate()).slice(-2);
return year + "-" + month + "-" + day;
}
function parseInputDate(value) {
if (!value) {
return null;
}
var parts = value.split("-");
return new Date(Number(parts[0]), Number(parts[1]) - 1, Number(parts[2]));
}
function getBaseApiUrl() {
return window.location.href.match(/http.+spreadjs\/SpreadJSTutorial\//)[0] + 'server/api';
}
function initSplitView(spread) {
var host = document.getElementById("split-view");
var content = host.getElementsByClassName("split-content")[0];
var panel = host.getElementsByClassName("split-panel")[0];
new SplitView({
host: host,
content: content,
panel: panel,
refreshContent: function() {
spread.refresh();
}
});
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta name="spreadjs culture" content="zh-cn" />
<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">
<link rel="stylesheet" type="text/css" href="$DEMOROOT$/spread/source/splitView/splitView.css">
<!-- Promise Polyfill for IE, https://www.npmjs.com/package/promise-polyfill -->
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
<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$/zh/purejs/node_modules/@grapecity-software/spread-sheets-tablesheet/dist/gc.spread.sheets.tablesheet.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-ganttsheet/dist/gc.spread.sheets.ganttsheet.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/splitView/splitView.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 id="split-view" class="sample-tutorial">
<div id="ss" class="sample-spreadsheets split-content"></div>
<div class="options-container split-panel">
<div class="option-row option-title">
配置进度线
</div>
<div class="option-block">
<div class="option-row option-title">
项目日期
</div>
<div class="option-row input-box">
<label for="current-date">当前日期</label>
<input type="date" id="current-date" />
</div>
<div class="option-row input-box">
<label for="status-date">状态日期</label>
<input type="date" id="status-date" />
</div>
</div>
<div class="option-block">
<div class="option-row option-title">
网格线
</div>
<div class="option-row">
<span class="line-swatch current-date-line"></span>
当前日期:绿色细线
</div>
<div class="option-row">
<span class="line-swatch status-date-line"></span>
状态日期:蓝色点划线
</div>
</div>
<div class="option-block">
<div class="option-row option-title">
进度线
</div>
<div class="option-row">
<label class="option-checkbox-label">
<input type="checkbox" id="display" checked />
显示
</label>
</div>
<div class="option-row selection-box">
<label for="reference-date">参考日期</label>
<select id="reference-date">
<option value="statusDate" selected>状态日期</option>
<option value="currentDate">当前日期</option>
</select>
</div>
</div>
<div class="option-block">
<div class="option-row option-title">
路径类型
</div>
<div class="option-row">
<label class="option-checkbox-label">
<input type="checkbox" id="return-to-reference-date" />
返回参考日期
</label>
</div>
<div class="option-row selection-box">
<label for="point-connection-type">连接方式</label>
<select id="point-connection-type">
<option value="orthogonal" selected>正交</option>
<option value="diagonal">对角线</option>
</select>
</div>
</div>
<div class="option-block">
<div class="option-row option-title">
线条样式
</div>
<div class="option-row selection-box">
<label for="line-type">线型</label>
<select id="line-type">
<option value="thin" selected>细线</option>
<option value="dashed">虚线</option>
<option value="dotted">点线</option>
<option value="dashDot">点划线</option>
<option value="empty">无</option>
</select>
</div>
<div class="option-row input-box">
<label for="line-color">颜色</label>
<input type="text" id="line-color" value="#C0504D" />
<div class="option-info valid">* 有效值:颜色字符串</div>
</div>
</div>
<div class="option-block">
<div class="option-row option-title">
点样式
</div>
<div class="option-row selection-box">
<label for="point-shape">形状</label>
<select id="point-shape">
<option value="arrowDown">下箭头</option>
<option value="arrowUp">上箭头</option>
<option value="caretDownTop">顶部下尖角</option>
<option value="caretUpBottom">底部上尖角</option>
<option value="circleDiamond" selected>圆形菱形</option>
<option value="circle">圆形</option>
<option value="circleArrowDown">圆形下箭头</option>
<option value="circleArrowUp">圆形上箭头</option>
<option value="circleTriangleDown">圆形下三角</option>
<option value="circleTriangleUp">圆形上三角</option>
<option value="diamond">菱形</option>
<option value="houseDown">下房形</option>
<option value="houseUp">上房形</option>
<option value="leftBracket">左括号</option>
<option value="leftFade">左渐隐</option>
<option value="lineShape">线形</option>
<option value="rightBracket">右括号</option>
<option value="rightFade">右渐隐</option>
<option value="square">方形</option>
<option value="star">星形</option>
<option value="triangleDown">下三角</option>
<option value="triangleLeft">左三角</option>
<option value="triangleRight">右三角</option>
<option value="triangleUp">上三角</option>
</select>
</div>
<div class="option-row input-box">
<label for="point-color">颜色</label>
<input type="text" id="point-color" value="#C0504D" />
<div class="option-info valid">* 有效值:颜色字符串</div>
</div>
</div>
<div class="option-block">
<div class="option-row option-title">
日期标签样式
</div>
<div class="option-row">
<label class="option-checkbox-label">
<input type="checkbox" id="label-display" checked />
显示
</label>
</div>
<div class="option-row input-box">
<label for="label-format">格式</label>
<input type="text" id="label-format" value="yyyy-MM-dd" />
</div>
<div class="option-row input-box">
<label for="label-font">字体</label>
<input type="text" id="label-font" value="10pt Calibri" />
</div>
</div>
<div class="option-block">
<div class="option-row">
<input type="button" id="apply-progress-line" class="option-button" value="应用进度线">
</div>
</div>
</div>
</div>
</body>
</html>
.sample-tutorial {
width: 100%;
height: 100%;
}
body, html {
padding: 0;
margin: 0;
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
}
.sample-spreadsheets {
width: 100%;
height: 100%;
}
.options-container {
float: right;
width: 300px;
padding: 12px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
box-shadow: inset 0px 0 4px 0 rgba(0,0,0,0.4);
}
.option-block {
background: #fff;
padding: 8px;
margin: 12px 0;
border-radius: 4px;
border: 1px dashed #82bc00;
box-shadow: 0px 0 6px 0 rgba(0,0,0,0.1);
}
.option-row {
font-size: 14px;
box-sizing: border-box;
padding: 4px 0;
color: #656565;
}
.option-title {
font-weight: bold;
color: #656565;
}
.option-info {
font-size: 12px;
color: #919191;
margin-top: 6px;
font-weight: normal;
}
.option-info.valid {
color: #82bc00;
}
.option-button {
width: 100%;
padding: 0;
line-height: 22px;
background: #82bc00;
color: #fff;
transition: 0.3s;
cursor: pointer;
outline: none;
border-radius: 4px;
box-sizing: border-box;
box-shadow: 0 1px 4px 0 rgba(0,0,0,0.3);
border: none;
}
.option-button:hover {
background: #82bc00;
color: #fff;
box-shadow: 0 3px 8px 0 rgba(0,0,0,0.4);
}
.option-checkbox-label {
cursor: pointer;
}
.selection-box {
position: relative;
}
.selection-box > select {
text-align: left;
width: 100%;
height: 20px;
padding: 0;
line-height: 20px;
background: transparent;
border: none;
border-bottom: 2px solid #656565;
color: #656565;
transition: 0.3s;
cursor: pointer;
outline: none;
box-sizing: border-box;
}
.selection-box > select > option {
background: white;
}
.selection-box > select:focus {
border-bottom: 2px solid #82bc00;
color: #82bc00;
box-shadow: 0 2px 6px 0 rgba(0,0,0,0.3);
}
.selection-box > label {
position: absolute;
cursor: pointer;
font-size: 12px;
color: #fff;
background: #656565;
padding: 0 4px;
right: 0;
top: 6px;
box-shadow: 0 1px 4px 0 rgba(0,0,0,0.3);
}
.input-box {
position: relative;
}
.input-box > input[type=text],
.input-box > input[type=date] {
width: 100%;
height: 22px;
background: transparent;
border: none;
color: #656565;
border-bottom: 2px solid #656565;
outline: none;
box-sizing: border-box;
transition: 0.3s;
}
.input-box > input[type=text]:focus,
.input-box > input[type=date]:focus {
color: #82bc00;
border-bottom: 2px solid #82bc00;
}
.input-box > input:disabled {
color: #b0b0b0;
border-bottom-color: #c9c9c9;
}
.input-box > label {
cursor: pointer;
position: absolute;
right: 0;
top: 5px;
font-size: 12px;
color: #fff;
background: #656565;
padding: 0 4px;
box-shadow: 0 1px 4px 0 rgba(0,0,0,0.3);
}
.line-swatch {
display: inline-block;
width: 36px;
height: 0;
margin-right: 6px;
vertical-align: middle;
}
.current-date-line {
border-top: 2px solid #4A8F40;
}
.status-date-line {
border-top: 2px dashed #5B9BD5;
border-top-style: dashed;
}