C++를 사용하여 프레젠테이션에서 차트 데이터 레이블 관리
소개
데이터 레이블은 차트 시리즈와 개별 데이터 포인트에 대한 정보를 표시하여 독자가 값을 식별하고 차트를 이해하도록 도와줍니다. 이 문서에서는 값 서식 지정, 백분율 표시, 레이블 텍스트 읽기, 축 최대값을 초과하는 레이블 제어, 범주 축 레이블 간격 조정 및 파이 차트 레이블 위치 지정 방법을 설명합니다.
표시된 데이터 레이블 수 세기
프레젠테이션을 게시하기 전에 표시하도록 활성화된 데이터 레이블 수를 확인하려면 get_CountOfVisibleDataLabels를 사용합니다. 전체 레이블 수를 반환하는 get_Count와 달리, 시리즈 기본값 및 개별 레이블 설정을 고려합니다.
이 예제는 하나의 시리즈와 세 개의 데이터 포인트를 가진 열 차트를 생성합니다. 시리즈의 get_Labels 컬렉션에 접근하고 설정이 변경될 때 표시된 개수를 출력합니다: 0, 3, 2, 3. 전체 개수는 3으로 유지됩니다.
#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <DOM/Chart/IDataLabel.h>
#include <DOM/Chart/IDataLabelCollection.h>
#include <DOM/Chart/IDataLabelFormat.h>
#include <system/console.h>
#include <system/object_ext.h>
#include <system/smart_ptr.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 50, 50, 500, 300);
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
auto seriesCell = workbook->GetCell(0, 0, 1, ObjectExt::Box<String>(u"Values"));
auto series = chart->get_ChartData()->get_Series()->Add(seriesCell, chart->get_Type());
for (auto i = 0; i < 3; i++)
{
auto categoryCell = workbook->GetCell(0, i + 1, 0, ObjectExt::Box(String::Format(u"Category {0}", i + 1)));
chart->get_ChartData()->get_Categories()->Add(categoryCell);
auto valueCell = workbook->GetCell(0, i + 1, 1, ObjectExt::Box((i + 1) * 10));
series->get_DataPoints()->AddDataPointForBarSeries(valueCell);
}
auto labels = series->get_Labels();
Console::WriteLine(String::Format(u"Total: {0}; visible: {1}", labels->get_Count(), labels->get_CountOfVisibleDataLabels())); // 전체: 3; 표시됨: 0
labels->get_DefaultDataLabelFormat()->set_ShowValue(true);
Console::WriteLine(String::Format(u"Visible: {0}", labels->get_CountOfVisibleDataLabels())); // 표시됨: 3
labels->idx_get(1)->get_DataLabelFormat()->set_ShowValue(false);
Console::WriteLine(String::Format(u"Visible: {0}", labels->get_CountOfVisibleDataLabels())); // 표시됨: 2
labels->idx_get(1)->get_DataLabelFormat()->set_ShowLegendKey(true);
Console::WriteLine(String::Format(u"Visible: {0}", labels->get_CountOfVisibleDataLabels())); // 표시됨: 3
시리즈의 get_DefaultDataLabelFormat은 기본 설정을 제공합니다. 여기서 true와 함께 set_ShowValue를 호출하면 세 레이블 모두에 대한 값이 표시됩니다. 하나의 레이블에 대해 false를 get_DataLabelFormat으로 전달하면 선택된 레이블에 대한 해당 설정이 재정의됩니다. true와 함께 set_ShowLegendKey를 호출하면 값이 숨겨진 상태라도 해당 레이블이 다시 표시된 것으로 간주됩니다.
표시된 개수는 가시성 설정을 반영합니다. 레이블이 겹치거나 잘리거나 렌더링된 차트에서 읽을 수 있는지는 측정하지 않습니다.
차트 데이터 레이블에서 데이터 정밀도 설정
시리즈 값을 서식 지정하려면 set_NumberFormatOfValues를 사용합니다. 이 예제는 기본 데이터를 가진 꺾은선 차트를 생성하고, 데이터 테이블을 표시하며, 첫 번째 시리즈에 대한 값 레이블을 활성화합니다. #,##0.00 형식은 천 단위 구분 기호와 두 자리 소수점을 표시하지만 기본 값은 변경하지 않습니다.
#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IDataLabelCollection.h>
#include <DOM/Chart/IDataLabelFormat.h>
#include <Export/SaveFormat.h>
#include <system/smart_ptr.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::Line, 50, 50, 450, 300);
chart->set_HasDataTable(true);
auto series = chart->get_ChartData()->get_Series()->idx_get(0);
series->set_NumberFormatOfValues(u"#,##0.00");
series->get_Labels()->get_DefaultDataLabelFormat()->set_ShowValue(true);
presentation->Save(u"PrecisionOfDatalabels_out.pptx", SaveFormat::Pptx);
차트에 포함된 워크북의 숫자 셀을 서식 지정하려면 Set Custom Number Formats for Workbook Cells를 참조하세요. 해당 예제는 저장된 숫자 값을 유지하면서 원본 셀 형식을 따르는 값 레이블을 보여줍니다.
백분율을 레이블로 표시
누적 세로 막대 차트의 경우, 각 값을 해당 범주의 총합에 대한 백분율로 계산하고 get_TextFrameForOverriding에서 반환된 텍스트 프레임에 텍스트를 할당합니다. 이 예제는 기본 차트 데이터를 사용하고 8포인트 글꼴에 소수점 두 자리 백분율을 표시합니다. 총합이 0인 범주는 0으로 나누는 오류를 방지하기 위해 건너뜁니다. 차트 데이터가 변경되면 사용자 정의 레이블 텍스트를 다시 계산합니다.
#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <DOM/Chart/IChartDataPoint.h>
#include <DOM/Chart/IDoubleChartValue.h>
#include <DOM/Chart/IDataLabel.h>
#include <DOM/Chart/IDataLabelCollection.h>
#include <DOM/Chart/IDataLabelFormat.h>
#include <DOM/Portion.h>
#include <DOM/IPortionFormat.h>
#include <DOM/ITextFrame.h>
#include <DOM/IParagraph.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IPortionCollection.h>
#include <system/convert.h>
#include <vector>
#include <Export/SaveFormat.h>
#include <system/smart_ptr.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::StackedColumn, 20, 20, 400, 400);
auto categoryTotals = std::vector<double>(chart->get_ChartData()->get_Categories()->get_Count(), 0.0);
for (auto k = 0; k < chart->get_ChartData()->get_Categories()->get_Count(); k++)
{
for (auto i = 0; i < chart->get_ChartData()->get_Series()->get_Count(); i++)
{
auto series = chart->get_ChartData()->get_Series()->idx_get(i);
auto pointValue = Convert::ToDouble(series->get_DataPoint(k)->get_Value()->get_Data());
categoryTotals[k] += pointValue;
}
}
for (auto x = 0; x < chart->get_ChartData()->get_Series()->get_Count(); x++)
{
auto series = chart->get_ChartData()->get_Series()->idx_get(x);
series->get_Labels()->get_DefaultDataLabelFormat()->set_ShowLegendKey(false);
for (auto j = 0; j < series->get_DataPoints()->get_Count(); j++)
{
auto label = series->get_DataPoint(j)->get_Label();
if (categoryTotals[j] == 0)
{
continue;
}
auto pointValue = Convert::ToDouble(series->get_DataPoint(j)->get_Value()->get_Data());
auto dataPointPercent = (pointValue / categoryTotals[j]) * 100;
auto portion = MakeObject<Portion>();
portion->set_Text(String::Format(u"{0:F2} %", dataPointPercent));
portion->get_PortionFormat()->set_FontHeight(8.0f);
label->get_TextFrameForOverriding()->set_Text(u"");
auto paragraph = label->get_TextFrameForOverriding()->get_Paragraphs()->idx_get(0);
paragraph->get_Portions()->Add(portion);
label->get_DataLabelFormat()->set_ShowValue(true);
label->get_DataLabelFormat()->set_ShowSeriesName(false);
label->get_DataLabelFormat()->set_ShowPercentage(false);
label->get_DataLabelFormat()->set_ShowLegendKey(false);
label->get_DataLabelFormat()->set_ShowCategoryName(false);
label->get_DataLabelFormat()->set_ShowBubbleSize(false);
}
}
presentation->Save(u"DisplayPercentageAsLabels_out.pptx", SaveFormat::Pptx);
차트 데이터 레이블에 백분율 기호 설정
값이 분수로 저장된 경우, 백분율을 표시하려면 set_NumberFormat를 사용합니다. 레이블 형식을 원본 셀과 독립적으로 적용하려면 set_IsNumberFormatLinkedToSource에 false를 전달합니다.
이 예제는 네 개 범주에 걸쳐 빨간색 및 파란색 시리즈를 갖는 100% 누적 세로 막대 차트를 생성합니다. 각 값 쌍은 1을 합산합니다. 레이블 형식 0.0%는 0.30을 30.0%로 표시하며, 세로 축은 두 자리 소수점을 사용합니다. 두 시리즈 모두 흰색 10포인트 레이블 텍스트를 사용합니다.
#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <DOM/Chart/IFormat.h>
#include <DOM/Chart/IDataLabelCollection.h>
#include <DOM/Chart/IDataLabelFormat.h>
#include <DOM/Chart/IChartTextFormat.h>
#include <DOM/Chart/IChartPortionFormat.h>
#include <DOM/FillType.h>
#include <DOM/IFillFormat.h>
#include <DOM/IColorFormat.h>
#include <drawing/color.h>
#include <system/object_ext.h>
#include <Export/SaveFormat.h>
#include <system/smart_ptr.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::PercentsStackedColumn, 20, 20, 500, 400);
chart->get_Axes()->get_VerticalAxis()->set_IsNumberFormatLinkedToSource(false);
chart->get_Axes()->get_VerticalAxis()->set_NumberFormat(u"0.00%");
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
auto worksheetIndex = 0;
for (auto i = 0; i < 4; i++)
{
auto categoryCell = workbook->GetCell(worksheetIndex, i + 1, 0, ObjectExt::Box(String::Format(u"Category {0}", i + 1)));
chart->get_ChartData()->get_Categories()->Add(categoryCell);
}
String seriesNames[] = { u"Reds", u"Blues" };
Color seriesColors[] = { Color::get_Red(), Color::get_Blue() };
double values[2][4] = { { 0.30, 0.50, 0.80, 0.65 }, { 0.70, 0.50, 0.20, 0.35 } };
for (auto i = 0; i < 2; i++)
{
auto seriesCell = workbook->GetCell(worksheetIndex, 0, i + 1, ObjectExt::Box(seriesNames[i]));
auto series = chart->get_ChartData()->get_Series()->Add(seriesCell, chart->get_Type());
for (auto j = 0; j < 4; j++)
{
auto valueCell = workbook->GetCell(worksheetIndex, j + 1, i + 1, ObjectExt::Box(values[i][j]));
series->get_DataPoints()->AddDataPointForBarSeries(valueCell);
}
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(seriesColors[i]);
auto labelFormat = series->get_Labels()->get_DefaultDataLabelFormat();
labelFormat->set_ShowValue(true);
labelFormat->set_IsNumberFormatLinkedToSource(false);
labelFormat->set_NumberFormat(u"0.0%");
labelFormat->get_TextFormat()->get_PortionFormat()->set_FontHeight(10);
labelFormat->get_TextFormat()->get_PortionFormat()->get_FillFormat()->set_FillType(FillType::Solid);
labelFormat->get_TextFormat()->get_PortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_White());
}
presentation->Save(u"SetDataLabelsPercentageSign_out.pptx", SaveFormat::Pptx);
데이터 레이블 실제 텍스트 읽기
데이터 레이블 설정에 의해 생성된 텍스트를 가져오려면 GetActualLabelText를 사용합니다. 이는 레이블을 보고서에 추출하거나 프레젠테이션 내용을 검색하거나 생성된 차트를 검증할 때 유용합니다. 아래 예제에서는 기본 data label format이 각 범주 이름, 시리즈 이름 및 값을 결합합니다. 하나의 포인트는 값을 백분율로 서식 지정하고, 다른 포인트는 get_TextFrameForOverriding에서 사용자 정의 텍스트를 사용합니다.
#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <DOM/Chart/IChartDataPoint.h>
#include <DOM/Chart/IDoubleChartValue.h>
#include <DOM/Chart/IDataLabel.h>
#include <DOM/Chart/IDataLabelCollection.h>
#include <DOM/Chart/IDataLabelFormat.h>
#include <DOM/ITextFrame.h>
#include <system/console.h>
#include <system/object_ext.h>
#include <system/smart_ptr.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 20, 20, 500, 300);
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
auto firstCategoryCell = workbook->GetCell(0, 1, 0, ObjectExt::Box<String>(u"Q1"));
chart->get_ChartData()->get_Categories()->Add(firstCategoryCell);
auto secondCategoryCell = workbook->GetCell(0, 2, 0, ObjectExt::Box<String>(u"Q2"));
chart->get_ChartData()->get_Categories()->Add(secondCategoryCell);
auto northSeriesCell = workbook->GetCell(0, 0, 1, ObjectExt::Box<String>(u"North"));
auto north = chart->get_ChartData()->get_Series()->Add(northSeriesCell, chart->get_Type());
auto northFirstValueCell = workbook->GetCell(0, 1, 1, ObjectExt::Box(0.25));
north->get_DataPoints()->AddDataPointForBarSeries(northFirstValueCell);
auto northSecondValueCell = workbook->GetCell(0, 2, 1, ObjectExt::Box(0.75));
north->get_DataPoints()->AddDataPointForBarSeries(northSecondValueCell);
auto southSeriesCell = workbook->GetCell(0, 0, 2, ObjectExt::Box<String>(u"South"));
auto south = chart->get_ChartData()->get_Series()->Add(southSeriesCell, chart->get_Type());
auto southFirstValueCell = workbook->GetCell(0, 1, 2, ObjectExt::Box(0.40));
south->get_DataPoints()->AddDataPointForBarSeries(southFirstValueCell);
auto southSecondValueCell = workbook->GetCell(0, 2, 2, ObjectExt::Box(0.60));
south->get_DataPoints()->AddDataPointForBarSeries(southSecondValueCell);
for (auto i = 0; i < chart->get_ChartData()->get_Series()->get_Count(); i++)
{
auto series = chart->get_ChartData()->get_Series()->idx_get(i);
auto format = series->get_Labels()->get_DefaultDataLabelFormat();
format->set_ShowCategoryName(true);
format->set_ShowSeriesName(true);
format->set_ShowValue(true);
}
north->get_Label(1)->get_DataLabelFormat()->set_IsNumberFormatLinkedToSource(false);
north->get_Label(1)->get_DataLabelFormat()->set_NumberFormat(u"0%");
south->get_Label(0)->get_TextFrameForOverriding()->set_Text(u"Reviewed");
for (auto i = 0; i < chart->get_ChartData()->get_Series()->get_Count(); i++)
{
auto series = chart->get_ChartData()->get_Series()->idx_get(i);
for (auto j = 0; j < series->get_DataPoints()->get_Count(); j++)
{
auto point = series->get_DataPoint(j);
auto label = point->get_Label();
if (!label->get_IsVisible())
{
continue;
}
Console::WriteLine(String::Format(u"Value: {0}; label: {1}", point->get_Value()->get_Data(), label->GetActualLabelText()));
}
}
데이터 포인트에 저장된 수치는 0.75이며, 레이블에 75%와 범주 및 시리즈 이름이 표시되더라도 값은 변하지 않습니다. 사용자 정의 텍스트는 생성된 레이블 텍스트를 대체합니다. GetActualLabelText는 두 경우 모두 결과 레이블 문자열을 반환합니다. 표시된 레이블만 추출하려면 위와 같이 별도로 get_IsVisible을 확인하세요.
축 최대값을 초과하는 데이터 레이블 제어
축 범위를 수동으로 제한하면 일부 데이터 포인트가 최대값을 초과할 수 있습니다. set_ShowDataLabelsOverMaximum을 사용하여 해당 데이터 레이블을 표시할지 여부를 제어합니다. 이 설정은 레이블 가시성을 변경하지만 축 범위나 기본 데이터 값은 변경하지 않습니다.
아래 예제는 값이 60과 120인 2D 클러스터형 열 차트를 생성합니다. 세로 축에 대해 set_IsAutomaticMaxValue를 false로 설정하고 set_MaxValue를 100으로 지정합니다. 첫 번째 슬라이드는 최대값을 초과하는 레이블을 허용하고, 해당 슬라이드 복사본은 이를 비활성화합니다. 두 슬라이드는 DataLabelsOverMaximum.pptx에 저장됩니다.
set_ShowValue로 값 레이블을 활성화합니다. 차트 수준 설정만으로는 값 표시가 자동으로 활성화되지 않으며 개별 레이블의 비활성화된 값 표시를 재정의하지도 않습니다. 이 예제는 전체 시리즈에 값을 활성화하고 set_Position를 사용하여 각 열의 외부 끝에 레이블을 배치합니다.
#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <DOM/Chart/IDataLabelCollection.h>
#include <DOM/Chart/IDataLabelFormat.h>
#include <DOM/Chart/LegendDataLabelPosition.h>
#include <Export/SaveFormat.h>
#include <system/object_ext.h>
#include <system/smart_ptr.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 50, 50, 600, 400);
chart->set_HasLegend(false);
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
auto firstCategory = workbook->GetCell(0, 1, 0, ObjectExt::Box<String>(u"Within range"));
auto secondCategory = workbook->GetCell(0, 2, 0, ObjectExt::Box<String>(u"Above maximum"));
chart->get_ChartData()->get_Categories()->Add(firstCategory);
chart->get_ChartData()->get_Categories()->Add(secondCategory);
auto seriesName = workbook->GetCell(0, 0, 1, ObjectExt::Box<String>(u"Values"));
auto series = chart->get_ChartData()->get_Series()->Add(seriesName, chart->get_Type());
auto firstValue = workbook->GetCell(0, 1, 1, ObjectExt::Box(60));
auto secondValue = workbook->GetCell(0, 2, 1, ObjectExt::Box(120));
series->get_DataPoints()->AddDataPointForBarSeries(firstValue);
series->get_DataPoints()->AddDataPointForBarSeries(secondValue);
series->get_Labels()->get_DefaultDataLabelFormat()->set_ShowValue(true);
series->get_Labels()->get_DefaultDataLabelFormat()->set_Position(LegendDataLabelPosition::OutsideEnd);
chart->get_Axes()->get_VerticalAxis()->set_IsAutomaticMaxValue(false);
chart->get_Axes()->get_VerticalAxis()->set_MaxValue(100);
chart->set_ShowDataLabelsOverMaximum(true);
auto secondSlide = presentation->get_Slides()->AddClone(slide);
auto secondChart = ExplicitCast<IChart>(secondSlide->get_Shape(0));
secondChart->set_ShowDataLabelsOverMaximum(false);
presentation->Save(u"DataLabelsOverMaximum.pptx", SaveFormat::Pptx);
다음 이미지는 Microsoft PowerPoint에서 렌더링된 저장된 슬라이드를 보여줍니다. true인 경우 레이블 120이 상한선에 표시되고, false인 경우 숨겨집니다. 레이블 60은 계속 표시되며, 축 최대값은 100으로 유지되고 두 번째 데이터 포인트는 두 경우 모두 120으로 유지됩니다.
| ShowDataLabelsOverMaximum = true | ShowDataLabelsOverMaximum = false |
|---|---|
![]() |
![]() |
Chart Type
이 예제는 값 축이 있는 2D 세로 막대 차트를 사용합니다. 파이 차트 및 도넛 차트와 같이 값 축이 없는 차트는 이러한 방식으로 제한할 축 최대값이 없습니다.축으로부터 레이블 거리 설정
set_LabelOffset을 사용하여 범주 축 레이블과 축 사이의 거리를 제어합니다. 값은 축 레이블의 최대 글꼴 크기의 백분율입니다. 이 예제는 클러스터형 열 차트를 생성하고 가로 축 레이블 오프셋을 500으로 설정합니다. 이 설정은 개별 데이터 포인트에 붙은 레이블이 아니라 범주 축 레이블에 영향을 줍니다.
#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <Export/SaveFormat.h>
#include <system/smart_ptr.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 20, 20, 500, 300);
chart->get_Axes()->get_HorizontalAxis()->set_LabelOffset(500);
presentation->Save(u"SetCategoryAxisLabelDistance_out.pptx", SaveFormat::Pptx);
레이블 위치 조정
파이 차트에서 데이터 레이블 위치를 조정하여 간격을 개선하고 리더 라인을 위한 공간을 확보합니다.
이 예제는 첫 번째 데이터 포인트의 값을 표시하고 레이블을 조각 외부에 배치하며, set_X 및 set_Y를 사용해 오프셋을 조정합니다. 이러한 오프셋은 차트 너비와 높이에 각각 상대적입니다.
#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IDataLabel.h>
#include <DOM/Chart/IDataLabelFormat.h>
#include <DOM/Chart/LegendDataLabelPosition.h>
#include <Export/SaveFormat.h>
#include <system/smart_ptr.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::Pie, 50, 50, 200, 200);
auto series = chart->get_ChartData()->get_Series();
auto label = series->idx_get(0)->get_Label(0);
label->get_DataLabelFormat()->set_ShowValue(true);
label->get_DataLabelFormat()->set_Position(LegendDataLabelPosition::OutsideEnd);
label->set_X(0.71f);
label->set_Y(0.04f);
presentation->Save(u"presentation.pptx", SaveFormat::Pptx);

리더 라인 서식 지정
파이 차트 레이블이 조각 외부에 있을 때, 리더 라인은 각 값을 올바른 조각에 연결하는 데 도움이 됩니다. 대비되는 색상과 약간 두꺼운 선은 이 연결을 더 쉽게 따라볼 수 있게 하며, 투명도와 대시 스타일은 라인을 시각적으로 눈에 거슬리지 않게 유지합니다.
get_LeaderLinesFormat을 사용하여 시리즈의 데이터 레이블 컬렉션 수준에서 리더 라인을 서식 지정합니다. series->get_Labels()->get_LeaderLinesFormat()->get_Line()에 있는 설정은 개별 레이블이 아니라 컬렉션에 적용됩니다.
이 예제는 기본 데이터를 가진 파이 차트를 생성하고 조각 외부에 값을 표시하며 set_ShowLeaderLines으로 리더 라인을 활성화합니다. set_X 및 set_Y 오프셋을 사용하여 두 레이블을 조각으로부터 더 멀리 이동합니다. 오프셋은 차트 너비와 높이의 비율로 표현됩니다. 입력 프레젠테이션은 필요하지 않습니다.
get_FillFormat에서 반환된 채우기 서식은 알파 값 191(255 중)인 단색 파랑으로 설정되어 약 25% 투명도를 제공합니다. set_Width으로 두께를 2포인트로 설정하고, set_DashStyle으로 선을 대시형으로 만듭니다.
#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IDataLabel.h>
#include <DOM/Chart/IDataLabelCollection.h>
#include <DOM/Chart/IDataLabelFormat.h>
#include <DOM/Chart/IChartLinesFormat.h>
#include <DOM/Chart/LegendDataLabelPosition.h>
#include <DOM/ILineFormat.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/IColorFormat.h>
#include <DOM/FillType.h>
#include <DOM/LineDashStyle.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::Pie, 80, 50, 560, 400);
chart->set_HasLegend(false);
auto series = chart->get_ChartData()->get_Series()->idx_get(0);
auto labelFormat = series->get_Labels()->get_DefaultDataLabelFormat();
labelFormat->set_ShowValue(true);
labelFormat->set_Position(LegendDataLabelPosition::OutsideEnd);
labelFormat->set_ShowLeaderLines(true);
// 두 레이블을 조각에서 더 멀리 이동합니다.
series->get_Label(0)->set_X(0.10f);
series->get_Label(0)->set_Y(-0.05f);
series->get_Label(1)->set_X(-0.10f);
series->get_Label(1)->set_Y(0.05f);
auto line = series->get_Labels()->get_LeaderLinesFormat()->get_Line();
line->get_FillFormat()->set_FillType(FillType::Solid);
line->get_FillFormat()->get_SolidFillColor()->set_Color(Color::FromArgb(191, 30, 90, 160));
line->set_Width(2);
line->set_DashStyle(LineDashStyle::Dash);
presentation->Save(u"FormattedLeaderLines.pptx", SaveFormat::Pptx);
리더 라인을 활성화해도 모든 레이블에 선이 나타난다는 보장은 없습니다. 가시성은 레이블 배치와 차트 레이아웃 동작에 따라 달라집니다. 이 예제에서는 두 개의 이동된 레이블에 가시적인 리더 라인이 있으며, 조각에 가까이 남은 레이블은 필요하지 않습니다.
다음 이미지는 Aspose.Slides로 렌더링된 저장된 FormattedLeaderLines.pptx를 보여줍니다.

열 차트 위에 여러 행의 데이터 레이블 추가
이 예제는 플롯 영역 위에 두 행의 데이터 레이블이 있는 열 차트를 생성합니다. Series A는 표시된 열을 보여주고, Series B와 Series C는 추가 레이블을 제공합니다. 이들의 열은 채우기와 외곽선을 제거하여 숨깁니다. set_Overlap 메서드는 세 시리즈를 동일한 범주 중심에 정렬합니다.
get_PlotArea 설정은 레이블 행을 위한 공간을 예약합니다. ValidateChartLayout으로 기본 위치를 계산한 후, set_X와 set_Y를 사용하여 수평 정렬을 유지하고 수직 오프셋을 적용해 레이블을 두 행으로 배치합니다. 숫자는 시리즈 값에 연결된 데이터 레이블로 남아 있으며, 행 머리글만 별도의 텍스트 도형입니다.
#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <DOM/Chart/IChartDataPoint.h>
#include <DOM/Chart/IChartSeriesGroup.h>
#include <DOM/Chart/IFormat.h>
#include <DOM/Chart/IDataLabelCollection.h>
#include <DOM/Chart/IDataLabelFormat.h>
#include <DOM/Chart/IDataLabel.h>
#include <DOM/Chart/IChartTextFormat.h>
#include <DOM/Chart/IChartPortionFormat.h>
#include <DOM/Chart/LegendDataLabelPosition.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <DOM/Chart/IChartPlotArea.h>
#include <DOM/Chart/LayoutTargetType.h>
#include <DOM/FillType.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IColorFormat.h>
#include <DOM/ShapeType.h>
#include <DOM/IAutoShape.h>
#include <DOM/ITextFrame.h>
#include <DOM/ITextFrameFormat.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IParagraph.h>
#include <DOM/IPortionCollection.h>
#include <DOM/IPortion.h>
#include <DOM/IPortionFormat.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/object_ext.h>
#include <system/smart_ptr.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 40, 40, 640, 200);
chart->set_HasTitle(false);
chart->set_HasLegend(false);
chart->get_TextFormat()->get_PortionFormat()->set_FontHeight(12);
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
workbook->Clear(0);
String categories[] = { u"North", u"South", u"East", u"West" };
String seriesNames[] = { u"Series A", u"Series B", u"Series C" };
double seriesValues[3][4] = {
{ 35, 42, 28, 47 },
{ 22, 31, 19, 26 },
{ 12, 16, 14, 18 }
};
for (auto categoryIndex = 0; categoryIndex < 4; categoryIndex++)
{
auto categoryCell = workbook->GetCell(0, categoryIndex + 1, 0, ObjectExt::Box(categories[categoryIndex]));
chart->get_ChartData()->get_Categories()->Add(categoryCell);
}
for (auto seriesIndex = 0; seriesIndex < 3; seriesIndex++)
{
auto seriesCell = workbook->GetCell(0, 0, seriesIndex + 1, ObjectExt::Box(seriesNames[seriesIndex]));
auto series = chart->get_ChartData()->get_Series()->Add(seriesCell, ChartType::ClusteredColumn);
for (auto categoryIndex = 0; categoryIndex < 4; categoryIndex++)
{
auto valueCell = workbook->GetCell(0, categoryIndex + 1, seriesIndex + 1, ObjectExt::Box(seriesValues[seriesIndex][categoryIndex]));
series->get_DataPoints()->AddDataPointForBarSeries(valueCell);
}
if (seriesIndex > 0)
{
// B와 C의 열을 숨기지만 데이터 레이블은 유지합니다.
series->get_Format()->get_Fill()->set_FillType(FillType::NoFill);
series->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::NoFill);
auto labelFormat = series->get_Labels()->get_DefaultDataLabelFormat();
labelFormat->set_ShowValue(true);
labelFormat->get_TextFormat()->get_PortionFormat()->set_FontHeight(12);
auto labelFill = labelFormat->get_TextFormat()->get_PortionFormat()->get_FillFormat();
labelFill->set_FillType(FillType::Solid);
labelFill->get_SolidFillColor()->set_Color(Color::get_Black());
labelFormat->set_Position(LegendDataLabelPosition::InsideBase);
}
}
// 세 시리즈를 동일한 범주 중심에 정렬합니다.
chart->get_ChartData()->get_Series()->idx_get(0)->get_ParentSeriesGroup()->set_Overlap(100);
// 이 간결한 예제에서는 눈금선을 줄입니다.
chart->get_Axes()->get_VerticalAxis()->set_IsAutomaticMajorUnit(false);
chart->get_Axes()->get_VerticalAxis()->set_MajorUnit(10);
// 플롯 위에 두 행의 데이터 레이블을 위한 공간을 예약합니다.
chart->get_PlotArea()->set_LayoutTargetType(LayoutTargetType::Inner);
chart->get_PlotArea()->set_X(0.15f);
chart->get_PlotArea()->set_Y(0.32f);
chart->get_PlotArea()->set_Width(0.80f);
chart->get_PlotArea()->set_Height(0.48f);
chart->ValidateChartLayout();
for (auto seriesIndex = 1; seriesIndex < 3; seriesIndex++)
{
auto series = chart->get_ChartData()->get_Series()->idx_get(seriesIndex);
auto rowTop = seriesIndex == 1 ? 0.15f : 0.03f;
for (auto categoryIndex = 0; categoryIndex < 4; categoryIndex++)
{
auto dataLabel = series->get_DataPoint(categoryIndex)->get_Label();
// 기본 수평 위치를 유지합니다. Y는 다음에서 오프셋입니다
// 기본 레이블 위치이며, 차트 높이 비율로 표현됩니다.
dataLabel->set_X(0);
dataLabel->set_Y(rowTop - dataLabel->get_ActualY() / chart->get_Height());
}
// 행 머리글만 별도의 텍스트 도형입니다.
auto rowHeading = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, chart->get_X(), chart->get_Y() + rowTop * chart->get_Height(), 85, 18);
rowHeading->get_FillFormat()->set_FillType(FillType::NoFill);
rowHeading->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
rowHeading->AddTextFrame(seriesNames[seriesIndex]);
rowHeading->get_TextFrame()->get_TextFrameFormat()->set_MarginTop(0);
rowHeading->get_TextFrame()->get_TextFrameFormat()->set_MarginBottom(0);
auto headingFormat = rowHeading->get_TextFrame()->get_Paragraphs()->idx_get(0)->get_Portions()->idx_get(0)->get_PortionFormat();
headingFormat->set_FontHeight(12);
headingFormat->get_FillFormat()->set_FillType(FillType::Solid);
headingFormat->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
}
presentation->Save(u"multiple-rows-of-labels.pptx", SaveFormat::Pptx);

FAQ
밀집된 차트에서 데이터 레이블이 겹치는 것을 어떻게 방지할 수 있나요?
자동 레이블 배치, 리더 라인 및 작은 글꼴 크기를 결합합니다; 필요하면 일부 필드(예: 범주)를 숨기거나 극값이나 핵심 포인트에만 레이블을 표시합니다.
값이 0, 음수 또는 비어 있는 경우에만 레이블을 비활성화하려면 어떻게 해야 하나요?
레이블을 활성화하기 전에 데이터 포인트를 필터링하고, 정의된 규칙에 따라 0, 음수 또는 누락된 값에 대한 표시를 끕니다.
PDF/이미지로 내보낼 때 일관된 레이블 스타일을 보장하려면 어떻게 해야 하나요?
폰트 패밀리와 크기를 명시적으로 설정하고, 렌더링 환경에 해당 폰트가 존재하는지 확인하여 대체 폰트 사용을 방지합니다.

