使用 JavaScript 在演示文稿中管理图表数据系列

概述

图表可以将绘制的数值存储在工作簿单元格中,或直接以数值字面量的形式存储。一个ChartSeries表示一组相关的数值,每个ChartDataPoint代表该系列中的一个点。ChartCategory对象提供系列共享的标签或分组值。在使用默认数据创建的图表中,系列名称、类别和点值使用来自图表工作簿的ChartDataCell对象。

对于典型的类别图,默认工作簿使用第 0 行存放系列名称,第 0 列存放类别名称,其余单元格存放系列数值。传递给ChartDataWorkbook.getCell的工作表、行和列索引都是从零开始的。这种布局在创建带有默认数据的图表时很有用,但不要假设每个现有图表都使用它。对于已加载的演示文稿,请在更改工作簿值之前检查系列、类别和数据点引用的单元格。

图表设置有三种不同的作用域:

当未显式设置点或系列填充时,图表样式和主题决定自动外观。当系列和点的格式同时存在时,点的格式优先于该点的系列格式。

chart-series-powerpoint

对于簇状柱形图,请查看更改 3D 系列类型或条形形状如何影响第一系列。

使用数值字面量设置系列值

当系列值来源于应用程序数据或计算且不需要工作表单元格引用时,请使用数值字面量。调用setDataSourceTypeForValues并传入DataSourceType.DoubleLiterals后,再使用setAsLiteralDouble为每个点的getValue结果赋值。

集合设置控制该系列中所有点值的源类型。每个点值的getDataSourceType方法会报告选定的类型;通过集合设置可以更改它。使用Worksheet时,调用getAsCell访问引用的单元格。使用DoubleLiterals时,调用getAsLiteralDouble或setAsLiteralDouble直接读取或写入数字。

下面的示例创建一个默认系列和类别的柱形图,然后为其第一系列的现有点分配计算得到的 10、20 等值。它会保存演示文稿、重新打开并打印存储的值。

const aspose = { slides: require("aspose.slides.via.java") };

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 20, 20, 500, 300);
    const series = chart.getChartData().getSeries().get_Item(0);

    series.getDataPoints().setDataSourceTypeForValues(aspose.slides.DataSourceType.DoubleLiterals);
    for (let i = 0; i < series.getDataPoints().size(); i++) {
        series.getDataPoints().get_Item(i).getValue().setAsLiteralDouble((i + 1) * 10);
    }

    presentation.save("numeric_literals.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

const reopenedPresentation = new aspose.slides.Presentation("numeric_literals.pptx");
try {
    const reopenedSlide = reopenedPresentation.getSlides().get_Item(0);
    const reopenedChart = reopenedSlide.getShapes().get_Item(0);
    const reopenedSeries = reopenedChart.getChartData().getSeries().get_Item(0);
    for (let i = 0; i < reopenedSeries.getDataPoints().size(); i++) {
        const point = reopenedSeries.getDataPoints().get_Item(i);
        console.log(point.getValue().getAsLiteralDouble());
    }
} finally {
    reopenedPresentation.dispose();
}

点值的源类型与ChartData.getDataSourceType分开,后者标识图表的内部或外部工作簿配置。使用数值字面量本身并不会移除嵌入的工作簿。在本例中,工作簿仍然用于类别标签、系列名称以及其他系列。请参阅Chart Workbook了解工作簿管理。

对于散点图和气泡图,使用setDataSourceTypeForXValues和setDataSourceTypeForYValues分别选择getXValue和getYValue的来源。气泡图还使用setDataSourceTypeForBubbleSizes为getBubbleSize指定来源。在为相应值的setAsLiteralDouble方法赋数之前,请将每个适用的来源设置为DoubleLiterals。

设置图表系列的重叠度

ChartSeries.getOverlap报告 2D 图表中条形或柱形的重叠程度,范围为 -100 到 100%。它是父系列组设置的只读投影。使用ChartSeriesGroup.setOverlap可更新该组中每个兼容系列的重叠度。此选项适用于显示分组条形或柱形的图表类型;对组合图中不相关的系列组没有影响。

下面的示例为包含第一系列的组设置重叠度:

const aspose = {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const firstSlideIndex = 0;
const firstSeriesIndex = 0;
const overlapPercent = java.newByte(30);

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(firstSlideIndex);

    // 新建的图表包含示例系列、类别和数值。
    const chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 20, 20, 500, 200);

    const series = chart.getChartData().getSeries().get_Item(firstSeriesIndex);
    series.getParentSeriesGroup().setOverlap(overlapPercent);

    presentation.save("series_overlap.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

结果:

The series overlap

更改系列填充颜色

使用ChartSeries.getFormat为整个系列设置默认填充。如果某个点已经具有显式填充,其ChartDataPoint.getFormat会覆盖该点的系列填充。

下面的示例为第一系列应用纯蓝色填充:

const aspose = {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const firstSlideIndex = 0;
const firstSeriesIndex = 0;
const solidFillType = java.newByte(aspose.slides.FillType.Solid);
const blueColor = java.getStaticFieldValue("java.awt.Color", "BLUE");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(firstSlideIndex);

    const chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 20, 20, 500, 200);

    const series = chart.getChartData().getSeries().get_Item(firstSeriesIndex);
    series.getFormat().getFill().setFillType(solidFillType);
    series.getFormat().getFill().getSolidFillColor().setColor(blueColor);

    presentation.save("series_color.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

结果:

The color of the series

更改系列名称

系列名称存储在图表数据工作簿中,通常显示在图例中。在为簇状柱形图创建的默认工作簿中,单元格 B1(第 0 行,第 1 列)包含第一系列的名称。下面示例中的命名常量明确了该结构:

const aspose = {};
aspose.slides = require("aspose.slides.via.java");

const firstSlideIndex = 0;
const worksheetIndex = 0;
const seriesNameRowIndex = 0;
const firstSeriesColumnIndex = 1;

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(firstSlideIndex);

    const chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 20, 20, 500, 200);

    const workbook = chart.getChartData().getChartDataWorkbook();
    const seriesNameCell = workbook.getCell(worksheetIndex, seriesNameRowIndex, firstSeriesColumnIndex);
    seriesNameCell.setValue("Revenue");

    presentation.save("series_name.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

您也可以直接更新由ChartSeries.getName引用的单元格。此方式避免了对现有图表中行列位置的假设:

const aspose = {};
aspose.slides = require("aspose.slides.via.java");

const firstSlideIndex = 0;
const firstSeriesIndex = 0;
const firstNameCellIndex = 0;

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(firstSlideIndex);

    const chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 20, 20, 500, 200);

    const series = chart.getChartData().getSeries().get_Item(firstSeriesIndex);
    const seriesNameCell = series.getName().getAsCells().get_Item(firstNameCellIndex);
    seriesNameCell.setValue("Revenue");

    presentation.save("series_name.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

结果:

The series name

使用多个单元格创建带名称的系列

当产品名称和报告期间分别存放在不同工作簿单元格中时,复合系列名称非常有用。例如,您可以将 B1 中的 Product A 与 C1 中的 2026 合并为单个系列名称,同时保持两部分仍链接到各自的源单元格。

使用ChartDataWorkbook.getCellCollection获取名称范围,然后将该集合传递给ChartSeriesCollection.add。skipHiddenCells 参数决定是否排除隐藏单元格:true 排除,false 包含。本示例使用 false 包含名称范围中的所有单元格。

下面的示例创建一个包含一个系列和两个数据点的演示文稿。单元格 B1:C1 仅提供系列名称;A2:A3 提供类别标签;B2:B3 提供数值。

const aspose = {};
aspose.slides = require("aspose.slides.via.java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 50, 50, 620, 180);

    chart.getChartData().getSeries().clear();
    chart.getChartData().getCategories().clear();
    chart.setLegend(true);

    const workbook = chart.getChartData().getChartDataWorkbook();
    workbook.clear(0);

    // 这两个单元格提供系列名称。
    workbook.getCell(0, 0, 1, "Product A");
    workbook.getCell(0, 0, 2, "2026");
    const nameCells = workbook.getCellCollection("Sheet1!$B$1:$C$1", false);
    const series = chart.getChartData().getSeries().add(nameCells, aspose.slides.ChartType.ClusteredColumn);

    // 分离的单元格提供类别和数值数据点。
    const northCategory = workbook.getCell(0, 1, 0, "North");
    const southCategory = workbook.getCell(0, 2, 0, "South");
    chart.getChartData().getCategories().add(northCategory);
    chart.getChartData().getCategories().add(southCategory);
    const northValue = workbook.getCell(0, 1, 1, 120);
    const southValue = workbook.getCell(0, 2, 1, 150);
    series.getDataPoints().addDataPointForBarSeries(northValue);
    series.getDataPoints().addDataPointForBarSeries(southValue);

    presentation.save("composite_series_name.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

生成的系列名称为 Product A 2026,两个单元格值之间有一个空格。图例将其显示为一个条目。下图展示了结果:

Column chart with North and South values and the composite series name Product A 2026 in the legend

获取自动系列填充颜色

ChartSeries.getAutomaticSeriesColor返回根据系列索引和图表样式计算出的颜色。当系列填充未显式定义时会使用此颜色。调用该方法只读取计算得到的颜色,不会分配新的填充。

下面的示例打印每个默认系列的自动颜色:

const aspose = {};
aspose.slides = require("aspose.slides.via.java");

const firstSlideIndex = 0;

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(firstSlideIndex);

    const chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 20, 20, 500, 200);

    const seriesCount = chart.getChartData().getSeries().size();
    for (let seriesIndex = 0; seriesIndex < seriesCount; seriesIndex++) {
        const series = chart.getChartData().getSeries().get_Item(seriesIndex);
        const automaticColor = series.getAutomaticSeriesColor();
        const automaticColorText = automaticColor.toString();
        console.log("Series " + seriesIndex + ": " + automaticColorText);
    }
} finally {
    presentation.dispose();
}

默认图表样式的示例输出:

Series 0: java.awt.Color[r=79,g=129,b=189]
Series 1: java.awt.Color[r=192,g=80,b=77]
Series 2: java.awt.Color[r=155,g=187,b=89]

具体颜色取决于图表样式和主题。

为图表系列设置负值反转填充颜色

对于条形、柱形和气泡系列,使用ChartSeries.setInvertIfNegative可以在负值时显示不同的填充。先将常规系列填充设为实色,启用反转,然后通过ChartSeries.getInvertedSolidFillColor指定负值颜色。负数在工作簿中保持不变,仅改变其显示颜色。

下面的示例用一个系列替代默认图表数据。工作表第 0 行存放系列名称,第 0 列存放类别名称,第 1 列存放数值:

const aspose = {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const firstSlideIndex = 0;
const worksheetIndex = 0;
const headerRowIndex = 0;
const categoryColumnIndex = 0;
const firstSeriesColumnIndex = 1;
const firstDataRowIndex = 1;
const solidFillType = java.newByte(aspose.slides.FillType.Solid);
const redColor = java.getStaticFieldValue("java.awt.Color", "RED");

const categoryNames = ["Category 1", "Category 2", "Category 3"];
const seriesValues = [-20, 50, -30];

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(firstSlideIndex);

    const chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 20, 20, 500, 200);
    const chartData = chart.getChartData();
    const workbook = chartData.getChartDataWorkbook();

    chartData.getSeries().clear();
    chartData.getCategories().clear();

    const seriesNameCell = workbook.getCell(worksheetIndex, headerRowIndex, firstSeriesColumnIndex, "Series 1");
    const chartType = chart.getType();
    const series = chartData.getSeries().add(seriesNameCell, chartType);

    for (let categoryIndex = 0; categoryIndex < categoryNames.length; categoryIndex++) {
        const dataRowIndex = firstDataRowIndex + categoryIndex;
        const categoryName = categoryNames[categoryIndex];
        const seriesValue = seriesValues[categoryIndex];

        const categoryCell = workbook.getCell(worksheetIndex, dataRowIndex, categoryColumnIndex, categoryName);
        chartData.getCategories().add(categoryCell);

        const valueCell = workbook.getCell(worksheetIndex, dataRowIndex, firstSeriesColumnIndex, seriesValue);
        series.getDataPoints().addDataPointForBarSeries(valueCell);
    }

    const automaticSeriesColor = series.getAutomaticSeriesColor();
    series.getFormat().getFill().setFillType(solidFillType);
    series.getFormat().getFill().getSolidFillColor().setColor(automaticSeriesColor);
    series.setInvertIfNegative(true);
    series.getInvertedSolidFillColor().setColor(redColor);

    presentation.save("inverted_solid_fill_color.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

结果:

The inverted solid fill color

您也可以通过ChartDataPoint.setInvertIfNegative为单个点启用反转。以下示例在系列整体关闭反转的情况下,仅为选定点启用,并为该点分配负值以便看到效果:

const aspose = {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const firstSlideIndex = 0;
const firstSeriesIndex = 0;
const targetDataPointIndex = 2;
const negativeValue = -30;
const solidFillType = java.newByte(aspose.slides.FillType.Solid);
const redColor = java.getStaticFieldValue("java.awt.Color", "RED");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(firstSlideIndex);

    const chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 20, 20, 500, 200);

    const series = chart.getChartData().getSeries().get_Item(firstSeriesIndex);
    const automaticSeriesColor = series.getAutomaticSeriesColor();
    series.getFormat().getFill().setFillType(solidFillType);
    series.getFormat().getFill().getSolidFillColor().setColor(automaticSeriesColor);
    series.getInvertedSolidFillColor().setColor(redColor);
    series.setInvertIfNegative(false);

    const dataPoint = series.getDataPoints().get_Item(targetDataPointIndex);
    dataPoint.getValue().getAsCell().setValue(negativeValue);
    dataPoint.setInvertIfNegative(true);

    presentation.save("data_point_invert_color_if_negative.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

清除特定数据点的值

若要使某一点为空而不删除其他点,请将其对应的工作簿单元格设为 null。对于柱形图,可通过ChartDataPoint.getValue获取绘制的数值。数据点仍保持在相同的类别位置,但图表会根据空值设置将其视为空白。

下面的示例仅清除第一系列的第二个点:

const aspose = {};
aspose.slides = require("aspose.slides.via.java");

const firstSlideIndex = 0;
const firstSeriesIndex = 0;
const targetDataPointIndex = 1;

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(firstSlideIndex);

    const chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 20, 20, 500, 200);

    const series = chart.getChartData().getSeries().get_Item(firstSeriesIndex);
    const dataPoint = series.getDataPoints().get_Item(targetDataPointIndex);
    dataPoint.getValue().getAsCell().setValue(null);

    presentation.save("clear_data_point_value.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

散点图使用独立的 X、Y 单元格,气泡图还使用大小单元格。仅清除表示您想删除的数值的单元格即可。不要在只想保留其他点时调用ChartDataPointCollection.clear,因为该方法会删除该系列的所有数据点。

控制空单元格的显示方式

隐藏的单元格即使包含数值也属于另一种情况。若要包含或排除隐藏行列中的数据,请参阅Include Data from Hidden Rows and Columns。

空工作簿单元格表示缺失数据;包含 0 的单元格表示已知数值。调用ChartDataCell.setValue并传入 null 可将单元格设为空。数值零始终保持为零,不受空单元格设置影响。

使用Chart.setDisplayBlanksAs选择图表如何显示空单元格。此设置适用于整个图表,会改变空白的绘制方式,而不会将空工作簿单元格填充为零或插值。

下面的自包含示例创建一个包含一个系列的折线图,清除第 3 天的值,并使用每种模式分别保存同一图表。无需输入文件。ChartDataWorkbook使用工作表 0,第 0 列存放类别标签,第 1 列存放数值;第 0 行保存系列名称。最终数据为 10, 20, empty, 30, 40。

const aspose = {};
aspose.slides = require("aspose.slides.via.java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const chart = slide.getShapes().addChart(aspose.slides.ChartType.LineWithMarkers, 40, 40, 640, 400);
    const chartData = chart.getChartData();
    const workbook = chartData.getChartDataWorkbook();

    chartData.getSeries().clear();
    chartData.getCategories().clear();

    const seriesNameCell = workbook.getCell(0, 0, 1, "Measurements");
    const series = chartData.getSeries().add(seriesNameCell, chart.getType());
    const values = [10, 20, 25, 30, 40];

    for (let i = 0; i < values.length; i++) {
        const categoryCell = workbook.getCell(0, i + 1, 0, "Day " + (i + 1));
        chartData.getCategories().add(categoryCell);
        const valueCell = workbook.getCell(0, i + 1, 1, values[i]);
        series.getDataPoints().addDataPointForLineSeries(valueCell);
    }

    // 将第 3 天真正留空,同时保留其类别和数据点。
    workbook.getCell(0, 3, 1).setValue(null);

    const modes = [aspose.slides.DisplayBlanksAsType.Gap, aspose.slides.DisplayBlanksAsType.Zero, aspose.slides.DisplayBlanksAsType.Span];
    const modeNames = ["Gap", "Zero", "Span"];
    for (let i = 0; i < modes.length; i++) {
        chart.setDisplayBlanksAs(modes[i]);
        presentation.save("empty_cells_" + modeNames[i] + ".pptx", aspose.slides.SaveFormat.Pptx);
    }
} finally {
    presentation.dispose();
}

每个输出文件在保存前都设置了相应模式:empty_cells_Gap.pptx、empty_cells_Zero.pptx 与 empty_cells_Span.pptx。若只需一种版本,只需在保存演示文稿前设置所需模式即可,无需遍历所有模式。

下面的比较展示了三个文件中的相同数据。第 3 天在工作簿中始终为空:

Line charts with identical data: Gap breaks the line at Day 3, Zero drops the line to zero, and Span connects Day 2 to Day 4.

可见效果取决于图表类型。折线图可以直观比较三种模式。条形和柱形图没有连线可跨越缺失的类别,因此 Span 无法产生上图所示的连接段;缺失的柱形和零高的柱形在外观上也可能相似。同样,仅使用标记的散点图也没有连线。不要期望每种图表类型都能得到三种不同的结果;请根据实际使用的图表类型检查输出。

设置系列间隙宽度

间隙宽度是相邻条形或柱形簇之间的空间,表示为条形或柱形宽度的百分比。与重叠度类似,它属于父系列组,而不是单个系列。对该组调用一次ChartSeriesGroup.setGapWidth即可。数值越大,簇之间的空间越宽;数值越小,簇越密集。

下面的示例修改间隙宽度并仅保存最终的演示文稿:

const aspose = {};
aspose.slides = require("aspose.slides.via.java");

const firstSlideIndex = 0;
const firstSeriesIndex = 0;
const gapWidthPercent = 30;

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(firstSlideIndex);

    const chart = slide.getShapes().addChart(aspose.slides.ChartType.StackedColumn, 20, 20, 500, 200);

    const series = chart.getChartData().getSeries().get_Item(firstSeriesIndex);
    series.getParentSeriesGroup().setGapWidth(gapWidthPercent);

    presentation.save("gap_width_30.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

结果:

The gap width

显示或隐藏系列线

系列线用于在图表中视觉上连接相关部分。使用ChartSeriesGroup.setSeriesLines可以在受支持的堆积条形图和组合饼图(Pie of Pie)中显示或隐藏它们。在二维堆积条形图和柱形图中,它们连接跨类别的对应堆叠边界;在 Pie of Pie 和 Bar of Pie 图表中,它们连接主图与次图。该设置并非通用于其他图表类型的连接器。

通过ChartSeries.getParentSeriesGroup访问该组。设置会应用于该组内的每个系列。ChartSeries.hasSeriesLines为只读属性,反映父组的设置;请调用组的 setter 进行更新。

下面的示例创建一个带默认数据的PieOfPie图表,分别保存启用和禁用系列线的两个演示文稿,以供比较。

const aspose = { slides: require("aspose.slides.via.java") };

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const chart = slide.getShapes().addChart(aspose.slides.ChartType.PieOfPie, 50, 50, 600, 400);
    const series = chart.getChartData().getSeries().get_Item(0);
    const seriesGroup = series.getParentSeriesGroup();

    seriesGroup.setSeriesLines(true);
    presentation.save("series_lines_enabled.pptx", aspose.slides.SaveFormat.Pptx);

    seriesGroup.setSeriesLines(false);
    presentation.save("series_lines_disabled.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}
启用系列线 禁用系列线
Pie of Pie chart with series lines enabled Pie of Pie chart with series lines disabled
两条线将主饼图连接到次饼图。 两个饼图均可见,但连接线被隐藏。

系列线不同于数据标签的引线(将标签连接到数据点)、网格线(标记坐标轴间隔)以及折线图中的线(表示数据值)。此设置不控制上述元素。

常见问题

哪些图表类型支持数据系列?

所有由ChartType枚举表示的图表类型都使用图表数据,但它们的系列并非全部拥有相同的值结构或设置。例如,类别图使用类别和数值,散点图使用 X 与 Y 值,气泡图还附加气泡大小。请使用与系列类型相匹配的数据点创建方法。重叠度和间隙宽度等选项仅适用于兼容的条形或柱形组。

什么是图表系列组?

ChartSeriesGroup包含共享组级绘图设置的兼容系列。组合图可以包含多个组,因此通过某个系列访问的组的更改不一定会影响图表中的所有系列。

新创建的图表会包含默认数据吗?

会。默认情况下,ShapeCollection.addChart会创建示例系列、类别和数值。您可以编辑这些单元格,或在添加完全自定义的数据集之前先清除系列和类别集合。也有重载可以在不生成默认数据的情况下创建图表。

图表对象如何关联到工作簿单元格?

系列名称、类别标签和数据点数值都引用ChartDataWorkbook中的单元格。更改被引用的单元格会更新相应的图表元素。构建自定义数据时,请保持类别行和系列值行对齐,以确保每个点绘制在预期的类别下。

如何只清除一个点而不是整个系列?

将相关的值单元格设为 null,即可保留点的类别位置而使其成为空点。仅在确实想删除该系列所有点时才使用ChartDataPointCollection.clear,因为该方法会从集合中移除所有数据点。

空点会如何显示?

结果取决于图表类型以及通过Chart.setDisplayBlanksAs配置的值。支持的图表可以将空白显示为间隙、零值或连接相邻点。请选择符合您演示文稿中缺失数据含义的设置。完整示例和可视化比较见Control the Display of Empty Cells。

负值的格式如何设置?

对于受支持的条形、柱形和气泡系列,调用ChartSeries.setInvertIfNegative并设置ChartSeries.getInvertedSolidFillColor返回的颜色。您也可以使用ChartDataPoint.setInvertIfNegative为单个点覆盖此行为。这些方法影响格式,而非存储的数值。

当系列和点都设置格式时,哪个生效?

显式的数据点格式在该点上优先。其他点继续使用显式的系列格式,或在系列格式未定义时使用自动的图表样式和主题。组设置(如重叠度和间隙宽度)控制布局,不会覆盖点级别的格式。

图表的系列数量有限制吗?

Aspose.Slides 并未设置单独的固定系列计数上限。实际限制取决于演示文稿文件的约束、可用内存、渲染时间以及图表的可读性。

当列间距过近或过远时该怎么办?

在相应的父系列组上调用ChartSeriesGroup.setGapWidth。增大数值会扩大簇之间的间距,减小数值则使簇更紧凑。