Làm việc với biểu đồ
Phương pháp mới insertChart đã được thêm vào lớp DocumentBuilder. Vậy chúng ta sẽ xem cách chèn biểu đồ cột đơn giản vào tài liệu bằng phương pháp chèn biểu đồ.
Làm thế nào để chèn một biểu đồ từ đầu bằng cách sử dụng Aspose.Words
Trong phần này chúng ta sẽ tìm hiểu cách chèn một biểu đồ vào tài liệu.
Chèn Sơ đồ Cột
Mã đoạn sau cho thấy cách chèn biểu đồ cột:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Add chart with default data. You can specify different chart types and sizes. | |
Shape shape = builder.insertChart(ChartType.COLUMN, 432, 252); | |
// Chart property of Shape contains all chart related options. | |
Chart chart = shape.getChart(); | |
// Get chart series collection. | |
ChartSeriesCollection seriesColl = chart.getSeries(); | |
// Delete default generated series. | |
seriesColl.clear(); | |
// Create category names array, in this example we have two categories. | |
String[] categories = new String[]{"AW Category 1", "AW Category 2"}; | |
// Adding new series. Please note, data arrays must not be empty and arrays must be the same size. | |
seriesColl.add("AW Series 1", categories, new double[]{1, 2}); | |
seriesColl.add("AW Series 2", categories, new double[]{3, 4}); | |
seriesColl.add("AW Series 3", categories, new double[]{5, 6}); | |
seriesColl.add("AW Series 4", categories, new double[]{7, 8}); | |
seriesColl.add("AW Series 5", categories, new double[]{9, 10}); | |
doc.save(dataDir + "TestInsertChartColumn1_out.docx"); |
Mã này cho kết quả sau:
Có bốn loại quá tải khác nhau cho phương thức ‘Add’, những phương thức này được mở rộng để bao gồm tất cả các biến thể có thể xảy ra của nguồn dữ liệu cho tất cả các loại biểu đồ:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert Column chart. | |
Shape shape = builder.insertChart(ChartType.COLUMN, 432, 252); | |
Chart chart = shape.getChart(); | |
// Use this overload to add series to any type of Bar, Column, Line and Surface charts. | |
chart.getSeries().add("AW Series 1", new String[]{"AW Category 1", "AW Category 2"}, new double[]{1, 2}); | |
doc.save(dataDir + "TestInsertColumnChart2_out.docx"); |
Mã này tạo ra kết quả sau đây:
Chèn biểu đồ phân tán
Mã sau đây cho thấy cách chèn biểu đồ phân tán:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert Scatter chart. | |
Shape shape = builder.insertChart(ChartType.SCATTER, 432, 252); | |
Chart chart = shape.getChart(); | |
// Use this overload to add series to any type of Scatter charts. | |
chart.getSeries().add("AW Series 1", new double[]{0.7, 1.8, 2.6}, new double[]{2.7, 3.2, 0.8}); | |
doc.save(dataDir + "TestInsertScatterChart_out.docx"); |
Mã này cho ra kết quả sau:
Chèn biểu đồ khu vực
Mã ví dụ cho thấy cách chèn biểu đồ khu vực:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert Area chart. | |
Shape shape = builder.insertChart(ChartType.AREA, 432, 252); | |
Chart chart = shape.getChart(); | |
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); | |
Date date1 = sdf.parse("01/01/2016"); | |
Date date2 = sdf.parse("02/02/2016"); | |
Date date3 = sdf.parse("03/03/2016"); | |
Date date4 = sdf.parse("04/04/2016"); | |
Date date5 = sdf.parse("05/05/2016"); | |
// Use this overload to add series to any type of Area, Radar and Stock charts. | |
chart.getSeries().add("AW Series 1", new Date[]{date1, date2, date3, date4, date5}, new double[]{32, 32, 28, 12, 15}); | |
doc.save(dataDir + "TestInsertAreaChart_out.docx"); |
Mã này cho ra kết quả sau đây:
Chèn biểu đồ bong bóng
Mã ví dụ sau cho thấy cách chèn một biểu đồ bong bóng:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert Bubble chart. | |
Shape shape = builder.insertChart(ChartType.BUBBLE, 432, 252); | |
Chart chart = shape.getChart(); | |
// Use this overload to add series to any type of Bubble charts. | |
chart.getSeries().add("AW Series 1", new double[]{0.7, 1.8, 2.6}, new double[]{2.7, 3.2, 0.8}, new double[]{10, 4, 8}); | |
doc.save(dataDir + "TestInsertBubbleChart_out.docx"); |
Mã tạo ra kết quả sau đây:
Làm việc với các biểu đồ thông qua Shape.Chart
đối tượng
Khi bảng đã được chèn và điền bằng dữ liệu, bạn có thể thay đổi kiểu dáng của nó. Shape.Chart thuộc tính chứa tất cả các tùy chọn liên quan đến đồ thị có sẵn thông qua công cộng API.
Ví dụ thay đổi tiêu đề biểu đồ hoặc hành vi huyền thoại:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertChart(ChartType.LINE, 432, 252); | |
Chart chart = shape.getChart(); | |
// Determines whether the title shall be shown for this chart. Default is true. | |
chart.getTitle().setShow(true); | |
// Setting chart Title. | |
chart.getTitle().setText("Sample Line Chart Title"); | |
// Determines whether other chart elements shall be allowed to overlap title. | |
chart.getTitle().setOverlay(false); | |
// Please note if null or empty value is specified as title text, auto generated title will be shown. | |
// Determines how legend shall be shown for this chart. | |
chart.getLegend().setPosition(LegendPosition.LEFT); | |
chart.getLegend().setOverlay(true); | |
doc.save(dataDir + "ChartAppearance_out.docx"); |
Mã tạo ra kết quả sau đây:
Cách làm việc với ChartSeriesCollection của biểu đồ
Hãy xem vào ChartSeries bộ sưu tập. Tất cả các loạt biểu đồ đều có sẵn thông qua bộ sưu tập chart.getSeries(), là Iterable:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertChart(ChartType.LINE, 432, 252); | |
// Chart property of Shape contains all chart related options. | |
Chart chart = shape.getChart(); | |
// Get chart series collection. | |
ChartSeriesCollection seriesCollection = chart.getSeries(); | |
// Check series count. | |
System.out.println(seriesCollection.getCount()); |
Bạn có thể loại bỏ từng tập theo lượt hoặc xóa tất cả chúng cũng như thêm một tập mới nếu cần thiết. Biểu đồ mới chèn có một số chuỗi mặc định được thêm vào bộ sưu tập này. Để bỏ chúng, bạn cần gọi phương pháp chart.getSeries (). clear ().
Làm việc với lớp Single ChartSeries
Dưới đây là cách để làm việc với một bộ sưu tập cụ thể.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertChart(ChartType.LINE, 432, 252); | |
// Get first series. | |
ChartSeries series0 = shape.getChart().getSeries().get(0); | |
// Get second series. | |
ChartSeries series1 = shape.getChart().getSeries().get(1); | |
// Change first series name. | |
series0.setName("My Name1"); | |
// Change second series name. | |
series1.setName("My Name2"); | |
// You can also specify whether the line connecting the points on the chart shall be smoothed using Catmull-Rom splines. | |
series0.setSmooth(true); | |
series1.setSmooth(true); | |
doc.save(dataDir + "SingleChartSeries_out.docx"); |
Xin xem kết quả ở bên dưới:
Tất cả các giá trị đơn ChartSeries có các tùy chọn mặc định ChartDataPoint, xin thử sử dụng mã sau để thay đổi chúng:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertChart(ChartType.LINE, 432, 252); | |
// Get first series. | |
ChartSeries series0 = shape.getChart().getSeries().get(0); | |
// Get second series. | |
ChartSeries series1 = shape.getChart().getSeries().get(1); | |
// Specifies whether by default the parent element shall inverts its colors if the value is negative. | |
series0.setInvertIfNegative(true); | |
// Set default marker symbol and size. | |
series0.getMarker().setSymbol(MarkerSymbol.CIRCLE); | |
series0.getMarker().setSize(15); | |
series1.getMarker().setSymbol(MarkerSymbol.STAR); | |
series1.getMarker().setSize(10); | |
doc.save(dataDir + "ChartDataPoints_out.docx");// |
Cách làm việc với Single ChartDataPoint của một ChartSeries
Sử dụng ChartDataPoint, bạn có thể tùy chỉnh định dạng của một điểm dữ liệu đơn trong chuỗi biểu đồ:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertChart(ChartType.LINE, 432, 252); | |
// Get first series. | |
ChartSeries series0 = shape.getChart().getSeries().get(0); | |
// Get second series. | |
ChartSeries series1 = shape.getChart().getSeries().get(1); | |
ChartDataPointCollection dataPointCollection = series0.getDataPoints(); | |
// Add data point to the first and second point of the first series. | |
ChartDataPoint dataPoint00 = dataPointCollection.add(0); | |
ChartDataPoint dataPoint01 = dataPointCollection.add(1); | |
// Set explosion. | |
dataPoint00.setExplosion(50); | |
// Set marker symbol and size. | |
dataPoint00.getMarker().setSymbol(MarkerSymbol.CIRCLE); | |
dataPoint00.getMarker().setSize(15); | |
dataPoint01.getMarker().setSymbol(MarkerSymbol.DIAMOND); | |
dataPoint01.getMarker().setSize(20); | |
// Add data point to the third point of the second series. | |
ChartDataPoint dataPoint12 = series1.getDataPoints().add(2); | |
dataPoint12.setInvertIfNegative(true); | |
dataPoint12.getMarker().setSymbol(MarkerSymbol.STAR); | |
dataPoint12.getMarker().setSize(20); | |
doc.save(dataDir + "SingleChartDataPointOfAChartSeries_out.docx"); |
Hãy xem kết quả dưới đây:
Làm thế nào để làm việc với ChartDataLabel của một Single ChartSeries
Sử dụng ChartDataLabel, bạn có thể chỉ định định dạng của một nhãn dữ liệu duy nhất trên chuỗi biểu đồ, chẳng hạn như hiển thị / ẩn LegendKey, CategoryName, SeriesName, giá trị,.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertChart(ChartType.BAR, 432, 252); | |
// Get first series. | |
ChartSeries series0 = shape.getChart().getSeries().get(0); | |
series0.hasDataLabels(true); | |
// Set properties. | |
series0.getDataLabels().setShowLegendKey(true); | |
// By default, when you add data labels to the data points in a pie chart, leader lines are displayed for data labels that are | |
// positioned far outside the end of data points. Leader lines create a visual connection between a data label and its | |
// corresponding data point. | |
series0.getDataLabels().setShowLeaderLines(true); | |
series0.getDataLabels().setShowCategoryName(false); | |
series0.getDataLabels().setShowPercentage(false); | |
series0.getDataLabels().setShowSeriesName(true); | |
series0.getDataLabels().setShowValue(true); | |
series0.getDataLabels().setSeparator("/"); | |
series0.getDataLabels().setShowValue(true); | |
doc.save(dataDir + "ChartDataLabelOfASingleChartSeries_out.docx"); |
Xin vui lòng xem kết quả dưới đây:
Làm thế nào để xác định các tùy chọn mặc định cho ChartDataLabels của ChartSeries
Lớp ChartDataLabelCollection xác định các thuộc tính có thể được sử dụng để đặt tùy chọn mặc định cho ChartDataLabels cho Biểu đồ Series. Những tính năng này bao gồm setShowCategoryName, setShowBubbleSize, setShowPercentage, setShowSeriesName, setShowValue, vv
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertChart(ChartType.PIE, 432, 252); | |
Chart chart = shape.getChart(); | |
chart.getSeries().clear(); | |
ChartSeries series = chart.getSeries().add("Series 1", new String[] { "Category1", "Category2", "Category3" }, | |
new double[] { 2.7, 3.2, 0.8 }); | |
ChartDataLabelCollection labels = series.getDataLabels(); | |
labels.setShowPercentage(true); | |
labels.setShowValue(true); | |
labels.setShowLeaderLines(false); | |
labels.setSeparator(" - "); | |
doc.save(dataDir + "Demo.docx"); |
Xin vui lòng xem kết quả dưới đây:
Làm sao định dạng số của nhãn dữ liệu biểu đồ
Sử dụng thuộc tính NumberFormat, bạn có thể chỉ định số dạng của một nhãn dữ liệu đơn trong biểu đồ.
Mã ví dụ sau cho thấy cách định dạng một số của nhãn dữ liệu:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Add chart with default data. | |
Shape shape = builder.insertChart(ChartType.LINE, 432, 252); | |
Chart chart = shape.getChart(); | |
chart.getTitle().setText("Data Labels With Different Number Format"); | |
// Delete default generated series. | |
chart.getSeries().clear(); | |
// Add new series | |
ChartSeries series0 = chart.getSeries().add("AW Series 0", new String[] { "AW0", "AW1", "AW2" }, | |
new double[] { 2.5, 1.5, 3.5 }); | |
series0.hasDataLabels(true); | |
// Set currency format code. | |
series0.getDataLabels().get(0).getNumberFormat().setFormatCode("\"$\"#,##0.00"); | |
// Set date format code. | |
series0.getDataLabels().get(1).getNumberFormat().setFormatCode("d/mm/yyyy"); | |
// Set percentage format code. | |
series0.getDataLabels().get(2).getNumberFormat().setFormatCode("0.00%"); | |
// Or you can set format code to be linked to a source cell, | |
// in this case NumberFormat will be reset to general and inherited from a | |
// source cell. | |
series0.getDataLabels().get(2).getNumberFormat().isLinkedToSource(true); | |
doc.save(dataDir + "NumberFormat_DataLabel_out.docx"); |
Cách thiết lập thuộc tính trục biểu đồ
Nếu bạn muốn làm việc với trục biểu đồ, quy mô và đơn vị hiển thị cho trục giá trị, vui lòng sử dụng các lớp ChartAxis, AxisDisplayUnit, và AxisScaling.
Ví dụ sau cho thấy cách định nghĩa các thuộc tính trục X và Y:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert chart. | |
Shape shape = builder.insertChart(ChartType.AREA, 432, 252); | |
Chart chart = shape.getChart(); | |
// Clear demo data. | |
chart.getSeries().clear(); | |
// Fill data. | |
chart.getSeries().add("AW Series 1", | |
new Date[]{new Date(2002, 1, 1), new Date(2002, 6, 1), new Date(2015, 7, 1), new Date(2015, 8, 1), new Date(2015, 9, 1)}, | |
new double[]{640, 320, 280, 120, 150}); | |
ChartAxis xAxis = chart.getAxisX(); | |
ChartAxis yAxis = chart.getAxisY(); | |
// Change the X axis to be category instead of date, so all the points will be put with equal interval on the X axis. | |
xAxis.setCategoryType(AxisCategoryType.CATEGORY); | |
// Define X axis properties. | |
xAxis.setCrosses(AxisCrosses.CUSTOM); | |
xAxis.setCrossesAt(3); // measured in display units of the Y axis (hundreds) | |
xAxis.setReverseOrder(true); | |
xAxis.setMajorTickMark(AxisTickMark.CROSS); | |
xAxis.setMinorTickMark(AxisTickMark.OUTSIDE); | |
xAxis.setTickLabelOffset(200); | |
// Define Y axis properties. | |
yAxis.setTickLabelPosition(AxisTickLabelPosition.HIGH); | |
yAxis.setMajorUnit(100); | |
yAxis.setMinorUnit(50); | |
yAxis.getDisplayUnit().setUnit(AxisBuiltInUnit.HUNDREDS); | |
yAxis.getScaling().setMinimum(new AxisBound(100)); | |
yAxis.getScaling().setMaximum(new AxisBound(700)); | |
dataDir = dataDir + "SetAxisProperties_out.docx"; | |
doc.save(dataDir); |
Cách Đặt Date Giá trị Thời gian của Trục
Mã ví dụ sau cho thấy cách đặt giá trị ngày/giờ cho các thuộc tính trục:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert chart. | |
Shape shape = builder.insertChart(ChartType.COLUMN, 432, 252); | |
Chart chart = shape.getChart(); | |
// Clear demo data. | |
chart.getSeries().clear(); | |
// Fill data. | |
chart.getSeries().add("AW Series 1", | |
new Date[]{new Date(2017, 11, 06), new Date(2017, 11, 9), new Date(2017, 11, 15), | |
new Date(2017, 11, 21), new Date(2017, 11, 25), new Date(2017, 11, 29)}, | |
new double[]{1.2, 0.3, 2.1, 2.9, 4.2, 5.3} | |
); | |
// Set X axis bounds. | |
ChartAxis xAxis = chart.getAxisX(); | |
xAxis.getScaling().setMinimum(new AxisBound(new Date(2017, 11, 5).getTime())); | |
xAxis.getScaling().setMaximum(new AxisBound(new Date(2017, 12, 3).getTime())); | |
// Set major units to a week and minor units to a day. | |
xAxis.setMajorUnit(7); | |
xAxis.setMinorUnit(1); | |
xAxis.setMajorTickMark(AxisTickMark.CROSS); | |
xAxis.setMinorTickMark(AxisTickMark.OUTSIDE); | |
dataDir = dataDir + "SetDateTimeValuesToAxis_out.docx"; | |
doc.save(dataDir); |
Cách định dạng giá trị số của trục
Mã ví dụ sau cho thấy cách thay đổi định dạng số trên trục giá trị:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert chart. | |
Shape shape = builder.insertChart(ChartType.COLUMN, 432, 252); | |
Chart chart = shape.getChart(); | |
// Clear demo data. | |
chart.getSeries().clear(); | |
// Fill data. | |
chart.getSeries().add("AW Series 1", | |
new String[]{"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}, | |
new double[]{1900000, 850000, 2100000, 600000, 1500000}); | |
// Set number format. | |
chart.getAxisY().getNumberFormat().setFormatCode("#,##0"); | |
dataDir = dataDir + "FormatAxisNumber_out.docx"; | |
doc.save(dataDir); |
Cách thiết lập giới hạn trục
Lớp AxisBound
đại diện cho giới hạn tối thiểu hoặc tối đa của các giá trị trục. Bound có thể được chỉ định dưới dạng một giá trị số, ngày giờ hoặc một giá trị đặc biệt “auto”.
Ví dụ mã sau cho thấy cách thiết lập ranh giới của một trục:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert chart. | |
Shape shape = builder.insertChart(ChartType.COLUMN, 432, 252); | |
Chart chart = shape.getChart(); | |
// Clear demo data. | |
chart.getSeries().clear(); | |
// Fill data. | |
chart.getSeries().add("AW Series 1", | |
new String[]{"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}, | |
new double[]{1.2, 0.3, 2.1, 2.9, 4.2}); | |
chart.getAxisY().getScaling().setMinimum(new AxisBound(0)); | |
chart.getAxisY().getScaling().setMaximum(new AxisBound(6)); | |
dataDir = dataDir + "SetboundsOfAxis_out.docx"; | |
doc.save(dataDir); |
Cách đặt khoảng thời gian giữa nhãn
Mã ví dụ sau cho thấy cách đặt khoảng thời gian giữa các nhãn trên trục:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert chart. | |
Shape shape = builder.insertChart(ChartType.COLUMN, 432, 252); | |
Chart chart = shape.getChart(); | |
// Clear demo data. | |
chart.getSeries().clear(); | |
// Fill data. | |
chart.getSeries().add("AW Series 1", | |
new String[]{"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}, | |
new double[]{1.2, 0.3, 2.1, 2.9, 4.2}); | |
chart.getAxisX().setTickLabelSpacing(2); | |
dataDir = dataDir + "SetIntervalUnitBetweenLabelsOnAxis_out.docx"; | |
doc.save(dataDir); |
Cách ẩn trục biểu đồ
Nếu bạn muốn hiển thị hoặc ẩn trục biểu đồ, bạn có thể đơn giản đạt được điều này bằng cách đặt giá trị của thuộc tính ChartAxis.Hidden
Mã ví dụ bên dưới cho thấy cách ẩn trục Y của biểu đồ:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert chart. | |
Shape shape = builder.insertChart(ChartType.COLUMN, 432, 252); | |
Chart chart = shape.getChart(); | |
// Clear demo data. | |
chart.getSeries().clear(); | |
// Fill data. | |
chart.getSeries().add("AW Series 1", | |
new String[]{"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}, | |
new double[]{1.2, 0.3, 2.1, 2.9, 4.2}); | |
// Hide the Y axis. | |
chart.getAxisY().setHidden(true); | |
dataDir = dataDir + "HideChartAxis_out.docx"; | |
doc.save(dataDir); |
Làm thế nào để sắp xếp nhãn biểu đồ
Nếu bạn muốn đặt một kiểu căn chỉnh văn bản cho các nhãn đa dòng, bạn có thể dễ dàng đạt được điều đó bằng cách đặt giá trị của setTickLabelAlignment() thuộc tính.
Mã ví dụ sau cho thấy cách đánh dấu căn nhãn:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Document.docx"); | |
Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true); | |
ChartAxis axis = shape.getChart().getAxisX(); | |
//This property has effect only for multi-line labels. | |
axis.setTickLabelAlignment(ParagraphAlignment.RIGHT); | |
doc.save(dataDir + "Document_out.docx"); |
Cách đặt định dạng màu lấp và nét viền
Định dạng điền và nét vẽ có thể được đặt cho các chuỗi biểu đồ, điểm dữ liệu và dấu hiệu. Để làm được điều này, bạn cần sử dụng các thuộc tính của kiểu ChartFormat
trong lớp ChartSeries, ChartDataPoint và ChartMarker; cũng như các đại diện cho một số thuộc tính, chẳng hạn như ForeColor, BackColor, Visible và Transparency trong lớp Stroke
.
Ví dụ về mã này cho thấy cách đặt màu của chuỗi:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Column, 432, 252);
Chart chart = shape.Chart;
ChartSeriesCollection seriesColl = chart.Series;
// Delete default generated series.
seriesColl.Clear();
// Create category names array.
string[] categories = new string[] { "AW Category 1", "AW Category 2" };
// Adding new series. Value and category arrays must be the same size.
ChartSeries series1 = seriesColl.Add("AW Series 1", categories, new double[] { 1, 2 });
ChartSeries series2 = seriesColl.Add("AW Series 2", categories, new double[] { 3, 4 });
ChartSeries series3 = seriesColl.Add("AW Series 3", categories, new double[] { 5, 6 });
// Set series color.
series1.Format.Fill.ForeColor = Color.Red;
series2.Format.Fill.ForeColor = Color.Yellow;
series3.Format.Fill.ForeColor = Color.Blue;
doc.Save("ColumnColor.docx");
Mã ví dụ cho thấy cách đặt màu và trọng lượng dòng:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Line, 432, 252);
Chart chart = shape.Chart;
ChartSeriesCollection seriesColl = chart.Series;
// Delete default generated series.
seriesColl.Clear();
// Adding new series.
ChartSeries series1 = seriesColl.Add("AW Series 1", new double[] { 0.7, 1.8, 2.6 }, new double[] { 2.7, 3.2, 0.8 });
ChartSeries series2 = seriesColl.Add("AW Series 2", new double[] { 0.5, 1.5, 2.5 }, new double[] { 3, 1, 2 });
// Set series color.
series1.Format.Stroke.ForeColor = Color.Red;
series1.Format.Stroke.Weight = 5;
series2.Format.Stroke.ForeColor = Color.LightGreen;
series2.Format.Stroke.Weight = 5;
doc.Save("LineColorAndWeight.docx");