diff --git a/components/nicechart/source/NiceChart.pas b/components/nicechart/source/NiceChart.pas index af1615de8..caafcf28b 100644 --- a/components/nicechart/source/NiceChart.pas +++ b/components/nicechart/source/NiceChart.pas @@ -136,6 +136,7 @@ type procedure InternalClear; procedure InternalPaint(ACanvas: TCanvas); procedure Calculate(AWidth, AHeight: Integer); + procedure DoCalculate(ACanvas: TCanvas; AWidth, AHeight: Integer); procedure DoPaint; procedure SetGridColor(const Value: TColor); procedure SetShowLegend(const Value: Boolean); @@ -908,6 +909,26 @@ begin end; procedure TNiceChart.Calculate(AWidth, AHeight: Integer); +var + bmp: TBitmap; +begin + if Canvas.HandleAllocated then + DoCalculate(Canvas, AWidth, AHeight) + else + begin + // Use an auxiliary bitmap in case of early calls when the Canvas has not handle, yet. + bmp := TBitmap.Create; + try + bmp.Width := AWidth; + bmp.Height := AHeight; + DoCalculate(bmp.Canvas, AWidth, AHeight); + finally + bmp.Free; + end; + end; +end; + +procedure TNiceChart.DoCalculate(ACanvas: TCanvas; AWidth, AHeight: Integer); var x, w, h, y, g: Integer; Titled: Boolean; @@ -924,24 +945,24 @@ begin Titled := False; if FShowTitle and (FTitle <> '') then begin - Canvas.Font.Assign(TitleFont); - w := Canvas.TextHeight(FTitle); + ACanvas.Font.Assign(TitleFont); + w := ACanvas.TextHeight(FTitle); RcTitle := Rect(RcChart.Left, RcChart.Top, RcChart.Right, RcChart.Left + w); - DrawText(Canvas.Handle, PChar(FTitle), Length(FTitle), RcTitle, + DrawText(ACanvas.Handle, PChar(FTitle), Length(FTitle), RcTitle, DT_CENTER or DT_VCENTER or DT_WORDBREAK or DT_CALCRECT); RcChart.Top := RcTitle.Bottom + FInnerMargin; Titled := True; end else SetRectEmpty(RcTitle); - Canvas.Font.Assign(FNormalFont); - h := Canvas.TextHeight('Ag'); + ACanvas.Font.Assign(FNormalFont); + h := ACanvas.TextHeight('Ag'); RcChart.Bottom := RcChart.Bottom - (2 * h) - FInnerMargin - FTickLength - FSmallmargin; BuildYAxis; w := 0; - for x := 0 to YAxis.Count-1 - do w := Max(w, Canvas.TextWidth(PAxisInfo(YAxis[x])^.Caption)); + for x := 0 to YAxis.Count-1 do + w := Max(w, ACanvas.TextWidth(PAxisInfo(YAxis[x])^.Caption)); RcChart.Left := RcChart.Left + h + FInnerMargin + w + FTickLength + FSmallMargin; RcTitle.Left := RcChart.Left; RcTitle.Right := RcChart.Right; @@ -949,16 +970,16 @@ begin if FShowLegend and (List.Count > 0) then begin - Canvas.Font.Assign(FNormalFont); + ACanvas.Font.Assign(FNormalFont); w := 0; h := FInnerMargin; - g := Canvas.TextHeight('Ag'); + g := ACanvas.TextHeight('Ag'); for x := 0 to List.Count-1 do begin TNiceSeries(List[x]).Top := h; Temp.Text := Trim(TNiceSeries(List[x]).FCaption); - for y := 0 to Temp.Count-1 - do w := Max(w, Canvas.TextWidth(Trim(Temp[y]))); + for y := 0 to Temp.Count-1 do + w := Max(w, ACanvas.TextWidth(Trim(Temp[y]))); h := h + Max(FLegendItemSize, Temp.Count * g); if (x <> List.Count-1) then h := h + FSmallMargin;