Create a Box and Whisker Chart in Java Programmatically

กราฟมีบทบาทสำคัญในการแสดงข้อมูล พวกเขาช่วยในการสื่อสารข้อมูลที่ซับซ้อนได้อย่างชัดเจน กราฟ Box และ Whisker เป็นเครื่องมือที่มีประโยชน์โดยเฉพาะสำหรับการวิเคราะห์ทางสถิติ มันแสดงการกระจายของข้อมูลและระบุค่าผิดปกติได้อย่างมีประสิทธิภาพ หลายอุตสาหกรรม เช่น การเงิน การดูแลสุขภาพ และการศึกษา ขึ้นอยู่กับกราฟเหล่านี้เพื่อการตัดสินใจ ในโพสต์นี้เราจะสำรวจวิธีการสร้างกราฟ Box และ Whisker ใน Java โดยใช้ Aspose.Slides สำหรับ Java.

บทความนี้จะครอบคลุมหัวข้อต่อไปนี้:

การติดตั้ง Java Graph Library

Aspose.Slides สำหรับ Java ช่วยให้การสร้างกราฟเช่นกราฟ Box และ Whisker เป็นเรื่องง่าย ไลบรารีนี้ถูกออกแบบมาโดยเฉพาะสำหรับการจัดการงานนำเสนอ PowerPoint ใน Java มันมีฟีเจอร์ที่ทรงพลังเพื่อสร้าง แก้ไข และจัดการสไลด์และกราฟได้อย่างราบรื่น Aspose.Slides ช่วยเพิ่มประสิทธิภาพการทำงานโดยการให้ API ระดับสูง ทำให้การพัฒนากราฟที่ซับซ้อนใน Java เป็นเรื่องง่าย

Aspose.Slides สำหรับ Java มีคุณสมบัติสำคัญหลายประการ:

  • ความสะดวกในการใช้งาน: เพิ่มไลบรารีลงในโปรเจ็กต์ Java ของคุณได้อย่างง่ายดายโดยใช้ Maven หรือ Gradle
  • ตัวเลือกการปรับแต่งขั้นสูง: ปรับแต่งองค์ประกอบ กราฟ สไตล์ และรูปแบบตามที่คุณต้องการ
  • ผลลัพธ์คุณภาพสูง: สร้างงานนำเสนอในระดับมืออาชีพที่รักษาความละเอียดทางภาพสูง
  • เอกสารที่ครอบคลุม: เข้าถึงเอกสารและตัวอย่างที่ละเอียดเพื่อช่วยให้คุณเริ่มต้นได้อย่างรวดเร็ว

หากต้องการเริ่มต้นใช้งาน Aspose.Slides สำหรับ Java ให้ดาวน์โหลดได้จาก ที่นี่ และเพิ่มลงในโปรเจ็กต์ของคุณ ด้านล่างนี้คือการพึ่งพา Maven ที่ต้องใส่ใน pom.xml ของคุณ:

<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>

สร้างกราฟ Box และ Whisker ใน Java - ตัวอย่างโค้ด

ทำตามขั้นตอนด้านล่างเพื่อสร้างกราฟ Box และ Whisker ใน Java โดยใช้ Aspose.Slides สำหรับ Java:

  1. สร้างออบเจ็กต์ของคลาส Presentation และโหลดไฟล์ PPTX ต้นฉบับ
  2. เรียกใช้เมธอด addChart เพื่อสร้างกราฟในสไลด์
  3. เมธอด clear จะล้างหมวดหมู่และชุดข้อมูล
  4. getChartDataWorkbook จะได้รับเซลล์สำหรับสร้างเซลล์ที่ใช้สำหรับชุดข้อมูลหรือหมวดหมู่
  5. เรียกใช้ฟังก์ชัน add เพื่อเพิ่มหมวดหมู่
  6. สร้างชุดข้อมูลกราฟใหม่และ add ลงในคอลเลกชัน
  7. เรียกใช้เมธอด setQuartileMethod
  8. ส่งค่าบูลีนไปยังเมธอด setShowMeanLine, setShowMeanMarkers, setShowOutlierPoints
  9. เรียกใช้เมธอด addDataPointForBoxAndWhiskerSeries เพื่อสร้างจุดข้อมูลใหม่และเพิ่มลงในคอลเลกชัน
  10. เมธอด save จะบันทึกไฟล์ PPTX ที่มีกราฟ Box ลงในดิสก์

นี่คือตัวอย่างโค้ด:

public static void main(String[] args)
{
// Set the path to the working directory.
String dir = "/Desktop/";
// Create an instance of the Presentation class and load the source PPTX file.
Presentation pre = new Presentation(dir + "output.pptx");
try
{
// Invoke the addChart method to create a chart into a slide.
IChart chart = pre.getSlides().get_Item(0).getShapes().addChart(ChartType.BoxAndWhisker, 50, 50, 500, 400);
// The clear method will clear the categories and series.
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
// getChartDataWorkbook gets the cells factory to create cells used for chart series or categories.
IChartDataWorkbook wb = chart.getChartData().getChartDataWorkbook();
wb.clear(0);
// Invoke the add function to add the categories.
chart.getChartData().getCategories().add(wb.getCell(0, "A1", "Category 1"));
chart.getChartData().getCategories().add(wb.getCell(0, "A2", "Category 1"));
chart.getChartData().getCategories().add(wb.getCell(0, "A3", "Category 1"));
chart.getChartData().getCategories().add(wb.getCell(0, "A4", "Category 1"));
chart.getChartData().getCategories().add(wb.getCell(0, "A5", "Category 1"));
chart.getChartData().getCategories().add(wb.getCell(0, "A6", "Category 1"));
// Create a new chart series and add it to the collection.
IChartSeries series = chart.getChartData().getSeries().add(ChartType.BoxAndWhisker);
// Represent quartile method.
series.setQuartileMethod(QuartileMethodType.Exclusive);
// Pass boolean values to the setShowMeanLine, setShowMeanMarkers, setShowOutlierPoints methods.
series.setShowMeanLine(true);
series.setShowMeanMarkers(true);
series.setShowOutlierPoints(true);
// Call the addDataPointForBoxAndWhiskerSeries method to create the new data point and adds it to the end of collection.
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(wb.getCell(0, "B1", 15));
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(wb.getCell(0, "B2", 41));
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(wb.getCell(0, "B3", 16));
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(wb.getCell(0, "B4", 10));
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(wb.getCell(0, "B5", 23));
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(wb.getCell(0, "B6", 16));
// The save method will save the PPTX file containing the Box chart onto the disk.
pre.save(dir+"BoxAndWhisker.pptx", SaveFormat.Pptx);
}
finally
{
if (pre != null) pre.dispose();
}
}
ผลลัพธ์:
box-chart-maker

รับใบอนุญาตฟรี

สนใจในการสำรวจผลิตภัณฑ์ Aspose? เยี่ยมชม ที่นี่ เพื่อขอรับใบอนุญาตชั่วคราวฟรีสำหรับ Aspose.Slides การเริ่มต้นทำได้ง่ายและปลดล็อกศักยภาพทั้งหมดของแอปพลิเคชัน Java ของคุณ!

ทรัพยากรฟรี

นอกจากข้อมูลที่ให้ไว้ในบล็อกนี้แล้ว ยังมีทรัพยากรเพิ่มเติมอีกมากมาย ซึ่งรวมถึงการสอน เอกสาร API และฟอรัมชุมชน ซึ่งสามารถช่วยให้คุณเข้าใจและพัฒนาทักษะในการใช้งาน Aspose.Slides สำหรับ Java ได้ดียิ่งขึ้น

สรุป {#conclusion