简体   繁体   中英

OLE Automation Delphi Excel Chart Labels positioning

I am generating an excel Pie Chart from Delphi and all goes well except when I want the labels (with the values) to be sent to the exterior of the Pie

So my generated pie looks like this: 在此处输入图片说明

And I want to obtain this:

在此处输入图片说明

So my code looks like this:

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; } );

So it has LeaderLines, this far it works, but now I want to see the labels outside of the pie, distanced. How can I do this. I recorded a Macro in Excel but I cannot seem to be able to apply it to Delphi. The macro code that I want to use in Delphi is:

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

The Series object has the DataLabels method which if you omit the optional index parameter can return a DataLabels object which has the Position property, that you might see in your recorded macro. So I believe you can add the following line to the end of your posted code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM