자바에서 프로그래밍 방식으로 파이 차트 만들기

이 블로그 게시물은 자바에서 프로그래밍 방식으로 차트를 다루는 방법을 보여줍니다. 다시, 우리는 Aspose.Slides for Java를 탐색하고 이 파이 차트 API에서 제공하는 방법을 사용하여 자바에서 파이 차트를 만드는 방법을 구현할 것입니다. 따라서 이 기사는 귀하의 비즈니스 소프트웨어에 파이 차트 생성 모듈을 장착할 수 있는 사용자 정의 가능한 파이 차트 생성을 자동화하는 데 중점을 두겠습니다. 따라서 이 카테고리를 방문하여 Aspose.Slides for Java의 다른 주요 기능을 강조하는 블로그 게시물에 대해 알아보세요.

다음 사항을 다룰 것입니다:

파이 차트 API 설치

설치 단계는 몇 초가 걸립니다. 그러나 이 JAR 파일을 다운로드하거나 Maven 구성을 사용할 수 있습니다.

파이 차트 API 설치

자바에서 파이 차트 만들기 - 단계

다음 단계는 자바에서 파이 차트를 만드는 방법을 보여줍니다:

  • Presentation 클래스의 인스턴스를 생성합니다.
  • get_Item 메서드를 호출하여 첫 번째 슬라이드에 접근합니다.
  • addChart 메서드를 호출하여 기본 데이터로 차트를 추가합니다.
  • getChartDataWorkbook 함수를 호출하여 차트 데이터 워크시트를 가져옵니다.
  • add 메서드를 호출하여 새로운 카테고리를 추가합니다.
  • addDataPointForPieSeries 메서드를 호출하여 시리즈 데이터를 채웁니다.
  • getDataPoints().get_Item(0)는 이 컬렉션의 데이터 포인트 인덱스(이 컬렉션에서의 일련 번호)를 반환합니다.
  • setFillType 메서드를 호출하여 섹터 테두리를 설정합니다.
  • setFirstSliceAngle 메서드를 호출하여 파이 차트 섹터의 회전 각도를 설정합니다.
  • save 메서드를 호출하여 파이 차트가 포함된 프레젠테이션을 저장합니다.

자바 파이 차트 예제 코드

이제 코드를 스스로 설명하게 해봅시다:

// Create a Pie Chart in Java Programmatically
public class Main
{
public static void main(String[] args)
{
// The path to the documents directory.
String dataDir = "/file/";
// Instantiate an instance of the Presentation class.
Presentation presentation = new Presentation();
// Access first slide by calling the get_Item method.
ISlide slides = presentation.getSlides().get_Item(0);
// Add chart with default data by calling the addChart method.
IChart chart = slides.getShapes().addChart(ChartType.Pie, 100, 100, 400, 400);
// Set chart Title, height and some other configurations.
chart.getChartTitle().addTextFrameForOverriding("Sample Title");
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True);
chart.getChartTitle().setHeight(20);
chart.setTitle(true);
// Set first series to show Values.
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
// Setting the index of chart data sheet
int defaultWorksheetIndex = 0;
// Invoke the getChartDataWorkbook function to get the chart data worksheet.
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();
// Delete default generated series and categories.
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
// Add new categories by calling the add method.
chart.getChartData().getCategories().add(fact.getCell(0, 1, 0, "First Qtr"));
chart.getChartData().getCategories().add(fact.getCell(0, 2, 0, "2nd Qtr"));
chart.getChartData().getCategories().add(fact.getCell(0, 3, 0, "3rd Qtr"));
// Add new series
IChartSeries series = chart.getChartData().getSeries().add(fact.getCell(0, 0, 1, "Series 1"), chart.getType());
// Call the method to populate series data.
series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30));
// Not working in new version
// Adding new points and setting sector color
// series.IsColorVaried = true;
chart.getChartData().getSeriesGroups().get_Item(0).setColorVaried(true);
// getDataPoints().get_Item(0) will return index (serial number in this collection) of data point in this collection.
IChartDataPoint point = series.getDataPoints().get_Item(0);
point.getFormat().getFill().setFillType(FillType.Solid);
point.getFormat().getFill().getSolidFillColor().setColor(new Color(PresetColor.Cyan));
// Setting Sector border by calling the setFillType method.
point.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
point.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.GRAY);
point.getFormat().getLine().setWidth(3.0);
point.getFormat().getLine().setStyle(LineStyle.ThinThick);
point.getFormat().getLine().setDashStyle(LineDashStyle.DashDot);
IChartDataPoint point1 = series.getDataPoints().get_Item(1);
point1.getFormat().getFill().setFillType(FillType.Solid);
point1.getFormat().getFill().getSolidFillColor().setColor(new Color(PresetColor.Brown));
// Setting Sector border
point1.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
point1.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
point1.getFormat().getLine().setWidth(3.0);
point1.getFormat().getLine().setStyle(LineStyle.Single);
point1.getFormat().getLine().setDashStyle(LineDashStyle.LargeDashDot);
IChartDataPoint point2 = series.getDataPoints().get_Item(2);
point2.getFormat().getFill().setFillType(FillType.Solid);
point2.getFormat().getFill().getSolidFillColor().setColor(new Color(PresetColor.Coral));
// Setting Sector border
point2.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
point2.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.RED);
point2.getFormat().getLine().setWidth(2.0);
point2.getFormat().getLine().setStyle(LineStyle.ThinThin);
point2.getFormat().getLine().setDashStyle(LineDashStyle.LargeDashDotDot);
// Create custom labels for each of categories for new series
IDataLabel lbl1 = series.getDataPoints().get_Item(0).getLabel();
// lbl.setShowCategoryName(true);
lbl1.getDataLabelFormat().setShowValue(true);
IDataLabel lbl2 = series.getDataPoints().get_Item(1).getLabel();
lbl2.getDataLabelFormat().setShowValue(true);
lbl2.getDataLabelFormat().setShowLegendKey(true);
lbl2.getDataLabelFormat().setShowPercentage(true);
IDataLabel lbl3 = series.getDataPoints().get_Item(2).getLabel();
lbl3.getDataLabelFormat().setShowSeriesName(true);
lbl3.getDataLabelFormat().setShowPercentage(true);
// Showing Leader Lines for Chart
series.getLabels().getDefaultDataLabelFormat().setShowLeaderLines(true);
// Call the setFirstSliceAngle method to set rotation angle for Pie chart sectors.
chart.getChartData().getSeriesGroups().get_Item(0).setFirstSliceAngle(180);
// Save presentation with Pie chart by calling the save method.
presentation.save(dataDir + "PieChart_out.pptx", SaveFormat.Pptx);
}
}
아래 이미지에서 출력을 확인할 수 있습니다:

파이 차트 생성기

파이 차트 생성기 - 무료 라이센스 받기

평가 제한 없이 이 파이 차트 API를 사용해 볼 수 있는 무료 임시 라이센스를 받을 수 있습니다.

요약

결론적으로, 이 가이드는 Aspose.Slides for Java를 사용하여 자바에서 파이 차트를 만드는 방법을 다루었습니다. 또한 기능을 구현하기 위한 자바 파이 차트 예제 코드도 살펴보았습니다. 더불어, 이 파이 차트 API를 더 탐색할 수 있도록 문서 페이지, GitHub 저장소 및 API 참조로 이동할 수 있습니다. 마지막으로, aspose.com은 계속해서 글을 쓰고 있으니 최신 업데이트를 위해 연락해 주세요.

문의하기

질문이나 문의 사항이 있으시면 포럼에 알려주세요.

자주 묻는 질문 - FAQs

자바를 사용하여 파이 차트를 만드는 방법은 무엇인가요? Aspose.Slides for Java파이 차트를 프로그래밍 방식으로 생성하는 풍부한 기능 스택을 제공합니다. 무엇보다도, 링크를 방문하여 단계를 확인하세요.

특정 데이터로 파이 차트를 만드는 방법은 무엇인가요?섹션에서는 자바에서 파이 차트 생성기를 개발하는 방법을 보여줍니다.

참고 사항