Board index » delphi » Adding the labels in a data sheet to an excel chart

Adding the labels in a data sheet to an excel chart

I use the following to select a group of values to add to a 3d pie chart in
excel, but
I have not had much luck adding the labels in the range.  if i use a range
including the labels,
I get an error.

this works for just data
  XLApp.Workbooks[1].Sheets.Add(,,1,xlChart);
  Sheets := XLApp.Sheets;

  ARange := Sheets.Item[g_ReportSheetName].Range['B:3..B:30'];

  Sheets.Item['MyChart'].ChartType := xl3DPie;
  Sheets.Item['MyChart'].SeriesCollection.Item[1].Name := g_ReportSheetName;
  Sheets.Item['MyChart'].SeriesCollection.Item[1].Values := ARange;
  Sheets.Item['MyChart'].SeriesCollection.Item[1].HasDataLabels := True;

this doesn't when i want to include data labels
  ARange := Sheets.Item[g_ReportSheetName].Range['A:3..B:30'];

 

Re:Adding the labels in a data sheet to an excel chart


<<Jim Rearden:
I have not had much luck adding the labels in the range.

Quote

I am not quite sure what effect you want. This code works:

var
  DataSheet, ARange, Ch, Series: OleVariant;
  i: integer;
begin
  Datasheet := Excel.ActiveSheet;

  { Set up data }
  for i := 3 to 30 do
    Excel.Cells[i, 1].Value := 'Label' + IntToStr(i);
  ARange := Datasheet.Range['B3:B30'];
  ARange.Value := '=RAND()';

  { Set up chart }
  ARange := Datasheet.Range['A3:B30'];
  Ch := Excel.Workbooks[1].Sheets.Add(,,1,xlChart);
  Ch.ChartType := xl3DPie;
  Ch.SetSourceData(ARange, xlColumns);
  Ch.SeriesCollection(1).HasDataLabels := True;
  Ch.SeriesCollection(1).ApplyDataLabels(
                               xlDataLabelsShowValue);

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

Other Threads