概述
本 Demo 展示如何自定义 GanttSheet 图表区域的网格线样式。Demo 提供了交互式设置面板,允许用户选择不同的网格线类型(如甘特行、时间刻度列、项目节点等),并设置线条类型、颜色等属性。对于甘特行网格线,还支持设置间隔样式,实现交替显示不同样式的网格线效果。
实现思路
初始化 GanttSheet 并绑定数据源,加载项目任务数据
设置默认的网格线样式,包括底层刻度列网格线和甘特行网格线
创建右侧交互面板,提供网格线类型选择(影响项)和样式设置选项
对于甘特行(ganttRows),额外提供间隔网格线设置选项
用户点击"设置网格线"按钮后,将配置应用到 GanttSheet 的 gridlines 属性
代码解析
设置默认网格线样式
这段代码在 GanttSheet 初始化后设置默认的网格线样式。底层刻度列使用虚线样式和橙色;甘特行网格线使用细线和绿色,并设置了间隔为 2 的点划线样式蓝色网格线。
动态应用网格线样式
点击"设置网格线"按钮时,从交互面板获取用户设置的参数,构建网格线配置对象并应用到 GanttSheet 的对应网格线属性上。
运行效果
页面加载后显示甘特图,默认设置了底层刻度列网格线(虚线橙色)和甘特行网格线(细线绿色 + 间隔点划线蓝色)
在右侧面板的"影响项"下拉框中选择要设置的网格线类型(如 ganttRows、topTierColumn 等)
选择线条类型(如细线、虚线、点线、点划线)并输入颜色值
如果选择的是 ganttRows,还可以设置间隔网格线的样式(间隔数、间隔线条类型和颜色)
点击"设置网格线"按钮,甘特图区域的网格线会立即更新为设置的样式
API 参考
ganttSheet.gridlines
GanttSheet 的网格线集合对象,包含以下属性:
ganttRows:任务栏分隔网格线(支持间隔样式)
topTierColumn:顶层时间刻度列网格线
middleTierColumn:中层时间刻度列网格线
bottomTierColumn:底层时间刻度列网格线
currentDate:当前日期网格线
projectStart:项目开始日期网格线
projectFinish:项目结束日期网格线
网格线属性
常规样式网格线属性:
lineType:线条类型,使用 GC.Spread.Sheets.GanttSheet.GanttGridlineType 枚举值(如 thin、dashed、dotted、dashDot)
lineColor:线条颜色,字符串格式(如 "#ff0000" 或 "red")
间隔样式网格线额外属性(仅 ganttRows 支持):
interval:间隔计数,整数值,表示每隔几行显示间隔网格线
intervalLineType:间隔线条类型
intervalLineColor:间隔线条颜色
/*REPLACE_MARKER*/
/*DO NOT DELETE THESE COMMENTS*/
var myTable;
var ganttSheet;
window.onload = function() {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 0 });
initSpread(spread);
initSplitView(spread);
};
function initSpread(spread) {
spread.suspendPaint();
initDataSource(spread);
initGanttSheet(spread);
spread.resumePaint();
}
function initDataSource(spread) {
var tableName = "Gantt_Id";
var baseApiUrl = getBaseApiUrl();
var apiUrl = baseApiUrl + "/" + tableName;
var dataManager = spread.dataManager();
myTable = dataManager.addTable("myTable", {
batch: true,
remote: {
read: {
url: apiUrl
}
},
schema: {
hierarchy: {
type: "Parent",
column: "parentId"
},
columns: {
id: { isPrimaryKey: true },
taskNumber: { dataType: "rowOrder" }
}
}
});
}
function initGanttSheet(spread) {
ganttSheet = spread.addSheetTab(0, "GanttSheet", GC.Spread.Sheets.SheetType.ganttSheet);
var view = myTable.addView("ganttView", [
{ value: "taskNumber", caption: "NO.", width: 60 },
{ value: "name", caption: "Task Name", width: 200 },
{ value: "duration", caption: "Duration", width: 90 },
{ value: "predecessors", caption: "Predecessors", width: 120, visible: false }
]);
view.fetch().then(function() {
ganttSheet.bindGanttView(view);
ganttSheet.project.timescale.tierMode = GC.Spread.Sheets.GanttSheet.TimescaleTierMode.topMiddleBottom;
ganttSheet.project.timescale.topTier.unit = GC.Spread.Sheets.GanttSheet.TimescaleUnit.thirdsOfMonth;
}).then(function() {
ganttSheet.gridlines.bottomTierColumn = {
lineType: GC.Spread.Sheets.GanttSheet.GanttGridlineType.dashed,
lineColor: "#c85b11"
};
ganttSheet.gridlines.ganttRows.lineType = GC.Spread.Sheets.GanttSheet.GanttGridlineType.thin;
ganttSheet.gridlines.ganttRows.lineColor = "#abd08f";
ganttSheet.gridlines.ganttRows.interval = 2;
ganttSheet.gridlines.ganttRows.intervalLineType = GC.Spread.Sheets.GanttSheet.GanttGridlineType.dashDot;
ganttSheet.gridlines.ganttRows.intervalLineColor = "#9cc3e5";
});
initSidePanel(ganttSheet);
}
function initSidePanel(ganttSheet) {
var setButton = document.getElementById("set-gridline");
var intervalPanel = document.getElementById("interval-panel");
var affectItemOption = document.getElementById("affectItem");
var lineTypeOption = document.getElementById("lineType");
var lineColorOption = document.getElementById("lineColor");
var intervalLineTypeOption = document.getElementById("intervalLineType");
var intervalLineColorOption = document.getElementById("intervalLineColor");
var intervalOption = document.getElementById("interval");
var option = convertToNormalOptions(ganttSheet, "ganttRows");
syncOptionToPanel(option);
affectItemOption.addEventListener('change', function(e) {
var target = affectItemOption.value;
if (affectItemOption.value === "ganttRows") {
intervalPanel.classList.remove("hide");
} else {
intervalPanel.classList.add("hide");
}
var option = convertToNormalOptions(ganttSheet, target);
syncOptionToPanel(option);
});
setButton.addEventListener('click', function() {
var target = affectItemOption.value;
var lineType = Number(lineTypeOption.value);
var lineColor = lineColorOption.value;
var intervalLineType = Number(intervalLineTypeOption.value);
var intervalLineColor = intervalLineColorOption.value;
var interval = Number(intervalOption.value);
var option = convertToGanttGridlineOptions(lineType, lineColor, intervalLineType, intervalLineColor, interval);
if (option) {
ganttSheet.gridlines[target] = option;
}
});
function syncOptionToPanel(option) {
if (option.lineType) {
lineTypeOption.value = option.lineType;
}
if (option.lineColor) {
lineColorOption.value = option.lineColor;
}
if (option.intervalLineType) {
intervalLineTypeOption.value = option.intervalLineType;
}
if (option.intervalLineColor) {
intervalLineColorOption.value = option.intervalLineColor;
}
if (option.interval) {
intervalOption.value = option.interval;
}
}
}
function convertToGanttGridlineOptions(lineType, lineColor, intervalLineType, intervalLineColor, interval) {
var option = {};
if (typeof lineType === "number") {
option.lineType = lineType;
if (lineType !== 0) {
if (lineColor) {
option.lineColor = lineColor;
}
}
}
if (typeof intervalLineType === "number") {
option.intervalLineType = intervalLineType;
if (intervalLineType !== 0) {
if (lineType) {
option.intervalLineColor = intervalLineColor;
}
if (typeof interval === "number" && parseInt(interval) === parseFloat(interval)) {
option.interval = interval;
}
}
}
return option;
}
function convertToNormalOptions(ganttSheet, target) {
return ganttSheet.gridlines[target];
}
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-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$/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/splitView/splitView.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 selection-box">
<label for="affectItem">影响项</label>
<select id="affectItem">
<option value="ganttRows" selected="selected">ganttRows</option>
<option value="topTierColumn">topTierColumn</option>
<option value="middleTierColumn">middleTierColumn</option>
<option value="bottomTierColumn">bottomTierColumn</option>
<option value="currentDate">currentDate</option>
<option value="projectStart">projectStart</option>
<option value="projectFinish">projectFinish</option>
</select>
</div>
<div class="option-row selection-box">
<label for="lineType">线条类型</label>
<select id="lineType">
<option value="0">空</option>
<option value="1" selected="selected">细线</option>
<option value="3">虚线</option>
<option value="4">点线</option>
<option value="9">点划线</option>
</select>
</div>
<div class="option-row input-box">
<label for="lineColor">线条颜色</label>
<input type="text" id="lineColor" value="#abd08f" />
<div class="option-info valid">* 有效值:颜色字符串</div>
</div>
</div>
<div class="option-block toggle" id="interval-panel">
<div class="option-info toggle">* 当您选择“ganttRows”项时,切换“影响项”选项的面板。</div>
<div class="option-row option-title">
<div>设置间隔网格线样式:</div>
</div>
<div class="option-row selection-box">
<label for="intervalLineType">间隔线条类型</label>
<select id="intervalLineType">
<option value="0">空</option>
<option value="1">细线</option>
<option value="3">虚线</option>
<option value="4">点线</option>
<option value="9" selected="selected">点划线</option>
</select>
</div>
<div class="option-row input-box">
<label for="intervalLineColor">间隔线条颜色</label>
<input type="text" id="intervalLineColor" value="#9cc3e5" />
<div class="option-info valid">* 有效值:颜色字符串</div>
</div>
<div class="option-row input-box">
<label for="interval">间隔</label>
<input type="text" id="interval" value="2" />
<div class="option-info valid">* 有效值:整数</div>
</div>
</div>
<div class="option-row">
<input type="button" id="set-gridline" class="option-button" value="设置网格线" />
</div>
</div>
</div>
</html>
.options-container {
float: right;
width: 280px;
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-block.toggle {
border: 1px dotted #f7a711;
}
.option-row {
font-size: 14px;
box-sizing: border-box;
padding: 4px 0;
}
.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-info.toggle {
color: #f7a711;
}
.option-button {
width: 100%;
padding: 0;
line-height: 20px;
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 {
background: #fff;
border: 1px dashed #f7a711;
color: #f7a711;
padding: 2px 4px;
transition: 0.3s;
box-sizing: border-box;
cursor: pointer;
}
.option-checkbox.active {
color: #fff;
background: #f7a711;
box-shadow: 0 1px 4px 0 rgba(0,0,0,0.3);
border-radius: 4px;
}
.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] {
width: 100%;
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 {
color: #82bc00;
border-bottom: 2px solid #82bc00;
}
.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);
}