Sparklines in Aspose.Cells for Go via C++
Aspose.Cells supports creating sparklines inside worksheet cells. Sparklines are miniature charts that fit within a single cell, providing a quick visual representation of data trends. Aspose.Cells supports line, column, and win/loss sparklines, and each can be customized with respect to color, line weight, high/low points, and markers.
Introduction
Sparklines are tiny in-cell charts that are useful when you want to display a quick trend next to a row or column of data without taking up the space of a full chart. Excel supports three kinds of sparklines: line, column, and win/loss. Aspose.Cells mirrors this capability through the SparklineGroup and SparklineGroupCollection APIs found in the Aspose.Cells.Charts namespace.
In Aspose.Cells, every sparkline you add is created through worksheet.SparklineGroups.Add(...), which returns a SparklineGroup object. You can then use that object to set the sparkline type, the data range, the destination cell, and visual properties such as line color, line weight, markers, and high/low point indicators.
This article walks through each of the three sparkline types supported by Aspose.Cells — Line, Column, and Win/Loss — and shows how to add them, customize their colors, and save the resulting workbook.
Line Sparklines
A line sparkline draws a continuous line through the data points in a series, making it the most natural choice for showing trends over time. In Aspose.Cells, a line sparkline is created by passing SparklineType.Line to the SparklineGroups.Add method.
- Create a new
Workbookand access the first worksheet. - Populate a row of source data (for example, row 1, columns A through E) with the values you want to visualize.
- Build a
CellAreadescribing the destination cell where the sparkline will be drawn. - Call
worksheet.SparklineGroups.Add(SparklineType.Line, "A1:E1", false, dest). The third argument —false— tells Aspose.Cells that the data range is horizontal (a row), not vertical (a column). - Optionally customize the returned
SparklineGroup. For a line sparkline you can set the line color usinggroup.Line.Color(which expects aCellsColorfromAspose.Cells.Drawing), adjust the line weight, and toggle high/low point markers. - Save the workbook. The following example creates a workbook, writes the values 5, -3, 8, -2, 6 into cells A1 through E1, and adds a line sparkline in cell F1 that traces those values. It also customizes the line color to red and enables markers for the high and low points.
Column Sparklines
A column sparkline renders each data point as a vertical bar. This makes it well suited to data whose magnitude is meaningful — for example, monthly sales figures or counts. In Aspose.Cells, you create a column sparkline by passing SparklineType.Column to the SparklineGroups.Add method.
The procedure mirrors the line sparkline example:
- Create a new
Workbookand access the first worksheet. - Build a
CellAreadescribing the destination cell. - Call
worksheet.SparklineGroups.Add(SparklineType.Column, "A1:E1", false, dest). - Optionally customize the resulting
SparklineGroup— for example, by settinggroup.Typeto confirm the type, or by tweaking the bar color. - Save the workbook to a separate output file so it does not overwrite the line sparkline example. The example below writes the values 5, -3, 8, -2, 6 into A1:E1 and renders a column sparkline in F1. Negative values are drawn as bars going downward and positive values as bars going upward, which makes positive and negative contributions easy to spot at a glance.
Win/Loss Sparklines
A win/loss sparkline is a special variant of the column sparkline designed to show only two outcomes: a positive value is drawn as an “up” bar (a win) and a zero or negative value is drawn as a “down” bar (a loss). Win/loss sparklines are commonly used to visualize sequences of wins and losses, pass/fail results, or any binary outcome over time.
In Aspose.Cells, a win/loss sparkline is created by passing SparklineType.Stacked to the SparklineGroups.Add method. (Despite the name, SparklineType.Stacked is the enum value used to request the win/loss rendering.)
- Create a new
Workbookand access the first worksheet. - Populate the source range. Because win/loss sparklines treat every value as either a win or a loss, the magnitude of the value does not matter — only its sign does. Positive values become up bars and non-positive values become down bars.
- Build a
CellAreadescribing the destination cell. - Call
worksheet.SparklineGroups.Add(SparklineType.Stacked, "A1:E1", false, dest). - Optionally customize the returned
SparklineGroup, for example by setting accent colors for the win and loss bars. - Save the workbook under a distinct filename so all three examples can coexist on disk.
Combining All Three Sparkline Types
The combined example below creates a single workbook, populates row 1 with the values 5, -3, 8, -2, 6, and then adds three sparkline groups in cells F1, F2, and F3 — one of each type — so that the resulting file demonstrates all three sparkline styles at once.
Customizing Sparkline Appearance
Once a SparklineGroup has been created and added to worksheet.SparklineGroups, you can read or modify several of its visual properties before saving the workbook. The most commonly customized properties are:
group.Type— theSparklineType(Line, Column, or Stacked). It is set when the group is added, but you can read it back to confirm.group.Line.Color— the line color, expressed as aCellsColorcreated viaworkbook.CreateCellsColor(). This is the property to use for line sparkline stroke color.group.Line.Weight— the line weight in points. Higher values produce thicker lines.- High/Low point markers — flags that turn on small markers on the highest and lowest data points, useful for emphasizing extremes.
- First/Last/Negative point markers — flags that toggle markers on the first, last, and negative data points.
To change a color, always create a
CellsColorinstance and assign it to the relevant property. Do not assign a raw color value directly to sparkline color properties — they expect theCellsColortype fromAspose.Cells.Drawing. TheSparklineGroups.Addmethod itself returns a fully typedSparklineGroupobject, so you can chain property assignments on the return value or store it in a local variable and customize it before saving.