概述
这个 demo 只有一张柱形图,但右侧面板可以直接修改绘图区背景、透明度、边框和布局参数。
实现思路
初始化时创建示例数据和柱形图,并默认选中图表。
监听图表被选中事件,把当前绘图区配置读回面板。
点击应用按钮后,重新写入 plotArea 配置。
代码解析
这里直接对 plotArea 赋值,面板读取和回写的都是同一组属性。
运行效果
图表绘图区会显示自定义背景和边框。
面板中的数值修改后,绘图区位置和大小会同步变化。
选中其他图表时,面板会重新读取当前图表的绘图区状态。
window.onload = function () {
const spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 1 });
initSpread(spread);
initEvent(spread);
};
function initSpread(spread) {
const sheet = spread.getSheet(0);
sheet.suspendPaint();
sheet.setArray(0, 0, [
["", 'Chrome', 'Firefox', 'IE', 'Safari', 'Edge', 'Opera', '其他'],
["2014", 0.4966, 0.1801, 0.2455, 0.0470, 0.0, 0.0150, 0.0158],
["2015", 0.5689, 0.1560, 0.1652, 0.0529, 0.0158, 0.0220, 0.0192],
["2016", 0.6230, 0.1531, 0.1073, 0.0464, 0.0311, 0.0166, 0.0225],
["2017", 0.6360, 0.1304, 0.0834, 0.0589, 0.0443, 0.0223, 0.0246]
]);
const chart = sheet.charts.add('chart', GC.Spread.Sheets.Charts.ChartType.columnClustered, 30, 120, 480, 270, 'A1:D5', GC.Spread.Sheets.Charts.RowCol.columns);
chart.title({
text: '绘图区自定义'
});
chart.plotArea({
backColor: '#FF0000',
backColorTransparency: 0.6,
border: {
color: '#00FF00',
width: 3,
},
layout: {
x: 0.12,
y: 0.16,
width: 0.76,
height: 0.68
}
});
chart.isSelected(true);
readSetting(chart);
sheet.resumePaint();
};
function initEvent(spread) {
spread.bind(GC.Spread.Sheets.Events.FloatingElementSelected, function () {
var sheet = spread.getActiveSheet();
var chart = getActiveChart(sheet);
if (chart) {
readSetting(chart);
}
});
_getElementById("apply").addEventListener('click', function () {
applySetting(spread);
});
};
function readSetting(chart) {
const plotArea = chart.plotArea();
_getElementById("backColor").value = normalizeColor(plotArea.backColor, "#FF0000");
_getElementById("backColorTransparency").value = plotArea.backColorTransparency;
_getElementById("border-color").value = normalizeColor(plotArea.border.color, "#00FF00");
_getElementById("border-transparency").value = plotArea.border.transparency;
_getElementById("border-width").value = plotArea.border.width;
_getElementById("border-dashStyle").value = plotArea.border.dashStyle;
const layout = plotArea.layout || {};
_setInputValue("layoutX", layout.x);
_setInputValue("layoutY", layout.y);
_setInputValue("layoutWidth", layout.width);
_setInputValue("layoutHeight", layout.height);
}
function applySetting(spread) {
const sheet = spread.getActiveSheet();
const chart = getActiveChart(sheet);
if (!chart) {
return;
}
chart.plotArea({
backColor: _getText("backColor"),
backColorTransparency: _getFloat("backColorTransparency"),
border:{
color: _getText("border-color"),
width: _getValue("border-width"),
transparency: _getFloat("border-transparency"),
dashStyle: _getValue("border-dashStyle")
},
layout: {
x: _getFloat("layoutX"),
y: _getFloat("layoutY"),
width: _getFloat("layoutWidth"),
height: _getFloat("layoutHeight")
}
});
}
function getActiveChart(sheet) {
let activeChart = null;
sheet.charts.all().forEach(function (chart) {
if (chart.isSelected()) {
activeChart = chart;
}
});
return activeChart;
}
function _getElementById(id) {
return document.getElementById(id);
}
function _geBoolean(id) {
return _getElementById(id).checked;
}
function _getFloat(id) {
const value = parseFloat(_getElementById(id).value);
return isNaN(value) ? undefined : value;
}
function _getValue(id) {
return parseInt(_getElementById(id).value);
}
function _getText(id) {
return _getElementById(id).value;
}
function _setInputValue(id, value) {
_getElementById(id).value = value === undefined ? "" : value;
}
function normalizeColor(color, defaultColor) {
if (!color) {
return defaultColor;
}
if (typeof color !== "string") {
return defaultColor;
}
if (/^#[0-9a-f]{6}$/i.test(color)) {
return color;
}
if (/^#[0-9a-f]{3}$/i.test(color)) {
return "#" + color.slice(1).split("").map(function (value) {
return value + value;
}).join("");
}
const rgbMatch = color.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*[\d.]+)?\)$/i);
if (rgbMatch) {
return "#" + toHex(rgbMatch[1]) + toHex(rgbMatch[2]) + toHex(rgbMatch[3]);
}
const context = document.createElement("canvas").getContext("2d");
context.fillStyle = defaultColor;
context.fillStyle = color;
return /^#[0-9a-f]{6}$/i.test(context.fillStyle) ? context.fillStyle : defaultColor;
}
function toHex(value) {
const hex = parseInt(value, 10).toString(16);
return hex.length === 1 ? "0" + hex : hex;
}
<!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" />
<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-shapes/dist/gc.spread.sheets.shapes.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-charts/dist/gc.spread.sheets.charts.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 class="sample-tutorial">
<div id="ss" class="sample-spreadsheets"></div>
<div class="options-container">
<p>修改下面的属性后,点击“设置”按钮应用到图表。</p>
<div class="option-row" id="">
<label for="backColor">背景色</label>
<input type="color" id="backColor" value="#FF0000" />
</div>
<div class="option-row" id="">
<label for="backColorTransparency">背景透明度</label>
<input type="number" id="backColorTransparency" step="0.1" min="0" max="1" value="0.7" />
</div>
<hr>
<div class="option-row">
<label for="layoutX">绘图区 X(占图表比例)</label>
<input type="number" id="layoutX" step="0.01" min="0" max="1" />
</div>
<div class="option-row">
<label for="layoutY">绘图区 Y(占图表比例)</label>
<input type="number" id="layoutY" step="0.01" min="0" max="1" />
</div>
<div class="option-row">
<label for="layoutWidth">绘图区宽度(占图表比例)</label>
<input type="number" id="layoutWidth" step="0.01" min="0" max="1" />
</div>
<div class="option-row">
<label for="layoutHeight">绘图区高度(占图表比例)</label>
<input type="number" id="layoutHeight" step="0.01" min="0" max="1" />
</div>
<hr>
<div class="option-row" id="">
<label for="border-color">边框颜色:</label>
<input type="color" id="border-color" value="#00FF00" />
</div>
<div class="option-row" id="">
<label for="border-transparency">边框透明度:</label>
<input type="number" id="border-transparency" step="0.1" min="0" max="1" value="1" />
</div>
<div class="option-row" id="">
<label for="border-width">边框宽度:</label>
<input type="number" id="border-width" step="1" min="0" max="100" value="1" />
</div>
<div class="option-row" id="">
<label for="border-dashStyle">边框线型:</label>
<select id="border-dashStyle">
<option value="0" selected="selected">实线</option>
<option value="1">点线</option>
<option value="2">虚线</option>
<option value="3">长虚线</option>
<option value="4">点划线</option>
<option value="5">长点划线</option>
<option value="6">长双点划线</option>
<option value="7">系统虚线</option>
<option value="8">系统点线</option>
<option value="9">系统点划线</option>
<option value="10">系统双点划线</option>
</select>
</div>
<div class="option-row">
<input type="button" id="apply" value="设置" />
</div>
</div>
</div>
</div>
</body>
</html>
.sample-tutorial {
position: relative;
display: flex;
width: 100%;
height: 100%;
overflow: hidden;
background: #f5f7fb;
font-family: "Segoe UI", Arial, sans-serif;
}
.sample-spreadsheets {
flex: 1 1 auto;
min-width: 0;
height: 100%;
overflow: hidden;
}
.options-container {
flex: 0 0 300px;
width: 300px;
padding: 18px 16px;
height: 100%;
box-sizing: border-box;
background: #ffffff;
border-left: 1px solid #d9e1ee;
box-shadow: -8px 0 24px rgba(32, 45, 64, 0.08);
color: #27313f;
overflow: auto;
}
.options-container p {
margin: 0 0 18px;
padding: 12px 14px;
border: 1px solid #e1e8f2;
border-radius: 8px;
background: #f8fbff;
color: #526173;
font-size: 13px;
line-height: 1.5;
}
.option-row {
margin-bottom: 14px;
font-size: 14px;
}
.option-row:last-child {
margin-top: 20px;
margin-bottom: 0;
}
.option-row label {
display: block;
margin-bottom: 6px;
color: #344154;
font-size: 13px;
font-weight: 600;
}
.option-row input,
.option-row select {
box-sizing: border-box;
width: 100%;
height: 34px;
padding: 6px 10px;
border: 1px solid #cfd8e6;
border-radius: 6px;
background: #ffffff;
color: #1f2937;
font: inherit;
transition: border-color 0.15s ease, box-shadow 0.15s ease, background-color 0.15s ease;
}
.option-row input:hover,
.option-row select:hover {
border-color: #9db2ce;
}
.option-row input:focus,
.option-row select:focus {
border-color: #2f73d8;
box-shadow: 0 0 0 3px rgba(47, 115, 216, 0.16);
outline: none;
}
input[type="checkbox"] {
display: inline-block;
width: auto;
}
input[type="button"] {
height: 38px;
border: 0;
border-radius: 6px;
background: #2f73d8;
color: #ffffff;
font-weight: 600;
cursor: pointer;
box-shadow: 0 8px 18px rgba(47, 115, 216, 0.24);
}
input[type="button"]:hover {
background: #235fb7;
}
input[type="button"]:active {
background: #1d519d;
box-shadow: none;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: 0;
}