المحور X مقابل محور الفئة
سيناريوهات الاستخدام المحتملة
هناك أنواع مختلفة من المحاور X. بينما يكون المحور Y محور نوع قيمة، يمكن أن يكون المحور X محور نوع فئة أو محور نوع قيمة. باستخدام محور القيمة، يتم معاملة البيانات كبيانات عددية متغيرة بشكل مستمر، ويتم وضع العلامة في نقطة على طول المحور التي تتغير وفقًا لقيمتها العددية. باستخدام محور الفئة، يتم معاملة البيانات كسلسلة من علامات النص غير الرقمية، ويتم وضع العلامة في نقطة على طول المحور وفقًا لموقعها في التسلسل. يوضح المثال أدناه الفرق بين محاور القيمة والفئة. يتم عرض البيانات العينية لدينا في ملف الجدول العيني أدناه. تحتوي العمود الأول على بيانات محور X الخاصة بنا، والتي يمكن معاملتها كفئات أو قيم. لاحظ أن الأرقام ليست منتظمة بالتساوي، ولا تظهر حتى بترتيب عددي.
قم بالتعامل مع المحور X ومحور الفئة على غرار Microsoft Excel
سنعرض هذه البيانات على نوعين من الرسوم البيانية، الرسم البياني الأول هو رسم XY (الانتشار) مع المحور X كمحور القيمة، والرسم البياني الثاني هو رسم الخط مع المحور X كمحور الفئة.
كود العينة التالي يولد ملف إكسل الناتج.
الكود المثالي
// Create an instance of Workbook | |
Workbook workbook = new Workbook(); | |
// Access the first worksheet. | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Put the sample values used in charts | |
worksheet.getCells().get("A2").putValue(1); | |
worksheet.getCells().get("A3").putValue(3); | |
worksheet.getCells().get("A4").putValue(2.5); | |
worksheet.getCells().get("A5").putValue(3.5); | |
worksheet.getCells().get("B1").putValue("Cats"); | |
worksheet.getCells().get("C1").putValue("Dogs"); | |
worksheet.getCells().get("D1").putValue("Fishes"); | |
worksheet.getCells().get("B2").putValue(7); | |
worksheet.getCells().get("B3").putValue(6); | |
worksheet.getCells().get("B4").putValue(5); | |
worksheet.getCells().get("B5").putValue(4); | |
worksheet.getCells().get("C2").putValue(7); | |
worksheet.getCells().get("C3").putValue(5); | |
worksheet.getCells().get("C4").putValue(4); | |
worksheet.getCells().get("C5").putValue(3); | |
worksheet.getCells().get("D2").putValue(8); | |
worksheet.getCells().get("D3").putValue(7); | |
worksheet.getCells().get("D4").putValue(3); | |
worksheet.getCells().get("D5").putValue(2); | |
//Create Line Chart: X as Category Axis | |
int pieIdx = worksheet.getCharts().add(ChartType.LINE_WITH_DATA_MARKERS, 6, 15, 20, 21); | |
// Retrieve the Chart object | |
Chart chart = worksheet.getCharts().get(pieIdx); | |
// Add Series | |
chart.getNSeries().add("B2:D5", true); | |
// Set the category data | |
chart.getNSeries().setCategoryData("=Sheet1!$A$2:$A$5"); | |
// Set the first series name | |
chart.getNSeries().get(0).setName("Cats"); | |
// Set the second series name | |
chart.getNSeries().get(1).setName("Dogs"); | |
// Set the third series name | |
chart.getNSeries().get(2).setName("Fishes"); | |
// Set the Legend at the bottom of the chart area | |
chart.getLegend().setPosition(LegendPositionType.BOTTOM); | |
// Fill the PlotArea area with nothing | |
chart.getPlotArea().getArea().getFillFormat().setFillType(FillType.NONE); | |
// Create XY (Scatter) Chart: X as Value Axis | |
pieIdx = worksheet.getCharts().add(ChartType.SCATTER_CONNECTED_BY_LINES_WITH_DATA_MARKER, 6, 6, 20, 12); | |
// Retrieve the Chart object | |
chart = worksheet.getCharts().get(pieIdx); | |
// Add Series | |
chart.getNSeries().add("B2:D5", true); | |
// Set X values for series | |
chart.getNSeries().get(0).setXValues("{1,3,2.5,3.5}"); | |
chart.getNSeries().get(1).setXValues("{1,3,2.5,3.5}"); | |
chart.getNSeries().get(2).setXValues("{1,3,2.5,3.5}"); | |
// Set the first series name | |
chart.getNSeries().get(0).setName("Cats"); | |
// Set the second series name | |
chart.getNSeries().get(1).setName("Dogs"); | |
// Set the third series name | |
chart.getNSeries().get(2).setName("Fishes"); | |
// Set the Legend at the bottom of the chart area | |
chart.getLegend().setPosition(LegendPositionType.BOTTOM); | |
// Fill the PlotArea area with nothing | |
chart.getPlotArea().getArea().getFillFormat().setFillType(FillType.NONE); | |
// Save the Excel file | |
workbook.save("XAxis.xlsx"); | |