.NET에서 프레젠테이션 차트 서식 지정
Overview
이 문서는 Aspose.Slides를 사용하여 PowerPoint 프레젠테이션의 차트를 서식 지정하는 방법을 설명합니다. 축, 격자선, 제목, 범례, 플롯 영역 및 벽 채우기와 같은 핵심 차트 요소를 사용자 지정하여 차트 데이터의 외관과 가독성을 향상시키는 방법을 보여줍니다.
또한 차트 텍스트의 글꼴 속성을 설정하고, 차트 데이터에 사전 정의된 숫자 형식을 적용하며, 차트 영역에 둥근 모서리를 활성화하는 방법을 보여줍니다. 이러한 예제는 프레젠테이션에서 차트의 시각적 스타일과 데이터 표시를 모두 제어하는 방법을 보여줍니다.
Format Chart Entities
IChart을 사용하여 차트 제목, 축, 격자선, 범례 및 플롯 영역의 서식을 지정합니다. 이 예제는 마커와 기본 데이터를 가진 라인 차트를 생성한 다음 값 축과 카테고리 축에 별도의 텍스트 및 선 스타일을 적용합니다. 글꼴 크기와 선 두께는 포인트 단위로 지정되며, 카테고리 레이블은 45도 회전됩니다.
값 축은 NumberFormat을 소스 셀과 무관하게 사용합니다. 스케일은 -2에서 15까지이며, 주요 간격은 2, 부간격은 0.5입니다. 이 예제는 또한 백분율 서식을 DisplayUnit을 천 단위로 설정한 것과 결합하므로, 이러한 설정을 데이터의 의미와 규모에 맞게 조정하십시오. 범례는 Overlay가 비활성화되어 있기 때문에 플롯 영역 밖에 유지됩니다. Back-wall와 Floor 채우기는 BackWall와 Floor를 통해 구성되지만, 이러한 표면은 3D 차트에서만 표시됩니다.
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.LineWithMarkers, 50, 50, 500, 400);
chart.HasTitle = true;
chart.ChartTitle.AddTextFrameForOverriding("");
var chartTitle = chart.ChartTitle.TextFrameForOverriding.Paragraphs[0].Portions[0];
chartTitle.Text = "Sample Chart";
chartTitle.PortionFormat.FillFormat.FillType = FillType.Solid;
chartTitle.PortionFormat.FillFormat.SolidFillColor.Color = Color.Gray;
chartTitle.PortionFormat.FontHeight = 20;
chartTitle.PortionFormat.FontBold = NullableBool.True;
chartTitle.PortionFormat.FontItalic = NullableBool.True;
chart.Axes.VerticalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.Solid;
chart.Axes.VerticalAxis.MajorGridLinesFormat.Line.FillFormat.SolidFillColor.Color = Color.Blue;
chart.Axes.VerticalAxis.MajorGridLinesFormat.Line.Width = 5;
chart.Axes.VerticalAxis.MajorGridLinesFormat.Line.DashStyle = LineDashStyle.DashDot;
chart.Axes.VerticalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.Solid;
chart.Axes.VerticalAxis.MinorGridLinesFormat.Line.FillFormat.SolidFillColor.Color = Color.Red;
chart.Axes.VerticalAxis.MinorGridLinesFormat.Line.Width = 3;
chart.Axes.VerticalAxis.IsNumberFormatLinkedToSource = false;
chart.Axes.VerticalAxis.DisplayUnit = DisplayUnitType.Thousands;
chart.Axes.VerticalAxis.NumberFormat = "0.0%";
chart.Axes.VerticalAxis.IsAutomaticMajorUnit = false;
chart.Axes.VerticalAxis.IsAutomaticMaxValue = false;
chart.Axes.VerticalAxis.IsAutomaticMinorUnit = false;
chart.Axes.VerticalAxis.IsAutomaticMinValue = false;
chart.Axes.VerticalAxis.MaxValue = 15f;
chart.Axes.VerticalAxis.MinValue = -2f;
chart.Axes.VerticalAxis.MinorUnit = 0.5f;
chart.Axes.VerticalAxis.MajorUnit = 2.0f;
var valueAxisText = chart.Axes.VerticalAxis.TextFormat.PortionFormat;
valueAxisText.FontBold = NullableBool.True;
valueAxisText.FontHeight = 16;
valueAxisText.FontItalic = NullableBool.True;
valueAxisText.FillFormat.FillType = FillType.Solid;
valueAxisText.FillFormat.SolidFillColor.Color = Color.DarkGreen;
valueAxisText.LatinFont = new FontData("Times New Roman");
chart.Axes.VerticalAxis.HasTitle = true;
chart.Axes.VerticalAxis.Title.AddTextFrameForOverriding("");
var valueAxisTitle = chart.Axes.VerticalAxis.Title.TextFrameForOverriding.Paragraphs[0].Portions[0];
valueAxisTitle.Text = "Primary Axis";
valueAxisTitle.PortionFormat.FillFormat.FillType = FillType.Solid;
valueAxisTitle.PortionFormat.FillFormat.SolidFillColor.Color = Color.Gray;
valueAxisTitle.PortionFormat.FontHeight = 20;
valueAxisTitle.PortionFormat.FontBold = NullableBool.True;
valueAxisTitle.PortionFormat.FontItalic = NullableBool.True;
chart.Axes.HorizontalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.Solid;
chart.Axes.HorizontalAxis.MajorGridLinesFormat.Line.FillFormat.SolidFillColor.Color = Color.Green;
chart.Axes.HorizontalAxis.MajorGridLinesFormat.Line.Width = 5;
chart.Axes.HorizontalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.Solid;
chart.Axes.HorizontalAxis.MinorGridLinesFormat.Line.FillFormat.SolidFillColor.Color = Color.Yellow;
chart.Axes.HorizontalAxis.MinorGridLinesFormat.Line.Width = 3;
var categoryAxisText = chart.Axes.HorizontalAxis.TextFormat.PortionFormat;
categoryAxisText.FontBold = NullableBool.True;
categoryAxisText.FontHeight = 16;
categoryAxisText.FontItalic = NullableBool.True;
categoryAxisText.FillFormat.FillType = FillType.Solid;
categoryAxisText.FillFormat.SolidFillColor.Color = Color.Blue;
categoryAxisText.LatinFont = new FontData("Arial");
chart.Axes.HorizontalAxis.HasTitle = true;
chart.Axes.HorizontalAxis.Title.AddTextFrameForOverriding("");
var categoryAxisTitle = chart.Axes.HorizontalAxis.Title.TextFrameForOverriding.Paragraphs[0].Portions[0];
categoryAxisTitle.Text = "Sample Category";
categoryAxisTitle.PortionFormat.FillFormat.FillType = FillType.Solid;
categoryAxisTitle.PortionFormat.FillFormat.SolidFillColor.Color = Color.Gray;
categoryAxisTitle.PortionFormat.FontHeight = 20;
categoryAxisTitle.PortionFormat.FontBold = NullableBool.True;
categoryAxisTitle.PortionFormat.FontItalic = NullableBool.True;
chart.Axes.HorizontalAxis.TickLabelPosition = TickLabelPositionType.Low;
chart.Axes.HorizontalAxis.TickLabelRotationAngle = 45;
var legendText = chart.Legend.TextFormat.PortionFormat;
legendText.FontBold = NullableBool.True;
legendText.FontHeight = 16;
legendText.FontItalic = NullableBool.True;
legendText.FillFormat.FillType = FillType.Solid;
legendText.FillFormat.SolidFillColor.Color = Color.DarkRed;
chart.Legend.Overlay = false;
chart.BackWall.Thickness = 1;
chart.BackWall.Format.Fill.FillType = FillType.Solid;
chart.BackWall.Format.Fill.SolidFillColor.Color = Color.Orange;
chart.Floor.Format.Fill.FillType = FillType.Solid;
chart.Floor.Format.Fill.SolidFillColor.Color = Color.Red;
chart.PlotArea.Format.Fill.FillType = FillType.Solid;
chart.PlotArea.Format.Fill.SolidFillColor.Color = Color.LightCyan;
presentation.Save("FormattedChart_out.pptx", SaveFormat.Pptx);
Set Font Properties for a Chart
Chart.TextFormat을 통해 차트 텍스트의 기본 글꼴 크기를 설정합니다. 이 예제는 20포인트 글꼴을 가진 클러스터형 열 차트를 생성하고 ShowValue를 사용하여 첫 번째 시리즈에 값 레이블을 활성화합니다. 자체 명시적 글꼴 설정이 있는 텍스트 요소는 차트 기본값을 재정의할 수 있습니다.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 100, 100, 500, 400);
chart.TextFormat.PortionFormat.FontHeight = 20;
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;
presentation.Save("FontPropertiesForChart.pptx", SaveFormat.Pptx);
Set the Numeric Format
IChartDataCell.PresetNumberFormat을 사용하여 차트 워크북 셀에 내장 숫자 형식을 적용합니다. 이 예제는 기본 셀 기반 데이터를 가진 클러스터형 열 차트를 생성하고 각 시리즈 값 셀에 형식 인덱스 10(0.00%)을 적용합니다. 이는 기본 숫자를 변경하지 않고 워크북에 표시되는 값이 변하도록 합니다; 예를 들어 0.25는 25.00%로 표시됩니다. 축 및 데이터 레이블 형식은 별도로 구성할 수 있습니다.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 50, 50, 500, 400);
var series = chart.ChartData.Series;
foreach (var chartSeries in series)
{
foreach (var dataPoint in chartSeries.DataPoints)
{
dataPoint.Value.AsCell.PresetNumberFormat = 10; //0.00%
}
}
presentation.Save("PresetNumberFormat_out.pptx", SaveFormat.Pptx);
다음 표는 영어(United States) 로케일에 대한 대표적인 Excel 형식과 함께 내장 숫자 형식 인덱스를 나열합니다. 정확한 형식 문자열과 표시 방식은 애플리케이션 및 로케일에 따라 다를 수 있으며, 특히 통화와 날짜 형식에서 차이가 있습니다. 일부 Excel 형식은 OOXML 표준의 기본값과도 다르며, 이는 Microsoft’s number format implementation notes에서 설명되어 있습니다.
| 인덱스 | 형식 |
|---|---|
| 0 | General |
| 1 | 0 |
| 2 | 0.00 |
| 3 | #,##0 |
| 4 | #,##0.00 |
| 5 | "$"#,##0_);("$"#,##0) |
| 6 | "$"#,##0_);[Red]("$"#,##0) |
| 7 | "$"#,##0.00_);("$"#,##0.00) |
| 8 | "$"#,##0.00_);[Red]("$"#,##0.00) |
| 9 | 0% |
| 10 | 0.00% |
| 11 | 0.00E+00 |
| 12 | # ?/? |
| 13 | # ??/?? |
| 14 | m/d/yyyy |
| 15 | d-mmm-yy |
| 16 | d-mmm |
| 17 | mmm-yy |
| 18 | h:mm AM/PM |
| 19 | h:mm:ss AM/PM |
| 20 | h:mm |
| 21 | h:mm:ss |
| 22 | m/d/yyyy h:mm |
| 37 | #,##0_);(#,##0) |
| 38 | #,##0_);[Red](#,##0) |
| 39 | #,##0.00_);(#,##0.00) |
| 40 | #,##0.00_);[Red](#,##0.00) |
| 41 | _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_) |
| 42 | _("$"* #,##0_);_("$"* (#,##0);_("$"* "-"_);_(@_) |
| 43 | _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_) |
| 44 | _("$"* #,##0.00_);_("$"* (#,##0.00);_("$"* "-"??_);_(@_) |
| 45 | mm:ss |
| 46 | [h]:mm:ss |
| 47 | mm:ss.0 |
| 48 | ##0.0E+0 |
| 49 | @ |
Set Chart Area Rounded Borders
IChart.HasRoundedCorners를 활성화하여 차트 영역의 모서리를 둥글게 합니다. 이 예제는 클러스터형 열 차트를 생성하고 LineFormat을 사용하여 외곽선에 단색 회색 색상과 단일 선 스타일을 적용한 후 프레젠테이션을 저장합니다.
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 20, 100, 600, 400);
chart.LineFormat.FillFormat.FillType = FillType.Solid;
chart.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
chart.LineFormat.Style = LineStyle.Single;
chart.HasRoundedCorners = true;
presentation.Save("out.pptx", SaveFormat.Pptx);
Edit Existing Chart Annotations
차트에는 보고서 상태를 설명하는 텍스트 주석과 같은 사용자 지정 도형을 포함할 수 있습니다. 이러한 도형은 IChart.UserShapes를 통해 액세스할 수 있으며, 차트 API 참조의 Chart.UserShapes에도 나열됩니다. 그 Shapes 컬렉션은 차트가 소유한 도형을 포함합니다.
차트 위에 배치된 일반 텍스트 상자는 차트가 아닌 슬라이드에 속합니다. Slide.Shapes를 순회하면 차트와 일반 슬라이드 도형을 찾지만 차트 내부의 개별 도형은 열거되지 않습니다. 차트 제목과 데이터 레이블은 별개의 차트 요소이며, 이 컬렉션에 포함된 사용자 지정 주석이 아닙니다.
chart-annotations.pptx를 다운로드하고 애플리케이션 작업 디렉터리에 배치합니다. 첫 번째 슬라이드에는 “Preliminary results"라는 주석이 해제된 차트, 차트 소유 라인, 그리고 “Slide overlay"라는 일반 슬라이드 텍스트 상자가 포함되어 있습니다. 예제는 해당 슬라이드에서 첫 번째 차트를 찾아 보고서가 게시 준비가 되면 주석을 “Final results"로 변경합니다. 컬렉션이 비었는지 확인하고 TextFrame이 null이 아니고 예상 텍스트를 가진 IAutoShape만 편집합니다.
using System;
using System.Linq;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation("chart-annotations.pptx");
var slide = presentation.Slides[0];
var chart = slide.Shapes.OfType<IChart>().FirstOrDefault();
if (chart == null || chart.UserShapes.Shapes.Count == 0)
{
Console.WriteLine("No chart or chart-owned shapes found.");
return;
}
var updated = false;
foreach (var shape in chart.UserShapes.Shapes)
{
if (shape is IAutoShape annotation &&
annotation.TextFrame != null &&
annotation.TextFrame.Text == "Preliminary results")
{
annotation.TextFrame.Text = "Final results";
updated = true;
break;
}
}
if (updated)
{
presentation.Save("chart-annotations-updated.pptx", SaveFormat.Pptx);
}
else
{
Console.WriteLine("No matching text annotation found.");
}
선과 슬라이드 텍스트 상자는 변경되지 않은 채로 유지됩니다. 다음 이미지들은 입력과 저장 후 다시 연 결과를 보여줍니다:
| 편집 전 | 편집 후 |
|---|---|
![]() |
![]() |
Note
예제는 최상위 차트 소유 도형만 검사합니다. 주석이 IGroupShape 내부에 있는 경우 해당 그룹의 Shapes 컬렉션도 검사하십시오. 중첩 그룹에 대해 재귀적으로 반복하면서 자식 도형에 동일한 텍스트 검사를 적용합니다. 그룹 자체는 컨테이너이며 텍스트 상자로 취급하지 마십시오.FAQ
열/영역에 반투명 채우기를 적용하면서 테두리는 불투명하게 유지할 수 있나요?
예. 채우기 투명도와 외곽선은 별도로 구성됩니다. 이는 복잡한 시각화에서 격자와 데이터의 가독성을 향상시키는 데 유용합니다.
데이터 레이블이 겹칠 때 어떻게 처리할 수 있나요?
글꼴 크기를 줄이고, 필요 없는 레이블 구성 요소(예: 카테고리)를 비활성화하며, 레이블 위치를 조정하고, 필요 시 선택된 포인트에만 레이블을 표시하거나 형식을 ‘값 + 범례’로 전환하세요.
시리즈에 그라디언트 또는 패턴 채우기를 적용할 수 있나요?
예. 시리즈는 단색, 그라디언트 및 패턴 채우기를 지원합니다. 실제로는 그라디언트를 적게 사용하고, 격자와 텍스트와의 대비를 감소시키는 조합은 피하십시오.

