AddChart

AddChart(ChartType, float, float, float, float)

创建一个新的图表,使用示例系列数据和设置进行初始化,并将其添加到集合的末尾。

public IChart AddChart(ChartType type, float x, float y, float width, float height)
参数类型描述
typeChartType图表类型
xSingle新图表的 X 坐标
ySingle新图表的 Y 坐标
widthSingle图表的宽度
heightSingle图表的高度

返回值

创建的图表。

示例

以下示例演示如何在 PowerPoint 演示文稿中创建图表。

[C#]
// Instantiates the Presentation class that represents a PPTX file
using(Presentation pres = new Presentation()) {
  // Accesses the first slide
  ISlide sld = pres.Slides[0];
  // Adds a chart with its default data
  IChart chart = sld.Shapes.AddChart(ChartType.ClusteredColumn, 0, 0, 500, 500);
  // Sets the chart title
  chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
  chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
  chart.ChartTitle.Height = 20;
  chart.HasTitle = true;
  // Sets the first series to show values
  chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;
  // Sets the index for the chart data sheet
  int defaultWorksheetIndex = 0;
  // Gets the chart data worksheet
  IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;
  // Deletes the default generated series and categories
  chart.ChartData.Series.Clear();
  chart.ChartData.Categories.Clear();
  int s = chart.ChartData.Series.Count;
  s = chart.ChartData.Categories.Count;
  // Adds new series
  chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.Type);
  chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.Type);
  // Adds new categories
  chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
  chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
  chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));
  // Takes the first chart series
  IChartSeries series = chart.ChartData.Series[0];
  // Populates series data
  series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20));
  series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50));
  series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30));
  // Sets the fill color for the series
  series.Format.Fill.FillType = FillType.Solid;
  series.Format.Fill.SolidFillColor.Color = Color.Red;
  // Takes the second chart series
  series = chart.ChartData.Series[1];
  // Populates series data
  series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 2, 30));
  series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 2, 10));
  series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 2, 60));
  // Sets the fill color for series
  series.Format.Fill.FillType = FillType.Solid;
  series.Format.Fill.SolidFillColor.Color = Color.Green;
  // Sets the first label to show Category name
  IDataLabel lbl = series.DataPoints[0].Label;
  lbl.DataLabelFormat.ShowCategoryName = true;
  lbl = series.DataPoints[1].Label;
  lbl.DataLabelFormat.ShowSeriesName = true;
  // Sets the series to show the value for the third label
  lbl = series.DataPoints[2].Label;
  lbl.DataLabelFormat.ShowValue = true;
  lbl.DataLabelFormat.ShowSeriesName = true;
  lbl.DataLabelFormat.Separator = "/";
  // Saves the PPTX file to disk
  pres.Save("AsposeChart_out.pptx", SaveFormat.Pptx);
}

另请参阅


AddChart(ChartType, float, float, float, float, bool)

创建一个新的图表并将其添加到集合的末尾。

public IChart AddChart(ChartType type, float x, float y, float width, float height, 
    bool initWithSample)
参数类型描述
typeChartType图表类型
xSingle新图表的 X 坐标
ySingle新图表的 Y 坐标
widthSingle图表的宽度
heightSingle图表的高度
initWithSampleBoolean如果为 true,则新图表将使用示例系列数据和设置进行初始化。如果为 false,则新图表将没有系列和最少的设置。在这种情况下,图表创建将更快。

返回值

创建的图表。

另请参阅