Skip to main content

How to: Group and Ungroup Sparklines

  • 2 minutes to read

The examples below demonstrate how to group and ungroup sparklines on a worksheet.

Group Sparklines

As mentioned in the How to: Create Sparklines article, all sparklines in a worksheet are stored in sparkline groups. A group allows you to apply formatting and scaling options to all sparklines in the group at the same time. However, you can regroup the existing sparklines to form new groups with their own custom settings. To rearrange worksheet sparklines, follow the steps below.

  1. Get access to the sparklines you wish to regroup by their indices in the SparklineGroup.Sparklines collection.
  2. Call the SparklineGroupCollection.Add method overload and pass the following parameters: a list of the sparklines to be grouped together, and the SparklineGroupType enumeration member that specifies the type of the created group.

You can also move a single sparkline to another group by using the Sparkline.MoveTo method.

// Create a group of line sparklines. 
SparklineGroup lineGroup = worksheet.SparklineGroups.Add(worksheet["G4:G7"], worksheet["C4:F4,C5:F5,C6:F6, C7:F7"], SparklineGroupType.Line);

// Rearrange sparklines by grouping the second and fourth sparklines together and changing the group type to "Column". 
Sparkline sparklineG5 = lineGroup.Sparklines[1];
Sparkline sparklineG7 = lineGroup.Sparklines[3];
SparklineGroup columnGroup = worksheet.SparklineGroups.Add(new List<Sparkline> { sparklineG5, sparklineG7 }, SparklineGroupType.Column);

The image below shows the result.

SpreadsheetControl_Sparklines_RearrangeSparklines

Ungroup Sparklines

To split a set of grouped sparklines into individual sparkline charts, use the SparklineGroup.UnGroup method. This method splits the specified group into separate groups, each of which contains a single Sparkline object. All groups generated this way have the same type, formatting and axis options as the source group.

// Create a group of line sparklines.
SparklineGroup lineGroup = worksheet.SparklineGroups.Add(worksheet["G4:G7"], worksheet["C4:F4,C5:F5,C6:F6, C7:F7"], SparklineGroupType.Line);

// Split the created group into individual sparklines. 
lineGroup.UnGroup();
See Also