How to display owner-draw date/time labels.


This example draws the date in a box with the day name, date and month, and the week number.

TssResourceAllocationChart owner-drawn date label example

procedure TForm1.RACOwnerDrawDateLabel(Sender: TObject;
                                       Canvas: TCanvas;
                                       x: Integer;
                                       DateTime: TDateTime);
var
  d, xl, xr, y1, y2, y3, y4, y5, y6, th, tw1, tw2, tw3: integer;
  str1, str2, str3: string;
begin
  Canvas.Font.Assign(RAC.ChartAppearance.DateTimeFont);
  th := Canvas.TextHeight('Hg');
  y1 := 3; // top of box
  y2 := y1 + th;
  y3 := y2;
  y4 := y3 + th;
  y5 := y4;
  y6 := y5 + th;
  for d := trunc(RAC.StartAt) to trunc(RAC.EndAt) do begin
    xl := RAC.DateTimeToX(d) + 1; // left edge of box
    xr := RAC.DateTimeToX(d + 1) - 1; // right edge of box
    if (xl < RAC.PlotRect.Left) then
      xl := RAC.PlotRect.Left;
    if (xr > RAC.PlotRect.Right) then
      xr := RAC.PlotRect.Right;
    if xl < xr then // is there room for this day?
      with Canvas do begin
        Brush.Color := $FFEEEE;
        Brush.Style := bsSolid;
        Pen.Color := clBlue;
        Pen.Style := psSolid;
        Rectangle(xl, y1, xr, y6);
        Brush.Style := bsClear;
        str1 := FormatDateTime('ddd', d);
        str2 := FormatDateTime('d MMM', d);
        str3 := Format('W%d', [ISOWeekNumber(d)]);
        tw1 := TextWidth(str1);
        tw2 := TextWidth(str2);
        tw3 := TextWidth(str3);
        if (tw1 < xr - xl - 2) and
           (tw2 < xr - xl - 2) and
           (tw3 < xr - xl - 2) then begin // is there room for all texts?
          TextOut((xl + xr - tw1) div 2,
                  (y1 + y2 - TextHeight(str1)) div 2, str1);
          TextOut((xl + xr - tw2) div 2,
                  (y3 + y4 - TextHeight(str2)) div 2, str2);
          TextOut((xl + xr - tw3) div 2,
                  (y5 + y6 - TextHeight(str3)) div 2, str3);
        end;
      end;
  end;
end;
Note: This event handler can be invoked many times a second while the user is dragging the chart. It is important that it completes quickly in order to maintain a responsive user experience.
What People Say

This is a great component. I've been using it for a year. It's well written, performance is top notch, and the couple of time I've asked for support, I've gotten it right away from the author.
Steve McForest
More …

No malware No spam
McAfee Site Report
Web of Trust