diff --git a/components/fpspreadsheet/examples/visual/fpschart/fpschartlink/main.lfm b/components/fpspreadsheet/examples/visual/fpschart/fpschartlink/main.lfm index 4a953c2a6..76bfd4140 100644 --- a/components/fpspreadsheet/examples/visual/fpschart/fpschartlink/main.lfm +++ b/components/fpspreadsheet/examples/visual/fpschart/fpschartlink/main.lfm @@ -14,30 +14,6 @@ object Form1: TForm1 Top = 38 Width = 5 end - object Chart: TChart - Left = 528 - Height = 489 - Top = 38 - Width = 823 - AxisList = < - item - Marks.LabelBrush.Style = bsClear - Minors = <> - Title.LabelFont.Orientation = 900 - Title.LabelBrush.Style = bsClear - end - item - Alignment = calBottom - Marks.LabelBrush.Style = bsClear - Minors = <> - Title.LabelBrush.Style = bsClear - end> - Legend.SymbolWidth = 40 - Title.Text.Strings = ( - 'TAChart' - ) - Align = alClient - end object Panel1: TPanel Left = 0 Height = 38 @@ -48,7 +24,7 @@ object Form1: TForm1 BevelOuter = bvNone ClientHeight = 38 ClientWidth = 1351 - TabOrder = 2 + TabOrder = 1 object lblFileNames: TLabel AnchorSideLeft.Control = Panel1 AnchorSideTop.Control = Panel1 @@ -119,34 +95,83 @@ object Form1: TForm1 Caption = 'Panel2' ClientHeight = 489 ClientWidth = 523 - TabOrder = 3 - object sWorksheetGrid: TsWorksheetGrid - Left = 1 - Height = 375 - Top = 1 - Width = 521 - FrozenCols = 0 - FrozenRows = 0 - PageBreakPen.Color = clBlue - PageBreakPen.Style = psDash - ReadFormulas = False - TextOverflow = True - WorkbookSource = sWorkbookSource - Align = alClient - AutoAdvance = aaDown - Color = clWhite - DefaultColWidth = 64 - DefaultRowHeight = 22 - Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goRowSizing, goColSizing, goEditing, goSmoothScroll] - TabOrder = 0 - end + TabOrder = 2 object Memo: TMemo Left = 1 Height = 112 Top = 376 Width = 521 Align = alBottom + TabOrder = 0 + end + object sWorkbookTabControl1: TsWorkbookTabControl + Left = 1 + Height = 375 + Top = 1 + Width = 521 + TabIndex = 0 + Tabs.Strings = ( + 'Sheet1' + ) + Align = alClient TabOrder = 1 + WorkbookSource = sWorkbookSource + object sWorksheetGrid: TsWorksheetGrid + Left = 2 + Height = 350 + Top = 23 + Width = 517 + FrozenCols = 0 + FrozenRows = 0 + PageBreakPen.Color = clBlue + PageBreakPen.Style = psDash + ReadFormulas = False + TextOverflow = True + WorkbookSource = sWorkbookSource + Align = alClient + AutoAdvance = aaDown + Color = clWhite + DefaultColWidth = 64 + DefaultRowHeight = 22 + Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goRowSizing, goColSizing, goEditing, goSmoothScroll] + TabOrder = 1 + end + end + end + object Panel3: TPanel + Left = 528 + Height = 489 + Top = 38 + Width = 823 + Align = alClient + BevelOuter = bvNone + Caption = 'No chart on this worksheet' + ClientHeight = 489 + ClientWidth = 823 + TabOrder = 3 + object Chart: TChart + Left = 0 + Height = 489 + Top = 0 + Width = 823 + AxisList = < + item + Marks.LabelBrush.Style = bsClear + Minors = <> + Title.LabelFont.Orientation = 900 + Title.LabelBrush.Style = bsClear + end + item + Alignment = calBottom + Marks.LabelBrush.Style = bsClear + Minors = <> + Title.LabelBrush.Style = bsClear + end> + Legend.SymbolWidth = 40 + Title.Text.Strings = ( + 'TAChart' + ) + Align = alClient end end object sWorkbookSource: TsWorkbookSource diff --git a/components/fpspreadsheet/examples/visual/fpschart/fpschartlink/main.pas b/components/fpspreadsheet/examples/visual/fpschart/fpschartlink/main.pas index bb86be3dd..262c9fe6f 100644 --- a/components/fpspreadsheet/examples/visual/fpschart/fpschartlink/main.pas +++ b/components/fpspreadsheet/examples/visual/fpschart/fpschartlink/main.pas @@ -26,8 +26,10 @@ type OpenDialog: TOpenDialog; Panel1: TPanel; Panel2: TPanel; + Panel3: TPanel; Splitter1: TSplitter; sWorkbookSource: TsWorkbookSource; + sWorkbookTabControl1: TsWorkbookTabControl; sWorksheetGrid: TsWorksheetGrid; procedure btnBrowseClick(Sender: TObject); procedure btnOpenClick(Sender: TObject); @@ -52,7 +54,7 @@ implementation uses TypInfo, - TAChartUtils, TAChartAxis, TAChartAxisUtils, TACustomSeries, TATransformations; + TAChartUtils, TAChartAxis, TACustomSeries, TATransformations; { TForm1 } diff --git a/components/fpspreadsheet/source/common/fpschart.pas b/components/fpspreadsheet/source/common/fpschart.pas index 07c6fce10..1b8f6ab92 100644 --- a/components/fpspreadsheet/source/common/fpschart.pas +++ b/components/fpspreadsheet/source/common/fpschart.pas @@ -912,6 +912,8 @@ type property Items[AIndex: Integer]: TsChart read GetItem write SetItem; default; end; + TsChartArray = array of TsChart; + function ChartColor(AColor: TsColor; ATransparency: Single = 0.0): TsChartColor; diff --git a/components/fpspreadsheet/source/common/fpspreadsheet.pas b/components/fpspreadsheet/source/common/fpspreadsheet.pas index ab46344fd..e7c0c55dc 100644 --- a/components/fpspreadsheet/source/common/fpspreadsheet.pas +++ b/components/fpspreadsheet/source/common/fpspreadsheet.pas @@ -647,6 +647,7 @@ type function AddChart(AWidth, AHeight: Double; ARow, ACol: Cardinal; AOffsetX: Double = 0.0; AOffsetY: Double = 0.0): TsChart; function GetChartCount: Integer; + function GetCharts: TsChartArray; procedure RemoveAllCharts; procedure RemoveChart(AChart: TsChart); {$endif} @@ -949,6 +950,7 @@ type ARow, ACol: Cardinal; AOffsetX: Double = 0.0; AOffsetY: Double = 0.0): TsChart; function GetChartByIndex(AIndex: Integer): TsChart; function GetChartCount: Integer; + function GetChartIndex(AChart: TsChart): Integer; {$endif} { Utilities } diff --git a/components/fpspreadsheet/source/common/fpspreadsheet_chart.inc b/components/fpspreadsheet/source/common/fpspreadsheet_chart.inc index 00e6b952b..2744e1cd4 100644 --- a/components/fpspreadsheet/source/common/fpspreadsheet_chart.inc +++ b/components/fpspreadsheet/source/common/fpspreadsheet_chart.inc @@ -29,10 +29,8 @@ function TsWorksheet.GetChartCount: Integer; var i: Integer; chart: TsChart; - idx: Integer; begin Result := 0; - idx := GetIndex; for i := 0 to Workbook.GetChartCount-1 do begin chart := Workbook.GetChartByIndex(i); @@ -40,6 +38,30 @@ begin end; end; +{@@ ---------------------------------------------------------------------------- + Collects all charts which are linked to this worksheet +-------------------------------------------------------------------------------} +function TsWorksheet.GetCharts: TsChartArray; +var + i, j, n: Integer; + chart: TsChart; +begin + Result := nil; + n := Workbook.GetChartCount; + j := 0; + SetLength(Result, n); + for i := 0 to n-1 do + begin + chart := Workbook.GetChartByIndex(i); + if chart.Worksheet = self then + begin + Result[j] := chart; + inc(j); + end; + end; + SetLength(Result, j); +end; + {@@ ---------------------------------------------------------------------------- Destroys all charts from the worksheet and removes them from the workbook's internal FCharts list. @@ -107,7 +129,7 @@ begin end; {@@ ---------------------------------------------------------------------------- - Returns the chart having the given index in the worksheet's chart list + Returns the chart having the given index in the workbook's chart list -------------------------------------------------------------------------------} function TsWorkbook.GetChartByIndex(AIndex: Integer): TsChart; begin @@ -125,3 +147,14 @@ begin Result := FCharts.Count; end; +{@@ ---------------------------------------------------------------------------- + Returns the index of the given chart in the workbook's chart list +-------------------------------------------------------------------------------} +function TsWorkbook.GetChartIndex(AChart: TsChart): Integer; +begin + for Result := 0 to FCharts.Count-1 do + if FCharts[Result] = AChart then + exit; + Result := -1; +end; + diff --git a/components/fpspreadsheet/source/visual/fpspreadsheetchart.pas b/components/fpspreadsheet/source/visual/fpspreadsheetchart.pas index b88c93c7e..8ac97fba4 100644 --- a/components/fpspreadsheet/source/visual/fpspreadsheetchart.pas +++ b/components/fpspreadsheet/source/visual/fpspreadsheetchart.pas @@ -1969,6 +1969,8 @@ end; procedure TsWorkbookChartLink.ListenerNotification(AChangedItems: TsNotificationItems; AData: Pointer = nil); +var + charts: TsChartArray; begin Unused(AData); @@ -1978,6 +1980,19 @@ begin ClearChart; UpdateChart; end; + + // Another worksheet is selected --> Select the first chart of the worksheet + if (lniWorksheet in AChangedItems) and (WorkbookSource <> nil) then + begin + charts := WorkbookSource.Worksheet.GetCharts; + if Length(charts) > 0 then + WorkbookChartIndex := WorkbookSource.Workbook.GetChartIndex(charts[0]) + else + WorkbookChartIndex := -1; + ClearChart; + UpdateChart; + FChart.Visible := WorkbookChartIndex > -1; + end; end; procedure TsWorkbookChartLink.Notification(AComponent: TComponent; Operation: TOperation);