Reputation: 45
I need to create a chart in excel in a delphi application based on some data from a table.
I created the excel worksheet and filled n rows of data from the table. Now I need to make the chart based on the data.
We have to add two Legent entries(categories) - D5:D15 and E5:E15 and one Horizontal(Category) Axis labels -F5:F15
I tried the following to get this. When executed I got the error message
Method 'setsourcedata' not supported by automation object.
I tried the SeriesCollection.item.value
method then also the system gave the same message.
procedure TForm1.Make_a_Chart;
var
Sheets,Ch1 : Variant;
begin
ch1 := XLApp.ActiveWorkBook.Sheets[1].ChartObjects.Add ( 500,100,400,200 ); // creates a new chart in the specified
Sheets := XLApp.Sheets;
ch1.Chart.ChartWizard (
Sheets.Item['Delphi Data'].Range['D5:D15'], // 1 Source
xlBarStacked, // 2 The chart type.
8, // 3 Format - The option number for the built-in autoformats. Can be a number from 1 through 10, depending on the gallery type.
2, // 4 PlotBy - Specifies whether the data for each series is in rows or columns. Can be one of the following XlRowCol constants.
1, // 5 CategoryLabels - An integer specifying the number of rows or columns within the source range that contain category labels.
1, // 6 SeriesLabels - An integer specifying the number of rows or columns within the source range that contain series labels.
False, // 7 HasLegend - 'true' to include a legend.
'My Report', // 8 Title - The Chart control title text.
'Y Axis', // 9 CategoryTitle - The category axis title text.
'X Axis', // 10 ValueTitle - The value axis title text
2 // 11 ExtraTitle - The series axis title for 3-D charts or the second value axis title for 2-D charts.
);
ch1.setsourcedata(Sheets.Item['Delphi Data'].Range['E5:E15'],xlColumns,2);
ch1.Chart.ChartType := xlBarStacked;
// ch1.SeriesCollection.Item[1].Values := Sheets.Item['Delphi Data'].Range['E5:E15'];
end;
Upvotes: 2
Views: 5495
Reputation: 7062
You are calling SetSourceData on a ChartObject object, not a Chart object. The ChartObject object is a container for the Chart object. The Chart object has the method SetSourceData, so use this:
ch1.Chart.SetSourceData(Sheets.Item['Delphi Data'].Range['E5:E15'],xlColumns,2);
Edit: Your codesample is very confusing. Please, when asking a question, try to tidy your code. I see a lot of unused variables and you use a variable ch1
that isn't declared in the var section.
Upvotes: 4