إنشاء رسم بياني في عرض تقديمي باستخدام Microsoft PowerPoint
إنشاء رسم بياني
تصف أمثلة الكود أدناه عملية إضافة رسم بياني بسيط من نوع عمود مجمع ثلاثي الأبعاد باستخدام VSTO. تقوم بإنشاء مثيل للعروض التقديمية، وإضافة رسم بياني افتراضي إليه. ثم تستخدم دفتر عمل Microsoft Excel للوصول إلى بيانات الرسم البياني وتعديلها بالإضافة إلى تعيين خصائص الرسم البياني. أخيرًا، حفظ العرض التقديمي.
مثال VSTO
باستخدام VSTO، يتم تنفيذ الخطوات التالية:
- إنشاء مثيل لعرض تقديمي باستخدام Microsoft PowerPoint.
- إضافة شريحة فارغة إلى العرض التقديمي.
- إضافة رسم بياني من نوع عمود مجمع ثلاثي الأبعاد والوصول إليه.
- إنشاء مثيل جديد لدفتر عمل Microsoft Excel وتحميل بيانات الرسم البياني.
- الوصول إلى ورقة بيانات الرسم البياني باستخدام مثيل دفتر العمل من ملف العمل.
- تعيين نطاق الرسم البياني في ورقة العمل وإزالة السلسلتين 2 و 3 من الرسم البياني.
- تعديل بيانات فئة الرسم البياني في ورقة بيانات الرسم البياني.
- تعديل بيانات السلسلة 1 في ورقة بيانات الرسم البياني.
- الآن، الوصول إلى عنوان الرسم البياني وتعيين خصائص الخط ذات الصلة.
- الوصول إلى محور قيمة الرسم البياني وتعيين الوحدة الأساسية، والوحدات الفرعية، والقيمة القصوى والقيم الدنيا.
- الوصول إلى عمق الرسم البياني أو محور السلسلة وإزالته كما في هذا المثال، حيث يتم استخدام سلسلة واحدة فقط.
- الآن، تعيين زوايا دوران الرسم البياني في الاتجاهين X و Y.
- حفظ العرض التقديمي.
- إغلاق مثيلات Microsoft Excel و PowerPoint.
العرض التقديمي الناتج، الذي تم إنشاؤه باستخدام VSTO
EnsurePowerPointIsRunning(true, true); | |
//Instantiate slide object | |
Microsoft.Office.Interop.PowerPoint.Slide objSlide = null; | |
//Access the first slide of presentation | |
objSlide = objPres.Slides[1]; | |
//Select firs slide and set its layout | |
objSlide.Select(); | |
objSlide.Layout = Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank; | |
//Add a default chart in slide | |
objSlide.Shapes.AddChart(Microsoft.Office.Core.XlChartType.xl3DColumn, 20F, 30F, 400F, 300F); | |
//Access the added chart | |
Microsoft.Office.Interop.PowerPoint.Chart ppChart = objSlide.Shapes[1].Chart; | |
//Access the chart data | |
Microsoft.Office.Interop.PowerPoint.ChartData chartData = ppChart.ChartData; | |
//Create instance to Excel workbook to work with chart data | |
Microsoft.Office.Interop.Excel.Workbook dataWorkbook = (Microsoft.Office.Interop.Excel.Workbook)chartData.Workbook; | |
//Accessing the data worksheet for chart | |
Microsoft.Office.Interop.Excel.Worksheet dataSheet = dataWorkbook.Worksheets[1]; | |
//Setting the range of chart | |
Microsoft.Office.Interop.Excel.Range tRange = dataSheet.Cells.get_Range("A1", "B5"); | |
//Applying the set range on chart data table | |
Microsoft.Office.Interop.Excel.ListObject tbl1 = dataSheet.ListObjects["Table1"]; | |
tbl1.Resize(tRange); | |
//Setting values for categories and respective series data | |
((Microsoft.Office.Interop.Excel.Range)(dataSheet.Cells.get_Range("A2"))).FormulaR1C1 = "Bikes"; | |
((Microsoft.Office.Interop.Excel.Range)(dataSheet.Cells.get_Range("A3"))).FormulaR1C1 = "Accessories"; | |
((Microsoft.Office.Interop.Excel.Range)(dataSheet.Cells.get_Range("A4"))).FormulaR1C1 = "Repairs"; | |
((Microsoft.Office.Interop.Excel.Range)(dataSheet.Cells.get_Range("A5"))).FormulaR1C1 = "Clothing"; | |
((Microsoft.Office.Interop.Excel.Range)(dataSheet.Cells.get_Range("B2"))).FormulaR1C1 = "1000"; | |
((Microsoft.Office.Interop.Excel.Range)(dataSheet.Cells.get_Range("B3"))).FormulaR1C1 = "2500"; | |
((Microsoft.Office.Interop.Excel.Range)(dataSheet.Cells.get_Range("B4"))).FormulaR1C1 = "4000"; | |
((Microsoft.Office.Interop.Excel.Range)(dataSheet.Cells.get_Range("B5"))).FormulaR1C1 = "3000"; | |
//Setting chart title | |
ppChart.ChartTitle.Font.Italic = true; | |
ppChart.ChartTitle.Text = "2007 Sales"; | |
ppChart.ChartTitle.Font.Size = 18; | |
ppChart.ChartTitle.Font.Color = Color.Black.ToArgb(); | |
ppChart.ChartTitle.Format.Line.Visible = Microsoft.Office.Core.MsoTriState.msoTrue; | |
ppChart.ChartTitle.Format.Line.ForeColor.RGB = Color.Black.ToArgb(); | |
//Accessing Chart value axis | |
Microsoft.Office.Interop.PowerPoint.Axis valaxis = ppChart.Axes(Microsoft.Office.Interop.PowerPoint.XlAxisType.xlValue, Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlPrimary); | |
//Setting values axis units | |
valaxis.MajorUnit = 2000.0F; | |
valaxis.MinorUnit = 1000.0F; | |
valaxis.MinimumScale = 0.0F; | |
valaxis.MaximumScale = 4000.0F; | |
//Accessing Chart Depth axis | |
Microsoft.Office.Interop.PowerPoint.Axis Depthaxis = ppChart.Axes(Microsoft.Office.Interop.PowerPoint.XlAxisType.xlSeriesAxis, Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlPrimary); | |
Depthaxis.Delete(); | |
//Setting chart rotation | |
ppChart.Rotation = 20; //Y-Value | |
ppChart.Elevation = 15; //X-Value | |
ppChart.RightAngleAxes = false; | |
// Save the presentation as a PPTX | |
objPres.SaveAs("C:\\VSTOSampleChart", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue); | |
//objPres.SaveAs(@"..\..\..\VSTOSampleChart", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue); | |
//Close Workbook and presentation | |
dataWorkbook.Application.Quit(); | |
objPres.Application.Quit(); | |
public static void EnsurePowerPointIsRunning(bool blnAddPresentation) | |
{ | |
EnsurePowerPointIsRunning(blnAddPresentation, false); | |
} | |
public static void EnsurePowerPointIsRunning() | |
{ | |
EnsurePowerPointIsRunning(false, false); | |
} | |
public static void EnsurePowerPointIsRunning(bool blnAddPresentation, bool blnAddSlide) | |
{ | |
string strName = null; | |
// | |
//Try accessing the name property. If it causes an exception then | |
//start a new instance of PowerPoint | |
try | |
{ | |
strName = objPPT.Name; | |
} | |
catch (Exception ex) | |
{ | |
StartPowerPoint(); | |
} | |
// | |
//blnAddPresentation is used to ensure there is a presentation loaded | |
if (blnAddPresentation == true) | |
{ | |
try | |
{ | |
strName = objPres.Name; | |
} | |
catch (Exception ex) | |
{ | |
objPres = objPPT.Presentations.Add(MsoTriState.msoTrue); | |
} | |
} | |
// | |
//BlnAddSlide is used to ensure there is at least one slide in the | |
//presentation | |
if (blnAddSlide) | |
{ | |
try | |
{ | |
strName = objPres.Slides[1].Name; | |
} | |
catch (Exception ex) | |
{ | |
Microsoft.Office.Interop.PowerPoint.Slide objSlide = null; | |
Microsoft.Office.Interop.PowerPoint.CustomLayout objCustomLayout = null; | |
objCustomLayout = objPres.SlideMaster.CustomLayouts[1]; | |
objSlide = objPres.Slides.AddSlide(1, objCustomLayout); | |
objSlide.Layout = Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText; | |
objCustomLayout = null; | |
objSlide = null; | |
} | |
} | |
} |
مثال Aspose.Slides for Android عبر Java
باستخدام Aspose.Slides for Android عبر Java، يتم تنفيذ الخطوات التالية:
- إنشاء مثيل لعرض تقديمي باستخدام Microsoft PowerPoint.
- إضافة شريحة فارغة إلى العرض التقديمي.
- إضافة رسم بياني من نوع عمود مجمع ثلاثي الأبعاد والوصول إليه.
- الوصول إلى ورقة بيانات الرسم البياني باستخدام مثيل دفتر عمل Microsoft Excel من ملف العمل.
- إزالة السلسلتين 2 و 3 غير المستخدمتين.
- الوصول إلى فئات الرسم البياني وتعديل التسميات.
- الوصول إلى السلسلة 1 وتعديل قيم السلسلة.
- الآن، الوصول إلى عنوان الرسم البياني وتعيين خصائص الخط.
- الوصول إلى محور قيمة الرسم البياني وتعيين الوحدة الأساسية، والوحدات الفرعية، والقيمة القصوى والقيم الدنيا.
- الآن، تعيين زوايا دوران الرسم البياني في الاتجاهين X و Y.
- حفظ العرض التقديمي بتنسيق PPTX.
العرض التقديمي الناتج، الذي تم إنشاؤه باستخدام Aspose.Slides
//Create empty presentation | |
Presentation pres = new Presentation(); | |
//Accessing first slide | |
ISlide slide = pres.getSlides().get_Item(0); | |
//Addding default chart | |
IChart ppChart = slide.getShapes().addChart(ChartType.ClusteredColumn3D, 20F, 30F, 400F, 300F); | |
//Getting Chart data | |
IChartData chartData = ppChart.getChartData(); | |
//Removing Extra default series | |
chartData.getSeries().removeAt(1); | |
chartData.getSeries().removeAt(1); | |
//Modifying chart categories names | |
chartData.getCategories().get_Item(0).getAsCell().setValue("Bikes"); | |
chartData.getCategories().get_Item(1).getAsCell().setValue("Accessories"); | |
chartData.getCategories().get_Item(2).getAsCell().setValue("Repairs"); | |
chartData.getCategories().get_Item(3).getAsCell().setValue("Clothing"); | |
//Setting the index of chart data sheet | |
int defaultWorksheetIndex = 0; | |
//Getting the chart data worksheet | |
IChartDataWorkbook fact = ppChart.getChartData().getChartDataWorkbook(); | |
//Modifying chart series values for first category | |
chartData.getSeries().get_Item(0).getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 1000)); | |
chartData.getSeries().get_Item(0).getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 2500)); | |
chartData.getSeries().get_Item(0).getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 4000)); | |
chartData.getSeries().get_Item(0).getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 4, 1, 3000)); | |
//Setting Chart title | |
ppChart.hasTitle(true); | |
ppChart.getChartTitle().addTextFrameForOverriding("2007 Sales"); | |
IPortionFormat format = ppChart.getChartTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat(); | |
format.setFontItalic(NullableBool.True); | |
format.setFontHeight(18); | |
format.getFillFormat().setFillType(FillType.Solid); | |
format.getFillFormat().getSolidFillColor().setColor(java.awt.Color.BLACK); | |
////Setting Axis values | |
ppChart.getAxes().getVerticalAxis().isAutomaticMaxValue(false); | |
ppChart.getAxes().getVerticalAxis().isAutomaticMinValue(false); | |
ppChart.getAxes().getVerticalAxis().isAutomaticMajorUnit(false); | |
ppChart.getAxes().getVerticalAxis().isAutomaticMinorUnit(false); | |
ppChart.getAxes().getVerticalAxis().setMaxValue(4000.0F); | |
ppChart.getAxes().getVerticalAxis().setMinValue(0.0F); | |
ppChart.getAxes().getVerticalAxis().setMajorUnit(2000.0F); | |
ppChart.getAxes().getVerticalAxis().setMinorUnit(1000.0F); | |
ppChart.getAxes().getVerticalAxis().setTickLabelPosition(TickLabelPositionType.NextTo); | |
//Setting Chart rotation | |
ppChart.getRotation3D().setRotationX((byte)15); | |
ppChart.getRotation3D().setRotationY(20); | |
//Saving Presentation | |
pres.save("c:\\data\\AsposeSampleChart.pptx",com.aspose.slides.SaveFormat.Pptx); |