OLE自动化Delphi Excel图表标签定位

时间:2014-09-04 20:54:35

标签: excel delphi ole

我正在从Delphi生成一个excel饼图,一切顺利,除非我希望将标签(带有值)发送到Pie的外部

所以我生成的馅饼看起来像这样: enter image description here

我想得到这个:

enter image description here

所以我的代码看起来像这样:

if not CreateExcel then exit;
  VisibleExcel(false);

  if AddWorkBook then
  begin
    SelectSheet(1);
  end;

  AddChart(ChartName,xl3DPieExploded);//xl3DColumn);
  SetSourceData(1{ChartName},2,'A1:B5',xlColumns);

  randomize;
  VisibleExcel(false);
  E.ActiveWorkbook.Sheets.Item[2].Cells[1,1] :='Some text';
  E.ActiveWorkbook.Sheets.Item[2].Cells[1,2] :='Chart title';

  E.ActiveWorkbook.Sheets.Item[2].Cells[2,1] :='Mijloace de productie';
  E.ActiveWorkbook.Sheets.Item[2].Cells[3,1] :='Mediul de munca';
  E.ActiveWorkbook.Sheets.Item[2].Cells[4,1] :='Sarcina de munca';
  E.ActiveWorkbook.Sheets.Item[2].Cells[5,1] :='Executant';

  E.ActiveWorkbook.Sheets.Item[2].Cells[2,2] := FloatToStr(proc1)+'%';
  E.ActiveWorkbook.Sheets.Item[2].Cells[3,2] := FloatToStr(proc2)+'%';
  E.ActiveWorkbook.Sheets.Item[2].Cells[4,2] := FloatToStr(proc3)+'%';
  E.ActiveWorkbook.Sheets.Item[2].Cells[5,2] := FloatToStr(proc4)+'%';

// remove the legend
  E.ActiveWorkbook.Charts.Item['Chart1'].Select;
  E.ActiveChart.Legend.Select;
  E.Selection.Delete;
// apply labels
  E.ActiveWorkbook.Charts.Item['Chart1'].Select;
  E.ActiveChart.SeriesCollection(1).ApplyDataLabels;

// formatting chart labels.
E.ActiveChart.ApplyDataLabels(xlDataLabelsShowLabelAndPercent, false,true,
                              true{HasLeaderLines: OleVariant; }, false {ShowSeriesName: OleVariant;},
                              true {ShowCategoryName: OleVariant; }, true {ShowValue: OleVariant;},
                              false {ShowPercentage: OleVariant; }, false {ShowBubbleSize: OleVariant;},
                              '; '{Separator: OleVariant; } );

所以它有LeaderLines,这远远可行,但现在我想看到馅饼之外的标签,距离。 我怎样才能做到这一点。我在Excel中录制了一个宏,但似乎无法将其应用于Delphi。我想在Delphi中使用的宏代码是:

ActiveSheet.ChartObjects("Chart 1").Activate
Selection.Position = xlLabelPositionOutsideEnd

1 个答案:

答案 0 :(得分:3)

Series对象具有DataLabels方法,如果省略可选索引参数,则可以返回具有DataLabels属性的Position对象,您可以在其中看到你录制的宏。所以我相信你可以在发布的代码的末尾添加以下行:

E.ActiveChart.SeriesCollection(1).DataLabels.Position := xlLabelPositionOutsideEnd;