การทำงานกับแผนภูมิ

Contents
[ ]

มีการเพิ่มวิธี InsertChart ใหม่ลงในคลาส DocumentBuilder มาดูวิธีแทรกแผนภูมิคอลัมน์อย่างง่ายลงในเอกสารโดยใช้วิธี InsertChart

วิธีแทรกแผนภูมิ

ในส่วนนี้ เราจะได้เรียนรู้วิธีแทรกแผนภูมิลงในเอกสาร

แทรกแผนภูมิคอลัมน์

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกแผนภูมิคอลัมน์:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
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.Chart;
// Get chart series collection.
ChartSeriesCollection seriesColl = chart.Series;
// Check series count.
Console.WriteLine(seriesColl.Count);
// 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 });
dataDir = dataDir + @"TestInsertSimpleChartColumn_out.doc";
doc.Save(dataDir);

รหัสให้ผลลัพธ์ดังต่อไปนี้:

create-column-chart-aspose-words-net

มีโอเวอร์โหลดที่แตกต่างกันสี่แบบสำหรับวิธีการเพิ่มแบบอนุกรม ซึ่งเปิดเผยให้ครอบคลุมแหล่งข้อมูลรูปแบบต่างๆ ที่เป็นไปได้ทั้งหมดสำหรับแผนภูมิทุกประเภท:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert Column chart.
Shape shape = builder.InsertChart(ChartType.Column, 432, 252);
Chart chart = shape.Chart;
// Use this overload to add series to any type of Bar, Column, Line and Surface charts.
chart.Series.Add("AW Series 1", new string[] { "AW Category 1", "AW Category 2" }, new double[] { 1, 2 });
dataDir = dataDir + @"TestInsertChartColumn_out.doc";
doc.Save(dataDir);

รหัสให้ผลลัพธ์ดังต่อไปนี้:

create-column-chart-from-datasource-aspose-words-net

แทรกแผนภูมิกระจาย

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกแผนภูมิกระจาย:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithCharts();
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert Scatter chart.
Shape shape = builder.InsertChart(ChartType.Scatter, 432, 252);
Chart chart = shape.Chart;
// Use this overload to add series to any type of Scatter charts.
chart.Series.Add("AW Series 1", new double[] { 0.7, 1.8, 2.6 }, new double[] { 2.7, 3.2, 0.8 });
dataDir = dataDir + "TestInsertScatterChart_out.docx";
doc.Save(dataDir);

รหัสให้ผลลัพธ์ดังต่อไปนี้:

scatter-chart-aspose-words-net

แทรกแผนภูมิพื้นที่

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกแผนภูมิพื้นที่:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithCharts();
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert Area chart.
Shape shape = builder.InsertChart(ChartType.Area, 432, 252);
Chart chart = shape.Chart;
// Use this overload to add series to any type of Area, Radar and Stock charts.
chart.Series.Add("AW Series 1", new DateTime[] {
new DateTime(2002, 05, 01),
new DateTime(2002, 06, 01),
new DateTime(2002, 07, 01),
new DateTime(2002, 08, 01),
new DateTime(2002, 09, 01)},
new double[] { 32, 32, 28, 12, 15 });
dataDir = dataDir + @"TestInsertAreaChart_out.docx";
doc.Save(dataDir);

รหัสให้ผลลัพธ์ดังต่อไปนี้:

area-chart-aspose-words-net

แทรกแผนภูมิฟอง

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกแผนภูมิฟอง:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithCharts();
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert Bubble chart.
Shape shape = builder.InsertChart(ChartType.Bubble, 432, 252);
Chart chart = shape.Chart;
// Use this overload to add series to any type of Bubble charts.
chart.Series.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 });
dataDir = dataDir + @"TestInsertBubbleChart_out.docx";
doc.Save(dataDir);

รหัสให้ผลลัพธ์ดังต่อไปนี้:

bubble-chart-aspose-words-net

การทำงานกับแผนภูมิผ่านวัตถุ Shape.Chart

เมื่อแทรกแผนภูมิและเติมข้อมูลแล้ว คุณจะสามารถเปลี่ยนรูปลักษณ์ได้ คุณสมบัติ Shape.Chart มีตัวเลือกที่เกี่ยวข้องกับแผนภูมิทั้งหมดที่มีอยู่ใน API สาธารณะ

ตัวอย่างเช่น เปลี่ยนชื่อแผนภูมิหรือลักษณะการทำงานของคำอธิบายแผนภูมิ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithCharts();
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Line, 432, 252);
Chart chart = shape.Chart;
// Determines whether the title shall be shown for this chart. Default is true.
chart.Title.Show = true;
// Setting chart Title.
chart.Title.Text = "Sample Line Chart Title";
// Determines whether other chart elements shall be allowed to overlap title.
chart.Title.Overlay = 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.Legend.Position = LegendPosition.Left;
chart.Legend.Overlay = true;
dataDir = dataDir + @"SimpleLineChart_out.docx";
doc.Save(dataDir);

รหัสสร้างผลลัพธ์ดังต่อไปนี้:

line-chart-aspose-words-net

วิธีทำงานกับ ChartSeriesCollection ของแผนภูมิ

มาดูคอลเลกชัน ChartSeries กันดีกว่า ชุดแผนภูมิทั้งหมดมีอยู่ในคอลเลกชัน chart.Series ซึ่งก็คือ IEnumerable:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Get chart series collection.
ChartSeriesCollection seriesColl = chart.Series;
// Check series count.
Console.WriteLine(seriesColl.Count);

คุณสามารถลบซีรีส์ทีละรายการหรือล้างซีรีส์ทั้งหมด รวมถึงเพิ่มซีรี่ส์ใหม่ได้หากจำเป็น แผนภูมิที่แทรกใหม่มีชุดข้อมูลเริ่มต้นบางชุดที่เพิ่มลงในคอลเลกชันนี้ หากต้องการลบออกคุณต้องเรียกใช้วิธี แผนภูมิซีรีส์.เคลียร์()

การทำงานกับคลาส ChartSeries เดี่ยว

ต่อไปนี้เป็นวิธีทำงานกับซีรีส์เฉพาะ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Get first series.
ChartSeries series0 = shape.Chart.Series[0];
// Get second series.
ChartSeries series1 = shape.Chart.Series[1];
// Change first series name.
series0.Name = "My Name1";
// Change second series name.
series1.Name = "My Name2";
// You can also specify whether the line connecting the points on the chart shall be smoothed using Catmull-Rom splines.
series0.Smooth = true;
series1.Smooth = true;

โปรดดูผลลัพธ์ด้านล่าง:

line-chart-chartseries-aspose-words-net

ChartSeries เดียวทั้งหมดมีตัวเลือก ChartDataPoint เริ่มต้น โปรดลองใช้รหัสต่อไปนี้เพื่อเปลี่ยนแปลง:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Specifies whether by default the parent element shall inverts its colors if the value is negative.
series0.InvertIfNegative = true;
// Set default marker symbol and size.
series0.Marker.Symbol = MarkerSymbol.Circle;
series0.Marker.Size = 15;
series1.Marker.Symbol = MarkerSymbol.Star;
series1.Marker.Size = 10;

โปรดดูผลลัพธ์ด้านล่าง:

line-chart-chartdatapoint-aspose-words-net

วิธีทำงานกับ Single ChartDataPoint ของ ChartSeries

การใช้ ChartDataPoint คุณสามารถปรับแต่งการจัดรูปแบบของจุดข้อมูลจุดเดียวของชุดแผนภูมิได้:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithCharts();
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Line, 432, 252);
Chart chart = shape.Chart;
// Get first series.
ChartSeries series0 = shape.Chart.Series[0];
// Get second series.
ChartSeries series1 = shape.Chart.Series[1];
ChartDataPointCollection dataPointCollection = series0.DataPoints;
// 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.Explosion = 50;
// Set marker symbol and size.
dataPoint00.Marker.Symbol = MarkerSymbol.Circle;
dataPoint00.Marker.Size = 15;
dataPoint01.Marker.Symbol = MarkerSymbol.Diamond;
dataPoint01.Marker.Size = 20;
// Add data point to the third point of the second series.
ChartDataPoint dataPoint12 = series1.DataPoints.Add(2);
dataPoint12.InvertIfNegative = true;
dataPoint12.Marker.Symbol = MarkerSymbol.Star;
dataPoint12.Marker.Size = 20;
dataDir = dataDir + @"SingleChartDataPoint_out.docx";
doc.Save(dataDir);

โปรดดูผลลัพธ์ด้านล่าง:

line-chart-datapoint-aspose-words-net

วิธีทำงานกับ ChartDataLabel ของ ChartSeries เดี่ยว

การใช้ ChartDataLabel ทำให้คุณสามารถระบุการจัดรูปแบบของป้ายกำกับข้อมูลเดียวของชุดแผนภูมิได้ เช่น แสดง/ซ่อน LegendKey, CategoryName, SeriesName, Value ฯลฯ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Bar, 432, 252);
Chart chart = shape.Chart;
// Get first series.
ChartSeries series0 = shape.Chart.Series[0];
ChartDataLabelCollection labels = series0.DataLabels;
// Set properties.
labels.ShowLegendKey = 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.
labels.ShowLeaderLines = true;
labels.ShowCategoryName = false;
labels.ShowPercentage = false;
labels.ShowSeriesName = true;
labels.ShowValue = true;
labels.Separator = "/";
labels.ShowValue = true;
dataDir = dataDir + "SimpleBarChart_out.docx";
doc.Save(dataDir);

โปรดดูผลลัพธ์ด้านล่าง:

bar-chart-aspose-words-net

วิธีกำหนดตัวเลือกเริ่มต้นสำหรับ ChartDataLabels ของ ChartSeries

คลาส ChartDataLabelCollection กำหนดคุณสมบัติที่สามารถใช้เพื่อตั้งค่าตัวเลือกเริ่มต้นสำหรับ ChartDataLabels สำหรับแผนภูมิ Series คุณสมบัติเหล่านี้ได้แก่ ShowCategoryName, ShowBubbleSize, ShowPercentage, ShowSeriesName, ShowValue ฯลฯ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Pie, 432, 252);
Chart chart = shape.Chart;
chart.Series.Clear();
ChartSeries series = chart.Series.Add("Series 1",
new string[] { "Category1", "Category2", "Category3" },
new double[] { 2.7, 3.2, 0.8 });
ChartDataLabelCollection labels = series.DataLabels;
labels.ShowPercentage = true;
labels.ShowValue = true;
labels.ShowLeaderLines = false;
labels.Separator = " - ";
doc.Save(dataDir + "Demo.docx");

โปรดดูผลลัพธ์ด้านล่าง:

pie-chart-aspose-words-net

วิธีจัดรูปแบบป้ายข้อมูลแผนภูมิจำนวน

การใช้ NumberFormat ทำให้คุณสามารถระบุการจัดรูปแบบตัวเลขของป้ายกำกับข้อมูลเดียวของแผนภูมิได้

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีจัดรูปแบบป้ายกำกับข้อมูลจำนวนหนึ่ง:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
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.Chart;
chart.Title.Text = "Data Labels With Different Number Format";
// Delete default generated series.
chart.Series.Clear();
// Add new series
ChartSeries series1 = chart.Series.Add("AW Series 1",
new string[] { "AW0", "AW1", "AW2" },
new double[] { 2.5, 1.5, 3.5 });
series1.HasDataLabels = true;
series1.DataLabels.ShowValue = true;
series1.DataLabels[0].NumberFormat.FormatCode = "\"$\"#,##0.00";
series1.DataLabels[1].NumberFormat.FormatCode = "dd/mm/yyyy";
series1.DataLabels[2].NumberFormat.FormatCode = "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.
series1.DataLabels[2].NumberFormat.IsLinkedToSource = true;
dataDir = dataDir + @"NumberFormat_DataLabel_out.docx";
doc.Save(dataDir);

วิธีการตั้งค่าคุณสมบัติแกนแผนภูมิ

หากคุณต้องการทำงานกับแกนแผนภูมิ การปรับขนาด และหน่วยแสดงผลสำหรับแกนค่า โปรดใช้คลาส ChartAxis, AxisDisplayUnit และ AxisScaling

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีกำหนดคุณสมบัติแกน X และ Y:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert chart.
Shape shape = builder.InsertChart(ChartType.Area, 432, 252);
Chart chart = shape.Chart;
// Clear demo data.
chart.Series.Clear();
// Fill data.
chart.Series.Add("AW Series 1",
new DateTime[] { new DateTime(2002, 01, 01), new DateTime(2002, 06, 01), new DateTime(2002, 07, 01), new DateTime(2002, 08, 01), new DateTime(2002, 09, 01) },
new double[] { 640, 320, 280, 120, 150 });
ChartAxis xAxis = chart.AxisX;
ChartAxis yAxis = chart.AxisY;
// 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.CategoryType = AxisCategoryType.Category;
// Define X axis properties.
xAxis.Crosses = AxisCrosses.Custom;
xAxis.CrossesAt = 3; // measured in display units of the Y axis (hundreds)
xAxis.ReverseOrder = true;
xAxis.MajorTickMark = AxisTickMark.Cross;
xAxis.MinorTickMark = AxisTickMark.Outside;
xAxis.TickLabelOffset = 200;
// Define Y axis properties.
yAxis.TickLabelPosition = AxisTickLabelPosition.High;
yAxis.MajorUnit = 100;
yAxis.MinorUnit = 50;
yAxis.DisplayUnit.Unit = AxisBuiltInUnit.Hundreds;
yAxis.Scaling.Minimum = new AxisBound(100);
yAxis.Scaling.Maximum = new AxisBound(700);
dataDir = dataDir + @"SetAxisProperties_out.docx";
doc.Save(dataDir);

วิธีการตั้งค่าเวลา Date ของ Axis

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าวันที่/เวลาให้กับคุณสมบัติของแกน:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert chart.
Shape shape = builder.InsertChart(ChartType.Column, 432, 252);
Chart chart = shape.Chart;
// Clear demo data.
chart.Series.Clear();
// Fill data.
chart.Series.Add("AW Series 1",
new DateTime[] { new DateTime(2017, 11, 06), new DateTime(2017, 11, 09), new DateTime(2017, 11, 15),
new DateTime(2017, 11, 21), new DateTime(2017, 11, 25), new DateTime(2017, 11, 29) },
new double[] { 1.2, 0.3, 2.1, 2.9, 4.2, 5.3 });
// Set X axis bounds.
ChartAxis xAxis = chart.AxisX;
xAxis.Scaling.Minimum = new AxisBound((new DateTime(2017, 11, 05)).ToOADate());
xAxis.Scaling.Maximum = new AxisBound((new DateTime(2017, 12, 03)).ToOADate());
// Set major units to a week and minor units to a day.
xAxis.MajorUnit = 7;
xAxis.MinorUnit = 1;
xAxis.MajorTickMark = AxisTickMark.Cross;
xAxis.MinorTickMark = AxisTickMark.Outside;
dataDir = dataDir + @"SetDateTimeValuesToAxis_out.docx";
doc.Save(dataDir);

วิธีจัดรูปแบบค่าตัวเลขของแกน

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเปลี่ยนรูปแบบของตัวเลขบนแกนค่า:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert chart.
Shape shape = builder.InsertChart(ChartType.Column, 432, 252);
Chart chart = shape.Chart;
// Clear demo data.
chart.Series.Clear();
// Fill data.
chart.Series.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.AxisY.NumberFormat.FormatCode = "#,##0";
dataDir = dataDir + @"FormatAxisNumber_out.docx";
doc.Save(dataDir);

วิธีกำหนดขอบเขตของแกน

คลาส AxisBound แสดงถึงขอบเขตต่ำสุดหรือสูงสุดของค่าแกน ขอบเขตสามารถระบุเป็นตัวเลข วันที่-เวลา หรือค่า “อัตโนมัติ” พิเศษได้

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าขอบเขตของแกน:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert chart.
Shape shape = builder.InsertChart(ChartType.Column, 432, 252);
Chart chart = shape.Chart;
// Clear demo data.
chart.Series.Clear();
// Fill data.
chart.Series.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.AxisY.Scaling.Minimum = new AxisBound(0);
chart.AxisY.Scaling.Maximum = new AxisBound(6);
dataDir = dataDir + @"SetboundsOfAxis_out.docx";
doc.Save(dataDir);

วิธีการตั้งค่าหน่วยช่วงเวลาระหว่างป้ายกำกับ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าหน่วยช่วงเวลาระหว่างป้ายกำกับบนแกน:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert chart.
Shape shape = builder.InsertChart(ChartType.Column, 432, 252);
Chart chart = shape.Chart;
// Clear demo data.
chart.Series.Clear();
// Fill data.
chart.Series.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.AxisX.TickLabelSpacing = 2;
dataDir = dataDir + @"SetIntervalUnitBetweenLabelsOnAxis_out.docx";
doc.Save(dataDir);

วิธีซ่อนแกนแผนภูมิ

หากคุณต้องการแสดงหรือซ่อนแกนแผนภูมิ คุณสามารถทำได้โดยการตั้งค่าคุณสมบัติ ChartAxis.Hidden

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการซ่อนแกน Y ของแผนภูมิ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert chart.
Shape shape = builder.InsertChart(ChartType.Column, 432, 252);
Chart chart = shape.Chart;
// Clear demo data.
chart.Series.Clear();
// Fill data.
chart.Series.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.AxisY.Hidden = true;
dataDir = dataDir + @"HideChartAxis_out.docx";
doc.Save(dataDir);

วิธีจัดแนวฉลากแผนภูมิ

หากคุณต้องการตั้งค่าการจัดแนวข้อความสำหรับป้ายกำกับหลายบรรทัด คุณสามารถทำได้โดยการตั้งค่าคุณสมบัติ TickLabels.การจัดตำแหน่ง

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการทำเครื่องหมายการจัดตำแหน่งป้ายกำกับ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Document.docx");
Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
ChartAxis axis = shape.Chart.AxisX;
//This property has effect only for multi-line labels.
axis.TickLabelAlignment = ParagraphAlignment.Right;
doc.Save(dataDir + "Document_out.docx");

วิธีการตั้งค่าการเติมและการจัดรูปแบบเส้นขีด

สามารถตั้งค่าการจัดรูปแบบการเติมและเส้นขีดสำหรับชุดแผนภูมิ จุดข้อมูล และเครื่องหมายได้ ในการดำเนินการนี้ คุณจะต้องใช้คุณสมบัติของประเภท ChartFormat ในคลาส ChartSeries, ChartDataPoint และ ChartMarker รวมถึงนามแฝงสำหรับคุณสมบัติบางอย่าง เช่น ForeColor, BackColor, Visible และความโปร่งใสในคลาส Stroke

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าสีของซีรี่ส์:

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(dir + "ColumnColor.docx");

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าสีและน้ำหนักของเส้น:

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(dir + "LineColorAndWeight.docx");