坐标轴自定义

本示例演示如何在 SpreadJS 图表中自定义坐标轴,支持对数刻度、次要坐标轴、样式设置及方向反转等功能。适用于处理数据跨度大、需要双轴对比或特殊展示需求的各种业务场景。

概述 本 Demo 展示了如何自定义图表坐标轴的各种属性,包括对数轴、次要坐标轴、坐标轴样式、坐标轴交叉点、刻度标签间距和坐标轴方向反转。通过三个不同的图表,演示了坐标轴自定义的多种应用场景。 实现思路 创建第一个柱状图,演示对数轴和次要坐标轴的设置,展示如何为值轴设置对数刻度,以及如何显示和自定义次要分类轴 创建第二个折线图,演示坐标轴交叉点和刻度标签间距的设置,控制坐标轴的交叉位置和标签密度 创建第三个柱状图,演示如何反转坐标轴方向,使数值从大到小显示 代码解析 设置对数轴 这段代码通过设置 scaling.logBase 属性为 2,将主要值轴设置为以 2 为底的对数轴。对数轴适用于数据跨度较大的场景,可以更清晰地展示数值的变化趋势。logBase 的值必须在 2-1000 之间。 显示次要坐标轴 通过设置 secondaryCategory.visible 为 true,可以显示次要分类轴。然后通过 style 和 title 属性自定义次要坐标轴的颜色和标题。 设置坐标轴交叉点 crossPoint 属性用于设置坐标轴的交叉位置。这里将主要分类轴的交叉点设置为 6,即主要值轴会在分类轴的第 6 个位置交叉。 设置刻度标签间距 tickLabelSpacing 属性控制刻度标签的显示间隔,设置为 2 表示每隔一个刻度显示一个标签,适用于标签过多导致重叠的场景。 反转坐标轴方向 通过设置 scaling.orientation 属性为 AxisOrientation.maxMin,可以将值轴的方向反转,使数值从大到小显示(默认为 minMax,即从小到大)。 运行效果 第一个图表(柱状图):左侧显示产品销售数据,主要值轴使用对数刻度(底数为 2),颜色为红色;次要分类轴可见,颜色为绿色 第二个图表(折线图):展示财务收入数据,主要分类轴在中间位置交叉,刻度标签每隔一个显示;折线使用平滑曲线 第三个图表(柱状图):展示产品销售数据,值轴方向反转,数值从上到下依次增大 所有图表都设置了自定义的标题和坐标轴样式,视觉效果清晰直观 API 参考 chart.axes() 方法 坐标轴对象包含四个坐标轴: primaryCategory:主要分类轴 primaryValue:主要值轴 secondaryCategory:次要分类轴 secondaryValue:次要值轴 坐标轴主要属性 style:坐标轴样式,包含 color、fontFamily、fontSize 等 title:坐标轴标题,包含 text、color、fontSize 等 scaling:缩放属性,包含 logBase(对数底数)和 orientation(方向) crossPoint:坐标轴交叉点,可以是数字或 AxisCrossPoint 枚举值 tickLabelSpacing:刻度标签间距 visible:是否显示坐标轴
var spread; window.onload = function () { spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 2 }); var sheet = spread.getActiveSheet(); sheet.suspendPaint(); sheet.setValue(0, 1, "Apple"); sheet.setValue(0, 2, "Banana"); sheet.setValue(0, 3, "Pear"); sheet.setValue(1, 0, "People1"); sheet.setValue(2, 0, "People2"); sheet.setValue(3, 0, "People3"); for (var r = 1; r <= 3; r++) { for (var c = 1; c <= 3; c++) { sheet.setValue(r, c, parseInt(Math.random() * 100)); } } var dataArray = [ ["Month", 'Fund Income', 'Stock Income', 'Bank Interest Income'], ["Jan", 100, 2, 9], ["Feb", -96, 15, 10], ["Mar", 53, 88, 8], ["Apr", -15, 150, 1], ["May", 77, -52, 20], ["Jun", 20, 66, 6], ["Jul", 60, 2, 9], ["Aug", -26, 15, 30], ["Sep ", 23, 78, 8], ["Oct", -15, 100, 21], ["Nov", 57, -52, 3], ["Dec", 30, 55, 16], ]; sheet.setArray(0, 6, dataArray); sheet.getRange(1, 7, 12, 3).formatter("$#,##0.00"); //add chart var Charts = GC.Spread.Sheets.Charts; var columnChart = sheet.charts.add('columnChart', Charts.ChartType.columnClustered, 10, 100, 400, 350); var lineChart = sheet.charts.add('lineChart', GC.Spread.Sheets.Charts.ChartType.line, 420, 100, 400, 350); var orientationChart = sheet.charts.add('orientationChart', Charts.ChartType.columnClustered, 830, 100, 400, 350,'A1:D4'); var series = columnChart.series(); series.add({ chartType: Charts.ChartType.columnClustered, axisGroup: Charts.AxisGroup.primary, name: "Sheet1!$A$2", xValues: "Sheet1!$B$1:$D$1", yValues: "Sheet1!$B$2:$D$2" }); series.add({ chartType: Charts.ChartType.columnClustered, axisGroup: Charts.AxisGroup.primary, name: "Sheet1!$A$3", xValues: "Sheet1!$B$1:$D$1", yValues: "Sheet1!$B$3:$D$3" }); series.add({ chartType: Charts.ChartType.lineMarkers, axisGroup: Charts.AxisGroup.secondary, name: "Sheet1!$A$4", xValues: "Sheet1!$B$1:$D$1", yValues: "Sheet1!$B$4:$D$4" }); var axes = columnChart.axes(); axes.primaryCategory.style.color = 'orange'; axes.primaryCategory.title.color = 'orange'; axes.primaryCategory.title.text = 'Primary Category Axis'; axes.primaryValue.style.color = 'red'; axes.primaryValue.title.color = 'red'; axes.primaryValue.title.text = 'Primary Value Logarithmic Axis'; axes.primaryValue.title.fontSize = 16; axes.primaryValue.scaling = { logBase: 2 }; axes.secondaryCategory.visible = true; axes.secondaryCategory.style.color = 'green'; axes.secondaryCategory.title.color = 'green'; axes.secondaryCategory.title.text = 'Secondary Category Axis'; axes.secondaryCategory.title.fontSize = 16; axes.secondaryValue.style.color = 'blue'; axes.secondaryValue.title.color = 'blue'; axes.secondaryValue.format = 'General'; axes.secondaryValue.title.text = 'Secondary Value Axis'; var columnChartTitle = columnChart.title(); columnChartTitle.text = "Product Sales"; columnChart.title(columnChartTitle); columnChart.axes(axes); series = lineChart.series(); series.add({ chartType: Charts.ChartType.line, axisGroup: Charts.AxisGroup.primary, name: "Sheet1!$H$1", xValues: "Sheet1!$G$2:$G$13", yValues: "Sheet1!$H$2:$H$13" }); series.add({ chartType: Charts.ChartType.columnClustered, axisGroup: Charts.AxisGroup.secondary, name: "Sheet1!$I$1", xValues: "Sheet1!$G$2:$G$13", yValues: "Sheet1!$I$2:$I$13" }); series.add({ chartType: Charts.ChartType.columnClustered, axisGroup: Charts.AxisGroup.secondary, name: "Sheet1!$J$1", xValues: "Sheet1!$G$2:$G$13", yValues: "Sheet1!$J$2:$J$13" }); axes = lineChart.axes(); axes.primaryCategory.crossPoint = 6; axes.primaryCategory.tickLabelSpacing = 2; axes.secondaryValue.visible = false; lineChart.axes(axes); var series1 = lineChart.series().get(0); series1.smooth = true; lineChart.series().set(0, series1); var lineChartTitle = lineChart.title(); lineChartTitle.text = "First Half Financial Income"; lineChart.title(lineChartTitle); var axes2 = orientationChart.axes(); var orientationChartTitle = orientationChart.title(); orientationChartTitle.text = "Product Sales(reverse order)"; orientationChart.title(orientationChartTitle); axes2.primaryValue.scaling = { orientation: GC.Spread.Sheets.Charts.AxisOrientation.maxMin }; orientationChart.axes(axes2); 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-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> </div> </body> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: 100% ; height: 100%; overflow: hidden; float: left; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }