|
| 1 | +--- |
| 2 | +title: Generating Stacked Line Charts and Configuring Axes in Excel Using FloatingChartShape |
| 3 | +description: Learn how to generate stacked line dharts and configure axes in Excel spreadsheets using Telerik Document Processing's SpreadProcessing library. |
| 4 | +type: how-to |
| 5 | +page_title: Creating Stacked Line Charts and Setting Axes in Excel with the SpreadProcessing library |
| 6 | +meta_title: Creating Stacked Line Charts and Setting Axes in Excel with the SpreadProcessing library |
| 7 | +slug: generating-stacked-line-charts-configuring-axes-excel-floatingchartshape |
| 8 | +tags: spread, processing, telerik, document, processing, floating, chart, shape, line, graph, stacked, line, chart, x, axis, y |
| 9 | +res_type: kb |
| 10 | +ticketid: 1695510 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +| Version | Product | Author | |
| 16 | +| --- | --- | ---- | |
| 17 | +| 2025.3.806 | SpreadProcessing |[Yoan Karamanov](https://www.telerik.com/blogs/author/yoan-karamanov)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +This article demonstrates how to generate stacked line charts in worksheets using the SpreadProcessing library. It also covers how to specify the data sources for the X-axis (dates) and Y-axis (numerical values). Additionally, it explains how to configure chart properties such as marker types, line styles, and visibility to customize the chart's appearance. |
| 22 | + |
| 23 | +Key topics addressed in this guide include: |
| 24 | +- Creating line and stacked line charts in Excel using Telerik Document Processing. |
| 25 | +- Assigning specific data ranges to the X-axis (e.g., dates) and Y-axis (numerical values). |
| 26 | +- Customizing chart properties, including marker types for each series, line styles, and removing markers when needed. |
| 27 | +- Plotting multiple columns of data while ensuring correct alignment between axes. |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +## Solution |
| 32 | + |
| 33 | +To create a stacked line chart and configure the axes as desired, follow these steps: |
| 34 | + |
| 35 | +1. Define the `LineSeriesGroup` and set its grouping to `Stacked`. This ensures the chart is created as a stacked line chart. |
| 36 | +2. Create individual `[LineSeries]({%slug radspreadprocessing-features-charts-series%}#lineseries)` for each data column. Set the `Values` to represent numerical data and `Categories` to represent dates. |
| 37 | +3. Define the axes for the chart. Set the `CategoryAxis` to plot dates and the `ValueAxis` for numerical values. |
| 38 | +4. Create the chart and associate the `LineSeriesGroup` and axes with it. |
| 39 | +5. Replace the default chart in the `[FloatingChartShape]({%slug radspreadprocessing-features-charts-using-charts%}#floatingchartshape)` with the configured document chart`. Set the dimensions and add it to the worksheet. |
| 40 | + |
| 41 | +```csharp |
| 42 | +var fileBytes = File.ReadAllBytes("fileWithChartData.xlsx"); |
| 43 | + |
| 44 | +Workbook workbook = xlsxFormatProvider.Import(fileBytes, null); |
| 45 | +Worksheet worksheet = workbook.ActiveWorksheet; |
| 46 | + |
| 47 | +LineSeriesGroup seriesGroup = new LineSeriesGroup(); |
| 48 | +seriesGroup.Grouping = SeriesGrouping.Stacked; |
| 49 | + |
| 50 | +// Make the series one by one |
| 51 | +LineSeries lineSeries1 = new LineSeries(); |
| 52 | +lineSeries1.Values = new WorkbookFormulaChartData(worksheet, new CellRange(1, 1, 10, 1)); |
| 53 | +lineSeries1.Categories = new WorkbookFormulaChartData(worksheet, new CellRange(1, 0, 10, 0)); |
| 54 | +lineSeries1.Marker = new Marker(); |
| 55 | +lineSeries1.Marker.Symbol = MarkerStyle.Circle; |
| 56 | +seriesGroup.Series.Add(lineSeries1); |
| 57 | + |
| 58 | +LineSeries lineSeries2 = new LineSeries(); |
| 59 | +lineSeries2.Values = new WorkbookFormulaChartData(worksheet, new CellRange(1, 2, 10, 2)); |
| 60 | +lineSeries2.Categories = new WorkbookFormulaChartData(worksheet, new CellRange(1, 0, 10, 0)); |
| 61 | +lineSeries2.Marker = new Marker(); |
| 62 | +lineSeries2.Marker.Symbol = MarkerStyle.Plus; |
| 63 | +seriesGroup.Series.Add(lineSeries2); |
| 64 | + |
| 65 | +// Some axes |
| 66 | +SolidFill lineColor = new SolidFill(new ThemableColor(ThemeColorType.Background2)); |
| 67 | + |
| 68 | +AxisGroup axisGroup = new AxisGroup(); |
| 69 | +CategoryAxis categoryAxis = new CategoryAxis(); |
| 70 | +categoryAxis.Outline.Fill = lineColor; |
| 71 | +categoryAxis.MajorGridlines.Outline.Fill = lineColor; |
| 72 | +axisGroup.CategoryAxis = categoryAxis; |
| 73 | + |
| 74 | +ValueAxis valueAxis = new ValueAxis(); |
| 75 | +valueAxis.Outline.Fill = lineColor; |
| 76 | +valueAxis.MajorGridlines.Outline.Fill = lineColor; |
| 77 | +axisGroup.ValueAxis = valueAxis; |
| 78 | + |
| 79 | +// Here is the chart itself |
| 80 | +DocumentChart documentChart = new DocumentChart(); |
| 81 | +documentChart.SeriesGroups.Add(seriesGroup); |
| 82 | +documentChart.PrimaryAxes = axisGroup; |
| 83 | + |
| 84 | +// We'll make a dummy FloatingChartShape and we'll replace the inner chart. |
| 85 | +FloatingChartShape floatingChartShape = new FloatingChartShape(worksheet, new CellIndex(0, 5), new CellRange(0, 0, 4, 3), ChartType.Line); |
| 86 | +floatingChartShape.Chart = documentChart; |
| 87 | +floatingChartShape.Width = 400; |
| 88 | +floatingChartShape.Height = 250; |
| 89 | + |
| 90 | +worksheet.Charts.Add(floatingChartShape); |
| 91 | + |
| 92 | +string exportFileName = "fileWithChart.xlsx"; |
| 93 | +using (Stream str = File.OpenWrite(exportFileName)) |
| 94 | +{ |
| 95 | + xlsxFormatProvider.Export(workbook, str, null); |
| 96 | +} |
| 97 | +``` |
| 98 | +By following these steps, you can generate a stacked line chart without markers and configure the axes to display the required data. |
| 99 | + |
| 100 | +## See Also |
| 101 | + |
| 102 | +* [SpreadProcessing Overview]({%slug radspreadprocessing-overview%}) |
| 103 | +* [Charts Overview]({%slug radspreadprocessing-features-charts%}) |
0 commit comments