Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
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.
Workbook and access the first worksheet.CellArea describing the destination cell where the sparkline will be drawn.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).SparklineGroup. For a line sparkline you can set the line color using group.Line.Color (which expects a CellsColor from Aspose.Cells.Drawing), adjust the line weight, and toggle high/low point markers.using System;
using Aspose.Cells;
using Aspose.Cells.Charts;
using Aspose.Cells.Drawing;
namespace SparklineDemo
{
public class Program
{
public static void Main()
{
// Step 1: Create a Workbook and get the first worksheet
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;
// Step 2: Write sample values 5, -3, 8, -2, 6 into cells A1:E1
cells["A1"].PutValue(5);
cells["B1"].PutValue(-3);
cells["C1"].PutValue(8);
cells["D1"].PutValue(-2);
cells["E1"].PutValue(6);
// Step 3: Build a CellArea pointing to destination cell F1
CellArea dest = new CellArea();
dest.StartColumn = 5; // column F (0-indexed)
dest.EndColumn = 5;
dest.StartRow = 0; // row 1 (0-indexed)
dest.EndRow = 0;
// Step 4: Add a Line sparkline from A1:E1 into F1
// SparklineGroups.Add returns the index of the newly added group
int index = worksheet.SparklineGroups.Add(SparklineType.Line, "A1:E1", false, dest);
SparklineGroup group = worksheet.SparklineGroups[index];
// Step 5: Create a red CellsColor and assign it to the sparkline line color
CellsColor red = workbook.CreateCellsColor();
red.Color = System.Drawing.Color.Red;
group.SeriesColor = red;
// Step 6: Enable high-point and low-point markers
group.ShowHighPoint = true;
group.ShowLowPoint = true;
// Step 7: Save the workbook
workbook.Save("output_line.xlsx");
}
}
}
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:
Workbook and access the first worksheet.CellArea describing the destination cell.worksheet.SparklineGroups.Add(SparklineType.Column, "A1:E1", false, dest).SparklineGroup — for example, by setting group.Type to confirm the type, or by tweaking the bar color.using System;
using Aspose.Cells;
using Aspose.Cells.Charts;
using Aspose.Cells.Drawing;
namespace SparklineDemo
{
class Program
{
static void Main(string[] args)
{
// Step 1: Create a Workbook and get the first worksheet
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
// Step 2: Write sample values into A1:E1
int[] values = { 5, -3, 8, -2, 6 };
for (int i = 0; i < values.Length; i++)
{
worksheet.Cells[0, i].PutValue(values[i]);
}
// Step 3: Build a CellArea pointing to F1 (column index 5, row index 0)
CellArea dest = new CellArea();
dest.StartColumn = 5;
dest.EndColumn = 5;
dest.StartRow = 0;
dest.EndRow = 0;
// Step 4: Add a Column sparkline to the destination cell
int idx = worksheet.SparklineGroups.Add(
SparklineType.Column, "A1:E1", false, dest);
SparklineGroup group = worksheet.SparklineGroups[idx];
// Step 5: Confirm the sparkline type by reading group.Type
Console.WriteLine("Sparkline Type added: " + group.Type);
// Step 6: Save the workbook
workbook.Save("output_column.xlsx");
Console.WriteLine("Workbook saved as output_column.xlsx");
}
}
}
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.)
Workbook and access the first worksheet.CellArea describing the destination cell.worksheet.SparklineGroups.Add(SparklineType.Stacked, "A1:E1", false, dest).SparklineGroup, for example by setting accent colors for the win and loss bars.using System;
using Aspose.Cells;
using Aspose.Cells.Charts;
using Aspose.Cells.Drawing;
namespace SparklineDemo
{
class Program
{
static void Main(string[] args)
{
// Step 1: Create a Workbook and get the first worksheet
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Name = "WinLoss";
// Step 2: Populate sample data in row 1: A1=5, B1=-3, C1=8, D1=-2, E1=6
worksheet.Cells["A1"].PutValue(5);
worksheet.Cells["B1"].PutValue(-3);
worksheet.Cells["C1"].PutValue(8);
worksheet.Cells["D1"].PutValue(-2);
worksheet.Cells["E1"].PutValue(6);
// Step 3: Build a CellArea pointing to F1 (column 5, row 0)
CellArea dest = new CellArea();
dest.StartColumn = 5; // F
dest.EndColumn = 5;
dest.StartRow = 0; // row 1
dest.EndRow = 0;
// Step 4: Add a Win/Loss sparkline (SparklineType.Stacked)
int groupIndex = worksheet.SparklineGroups.Add(
SparklineType.Stacked,
"A1:E1",
false,
dest);
SparklineGroup group = worksheet.SparklineGroups[groupIndex];
// Step 5: Customize the sparkline group
// Enable high-point and low-point markers
group.ShowHighPoint = true;
group.ShowLowPoint = true;
group.ShowNegativePoints = true;
// Set the high-point color to green
CellsColor highColor = workbook.CreateCellsColor();
highColor.Color = System.Drawing.Color.Green;
group.HighPointColor = highColor;
// Set the low-point color to red
CellsColor lowColor = workbook.CreateCellsColor();
lowColor.Color = System.Drawing.Color.Red;
group.LowPointColor = lowColor;
// Set the negative-point color to orange
CellsColor negColor = workbook.CreateCellsColor();
negColor.Color = System.Drawing.Color.Orange;
group.NegativePointsColor = negColor;
// Set the default series color (used for positive bars)
CellsColor seriesColor = workbook.CreateCellsColor();
seriesColor.Color = System.Drawing.Color.SteelBlue;
group.SeriesColor = seriesColor;
// Step 6: Save the workbook
workbook.Save("output_winloss.xlsx");
Console.WriteLine("Workbook saved successfully: output_winloss.xlsx");
}
}
}
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.
using System;
using Aspose.Cells;
using Aspose.Cells.Charts;
// Step 1: Create a Workbook and get the first worksheet
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
// Step 2: Populate sample data in row 1 (A1:E1)
worksheet.Cells["A1"].PutValue(5);
worksheet.Cells["B1"].PutValue(-3);
worksheet.Cells["C1"].PutValue(8);
worksheet.Cells["D1"].PutValue(-2);
worksheet.Cells["E1"].PutValue(6);
// Step 3: Add a Line sparkline group at F1
CellArea lineArea = new CellArea();
lineArea.StartColumn = 5;
lineArea.EndColumn = 5;
lineArea.StartRow = 0;
lineArea.EndRow = 0;
int lineIdx = worksheet.SparklineGroups.Add(SparklineType.Line, "A1:E1", false, lineArea);
SparklineGroup lineGroup = worksheet.SparklineGroups[lineIdx];
// Customize the line sparkline color via CellsColor
CellsColor lineColor = workbook.CreateCellsColor();
lineColor.Color = System.Drawing.Color.Blue;
lineGroup.SeriesColor = lineColor;
// Step 4: Add a Column sparkline group at F2
CellArea columnArea = new CellArea();
columnArea.StartColumn = 5;
columnArea.EndColumn = 5;
columnArea.StartRow = 1;
columnArea.EndRow = 1;
int columnIdx = worksheet.SparklineGroups.Add(SparklineType.Column, "A1:E1", false, columnArea);
SparklineGroup columnGroup = worksheet.SparklineGroups[columnIdx];
// Customize the column sparkline series color
CellsColor columnColor = workbook.CreateCellsColor();
columnColor.Color = System.Drawing.Color.Green;
columnGroup.SeriesColor = columnColor;
// Step 5: Add a Win/Loss (Stacked) sparkline group at F3
CellArea stackedArea = new CellArea();
stackedArea.StartColumn = 5;
stackedArea.EndColumn = 5;
stackedArea.StartRow = 2;
stackedArea.EndRow = 2;
int stackedIdx = worksheet.SparklineGroups.Add(SparklineType.Stacked, "A1:E1", false, stackedArea);
SparklineGroup stackedGroup = worksheet.SparklineGroups[stackedIdx];
// Customize the win/loss sparkline series color
CellsColor stackedColor = workbook.CreateCellsColor();
stackedColor.Color = System.Drawing.Color.DarkOrange;
stackedGroup.SeriesColor = stackedColor;
// Step 6: Save the workbook
workbook.Save("output_all.xlsx");
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 — the SparklineType (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 a CellsColor created via workbook.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.CellsColor instance and assign it to the relevant property. Do not assign a System.Drawing.Color directly to sparkline color properties — they expect the CellsColor type from Aspose.Cells.Drawing. The SparklineGroups.Add method itself returns a fully typed SparklineGroup object, so you can chain property assignments on the return value or store it in a local variable and customize it before saving.Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.