diff --git a/index.json b/index.json index e6b002cd9b..4a84592cc7 100644 --- a/index.json +++ b/index.json @@ -11459,6 +11459,116 @@ "file": "iterate-through-each-sparkline-collect-average-values-and-store-results-in-a-list.cs", "title": "Iterate through each sparkline, collect average values, and store results in a list." }, + { + "category": "sparkline", + "file": "add-a-line-sparkline-to-column-b-using-data-range-a1a10-via-sparklinecollectionadd.cs", + "title": "Add a line sparkline to column B using data range A1:A10 via SparklineCollection.Add." + }, + { + "category": "sparkline", + "file": "adjust-sparkline-lighting-to-soft-mode-for-improved-appearance-in-excel-2010-files.cs", + "title": "Adjust sparkline lighting to 'Soft' mode for improved appearance in Excel 2010 files." + }, + { + "category": "sparkline", + "file": "apply-3d-formatting-with-thirty-degree-rotation-to-sparkline-group-located-at-g1.cs", + "title": "Apply 3D formatting with thirty-degree rotation to sparkline group located at G1." + }, + { + "category": "sparkline", + "file": "apply-custom-background-color-to-sparkline-chart-area-to-match-workbook-theme-colors.cs", + "title": "Apply custom background color to sparkline chart area to match workbook theme colors." + }, + { + "category": "sparkline", + "file": "apply-custom-line-weight-of-two-points-to-sparkline-group-situated-in-cell-k7.cs", + "title": "Apply custom line weight of two points to sparkline group situated in cell K7." + }, + { + "category": "sparkline", + "file": "batch-create-sparklines-for-rows-one-through-twenty-each-using-corresponding-column-data-range.cs", + "title": "Batch create sparklines for rows one through twenty, each using corresponding column data range." + }, + { + "category": "sparkline", + "file": "benchmark-performance-of-creating-five-thousand-sparklines-in-a-large-workbook-to-assess-scalability.cs", + "title": "Benchmark performance of creating five thousand sparklines in a large workbook to assess scalability." + }, + { + "category": "sparkline", + "file": "change-sparkline-material-to-plastic-to-affect-surface-shading-and-give-subtle-glossy-effect.cs", + "title": "Change sparkline material to 'Plastic' to affect surface shading and give subtle glossy effect." + }, + { + "category": "sparkline", + "file": "clone-an-existing-sparkline-group-and-place-the-clone-into-a-different-worksheet.cs", + "title": "Clone an existing sparkline group and place the clone into a different worksheet." + }, + { + "category": "sparkline", + "file": "configure-sparkline-axis-minimum-and-maximum-to-fixed-numeric-values-for-consistent-scaling.cs", + "title": "Configure sparkline axis minimum and maximum to fixed numeric values for consistent scaling." + }, + { + "category": "sparkline", + "file": "configure-sparkline-to-ignore-hidden-cells-while-calculating-values-to-reflect-only-visible-data.cs", + "title": "Configure sparkline to ignore hidden cells while calculating values to reflect only visible data." + }, + { + "category": "sparkline", + "file": "create-a-column-sparkline-in-cell-c5-based-on-values-from-d5d15-range.cs", + "title": "Create a column sparkline in cell C5 based on values from D5:D15 range." + }, + { + "category": "sparkline", + "file": "create-a-sparkline-referencing-data-from-another-worksheet-named-datasheet-for-crosssheet-trends.cs", + "title": "Create a sparkline referencing data from another worksheet named 'DataSheet' for cross\u2011sheet trends." + }, + { + "category": "sparkline", + "file": "delete-the-second-sparkline-in-the-first-sparkline-group-using-sparklinegroupremoveat-method.cs", + "title": "Delete the second sparkline in the first sparkline group using SparklineGroup.RemoveAt method." + }, + { + "category": "sparkline", + "file": "deserialize-sparkline-configuration-from-json-file-and-apply-settings-to-newly-created-workbook.cs", + "title": "Deserialize sparkline configuration from JSON file and apply settings to newly created workbook." + }, + { + "category": "sparkline", + "file": "enable-display-of-both-first-and-last-points-in-line-sparkline-located-at-i4.cs", + "title": "Enable display of both first and last points in line sparkline located at I4." + }, + { + "category": "sparkline", + "file": "enable-display-of-empty-cells-as-zero-values-in-sparkline-at-cell-m9-for-accurate-totals.cs", + "title": "Enable display of empty cells as zero values in sparkline at cell M9 for accurate totals." + }, + { + "category": "sparkline", + "file": "enable-sparkline-markers-only-for-outlier-points-exceeding-predefined-threshold-to-highlight-anomalies.cs", + "title": "Enable sparkline markers only for outlier points exceeding predefined threshold to highlight anomalies." + }, + { + "category": "sparkline", + "file": "export-a-specific-sparkline-as-a-png-image-file-to-designated-output-directory.cs", + "title": "Export a specific sparkline as a PNG image file to designated output directory." + }, + { + "category": "sparkline", + "file": "handle-null-values-in-source-data-range-gracefully-when-generating-sparkline-to-avoid-rendering-errors.cs", + "title": "Handle null values in source data range gracefully when generating sparkline to avoid rendering errors." + }, + { + "category": "sparkline", + "file": "insert-a-winloss-sparkline-at-cell-e2-referencing-data-in-f2f12-range.cs", + "title": "Insert a win/loss sparkline at cell E2 referencing data in F2:F12 range." + }, + { + "category": "sparkline", + "file": "iterate-through-each-sparkline-collect-average-values-and-store-results-in-a-list.cs", + "title": "Iterate through each sparkline, collect average values, and store results in a list." + }, { "category": "sparkline", "file": "load-an-existing-workbook-and-import-asposecellscharts-namespace-for-sparkline-operations.cs", diff --git a/sparkline/add-a-line-sparkline-to-column-b-using-data-range-a1a10-via-sparklinecollectionadd.cs b/sparkline/add-a-line-sparkline-to-column-b-using-data-range-a1a10-via-sparklinecollectionadd.cs new file mode 100644 index 0000000000..8aac1b511b --- /dev/null +++ b/sparkline/add-a-line-sparkline-to-column-b-using-data-range-a1a10-via-sparklinecollectionadd.cs @@ -0,0 +1,30 @@ +using Aspose.Cells; +using Aspose.Cells.Charts; + +class Program +{ + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet worksheet = workbook.Worksheets[0]; + + // Populate sample data in the range A1:A10 + for (int i = 0; i < 10; i++) + { + // Row index i, column index 0 corresponds to column A + worksheet.Cells[i, 0].PutValue(i + 1); + } + + // Add a sparkline group of type Line to the worksheet + int groupIndex = worksheet.SparklineGroups.Add(SparklineType.Line); + SparklineGroup sparklineGroup = worksheet.SparklineGroups[groupIndex]; + + // Add a sparkline that uses the data range A1:A10 + // Place the sparkline in column B (index 1) at the first row (index 0) + sparklineGroup.Sparklines.Add("A1:A10", 0, 1); + + // Save the workbook to a file + workbook.Save("SparklineLine.xlsx"); + } +} \ No newline at end of file diff --git a/sparkline/adjust-sparkline-lighting-to-soft-mode-for-improved-appearance-in-excel-2010-files.cs b/sparkline/adjust-sparkline-lighting-to-soft-mode-for-improved-appearance-in-excel-2010-files.cs new file mode 100644 index 0000000000..c4bf41686a --- /dev/null +++ b/sparkline/adjust-sparkline-lighting-to-soft-mode-for-improved-appearance-in-excel-2010-files.cs @@ -0,0 +1,46 @@ +using System; +using System.Drawing; +using Aspose.Cells; +using Aspose.Cells.Charts; +using Aspose.Cells.Drawing; + +class AdjustSparklineLighting +{ + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data for the sparkline + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(2); + sheet.Cells["C1"].PutValue(1); + sheet.Cells["D1"].PutValue(3); + + // Define the location where the sparkline will be placed + CellArea location = new CellArea + { + StartRow = 0, + EndRow = 0, + StartColumn = 4, + EndColumn = 4 + }; + + // Add a sparkline group (Line type) with the data range A1:D1 + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A1:D1", false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Add a sparkline to the group + group.Sparklines.Add(sheet.Name + "!A1:D1", 0, 4); + + // NOTE: + // Aspose.Cells does not expose a direct property to modify the lighting of a sparkline. + // Lighting (e.g., LightRigType.Soft) is applicable to 3‑D shape formatting, which is + // available for chart series and other shapes, but not for sparkline groups. + // Therefore, the sparkline lighting cannot be set to 'Soft' via the current API. + + // Save the workbook as an Excel 2010 file + workbook.Save("SparklineWithSoftLighting.xlsx", SaveFormat.Xlsx); + } +} \ No newline at end of file diff --git a/sparkline/agents.md b/sparkline/agents.md new file mode 100644 index 0000000000..4bbdb0656d --- /dev/null +++ b/sparkline/agents.md @@ -0,0 +1,113 @@ +--- +category: sparkline +framework: .NET +parent: ../agents.md +version: v2 +--- + +# Persona + +You are a C# developer specializing in Sparklines using Aspose.Cells for .NET. + +Generate simple, correct, production-quality examples that demonstrate ONE sparkline scenario at a time. + +--- + +# Scope + +- Standalone .cs examples +- One operation per example +- Fully runnable with dotnet run +- No external dependencies + +--- + +# Required Namespaces + +using System; +using Aspose.Cells; + +--- + +# Key APIs + +- SparklineGroup +- SparklineCollection +- SparklineGroupCollection +- Worksheet.SparklineGroups +- SparklineType + +--- + +# Common Pattern + +1. Create workbook +2. Add sample numeric data +3. Create sparkline +4. Configure sparkline properties +5. Save workbook +6. Print success message + +--- + +# Sparkline Rules + +- Use valid numeric source ranges +- Use SparklineType appropriate to the scenario +- Ensure destination cells are visible +- One example = one sparkline operation + +--- + +# Input Strategy + +- Do NOT rely on external XLSX files +- Create all sample data programmatically +- Keep examples self-contained + +--- + +# Output Rules + +- Always generate output.xlsx +- Ensure workbook is saved successfully +- Output files are written to the working directory + +--- + +# Common Tasks + +- Create line sparkline +- Create column sparkline +- Create win/loss sparkline +- Modify sparkline settings +- Access sparkline groups + +--- + +# Common Mistakes + +❌ var workbook = new Workbook(); +✅ Workbook workbook = new Workbook(); + +❌ Create sparklines without source data +✅ Populate worksheet data before creating sparklines + +❌ Workbook workbook = new Workbook("input.xlsx"); +✅ Workbook workbook = new Workbook(); + +--- + +# Code Simplicity + +- Keep examples concise +- Avoid unnecessary abstractions + +--- + +# General Rules + +Refer to the root agents.md for: +- Boundaries +- Testing requirements +- Build and run instructions diff --git a/sparkline/apply-3d-formatting-with-thirty-degree-rotation-to-sparkline-group-located-at-g1.cs b/sparkline/apply-3d-formatting-with-thirty-degree-rotation-to-sparkline-group-located-at-g1.cs new file mode 100644 index 0000000000..df64a01689 --- /dev/null +++ b/sparkline/apply-3d-formatting-with-thirty-degree-rotation-to-sparkline-group-located-at-g1.cs @@ -0,0 +1,45 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +class Sparkline3DExample +{ + static void Main() + { + try + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data for the sparkline (A1:A5) + for (int i = 0; i < 5; i++) + { + sheet.Cells[i, 0].PutValue(i + 1); // Values 1‑5 in column A + } + + // Define the location range for the sparkline group (G1:G5) + // Column G = index 6 (0‑based), rows 0‑4 + CellArea location = new CellArea + { + StartRow = 0, + EndRow = 4, + StartColumn = 6, + EndColumn = 6 + }; + + // Add a sparkline group of type Line with the data range A1:A5 + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A1:A5", false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Note: Sparkline groups do not support 3‑D formatting. + + // Save the workbook + workbook.Save("Sparkline3DExample.xlsx"); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/sparkline/apply-custom-background-color-to-sparkline-chart-area-to-match-workbook-theme-colors.cs b/sparkline/apply-custom-background-color-to-sparkline-chart-area-to-match-workbook-theme-colors.cs new file mode 100644 index 0000000000..706fc54c17 --- /dev/null +++ b/sparkline/apply-custom-background-color-to-sparkline-chart-area-to-match-workbook-theme-colors.cs @@ -0,0 +1,58 @@ +using System; +using System.Drawing; +using Aspose.Cells; +using Aspose.Cells.Charts; + +namespace AsposeCellsSparklineBackgroundDemo +{ + class Program + { + static void Main() + { + try + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data for the sparkline (A1:A4) + sheet.Cells["A1"].PutValue(5); + sheet.Cells["A2"].PutValue(3); + sheet.Cells["A3"].PutValue(8); + sheet.Cells["A4"].PutValue(2); + + // Define the cell where the sparkline will be placed (E1) + CellArea location = new CellArea + { + StartRow = 0, + EndRow = 0, + StartColumn = 4, + EndColumn = 4 + }; + + // Add a sparkline group of type Line using the data range A1:A4 + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A1:A4", false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // (Optional) Add additional sparklines if needed: + // group.Sparklines.Add("A1:A4", 0, 4); + + // Retrieve a theme accent color (Accent1) from the workbook's theme + Color themeAccentColor = workbook.GetThemeColor(ThemeColorType.Accent1); + + // Apply the theme color as the background of the sparkline cell (E1) + Style style = sheet.Cells[0, 4].GetStyle(); + style.ForegroundColor = themeAccentColor; // Set fill color + style.Pattern = BackgroundType.Solid; // Ensure solid fill + sheet.Cells[0, 4].SetStyle(style); + + // Save the workbook + workbook.Save("SparklineBackgroundThemeColor.xlsx", SaveFormat.Xlsx); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } + } +} \ No newline at end of file diff --git a/sparkline/apply-custom-line-weight-of-two-points-to-sparkline-group-situated-in-cell-k7.cs b/sparkline/apply-custom-line-weight-of-two-points-to-sparkline-group-situated-in-cell-k7.cs new file mode 100644 index 0000000000..e459a6c790 --- /dev/null +++ b/sparkline/apply-custom-line-weight-of-two-points-to-sparkline-group-situated-in-cell-k7.cs @@ -0,0 +1,41 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +class Program +{ + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Sample data for the sparkline (row 1, columns A to D) + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(2); + sheet.Cells["C1"].PutValue(1); + sheet.Cells["D1"].PutValue(3); + + // Define the location cell K7 (column K = 10, row 7 = index 6) + CellArea location = new CellArea + { + StartRow = 6, + EndRow = 6, + StartColumn = 10, + EndColumn = 10 + }; + + // Add a line sparkline group with the data range A1:D1 and place it at K7 + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A1:D1", false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // (Optional) Explicitly add the sparkline to the group + group.Sparklines.Add(sheet.Name + "!A1:D1", 0, 10); + + // Apply a custom line weight of two points + group.LineWeight = 2.0; + + // Save the workbook + workbook.Save("SparklineLineWeight_K7.xlsx", SaveFormat.Xlsx); + } +} \ No newline at end of file diff --git a/sparkline/batch-create-sparklines-for-rows-one-through-twenty-each-using-corresponding-column-data-range.cs b/sparkline/batch-create-sparklines-for-rows-one-through-twenty-each-using-corresponding-column-data-range.cs new file mode 100644 index 0000000000..8862caba70 --- /dev/null +++ b/sparkline/batch-create-sparklines-for-rows-one-through-twenty-each-using-corresponding-column-data-range.cs @@ -0,0 +1,45 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +class BatchSparklineDemo +{ + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data for 20 rows and 20 columns (A1:T20) + for (int row = 0; row < 20; row++) + { + for (int col = 0; col < 20; col++) + { + // Example data: (row index + 1) * (col index + 1) + sheet.Cells[row, col].PutValue((row + 1) * (col + 1)); + } + } + + // Add a sparkline group of type Line (default orientation is horizontal) + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Column where sparklines will be placed (V column, zero‑based index 21) + int sparklineColumn = 21; + + // Create a sparkline for each row (1‑through‑20) using the corresponding column data range + // Row i uses data from column i (A‑T) across rows 1‑20 (vertical orientation) + for (int i = 0; i < 20; i++) + { + // Build the data range string for column i (e.g., "A1:A20", "B1:B20", ...) + string columnLetter = ((char)('A' + i)).ToString(); + string dataRange = $"{columnLetter}1:{columnLetter}20"; + + // Add the sparkline at row i, column V + group.Sparklines.Add(dataRange, i, sparklineColumn); + } + + // Save the workbook with the created sparklines + workbook.Save("BatchSparklines.xlsx"); + } +} \ No newline at end of file diff --git a/sparkline/benchmark-performance-of-creating-five-thousand-sparklines-in-a-large-workbook-to-assess-scalability.cs b/sparkline/benchmark-performance-of-creating-five-thousand-sparklines-in-a-large-workbook-to-assess-scalability.cs new file mode 100644 index 0000000000..05ea3b3982 --- /dev/null +++ b/sparkline/benchmark-performance-of-creating-five-thousand-sparklines-in-a-large-workbook-to-assess-scalability.cs @@ -0,0 +1,68 @@ +using System; +using System.Diagnostics; +using System.IO; +using Aspose.Cells; +using Aspose.Cells.Charts; + +namespace SparklineBenchmark +{ + class Program + { + static void Main() + { + try + { + // Create a new workbook + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate source data for the sparklines (same range for all sparklines) + // Fill cells A1:D1 with sample values + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(2); + sheet.Cells["C1"].PutValue(1); + sheet.Cells["D1"].PutValue(3); + + // Define the location of the first sparkline (single cell) + // Subsequent sparklines will be added row‑by‑row + CellArea firstLocation = new CellArea + { + StartRow = 0, // Row 1 (zero‑based) + EndRow = 0, + StartColumn = 5, // Column F (zero‑based) + EndColumn = 5 + }; + + // Add a sparkline group with a valid single‑cell location + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A1:D1", false, firstLocation); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Benchmark the creation of 5,000 sparklines + Stopwatch sw = Stopwatch.StartNew(); + + for (int i = 0; i < 5000; i++) + { + // Add a sparkline for each row; data range is the same for all + // Location varies by row (column F, rows 1‑5000) + group.Sparklines.Add("A1:D1", i, 5); + } + + sw.Stop(); + Console.WriteLine($"Time to create 5,000 sparklines: {sw.ElapsedMilliseconds} ms"); + + // Save the workbook (ensure the directory exists) + string outputPath = "SparklineBenchmark.xlsx"; + if (File.Exists(outputPath)) + { + File.Delete(outputPath); + } + workbook.Save(outputPath); + Console.WriteLine($"Workbook saved to {Path.GetFullPath(outputPath)}"); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } + } +} \ No newline at end of file diff --git a/sparkline/change-sparkline-material-to-plastic-to-affect-surface-shading-and-give-subtle-glossy-effect.cs b/sparkline/change-sparkline-material-to-plastic-to-affect-surface-shading-and-give-subtle-glossy-effect.cs new file mode 100644 index 0000000000..fabf2c96f1 --- /dev/null +++ b/sparkline/change-sparkline-material-to-plastic-to-affect-surface-shading-and-give-subtle-glossy-effect.cs @@ -0,0 +1,72 @@ +using System; +using System.IO; +using Aspose.Cells; +using Aspose.Cells.Charts; +using Aspose.Cells.Drawing; +using System.Drawing; + +namespace AsposeCellsExamples +{ + public class SparklineMaterialPlasticDemo + { + public static void Run() + { + try + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data for the sparkline + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(2); + sheet.Cells["C1"].PutValue(1); + sheet.Cells["D1"].PutValue(3); + + // Define the location where the sparkline will be placed (E1) + CellArea location = new CellArea + { + StartRow = 0, + EndRow = 0, + StartColumn = 4, + EndColumn = 4 + }; + + // Add a line sparkline group using the data range A1:D1 + int sparklineGroupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A1:D1", false, location); + SparklineGroup sparklineGroup = sheet.SparklineGroups[sparklineGroupIndex]; + + // OPTIONAL: customize sparkline appearance (color, markers, etc.) + CellsColor seriesColor = workbook.CreateCellsColor(); + seriesColor.Color = Color.Orange; + sparklineGroup.SeriesColor = seriesColor; + sparklineGroup.ShowMarkers = true; + + // Demonstrate material change on a shape (TextBox) that supports ThreeDFormat + Shape shape = sheet.Shapes.AddShape(MsoDrawingType.TextBox, 1, 1, 200, 100, 0, 0); + shape.Text = "Sparkline Material: Plastic"; + + // Set ThreeD material to Plastic + shape.ThreeDFormat.Material = PresetMaterialType.Plastic; + + // Save the workbook + string outputPath = "SparklineMaterialPlasticDemo.xlsx"; + workbook.Save(outputPath, SaveFormat.Xlsx); + Console.WriteLine($"Workbook saved to {Path.GetFullPath(outputPath)}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Error: {ex.Message}"); + } + } + } + + // Entry point for the console application + public class Program + { + public static void Main(string[] args) + { + SparklineMaterialPlasticDemo.Run(); + } + } +} \ No newline at end of file diff --git a/sparkline/clone-an-existing-sparkline-group-and-place-the-clone-into-a-different-worksheet.cs b/sparkline/clone-an-existing-sparkline-group-and-place-the-clone-into-a-different-worksheet.cs new file mode 100644 index 0000000000..a7d155b9b8 --- /dev/null +++ b/sparkline/clone-an-existing-sparkline-group-and-place-the-clone-into-a-different-worksheet.cs @@ -0,0 +1,110 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +namespace SparklineCloneDemo +{ + class Program + { + static void Main() + { + try + { + // Create a new workbook + Workbook workbook = new Workbook(); + + // Source worksheet (contains the original sparkline group) + Worksheet sourceSheet = workbook.Worksheets[0]; + sourceSheet.Name = "Source"; + + // Target worksheet (where the cloned sparkline group will be placed) + Worksheet targetSheet = workbook.Worksheets.Add("Target"); + + // ------------------------------------------------- + // Populate sample data in the source worksheet + // ------------------------------------------------- + for (int i = 0; i < 5; i++) + { + // Fill column A with values 1..5 + sourceSheet.Cells[i, 0].PutValue(i + 1); + } + + // ------------------------------------------------- + // Add a sparkline group to the source worksheet + // ------------------------------------------------- + // Define where the sparkline will be displayed (cell B1) + CellArea sourceLocation = new CellArea + { + StartRow = 0, + EndRow = 0, + StartColumn = 1, + EndColumn = 1 + }; + + // Use a fully‑qualified data range (includes sheet name) to avoid reference errors + string sourceDataRange = $"{sourceSheet.Name}!A1:A5"; + + // Add the sparkline group (horizontal line type) + int sourceGroupIndex = sourceSheet.SparklineGroups.Add( + SparklineType.Line, // type + sourceDataRange, // data range (with sheet name) + false, // isVertical + sourceLocation); // location range + + SparklineGroup sourceGroup = sourceSheet.SparklineGroups[sourceGroupIndex]; + + // Add a sparkline item to the group + sourceGroup.Sparklines.Add(sourceDataRange, 0, 1); + + // Optional: customize the source sparkline group + sourceGroup.ShowHighPoint = true; + sourceGroup.ShowLowPoint = true; + sourceGroup.LineWeight = 1.0; + + // ------------------------------------------------- + // Clone the sparkline group into the target worksheet + // ------------------------------------------------- + // Retrieve the data range string from the first sparkline (includes sheet name) + string dataRange = sourceGroup.Sparklines[0].DataRange; + + // Define the location for the cloned sparkline in the target sheet (cell B1) + CellArea targetLocation = new CellArea + { + StartRow = 0, + EndRow = 0, + StartColumn = 1, + EndColumn = 1 + }; + + // Add a new sparkline group to the target sheet using the same type and data range + int targetGroupIndex = targetSheet.SparklineGroups.Add( + sourceGroup.Type, // same sparkline type as the source + dataRange, // same data range (refers to the source sheet) + false, // isVertical (same as source) + targetLocation); // location in the target sheet + + SparklineGroup targetGroup = targetSheet.SparklineGroups[targetGroupIndex]; + + // Add a sparkline item to the cloned group (same data range) + targetGroup.Sparklines.Add(dataRange, 0, 1); + + // Copy visual properties from the source group to the cloned group + targetGroup.ShowHighPoint = sourceGroup.ShowHighPoint; + targetGroup.ShowLowPoint = sourceGroup.ShowLowPoint; + targetGroup.LineWeight = sourceGroup.LineWeight; + targetGroup.SeriesColor = sourceGroup.SeriesColor; + targetGroup.HighPointColor = sourceGroup.HighPointColor; + targetGroup.LowPointColor = sourceGroup.LowPointColor; + + // ------------------------------------------------- + // Save the workbook + // ------------------------------------------------- + workbook.Save("ClonedSparklineGroup.xlsx"); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } + } +} \ No newline at end of file diff --git a/sparkline/configure-sparkline-axis-minimum-and-maximum-to-fixed-numeric-values-for-consistent-scaling.cs b/sparkline/configure-sparkline-axis-minimum-and-maximum-to-fixed-numeric-values-for-consistent-scaling.cs new file mode 100644 index 0000000000..4d8d9aa907 --- /dev/null +++ b/sparkline/configure-sparkline-axis-minimum-and-maximum-to-fixed-numeric-values-for-consistent-scaling.cs @@ -0,0 +1,51 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; +using System.Drawing; + +class SparklineAxisFixedScale +{ + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data for the sparkline + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(2); + sheet.Cells["C1"].PutValue(1); + sheet.Cells["D1"].PutValue(8); + + // Define the location where the sparkline will be placed (cell E1) + CellArea location = new CellArea + { + StartRow = 0, + EndRow = 0, + StartColumn = 4, + EndColumn = 4 + }; + + // Add a sparkline group of type Line, using the data range A1:D1 + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A1:D1", false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Add the sparkline to the group (optional, already added by Add method) + group.Sparklines.Add(sheet.Name + "!A1:D1", 0, 4); + + // Configure the vertical axis to use custom fixed minimum and maximum values + group.VerticalAxisMinValueType = SparklineAxisMinMaxType.Custom; + group.VerticalAxisMinValue = 0.0; // Fixed minimum + + group.VerticalAxisMaxValueType = SparklineAxisMinMaxType.Custom; + group.VerticalAxisMaxValue = 10.0; // Fixed maximum + + // Optional: set some visual properties + group.ShowHighPoint = true; + group.ShowLowPoint = true; + group.LineWeight = 1.0; + + // Save the workbook + workbook.Save("SparklineFixedAxis.xlsx"); + } +} \ No newline at end of file diff --git a/sparkline/configure-sparkline-to-ignore-hidden-cells-while-calculating-values-to-reflect-only-visible-data.cs b/sparkline/configure-sparkline-to-ignore-hidden-cells-while-calculating-values-to-reflect-only-visible-data.cs new file mode 100644 index 0000000000..df9591ac61 --- /dev/null +++ b/sparkline/configure-sparkline-to-ignore-hidden-cells-while-calculating-values-to-reflect-only-visible-data.cs @@ -0,0 +1,54 @@ +using System; +using System.Drawing; +using Aspose.Cells; +using Aspose.Cells.Charts; + +namespace SparklineHiddenDemo +{ + class Program + { + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data in row 1 + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(2); + sheet.Cells["C1"].PutValue(1); + sheet.Cells["D1"].PutValue(3); + + // Hide column B (index 1) to simulate hidden data + sheet.Cells.Columns[1].IsHidden = true; + + // Define the location where the sparkline will be placed (E1) + CellArea sparklineArea = new CellArea + { + StartColumn = 4, // Column E (0‑based index) + EndColumn = 4, + StartRow = 0, // Row 1 + EndRow = 0 + }; + + // Add a sparkline group that uses the data range A1:D1 + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A1:D1", false, sparklineArea); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Add the sparkline to the group (optional, already added by Add method) + // group.Sparklines.Add(sheet.Name + "!A1:D1", 0, 4); + + // Configure the sparkline to ignore hidden cells while calculating values + // Setting DisplayHidden to false ensures hidden rows/columns are not considered + group.DisplayHidden = false; + + // Optional: customize appearance + CellsColor seriesColor = workbook.CreateCellsColor(); + seriesColor.Color = Color.Blue; + group.SeriesColor = seriesColor; + + // Save the workbook + workbook.Save("SparklineIgnoreHidden.xlsx"); + } + } +} \ No newline at end of file diff --git a/sparkline/create-a-column-sparkline-in-cell-c5-based-on-values-from-d5d15-range.cs b/sparkline/create-a-column-sparkline-in-cell-c5-based-on-values-from-d5d15-range.cs new file mode 100644 index 0000000000..8a3bf05335 --- /dev/null +++ b/sparkline/create-a-column-sparkline-in-cell-c5-based-on-values-from-d5d15-range.cs @@ -0,0 +1,44 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +class CreateColumnSparkline +{ + static void Main() + { + try + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Define the location where the sparkline will be placed (cell C5) + // Row and column indices are zero‑based (C5 -> row 4, column 2) + CellArea location = new CellArea + { + StartRow = 4, + EndRow = 4, + StartColumn = 2, + EndColumn = 2 + }; + + // Define the data range for the sparkline (D5:D15 on the same sheet) + string dataRange = $"{sheet.Name}!D5:D15"; + + // Add a sparkline group of type Column. + // isVertical = false (horizontal orientation for the data series) + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Column, dataRange, false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // (Optional) Access the created sparkline if further customization is needed + // Sparkline sparkline = group.Sparklines[0]; + + // Save the workbook with the sparkline + workbook.Save("ColumnSparkline.xlsx"); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/sparkline/create-a-sparkline-referencing-data-from-another-worksheet-named-datasheet-for-crosssheet-trends.cs b/sparkline/create-a-sparkline-referencing-data-from-another-worksheet-named-datasheet-for-crosssheet-trends.cs new file mode 100644 index 0000000000..b5b3d570d2 --- /dev/null +++ b/sparkline/create-a-sparkline-referencing-data-from-another-worksheet-named-datasheet-for-crosssheet-trends.cs @@ -0,0 +1,66 @@ +using System; +using System.IO; +using Aspose.Cells; +using Aspose.Cells.Charts; + +namespace AsposeCellsSparklineCrossSheet +{ + class Program + { + static void Main() + { + try + { + // Create a new workbook + Workbook workbook = new Workbook(); + + // Add a worksheet that will hold the source data + Worksheet dataSheet = workbook.Worksheets[workbook.Worksheets.Add()]; + dataSheet.Name = "DataSheet"; + + // Populate sample data in DataSheet (A1:D5) + for (int row = 0; row < 5; row++) + { + for (int col = 0; col < 4; col++) + { + dataSheet.Cells[row, col].PutValue((row + 1) * (col + 1)); + } + } + + // Add a worksheet where the sparkline will be placed + Worksheet trendSheet = workbook.Worksheets[0]; // default first sheet + trendSheet.Name = "TrendSheet"; + + // Define the location range for the sparkline group (one sparkline per column of data) + // Since the data range has 4 columns, we need 4 cells to host the sparklines (E1:H1) + CellArea sparklineLocation = CellArea.CreateCellArea("E1", "H1"); + + // Data range format: SheetName!StartCell:EndCell + string dataRange = "DataSheet!A1:D5"; + + // Add a sparkline group on TrendSheet that references the cross‑sheet data range + int groupIndex = trendSheet.SparklineGroups.Add( + SparklineType.Line, // sparkline type + dataRange, // cross‑sheet data range + false, // plot by column (false = by column) + sparklineLocation); // where the sparklines will appear + + // Retrieve the created group (optional: customize appearance) + SparklineGroup group = trendSheet.SparklineGroups[groupIndex]; + group.ShowHighPoint = true; + group.ShowLowPoint = true; + + // Define output file path + string outputPath = "CrossSheetSparkline.xlsx"; + + // Save the workbook + workbook.Save(outputPath); + Console.WriteLine($"Workbook saved successfully to '{Path.GetFullPath(outputPath)}'."); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } + } +} \ No newline at end of file diff --git a/sparkline/delete-the-second-sparkline-in-the-first-sparkline-group-using-sparklinegroupremoveat-method.cs b/sparkline/delete-the-second-sparkline-in-the-first-sparkline-group-using-sparklinegroupremoveat-method.cs new file mode 100644 index 0000000000..85e9fc5e1a --- /dev/null +++ b/sparkline/delete-the-second-sparkline-in-the-first-sparkline-group-using-sparklinegroupremoveat-method.cs @@ -0,0 +1,64 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +namespace AsposeCellsSparklineRemovalDemo +{ + class Program + { + static void Main() + { + try + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data (two rows) that will be used for the sparklines + // Row 1 + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(2); + sheet.Cells["C1"].PutValue(8); + sheet.Cells["D1"].PutValue(3); + // Row 2 + sheet.Cells["A2"].PutValue(6); + sheet.Cells["B2"].PutValue(1); + sheet.Cells["C2"].PutValue(7); + sheet.Cells["D2"].PutValue(4); + + // Define the location range for two sparklines (cells E1 and F1) + // StartColumn = 4 (E), EndColumn = 5 (F), rows 0‑1 (E1:F2) + CellArea location = new CellArea + { + StartRow = 0, + EndRow = 1, + StartColumn = 4, + EndColumn = 5 + }; + + // Add a sparkline group with the data range A1:D2. + // The location range contains two cells, so two sparklines are created automatically. + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A1:D2", false, location); + SparklineGroup sparklineGroup = sheet.SparklineGroups[groupIndex]; + + // At this point sparklineGroup.Sparklines.Count == 2 + Console.WriteLine($"Initial sparkline count: {sparklineGroup.Sparklines.Count}"); + + // Delete the second sparkline (index 1) using RemoveAt on the Sparklines collection + sparklineGroup.Sparklines.RemoveAt(1); + + // Verify removal + Console.WriteLine($"Sparkline count after removal: {sparklineGroup.Sparklines.Count}"); + + // Save the workbook + string outputPath = "SparklineRemovalResult.xlsx"; + workbook.Save(outputPath); + Console.WriteLine($"Workbook saved to {outputPath}"); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } + } +} \ No newline at end of file diff --git a/sparkline/deserialize-sparkline-configuration-from-json-file-and-apply-settings-to-newly-created-workbook.cs b/sparkline/deserialize-sparkline-configuration-from-json-file-and-apply-settings-to-newly-created-workbook.cs new file mode 100644 index 0000000000..b3a7550155 --- /dev/null +++ b/sparkline/deserialize-sparkline-configuration-from-json-file-and-apply-settings-to-newly-created-workbook.cs @@ -0,0 +1,172 @@ +using System; +using System.IO; +using System.Text.Json; +using System.Drawing; +using System.Globalization; +using Aspose.Cells; +using Aspose.Cells.Charts; + +namespace SparklineFromJsonDemo +{ + // Represents the JSON structure for a sparkline group configuration + public class SparklineConfig + { + public string? DataRange { get; set; } // e.g. "A1:D1" + public string? LocationStart { get; set; } // e.g. "E1" + public string? LocationEnd { get; set; } // e.g. "E1" (same cell for single sparkline) + public bool IsVertical { get; set; } // false = by row, true = by column + public string? Type { get; set; } // "Line", "Column", "Stacked" + public string? SeriesColor { get; set; } // Hex color, e.g. "#FF6600" + public bool ShowHighPoint { get; set; } + public string? HighPointColor { get; set; } // Hex color + public bool ShowLowPoint { get; set; } + public string? LowPointColor { get; set; } // Hex color + public bool ShowFirstPoint { get; set; } + public string? FirstPointColor { get; set; } // Hex color + public bool ShowLastPoint { get; set; } + public string? LastPointColor { get; set; } // Hex color + public bool ShowMarkers { get; set; } + public string? MarkersColor { get; set; } // Hex color + public double LineWeight { get; set; } // Points + public string? PresetStyle { get; set; } // e.g. "Style5" + } + + class Program + { + static void Main() + { + try + { + // Path to the JSON configuration file + const string jsonPath = "sparklineConfig.json"; + + // Ensure the JSON file exists + if (!File.Exists(jsonPath)) + { + Console.WriteLine($"Configuration file not found: {jsonPath}"); + return; + } + + // Deserialize the JSON into a SparklineConfig object + SparklineConfig? config = JsonSerializer.Deserialize(File.ReadAllText(jsonPath)); + if (config == null) + { + Console.WriteLine("Failed to deserialize configuration."); + return; + } + + // ---------- Create a new workbook (lifecycle rule: create) ---------- + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate the data range referenced in the config (demo data) + if (!string.IsNullOrEmpty(config.DataRange)) + { + PopulateSampleData(sheet, config.DataRange); + } + + // Convert the string type to the SparklineType enum + SparklineType sparklineType = config.Type switch + { + "Line" => SparklineType.Line, + "Column" => SparklineType.Column, + "Stacked" => SparklineType.Stacked, + _ => SparklineType.Line + }; + + // Build the location CellArea from start/end cells + if (string.IsNullOrEmpty(config.LocationStart) || string.IsNullOrEmpty(config.LocationEnd)) + { + Console.WriteLine("LocationStart or LocationEnd is missing in configuration."); + return; + } + + CellArea locationArea = CellArea.CreateCellArea(config.LocationStart, config.LocationEnd); + + // Add the sparkline group (lifecycle rule: load – we are loading settings into the workbook) + int groupIdx = sheet.SparklineGroups.Add(sparklineType, config.DataRange, config.IsVertical, locationArea); + SparklineGroup group = sheet.SparklineGroups[groupIdx]; + + // Apply visual settings from the configuration + if (!string.IsNullOrEmpty(config.SeriesColor)) + group.SeriesColor = CreateCellsColor(workbook, config.SeriesColor); + + group.ShowHighPoint = config.ShowHighPoint; + if (config.ShowHighPoint && !string.IsNullOrEmpty(config.HighPointColor)) + group.HighPointColor = CreateCellsColor(workbook, config.HighPointColor); + + group.ShowLowPoint = config.ShowLowPoint; + if (config.ShowLowPoint && !string.IsNullOrEmpty(config.LowPointColor)) + group.LowPointColor = CreateCellsColor(workbook, config.LowPointColor); + + group.ShowFirstPoint = config.ShowFirstPoint; + if (config.ShowFirstPoint && !string.IsNullOrEmpty(config.FirstPointColor)) + group.FirstPointColor = CreateCellsColor(workbook, config.FirstPointColor); + + group.ShowLastPoint = config.ShowLastPoint; + if (config.ShowLastPoint && !string.IsNullOrEmpty(config.LastPointColor)) + group.LastPointColor = CreateCellsColor(workbook, config.LastPointColor); + + group.ShowMarkers = config.ShowMarkers; + if (config.ShowMarkers && !string.IsNullOrEmpty(config.MarkersColor)) + group.MarkersColor = CreateCellsColor(workbook, config.MarkersColor); + + if (config.LineWeight > 0) + group.LineWeight = config.LineWeight; + + if (!string.IsNullOrEmpty(config.PresetStyle) && + Enum.TryParse(config.PresetStyle, out var preset)) + { + group.PresetStyle = preset; + } + + // ---------- Save the workbook (lifecycle rule: save) ---------- + const string outputPath = "SparklineFromJsonDemo.xlsx"; + workbook.Save(outputPath); + Console.WriteLine($"Workbook saved to {outputPath}"); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } + + // Helper to create a CellsColor from a hex string (e.g., "#FF6600") + private static CellsColor CreateCellsColor(Workbook wb, string hex) + { + // Remove leading '#' if present + if (hex.StartsWith("#")) + hex = hex.Substring(1); + + // Parse ARGB (8 chars) or RGB (6 chars) + int argb = hex.Length == 8 + ? int.Parse(hex, NumberStyles.HexNumber) + : (int.Parse(hex, NumberStyles.HexNumber) | unchecked((int)0xFF000000)); + + Color color = Color.FromArgb(argb); + CellsColor cellsColor = wb.CreateCellsColor(); + cellsColor.Color = color; + return cellsColor; + } + + // Simple method to fill the data range with incremental numbers for demo purposes + private static void PopulateSampleData(Worksheet sheet, string dataRange) + { + // Split the range string (e.g., "A1:D1") into start and end cell names + string[] parts = dataRange.Split(':', StringSplitOptions.RemoveEmptyEntries); + if (parts.Length != 2) + throw new ArgumentException($"Invalid data range format: {dataRange}"); + + CellArea area = CellArea.CreateCellArea(parts[0], parts[1]); + + int value = 1; + for (int row = area.StartRow; row <= area.EndRow; row++) + { + for (int col = area.StartColumn; col <= area.EndColumn; col++) + { + sheet.Cells[row, col].PutValue(value++); + } + } + } + } +} \ No newline at end of file diff --git a/sparkline/enable-display-of-both-first-and-last-points-in-line-sparkline-located-at-i4.cs b/sparkline/enable-display-of-both-first-and-last-points-in-line-sparkline-located-at-i4.cs new file mode 100644 index 0000000000..0c5414ab41 --- /dev/null +++ b/sparkline/enable-display-of-both-first-and-last-points-in-line-sparkline-located-at-i4.cs @@ -0,0 +1,80 @@ +using System; +using System.Drawing; +using System.IO; +using Aspose.Cells; +using Aspose.Cells.Charts; + +namespace AsposeCellsSparklineDemo +{ + public class ShowFirstAndLastPoints + { + public static void Run() + { + try + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Add sample data for the sparkline (row 4, columns A to D) + sheet.Cells["A4"].PutValue(5); + sheet.Cells["B4"].PutValue(2); + sheet.Cells["C4"].PutValue(8); + sheet.Cells["D4"].PutValue(3); + + // Define the location cell I4 (column index 8, row index 3) + CellArea location = new CellArea + { + StartColumn = 8, + EndColumn = 8, + StartRow = 3, + EndRow = 3 + }; + + // Add a line sparkline group with the data range A4:D4 and place it at I4 + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A4:D4", false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Enable highlighting of the first and last points + group.ShowFirstPoint = true; + group.ShowLastPoint = true; + + // Set colors for the first and last points + CellsColor firstPointColor = workbook.CreateCellsColor(); + firstPointColor.Color = Color.Purple; + group.FirstPointColor = firstPointColor; + + CellsColor lastPointColor = workbook.CreateCellsColor(); + lastPointColor.Color = Color.Yellow; + group.LastPointColor = lastPointColor; + + // Define output file path + string outputPath = "SparklineFirstLastPoints.xlsx"; + + // Ensure the directory exists before saving + string outputDir = Path.GetDirectoryName(Path.GetFullPath(outputPath)); + if (!Directory.Exists(outputDir)) + { + Directory.CreateDirectory(outputDir); + } + + // Save the workbook + workbook.Save(outputPath); + Console.WriteLine($"Workbook saved successfully to '{outputPath}'."); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } + } + + // Entry point for the application + public class Program + { + public static void Main(string[] args) + { + ShowFirstAndLastPoints.Run(); + } + } +} \ No newline at end of file diff --git a/sparkline/enable-display-of-empty-cells-as-zero-values-in-sparkline-at-cell-m9-for-accurate-totals.cs b/sparkline/enable-display-of-empty-cells-as-zero-values-in-sparkline-at-cell-m9-for-accurate-totals.cs new file mode 100644 index 0000000000..f7260e455f --- /dev/null +++ b/sparkline/enable-display-of-empty-cells-as-zero-values-in-sparkline-at-cell-m9-for-accurate-totals.cs @@ -0,0 +1,45 @@ +using Aspose.Cells; +using Aspose.Cells.Charts; + +class Program +{ + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate row 9 (index 8) with sample data, leaving some cells empty + sheet.Cells["A9"].PutValue(10); + sheet.Cells["B9"].PutValue(null); // empty cell + sheet.Cells["C9"].PutValue(5); + sheet.Cells["D9"].PutValue(null); // empty cell + sheet.Cells["E9"].PutValue(7); + sheet.Cells["F9"].PutValue(3); + sheet.Cells["G9"].PutValue(null); // empty cell + sheet.Cells["H9"].PutValue(2); + sheet.Cells["I9"].PutValue(4); + sheet.Cells["J9"].PutValue(null); // empty cell + sheet.Cells["K9"].PutValue(6); + sheet.Cells["L9"].PutValue(8); + + // Define the location where the sparkline will be placed (cell M9) + CellArea location = new CellArea + { + StartRow = 8, // Row 9 (zero‑based index) + EndRow = 8, + StartColumn = 12, // Column M (zero‑based index) + EndColumn = 12 + }; + + // Add a line sparkline group that uses the data range A9:L9 + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A9:L9", false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Configure the group to treat empty cells as zeros for accurate totals + group.PlotEmptyCellsType = PlotEmptyCellsType.Zero; + + // Save the workbook + workbook.Save("SparklineEmptyAsZero.xlsx"); + } +} \ No newline at end of file diff --git a/sparkline/enable-sparkline-markers-only-for-outlier-points-exceeding-predefined-threshold-to-highlight-anomalies.cs b/sparkline/enable-sparkline-markers-only-for-outlier-points-exceeding-predefined-threshold-to-highlight-anomalies.cs new file mode 100644 index 0000000000..09fdb6d35c --- /dev/null +++ b/sparkline/enable-sparkline-markers-only-for-outlier-points-exceeding-predefined-threshold-to-highlight-anomalies.cs @@ -0,0 +1,60 @@ +using System; +using System.Drawing; +using Aspose.Cells; +using Aspose.Cells.Charts; + +namespace AsposeCellsSparklineOutlierDemo +{ + class Program + { + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Sample data – values above 15 will be considered outliers + double[] data = { 5, 8, 12, 20, 7, 30, 9, 4 }; + for (int i = 0; i < data.Length; i++) + { + sheet.Cells[0, i].PutValue(data[i]); + } + + // Define the location where the sparkline will be placed (cell E1) + CellArea sparklineLocation = new CellArea + { + StartRow = 0, + EndRow = 0, + StartColumn = 4, + EndColumn = 4 + }; + + // Add a line sparkline group for the data range A1:H1 + int groupIdx = sheet.SparklineGroups.Add(SparklineType.Line, "A1:H1", false, sparklineLocation); + SparklineGroup group = sheet.SparklineGroups[groupIdx]; + + // Add the sparkline to the group (the Add method also creates the sparkline) + group.Sparklines.Add(sheet.Name + "!A1:H1", 0, 4); + + // Enable markers – this will display a marker for each point + group.ShowMarkers = true; + + // Set marker color to red to make outliers stand out + CellsColor markerColor = workbook.CreateCellsColor(); + markerColor.Color = Color.Red; + group.MarkersColor = markerColor; + + // OPTIONAL: Highlight the highest and lowest points as well + group.ShowHighPoint = true; + group.HighPointColor = workbook.CreateCellsColor(); + group.HighPointColor.Color = Color.Green; + + group.ShowLowPoint = true; + group.LowPointColor = workbook.CreateCellsColor(); + group.LowPointColor.Color = Color.Blue; + + // Save the workbook + workbook.Save("SparklineOutlierDemo.xlsx"); + } + } +} \ No newline at end of file diff --git a/sparkline/export-a-specific-sparkline-as-a-png-image-file-to-designated-output-directory.cs b/sparkline/export-a-specific-sparkline-as-a-png-image-file-to-designated-output-directory.cs new file mode 100644 index 0000000000..a58acb74ac --- /dev/null +++ b/sparkline/export-a-specific-sparkline-as-a-png-image-file-to-designated-output-directory.cs @@ -0,0 +1,59 @@ +using System; +using System.IO; +using Aspose.Cells; +using Aspose.Cells.Charts; +using Aspose.Cells.Rendering; + +class ExportSparklineToPng +{ + static void Main() + { + // Define output directory and ensure it exists + string outputDir = Path.Combine(Environment.CurrentDirectory, "output"); + Directory.CreateDirectory(outputDir); + + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data for the sparkline (A1:D1) + sheet.Cells["A1"].PutValue(10); + sheet.Cells["B1"].PutValue(20); + sheet.Cells["C1"].PutValue(15); + sheet.Cells["D1"].PutValue(30); + + // Define the location cell where the sparkline will be placed (E1) + CellArea sparklineLocation = new CellArea + { + StartRow = 0, + EndRow = 0, + StartColumn = 4, + EndColumn = 4 + }; + + // Add a sparkline group of type Line and create the sparkline + int groupIdx = sheet.SparklineGroups.Add(SparklineType.Line, sheet.Name + "!A1:D1", false, sparklineLocation); + SparklineGroup group = sheet.SparklineGroups[groupIdx]; + Sparkline sparkline = group.Sparklines[0]; // the first (and only) sparkline in the group + + // Configure image options for PNG output + ImageOrPrintOptions imgOptions = new ImageOrPrintOptions + { + ImageType = Aspose.Cells.Drawing.ImageType.Png, + HorizontalResolution = 300, + VerticalResolution = 300, + Transparent = false + }; + + // Build full file path for the exported image + string imagePath = Path.Combine(outputDir, "sparkline.png"); + + // Export the sparkline to PNG using the ToImage method (string, ImageOrPrintOptions) + sparkline.ToImage(imagePath, imgOptions); + + // Optionally, save the workbook for reference + workbook.Save(Path.Combine(outputDir, "WorkbookWithSparkline.xlsx")); + + Console.WriteLine($"Sparkline image saved to: {imagePath}"); + } +} \ No newline at end of file diff --git a/sparkline/handle-null-values-in-source-data-range-gracefully-when-generating-sparkline-to-avoid-rendering-errors.cs b/sparkline/handle-null-values-in-source-data-range-gracefully-when-generating-sparkline-to-avoid-rendering-errors.cs new file mode 100644 index 0000000000..1db7ec7289 --- /dev/null +++ b/sparkline/handle-null-values-in-source-data-range-gracefully-when-generating-sparkline-to-avoid-rendering-errors.cs @@ -0,0 +1,45 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +class SparklineNullHandlingDemo +{ + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate the source range with some values and nulls (empty cells) + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(null); // empty cell + sheet.Cells["C1"].PutValue(1); + sheet.Cells["D1"].PutValue(null); // empty cell + sheet.Cells["E1"].PutValue(4); + + // Define where the sparkline will be placed (cell F1) + CellArea sparklineLocation = new CellArea + { + StartRow = 0, + EndRow = 0, + StartColumn = 5, + EndColumn = 5 + }; + + // Add a sparkline group for the range A1:E1 and place it in F1 + int groupIndex = sheet.SparklineGroups.Add( + SparklineType.Line, // type of sparkline + "A1:E1", // data range containing nulls + false, // plot by row (horizontal) + sparklineLocation); // location range + + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Configure how empty cells are handled to avoid rendering errors. + // Options: NotPlotted (gap), Zero (display as 0), Interpolated (estimate value) + group.PlotEmptyCellsType = PlotEmptyCellsType.Zero; + + // Save the workbook + workbook.Save("SparklineNullHandlingDemo.xlsx"); + } +} \ No newline at end of file diff --git a/sparkline/insert-a-winloss-sparkline-at-cell-e2-referencing-data-in-f2f12-range.cs b/sparkline/insert-a-winloss-sparkline-at-cell-e2-referencing-data-in-f2f12-range.cs new file mode 100644 index 0000000000..e17bf80475 --- /dev/null +++ b/sparkline/insert-a-winloss-sparkline-at-cell-e2-referencing-data-in-f2f12-range.cs @@ -0,0 +1,45 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +class Program +{ + static void Main() + { + try + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample win/loss data in the range F2:F12 + for (int i = 0; i < 11; i++) + { + // Alternating positive (win) and negative (loss) values + sheet.Cells[1 + i, 5].PutValue(i % 2 == 0 ? 1 : -1); + } + + // Define the location range for the sparkline (cells E2:E12) + CellArea location = new CellArea + { + StartRow = 1, // Row 2 (0‑based index) + EndRow = 11, // Row 12 + StartColumn = 4, // Column E (0‑based index) + EndColumn = 4 + }; + + // Add a Win/Loss sparkline (Stacked) that references F2:F12 + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Stacked, "F2:F12", false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // (Optional) Customize the sparkline group here, e.g., colors, line weight, etc. + + // Save the workbook + workbook.Save("WinLossSparkline.xlsx", SaveFormat.Xlsx); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/sparkline/iterate-through-each-sparkline-collect-average-values-and-store-results-in-a-list.cs b/sparkline/iterate-through-each-sparkline-collect-average-values-and-store-results-in-a-list.cs new file mode 100644 index 0000000000..07c224bda9 --- /dev/null +++ b/sparkline/iterate-through-each-sparkline-collect-average-values-and-store-results-in-a-list.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Aspose.Cells; +using Aspose.Cells.Charts; + +class Program +{ + static void Main() + { + try + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet worksheet = workbook.Worksheets[0]; + + // Populate sample data (two rows of values) + worksheet.Cells["A1"].PutValue(5); + worksheet.Cells["B1"].PutValue(2); + worksheet.Cells["C1"].PutValue(1); + worksheet.Cells["D1"].PutValue(3); + worksheet.Cells["A2"].PutValue(7); + worksheet.Cells["B2"].PutValue(4); + worksheet.Cells["C2"].PutValue(6); + worksheet.Cells["D2"].PutValue(8); + + // Define where the sparklines will be placed + CellArea location = new CellArea + { + StartRow = 0, + EndRow = 1, + StartColumn = 4, + EndColumn = 4 + }; + + // Add a sparkline group that uses the data range A1:D2 + int groupIndex = worksheet.SparklineGroups.Add(SparklineType.Line, "A1:D2", false, location); + SparklineGroup sparklineGroup = worksheet.SparklineGroups[groupIndex]; + + // List to hold the average of each sparkline's data range + List sparklineAverages = new List(); + + // Iterate through each sparkline in all groups + foreach (SparklineGroup group in worksheet.SparklineGroups) + { + foreach (Sparkline sparkline in group.Sparklines) + { + // Obtain the range referenced by the sparkline + Aspose.Cells.Range dataRange = worksheet.Cells.CreateRange(sparkline.DataRange); + + double sum = 0; + int count = 0; + + // Accumulate numeric values + foreach (Cell cell in dataRange) + { + if (cell.Value != null && double.TryParse(cell.Value.ToString(), out double val)) + { + sum += val; + count++; + } + } + + // Compute average (avoid division by zero) + double average = count > 0 ? sum / count : 0; + sparklineAverages.Add(average); + } + } + + // Output the collected averages + Console.WriteLine("Average values of each sparkline:"); + foreach (double avg in sparklineAverages) + { + Console.WriteLine(avg); + } + + // Save the workbook (ensure directory exists) + string outputPath = "SparklinesWithAverages.xlsx"; + try + { + workbook.Save(outputPath); + Console.WriteLine($"Workbook saved to '{Path.GetFullPath(outputPath)}'."); + } + catch (Exception ex) + { + Console.WriteLine($"Failed to save workbook: {ex.Message}"); + } + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/sparkline/load-an-existing-workbook-and-import-asposecellscharts-namespace-for-sparkline-operations.cs b/sparkline/load-an-existing-workbook-and-import-asposecellscharts-namespace-for-sparkline-operations.cs new file mode 100644 index 0000000000..0ec9445ffd --- /dev/null +++ b/sparkline/load-an-existing-workbook-and-import-asposecellscharts-namespace-for-sparkline-operations.cs @@ -0,0 +1,33 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +class Program +{ + static void Main() + { + // Path to the existing Excel file + string inputPath = "input.xlsx"; + + // Load the workbook from the file + Workbook workbook = new Workbook(inputPath); + + // Get the first worksheet + Worksheet sheet = workbook.Worksheets[0]; + + // Example sparkline operation: + // Define where the sparkline will be placed (cell F1) + CellArea location = CellArea.CreateCellArea("F1", "F1"); + + // Add a line‑type sparkline group using data from A1:D1 + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A1:D1", false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Optional customizations + group.ShowHighPoint = true; + group.ShowLowPoint = true; + + // Save the modified workbook + workbook.Save("output.xlsx"); + } +} \ No newline at end of file diff --git a/sparkline/move-a-sparkline-from-original-location-to-new-cell-range-by-removing-and-readding-programmatically.cs b/sparkline/move-a-sparkline-from-original-location-to-new-cell-range-by-removing-and-readding-programmatically.cs new file mode 100644 index 0000000000..b75ed92054 --- /dev/null +++ b/sparkline/move-a-sparkline-from-original-location-to-new-cell-range-by-removing-and-readding-programmatically.cs @@ -0,0 +1,57 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +namespace SparklineMoveDemo +{ + class Program + { + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate some sample data for the sparkline (A1:D1) + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(2); + sheet.Cells["C1"].PutValue(1); + sheet.Cells["D1"].PutValue(3); + + // Define the original location of the sparkline (cell E1) + CellArea originalLocation = new CellArea + { + StartRow = 0, + EndRow = 0, + StartColumn = 4, // Column E (0‑based index) + EndColumn = 4 + }; + + // Add a sparkline group with the original location + int groupIdx = sheet.SparklineGroups.Add(SparklineType.Line, "A1:D1", false, originalLocation); + SparklineGroup group = sheet.SparklineGroups[groupIdx]; + + // At this point the group already contains one sparkline at E1. + // Retrieve its index (first sparkline) + int sparklineIdx = 0; // only one sparkline exists + + // ----- Move the sparkline ----- + // 1. Remove the existing sparkline + group.Sparklines.RemoveAt(sparklineIdx); + + // 2. Define the new location (cell G1) + int newRow = 0; // same row + int newColumn = 6; // Column G (0‑based index) + + // 3. Re‑add the sparkline at the new location + group.Sparklines.Add("A1:D1", newRow, newColumn); + + // Optional: verify the new sparkline position + Sparkline movedSparkline = group.Sparklines[0]; + Console.WriteLine($"Sparkline moved to Row: {movedSparkline.Row}, Column: {movedSparkline.Column}"); + + // Save the workbook + workbook.Save("SparklineMoved.xlsx"); + } + } +} \ No newline at end of file diff --git a/sparkline/read-the-value-of-the-third-sparkline-in-the-second-sparkline-group-and-log-it.cs b/sparkline/read-the-value-of-the-third-sparkline-in-the-second-sparkline-group-and-log-it.cs new file mode 100644 index 0000000000..a019c38cca --- /dev/null +++ b/sparkline/read-the-value-of-the-third-sparkline-in-the-second-sparkline-group-and-log-it.cs @@ -0,0 +1,52 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +class Program +{ + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook wb = new Workbook(); + Worksheet ws = wb.Worksheets[0]; + + // Populate data for two rows (each row will be a sparkline group) + ws.Cells["A1"].PutValue(5); + ws.Cells["B1"].PutValue(3); + ws.Cells["C1"].PutValue(7); + ws.Cells["D1"].PutValue(2); + + ws.Cells["A2"].PutValue(4); + ws.Cells["B2"].PutValue(6); + ws.Cells["C2"].PutValue(1); + ws.Cells["D2"].PutValue(8); + + // Define locations where the sparklines will be placed + CellArea locGroup1 = CellArea.CreateCellArea("E1", "E1"); + CellArea locGroup2 = CellArea.CreateCellArea("E2", "E2"); + + // Add first sparkline group (row 1) and create three sparklines + int idxGroup1 = ws.SparklineGroups.Add(SparklineType.Line, "A1:D1", false, locGroup1); + SparklineGroup group1 = ws.SparklineGroups[idxGroup1]; + // The group already contains one sparkline at E1; add two more at F1 and G1 + group1.Sparklines.Add(ws.Name + "!A1:D1", 0, 5); // column F (index 5) + group1.Sparklines.Add(ws.Name + "!A1:D1", 0, 6); // column G (index 6) + + // Add second sparkline group (row 2) and create three sparklines + int idxGroup2 = ws.SparklineGroups.Add(SparklineType.Line, "A2:D2", false, locGroup2); + SparklineGroup group2 = ws.SparklineGroups[idxGroup2]; + // The group already contains one sparkline at E2; add two more at F2 and G2 + group2.Sparklines.Add(ws.Name + "!A2:D2", 1, 5); // column F (index 5) + group2.Sparklines.Add(ws.Name + "!A2:D2", 1, 6); // column G (index 6) + + // Access the third sparkline (index 2) in the second sparkline group + Sparkline thirdSparkline = group2.Sparklines[2]; + string dataRange = thirdSparkline.DataRange; + + // Log the data range + Console.WriteLine("Third sparkline in second group DataRange: " + dataRange); + + // Save the workbook (optional) + wb.Save("SparklinesDemo.xlsx", SaveFormat.Xlsx); + } +} \ No newline at end of file diff --git a/sparkline/remove-all-sparklines-from-active-worksheet-by-invoking-clear-method-on-each-sparklinegroup.cs b/sparkline/remove-all-sparklines-from-active-worksheet-by-invoking-clear-method-on-each-sparklinegroup.cs new file mode 100644 index 0000000000..7285760a7a --- /dev/null +++ b/sparkline/remove-all-sparklines-from-active-worksheet-by-invoking-clear-method-on-each-sparklinegroup.cs @@ -0,0 +1,36 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +class RemoveAllSparklines +{ + static void Main() + { + // Create a new workbook (or load an existing one) + Workbook workbook = new Workbook(); + + // Access the first worksheet (active worksheet) + Worksheet sheet = workbook.Worksheets[0]; + + // Example data and sparkline creation (optional, for demonstration) + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(2); + sheet.Cells["C1"].PutValue(1); + sheet.Cells["D1"].PutValue(3); + CellArea sparklineLocation = new CellArea { StartColumn = 4, EndColumn = 4, StartRow = 0, EndRow = 0 }; + int groupIdx = sheet.SparklineGroups.Add(SparklineType.Line, "A1:D1", false, sparklineLocation); + SparklineGroup group = sheet.SparklineGroups[groupIdx]; + group.Sparklines.Add(sheet.Name + "!A1:D1", 0, 4); + + // ----- Core logic: remove all sparklines from the active worksheet ----- + // Iterate through each SparklineGroup in the worksheet and clear its Sparklines collection. + foreach (SparklineGroup sg in sheet.SparklineGroups) + { + // Clear all sparklines within the current group. + sg.Sparklines.Clear(); + } + + // Save the workbook to verify that sparklines have been removed. + workbook.Save("RemovedAllSparklines.xlsx", SaveFormat.Xlsx); + } +} \ No newline at end of file diff --git a/sparkline/render-a-sparkline-to-a-memory-stream-for-embedding-into-a-pdf-report-later.cs b/sparkline/render-a-sparkline-to-a-memory-stream-for-embedding-into-a-pdf-report-later.cs new file mode 100644 index 0000000000..e79f71e7ab --- /dev/null +++ b/sparkline/render-a-sparkline-to-a-memory-stream-for-embedding-into-a-pdf-report-later.cs @@ -0,0 +1,87 @@ +using System; +using System.IO; +using Aspose.Cells; +using Aspose.Cells.Charts; +using Aspose.Cells.Rendering; + +namespace AsposeCellsDemo +{ + public class SparklineToStreamExample + { + // Renders a sparkline to a memory stream (PNG) for later embedding into a PDF. + public static MemoryStream RenderSparklineToStream() + { + try + { + // Create a new workbook and get the first worksheet. + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data that the sparkline will represent. + sheet.Cells["A1"].PutValue(10); + sheet.Cells["B1"].PutValue(20); + sheet.Cells["C1"].PutValue(15); + sheet.Cells["D1"].PutValue(30); + + // Define the cell where the sparkline will be placed. + CellArea location = new CellArea + { + StartRow = 0, + EndRow = 0, + StartColumn = 4, + EndColumn = 4 + }; + + // Add a sparkline group (Line type) and retrieve the first sparkline. + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, sheet.Name + "!A1:D1", false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + Sparkline sparkline = group.Sparklines[0]; + + // Configure image options for the output image. + ImageOrPrintOptions options = new ImageOrPrintOptions + { + ImageType = Aspose.Cells.Drawing.ImageType.Png, + HorizontalResolution = 300, + VerticalResolution = 300, + Transparent = false + }; + + // Render the sparkline into a memory stream. + MemoryStream stream = new MemoryStream(); + sparkline.ToImage(stream, options); + stream.Position = 0; // Reset stream position for subsequent reading. + + return stream; + } + catch (Exception ex) + { + Console.Error.WriteLine($"Error rendering sparkline: {ex.Message}"); + throw; + } + } + } + + public class Program + { + public static void Main() + { + try + { + using (MemoryStream sparklineStream = SparklineToStreamExample.RenderSparklineToStream()) + { + // Save the stream to a file to verify the output. + string outputPath = "sparkline.png"; + using (FileStream file = new FileStream(outputPath, FileMode.Create, FileAccess.Write)) + { + sparklineStream.CopyTo(file); + } + Console.WriteLine($"Sparkline image saved to {Path.GetFullPath(outputPath)}"); + } + } + catch (Exception ex) + { + Console.Error.WriteLine($"Unhandled exception: {ex.Message}"); + } + } + } +} \ No newline at end of file diff --git a/sparkline/save-the-modified-workbook-with-new-sparklines-to-an-xlsx-file-at-specified-path.cs b/sparkline/save-the-modified-workbook-with-new-sparklines-to-an-xlsx-file-at-specified-path.cs new file mode 100644 index 0000000000..328bc28f46 --- /dev/null +++ b/sparkline/save-the-modified-workbook-with-new-sparklines-to-an-xlsx-file-at-specified-path.cs @@ -0,0 +1,44 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +class SparklineSaveDemo +{ + static void Main() + { + // Path where the workbook with sparklines will be saved + string outputPath = @"C:\Temp\SparklinesDemo.xlsx"; + + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data for the sparkline (row 1, columns A‑D) + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(2); + sheet.Cells["C1"].PutValue(1); + sheet.Cells["D1"].PutValue(3); + + // Define the cell where the sparkline will be displayed (E1) + CellArea sparklineLocation = new CellArea + { + StartRow = 0, + EndRow = 0, + StartColumn = 4, // Column E (0‑based index) + EndColumn = 4 + }; + + // Add a sparkline group of type Line that uses the data range A1:D1 + int groupIndex = sheet.SparklineGroups.Add( + SparklineType.Line, + $"{sheet.Name}!A1:D1", + false, + sparklineLocation); + + // Retrieve the created group (optional: modify its properties here) + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Save the workbook to the specified path in XLSX format + workbook.Save(outputPath, SaveFormat.Xlsx); + } +} \ No newline at end of file diff --git a/sparkline/serialize-sparkline-group-settings-to-json-string-for-later-reuse-or-configuration-sharing.cs b/sparkline/serialize-sparkline-group-settings-to-json-string-for-later-reuse-or-configuration-sharing.cs new file mode 100644 index 0000000000..811d485e1d --- /dev/null +++ b/sparkline/serialize-sparkline-group-settings-to-json-string-for-later-reuse-or-configuration-sharing.cs @@ -0,0 +1,62 @@ +using System; +using System.Drawing; +using System.IO; +using System.Text; +using Aspose.Cells; +using Aspose.Cells.Charts; +using Aspose.Cells.Json; + +class SparklineGroupJsonSerialization +{ + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data for the sparkline + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(2); + sheet.Cells["C1"].PutValue(1); + sheet.Cells["D1"].PutValue(3); + + // Define the location where the sparkline will be placed + CellArea location = new CellArea + { + StartRow = 0, + EndRow = 0, + StartColumn = 4, + EndColumn = 4 + }; + + // Add a sparkline group (Line type) with the data range and location + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A1:D1", false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Add a sparkline to the group + group.Sparklines.Add(sheet.Name + "!A1:D1", 0, 4); + + // Configure some visual settings of the sparkline group + CellsColor seriesColor = workbook.CreateCellsColor(); + seriesColor.Color = Color.Orange; + group.SeriesColor = seriesColor; + group.ShowMarkers = true; + group.LineWeight = 2.0; + group.PresetStyle = SparklinePresetStyleType.Style3; + + // Prepare JSON save options + JsonSaveOptions jsonOptions = new JsonSaveOptions(); + + // Save the workbook (including sparkline settings) to a memory stream as JSON + using (MemoryStream ms = new MemoryStream()) + { + workbook.Save(ms, jsonOptions); + + // Convert the JSON bytes to a string for reuse or sharing + string jsonString = Encoding.UTF8.GetString(ms.ToArray()); + + // Output the JSON string (could be stored, transmitted, etc.) + Console.WriteLine(jsonString); + } + } +} \ No newline at end of file diff --git a/sparkline/set-sparkline-3d-depth-property-to-higher-value-to-enhance-visual-depth-perception.cs b/sparkline/set-sparkline-3d-depth-property-to-higher-value-to-enhance-visual-depth-perception.cs new file mode 100644 index 0000000000..5a8c729eee --- /dev/null +++ b/sparkline/set-sparkline-3d-depth-property-to-higher-value-to-enhance-visual-depth-perception.cs @@ -0,0 +1,66 @@ +using System; +using System.IO; +using Aspose.Cells; +using Aspose.Cells.Charts; + +namespace AsposeCellsExamples +{ + public class Sparkline3DDepthDemo + { + public static void Run() + { + try + { + // Create a new workbook + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data for a 3‑D chart + sheet.Cells["A1"].PutValue("Category"); + sheet.Cells["A2"].PutValue("Q1"); + sheet.Cells["A3"].PutValue("Q2"); + sheet.Cells["A4"].PutValue("Q3"); + sheet.Cells["B1"].PutValue("Series1"); + sheet.Cells["B2"].PutValue(120); + sheet.Cells["B3"].PutValue(150); + sheet.Cells["B4"].PutValue(180); + sheet.Cells["C1"].PutValue("Series2"); + sheet.Cells["C2"].PutValue(90); + sheet.Cells["C3"].PutValue(110); + sheet.Cells["C4"].PutValue(130); + + // Add a 3‑D column chart + int chartIdx = sheet.Charts.Add(ChartType.Column3D, 5, 0, 20, 8); + Chart chart = sheet.Charts[chartIdx]; + + // Set data range for the chart + chart.NSeries.Add("B2:C4", true); + chart.NSeries.CategoryData = "A2:A4"; + + // Increase the depth of the 3‑D chart + chart.DepthPercent = 300; // 20‑2000 range + + // Optional 3‑D visual adjustments + chart.Perspective = 40; // 0‑100 + chart.Elevation = 25; // degrees + + // Save the workbook + string outputPath = "Sparkline3DDepthDemo.xlsx"; + workbook.Save(outputPath, SaveFormat.Xlsx); + Console.WriteLine($"Workbook saved to '{Path.GetFullPath(outputPath)}'."); + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred: {ex.Message}"); + } + } + } + + public class Program + { + public static void Main(string[] args) + { + Sparkline3DDepthDemo.Run(); + } + } +} \ No newline at end of file diff --git a/sparkline/set-sparkline-group-orientation-to-vertical-for-columntype-sparklines-to-improve-readability.cs b/sparkline/set-sparkline-group-orientation-to-vertical-for-columntype-sparklines-to-improve-readability.cs new file mode 100644 index 0000000000..f65ffa4449 --- /dev/null +++ b/sparkline/set-sparkline-group-orientation-to-vertical-for-columntype-sparklines-to-improve-readability.cs @@ -0,0 +1,45 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +class SetSparklineOrientationVertical +{ + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data for the column‑type sparkline (by column) + sheet.Cells["A1"].PutValue(5); + sheet.Cells["A2"].PutValue(3); + sheet.Cells["A3"].PutValue(7); + sheet.Cells["A4"].PutValue(2); + sheet.Cells["A5"].PutValue(9); + + // Define where the sparkline(s) will be placed (e.g., cell B1) + CellArea location = new CellArea + { + StartRow = 0, // row 1 (zero‑based) + EndRow = 0, + StartColumn = 1, // column B (zero‑based) + EndColumn = 1 + }; + + // Add a column‑type sparkline group with vertical orientation (isVertical = true) + int groupIndex = sheet.SparklineGroups.Add( + SparklineType.Column, // column sparkline + "A1:A5", // data range + true, // plot by column (vertical orientation) + location); // location range + + // Retrieve the created group (optional, for further customization) + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Add the sparkline to the group (the Add method of Sparklines collection) + group.Sparklines.Add(sheet.Name + "!A1:A5", 0, 1); + + // Save the workbook + workbook.Save("SparklineVerticalOrientation.xlsx"); + } +} \ No newline at end of file diff --git a/sparkline/set-sparkline-group-to-display-markers-for-both-high-and-low-points-simultaneously.cs b/sparkline/set-sparkline-group-to-display-markers-for-both-high-and-low-points-simultaneously.cs new file mode 100644 index 0000000000..eba3ebcf93 --- /dev/null +++ b/sparkline/set-sparkline-group-to-display-markers-for-both-high-and-low-points-simultaneously.cs @@ -0,0 +1,74 @@ +using System; +using System.Drawing; +using System.IO; +using Aspose.Cells; +using Aspose.Cells.Charts; + +namespace AsposeCellsExamples +{ + public class SparklineHighLowMarkersDemo + { + public static void Run() + { + try + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data for the sparkline + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(2); + sheet.Cells["C1"].PutValue(8); + sheet.Cells["D1"].PutValue(3); + + // Define the location where the sparkline will be placed (E1) + CellArea location = new CellArea + { + StartColumn = 4, // Column E (0‑based index) + EndColumn = 4, + StartRow = 0, // Row 1 + EndRow = 0 + }; + + // Add a line sparkline group with the data range A1:D1 + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A1:D1", false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Add the sparkline to the group (optional, already added by Add method) + group.Sparklines.Add(sheet.Name + "!A1:D1", 0, 4); + + // Enable markers for both high and low points + group.ShowHighPoint = true; + group.ShowLowPoint = true; + + // Set colors for the high and low point markers + CellsColor highColor = workbook.CreateCellsColor(); + highColor.Color = Color.Green; + group.HighPointColor = highColor; + + CellsColor lowColor = workbook.CreateCellsColor(); + lowColor.Color = Color.Red; + group.LowPointColor = lowColor; + + // Save the workbook + string outputPath = "SparklineHighLowMarkers.xlsx"; + workbook.Save(outputPath); + Console.WriteLine($"Workbook saved to {Path.GetFullPath(outputPath)}"); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } + } + + // Application entry point + public class Program + { + public static void Main(string[] args) + { + SparklineHighLowMarkersDemo.Run(); + } + } +} \ No newline at end of file diff --git a/sparkline/set-sparkline-markers-to-display-high-points-using-red-color-for-sparkline-in-h3.cs b/sparkline/set-sparkline-markers-to-display-high-points-using-red-color-for-sparkline-in-h3.cs new file mode 100644 index 0000000000..ae3764d053 --- /dev/null +++ b/sparkline/set-sparkline-markers-to-display-high-points-using-red-color-for-sparkline-in-h3.cs @@ -0,0 +1,48 @@ +using System.Drawing; +using Aspose.Cells; +using Aspose.Cells.Charts; + +class SparklineHighPointMarker +{ + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Sample data for the sparkline (adjust range as needed) + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(2); + sheet.Cells["C1"].PutValue(8); + sheet.Cells["D1"].PutValue(3); + + // Define the location of the sparkline (cell H3) + CellArea sparklineArea = new CellArea + { + StartColumn = 7, // Column H (0‑based index) + EndColumn = 7, + StartRow = 2, // Row 3 (0‑based index) + EndRow = 2 + }; + + // Add a line sparkline group that uses the data range A1:D1 and places the sparkline in H3 + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A1:D1", false, sparklineArea); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Add the sparkline to the group (required step) + group.Sparklines.Add(sheet.Name + "!A1:D1", 2, 7); + + // Enable high‑point markers and set their color to red + group.ShowHighPoint = true; + CellsColor redColor = workbook.CreateCellsColor(); + redColor.Color = Color.Red; + group.HighPointColor = redColor; + + // (Optional) Show markers for all points; they will inherit the high‑point color for high values + group.ShowMarkers = true; + group.MarkersColor = redColor; + + // Save the workbook + workbook.Save("SparklineHighPointMarker.xlsx", SaveFormat.Xlsx); + } +} \ No newline at end of file diff --git a/sparkline/set-sparkline-to-show-negative-points-using-custom-blue-color-in-cell-j6.cs b/sparkline/set-sparkline-to-show-negative-points-using-custom-blue-color-in-cell-j6.cs new file mode 100644 index 0000000000..399e183caf --- /dev/null +++ b/sparkline/set-sparkline-to-show-negative-points-using-custom-blue-color-in-cell-j6.cs @@ -0,0 +1,50 @@ +using System; +using System.Drawing; +using Aspose.Cells; +using Aspose.Cells.Charts; + +namespace SparklineNegativePointsDemo +{ + class Program + { + static void Main() + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Sample data with negative values (row 6) + sheet.Cells["A6"].PutValue(5); + sheet.Cells["B6"].PutValue(-2); + sheet.Cells["C6"].PutValue(3); + sheet.Cells["D6"].PutValue(-4); + + // Define the location area for the sparkline (cell J6) + CellArea location = new CellArea + { + StartColumn = 9, // Column J (0‑based index) + EndColumn = 9, + StartRow = 5, // Row 6 (0‑based index) + EndRow = 5 + }; + + // Add a sparkline group of type Line, using the data range A6:D6 + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A6:D6", false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Add the sparkline to the group (the same range as above) + group.Sparklines.Add(sheet.Name + "!A6:D6", 5, 9); + + // Enable highlighting of negative points + group.ShowNegativePoints = true; + + // Set custom blue color for negative points + CellsColor blueColor = workbook.CreateCellsColor(); + blueColor.Color = Color.Blue; + group.NegativePointsColor = blueColor; + + // Save the workbook + workbook.Save("SparklineNegativePointsBlue.xlsx"); + } + } +} \ No newline at end of file diff --git a/sparkline/validate-that-sparkline-data-range-does-not-contain-errors-before-creating-the-sparkline-object.cs b/sparkline/validate-that-sparkline-data-range-does-not-contain-errors-before-creating-the-sparkline-object.cs new file mode 100644 index 0000000000..aea91c3a5b --- /dev/null +++ b/sparkline/validate-that-sparkline-data-range-does-not-contain-errors-before-creating-the-sparkline-object.cs @@ -0,0 +1,70 @@ +using System; +using System.IO; +using Aspose.Cells; +using Aspose.Cells.Charts; + +class SparklineValidationDemo +{ + static void Main() + { + try + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data, including a formula that will cause an error (#DIV/0!) + sheet.Cells["A1"].PutValue(5); + sheet.Cells["B1"].PutValue(0); + sheet.Cells["C1"].Formula = "=A1/B1"; // division by zero error + sheet.Cells["D1"].PutValue(3); + + // Define the data range for the sparkline + string dataRange = "A1:D1"; + + // Recalculate formulas so that error values are materialized + workbook.CalculateFormula(); + + // Validate that the data range does not contain any error cells + bool hasError = false; + string[] rangeParts = dataRange.Split(':'); + CellArea rangeArea = CellArea.CreateCellArea(rangeParts[0], rangeParts[1]); + + for (int row = rangeArea.StartRow; row <= rangeArea.EndRow && !hasError; row++) + { + for (int col = rangeArea.StartColumn; col <= rangeArea.EndColumn; col++) + { + Cell cell = sheet.Cells[row, col]; + if (cell.Type == CellValueType.IsError) + { + hasError = true; + Console.WriteLine($"Error detected in cell {cell.Name}"); + break; + } + } + } + + if (!hasError) + { + // Data range is clean – create the sparkline group + CellArea sparklineLocation = CellArea.CreateCellArea("E1", "E1"); + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, dataRange, false, sparklineLocation); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + Console.WriteLine("Sparkline created successfully."); + } + else + { + Console.WriteLine("Sparkline creation skipped due to errors in the data range."); + } + + // Save the workbook + string outputPath = "SparklineValidationDemo.xlsx"; + workbook.Save(outputPath); + Console.WriteLine($"Workbook saved to {Path.GetFullPath(outputPath)}"); + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/sparkline/write-a-unit-test-verifying-that-adding-a-sparkline-increases-sparkline-count-by-exactly-one.cs b/sparkline/write-a-unit-test-verifying-that-adding-a-sparkline-increases-sparkline-count-by-exactly-one.cs new file mode 100644 index 0000000000..0b78545ab3 --- /dev/null +++ b/sparkline/write-a-unit-test-verifying-that-adding-a-sparkline-increases-sparkline-count-by-exactly-one.cs @@ -0,0 +1,69 @@ +using System; +using Aspose.Cells; +using Aspose.Cells.Charts; + +namespace AsposeCellsTests +{ + public class SparklineTests + { + public void AddingSparkline_IncreasesCountByOne() + { + try + { + // Create a new workbook and get the first worksheet + Workbook workbook = new Workbook(); + Worksheet sheet = workbook.Worksheets[0]; + + // Populate sample data for the sparkline + sheet.Cells["A1"].PutValue(1); + sheet.Cells["B1"].PutValue(2); + sheet.Cells["C1"].PutValue(3); + sheet.Cells["D1"].PutValue(4); + + // Define the cell where the sparkline will be placed (E1) + CellArea location = new CellArea + { + StartRow = 0, + EndRow = 0, + StartColumn = 4, + EndColumn = 4 + }; + + // Add a sparkline group to the worksheet + int groupIndex = sheet.SparklineGroups.Add(SparklineType.Line, "A1:D1", false, location); + SparklineGroup group = sheet.SparklineGroups[groupIndex]; + + // Get the sparkline collection from the group + SparklineCollection sparklines = group.Sparklines; + + // Record the initial count (should be zero) + int initialCount = sparklines.Count; + + // Add a sparkline to the collection + sparklines.Add("A1:D1", 0, 4); + + // Verify that the count increased by exactly one + if (sparklines.Count != initialCount + 1) + { + throw new InvalidOperationException( + $"Expected sparkline count {initialCount + 1}, but got {sparklines.Count}."); + } + + Console.WriteLine("Test passed: Sparkline count increased by one."); + } + catch (Exception ex) + { + Console.WriteLine($"Error during test execution: {ex.Message}"); + } + } + } + + class Program + { + static void Main() + { + var test = new SparklineTests(); + test.AddingSparkline_IncreasesCountByOne(); + } + } +} \ No newline at end of file