Chart Series
A series is a row or column of numbers plotted in a chart.
Set Chart Series Overlap
With the ChartSeries.getOverlap method, you can specify how much bars and columns should overlap on a 2D chart (range: -100 to 100). This property applies to all series of the parent series group: this is a projection of the appropriate group property. Therefore, this property is read-only.
Use the ParentSeriesGroup.getOverlap
read/write property to set your preferred value for Overlap
.
- Create an instance of the Presentation class.
- Add a clustered column chart on a slide.
- Access the first chart series.
- Access the chart series'
ParentSeriesGroup
and set your preferred overlap value for the series. - Write the modified presentation to a PPTX file.
This JavaScript code shows you how to set the overlap for a chart series:
var pres = new aspose.slides.Presentation();
try {
// Adds chart
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 50, 50, 600, 400, true);
var series = chart.getChartData().getSeries();
if (series.get_Item(0).getOverlap() == 0) {
// Sets series overlap
series.get_Item(0).getParentSeriesGroup().setOverlap(-30);
}
// Writes the presentation file to disk
pres.save("SetChartSeriesOverlap_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Change Series Color
Aspose.Slides for Node.js via Java allows you to change a series' color this way:
- Create an instance of the Presentation class.
- Add chart on the slide.
- Access the series whose color you want to change.
- Set your preferred fill type and fill color.
- Save the modified presentation.
This JavaScript code shows you how to change a series' color:
var pres = new aspose.slides.Presentation("test.pptx");
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Pie, 50, 50, 600, 400);
var point = chart.getChartData().getSeries().get_Item(0).getDataPoints().get_Item(1);
point.setExplosion(30);
point.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
point.getFormat().getFill().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLUE"));
pres.save("output.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Change Series Category’s Color
Aspose.Slides for Node.js via Java allows you to change a series category’s color this way:
- Create an instance of the Presentation class.
- Add chart on the slide.
- Access the series category whose color you want to change.
- Set your preferred fill type and fill color.
- Save the modified presentation.
This code in JavaScript shows you how to change a series category’s color:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 50, 50, 600, 400);
var point = chart.getChartData().getSeries().get_Item(0).getDataPoints().get_Item(0);
point.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
point.getFormat().getFill().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLUE"));
pres.save("output.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Change Series' Name
By default, the legend names for a chart are the contents of cells above each column or row of data.
In our example (sample image),
- the columns are Series 1, Series 2, and Series 3;
- the rows are Category 1, Category 2, Category 3, and Category 4.
Aspose.Slides for Node.js via Java allows you to update or change a series name in its chart data and legend.
This JavaScript code shows you how to change a series' name in its chart data ChartDataWorkbook
:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Column3D, 50, 50, 600, 400, true);
var seriesCell = chart.getChartData().getChartDataWorkbook().getCell(0, 0, 1);
seriesCell.setValue("New name");
pres.save("pres.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
This JavaScript code shows you how to change a series name in its legend through Series
:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Column3D, 50, 50, 600, 400, true);
var series = chart.getChartData().getSeries().get_Item(0);
var name = series.getName();
name.getAsCells().get_Item(0).setValue("New name");
} finally {
if (pres != null) {
pres.dispose();
}
}
Set Chart Series Fill Color
Aspose.Slides for Node.js via Java allows you to set the automatic fill color for chart series inside a plot area this way:
- Create an instance of the Presentation class.
- Obtain a slide’s reference by its index.
- Add a chart with default data based on your preferred type (in the example below, we used
ChartType.ClusteredColumn
). - Access the chart series and set the fill color to Automatic.
- Save the presentation to a PPTX file.
This JavaScript code shows you how to set the automatic fill color for a chart series:
var pres = new aspose.slides.Presentation();
try {
// Creates a clustered column chart
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 100, 50, 600, 400);
// Sets series fill format to automatic
for (var i = 0; i < chart.getChartData().getSeries().size(); i++) {
chart.getChartData().getSeries().get_Item(i).getAutomaticSeriesColor();
}
// Writes the presentation file to disk
pres.save("AutoFillSeries_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Set Chart Series Invert Fill Colors
Aspose.Slides allows you to set the invert fill color for chart series inside a plot area this way:
- Create an instance of the Presentation class.
- Obtain a slide’s reference by its index.
- Add a chart with default data based on your preferred type (in the example below, we used
ChartType.ClusteredColumn
). - Access the chart series and set the fill color to invert.
- Save the presentation to a PPTX file.
This JavaScript code demonstrates the operation:
var inverColor = java.getStaticFieldValue("java.awt.Color", "RED");
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 100, 100, 400, 300);
var workBook = chart.getChartData().getChartDataWorkbook();
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
// Adds new series and categories
chart.getChartData().getSeries().add(workBook.getCell(0, 0, 1, "Series 1"), chart.getType());
chart.getChartData().getCategories().add(workBook.getCell(0, 1, 0, "Category 1"));
chart.getChartData().getCategories().add(workBook.getCell(0, 2, 0, "Category 2"));
chart.getChartData().getCategories().add(workBook.getCell(0, 3, 0, "Category 3"));
// Takes the first chart series and populates its series data.
var series = chart.getChartData().getSeries().get_Item(0);
series.getDataPoints().addDataPointForBarSeries(workBook.getCell(0, 1, 1, -20));
series.getDataPoints().addDataPointForBarSeries(workBook.getCell(0, 2, 1, 50));
series.getDataPoints().addDataPointForBarSeries(workBook.getCell(0, 3, 1, -30));
var seriesColor = series.getAutomaticSeriesColor();
series.setInvertIfNegative(true);
series.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
series.getFormat().getFill().getSolidFillColor().setColor(seriesColor);
series.getInvertedSolidFillColor().setColor(inverColor);
pres.save("SetInvertFillColorChart_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Set Series to Invert When Value is Negative
Aspose.Slides allows you to set inverts through theChartDataPoint.setInvertIfNegative
method. When an invert is set using the properties, the data point inverts its colors when it gets a negative value.
This JavaScript code demonstrates the operation:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 50, 50, 600, 400, true);
var series = chart.getChartData().getSeries();
chart.getChartData().getSeries().clear();
var chartSeries = series.add(chart.getChartData().getChartDataWorkbook().getCell(0, "B1"), chart.getType());
chartSeries.getDataPoints().addDataPointForBarSeries(chart.getChartData().getChartDataWorkbook().getCell(0, "B2", -5));
chartSeries.getDataPoints().addDataPointForBarSeries(chart.getChartData().getChartDataWorkbook().getCell(0, "B3", 3));
chartSeries.getDataPoints().addDataPointForBarSeries(chart.getChartData().getChartDataWorkbook().getCell(0, "B4", -2));
chartSeries.getDataPoints().addDataPointForBarSeries(chart.getChartData().getChartDataWorkbook().getCell(0, "B5", 1));
chartSeries.setInvertIfNegative(false);
chartSeries.getDataPoints().get_Item(2).setInvertIfNegative(true);
pres.save("out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Clear Specific Data Points' Data
Aspose.Slides for Node.js via Java allows you to clear the DataPoints
data for a specific chart series this way:
- Create an instance of the Presentation class.
- Obtain the reference of a slide through its index.
- Obtain the reference of a chart through its index.
- Iterate through all the chart
DataPoints
and setXValue
andYValue
to null. - Clear all
DataPoints
for specific chart series. - Write the modified presentation to a PPTX file.
This JavaScript code demonstrates the operation:
var pres = new aspose.slides.Presentation("TestChart.pptx");
try {
var sl = pres.getSlides().get_Item(0);
var chart = sl.getShapes().get_Item(0);
for (let i = 0; i < chart.getChartData().getSeries().get_Item(0).getDataPoints().size(); i++) {
let dataPoint = chart.getChartData().getSeries().get_Item(0).getDataPoints().get_Item(i);
dataPoint.getXValue().getAsCell().setValue(null);
dataPoint.getYValue().getAsCell().setValue(null);
}
chart.getChartData().getSeries().get_Item(0).getDataPoints().clear();
pres.save("ClearSpecificChartSeriesDataPointsData.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Set Series Gap Width
Aspose.Slides for Node.js via Java allows you to set a series' Gap Width through the GapWidth
property this way:
- Create an instance of the Presentation class.
- Access first slide.
- Add chart with default data.
- Access any chart series.
- Set the
GapWidth
property. - Write the modified presentation to a PPTX file.
This code in JavaScript shows you how to set a series' Gap Width:
// Creates empty presentation
var pres = new aspose.slides.Presentation();
try {
// Accesses the presentation's first slide
var slide = pres.getSlides().get_Item(0);
// Adds a chart with default data
var chart = slide.getShapes().addChart(aspose.slides.ChartType.StackedColumn, 0, 0, 500, 500);
// Sets the index of the chart data sheet
var defaultWorksheetIndex = 0;
// Gets the chart data worksheet
var fact = chart.getChartData().getChartDataWorkbook();
// Adds series
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType());
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.getType());
// Adds Categories
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));
// Takes the second chart series
var series = chart.getChartData().getSeries().get_Item(1);
// Populates the series data
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 2, 30));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 10));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 2, 60));
// Sets GapWidth value
series.getParentSeriesGroup().setGapWidth(50);
// Saves presentation to disk
pres.save("GapWidth_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}