Phát Triển Trình Tạo Biểu Đồ Tán Xạ trong Java

Mục Tiêu Của Chúng Tôi

Bài viết trên blog này sẽ cho phép bạn tự động hóa việc tạo biểu đồ một cách lập trình. Chúng tôi sẽ cố gắng làm cho mọi thứ khá đơn giản bằng cách viết một đoạn mã để phát triển trình tạo biểu đồ tán xạ trong Java. Aspose.Slides cho Java là một API Java mạnh mẽ nhưng nhẹ cho việc tạo và thao tác biểu đồ. Hơn nữa, API cấp cao này cung cấp một hệ sinh thái thân thiện với nhà phát triển so với các API cạnh tranh khác. Vậy, hãy tiếp tục và đi vào vấn đề.

Java API Cho Biểu Đồ - Cài Đặt

Bạn có thể xem chi tiết cài đặt tại đây. Tóm lại, bạn có thể tải xuống tệp JAR này hoặc sử dụng các cấu hình Maven sau để cài đặt thư viện biểu đồ Java này:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository> 
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>24.4</version>
    <classifier>jdk16</classifier>
</dependency>

Phát Triển Trình Tạo Biểu Đồ Tán Xạ - Đoạn Mã

Vậy, chúng ta sẽ viết các bước để thực hiện:

  • Tạo một thể hiện của lớp Presentation.
  • Gọi phương thức get_Item để truy cập trang chiếu đầu tiên.
  • Tạo biểu đồ mặc định bằng cách gọi phương thức addChart.
  • Gọi phương thức getChartDataWorkbook để lấy bảng dữ liệu biểu đồ.
  • Thêm một danh mục biểu đồ mới bằng cách gọi phương thức add.
  • Thêm một điểm mới (1:3) bằng cách gọi phương thức addDataPointForScatterSeries.
  • Đặt loại chuỗi bằng cách gọi phương thức setType.
  • Đặt kích thước đánh dấu trong biểu đồ bằng cách gọi phương thức setSize.
  • Gọi phương thức setSymbol để đặt kiểu đánh dấu trong biểu đồ.
  • Lưu tệp PPTX/PPT đầu ra trên đĩa bằng cách gọi phương thức save.

Dưới đây là mẫu mã cho thấy cách phát triển một trình tạo biểu đồ tán xạ trong Java một cách lập trình:

package com.example;
import com.aspose.slides.*;
import java.io.File;
public class main
{
public static void main(String[] args)
{
// The path to the documents directory.
String dataDir = "/Desktop/";
// Create directory if it is not already present.
boolean IsExists = new File(dataDir).exists();
if (!IsExists)
new File(dataDir).mkdirs();
// Create an instance of the Presentation class.
Presentation pres = new Presentation();
// Invoke the get_Item method to access the first slide.
ISlide slide = pres.getSlides().get_Item(0);
// Creating the default chart by calling the addChart method.
IChart chart = slide.getShapes().addChart(ChartType.ScatterWithSmoothLines, 0, 0, 400, 400);
// Getting the default chart data worksheet index.
int defaultWorksheetIndex = 0;
// Call the getChartDataWorkbook method to get the chart data worksheet.
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();
// Delete demo series
chart.getChartData().getSeries().clear();
// Add new chart category by calling the add method.
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.getType());
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 1, 3, "Series 2"), chart.getType());
// Take first chart series.
IChartSeries series = chart.getChartData().getSeries().get_Item(0);
// Add new point (1:3) by calling the addDataPointForScatterSeries method.
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 1), fact.getCell(defaultWorksheetIndex, 2, 2, 3));
// Add new point (2:10)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 2), fact.getCell(defaultWorksheetIndex, 3, 2, 10));
// Set the type of series by calling the setType method.
series.setType(ChartType.ScatterWithStraightLinesAndMarkers);
// Set the marker size in the chart by calling the setSize method.
series.getMarker().setSize(10);
// Invoke the setSymbol method to set the marker style in the chart.
series.getMarker().setSymbol(MarkerStyleType.Star);
// Take second chart series
series = chart.getChartData().getSeries().get_Item(1);
// Add new point (5:2) there.
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 3, 5), fact.getCell(defaultWorksheetIndex, 2, 4, 2));
// Add new point (3:1)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 3, 3), fact.getCell(defaultWorksheetIndex, 3, 4, 1));
// Add new point (2:2)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 4, 3, 2), fact.getCell(defaultWorksheetIndex, 4, 4, 2));
// Add new point (5:1)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 5, 3, 5), fact.getCell(defaultWorksheetIndex, 5, 4, 1));
// Changing the chart series marker
series.getMarker().setSize(10);
series.getMarker().setSymbol(MarkerStyleType.Circle);
// Save the output PPTX/PPT file on the disk by calling the save method.
pres.save(dataDir + "AsposeChart_out.pptx", SaveFormat.Pptx);
}
}
java-api-for-charts

Nhận Giấy Phép Miễn Phí

Bạn có thể nhận một giấy phép tạm thời miễn phí để thử nghiệm thư viện biểu đồ Java này mà không gặp giới hạn đánh giá.

Tóm Tắt

Trong khi Aspose.Slides cho Java cung cấp một loạt các tính năng để tạo và thao tác biểu đồ một cách lập trình, có tài liệu toàn diện có sẵn. Ngoài ra, bạn có thể nhảy đến refs API và repo GitHub để bắt đầu phát triển một trình tạo biểu đồ tán xạ trong Java. Hơn nữa, bạn có thể truy cập liên kết này để có cái nhìn nhanh về tất cả các tính năng được cung cấp.

Hãy Liên Hệ Với Chúng Tôi

Bạn có thể cho chúng tôi biết về các câu hỏi hoặc thắc mắc của bạn trên diễn đàn của chúng tôi.

Xem Thêm