Page 1 of 1

DateField as Bottom Axis

Posted: Wed Aug 18, 2004 10:48 pm
by 8573527
Hi,

I think I am bugging you too much but I am edge of finishing a project and there is 1 more problem left.

We are creating a time line analysis chart where we need to show range of dates on BottomAxis and numeric data on Left axis. Everything is working except in Bottom Axis Date values are converted into Double and then displayed.

so rather than showing 19 Aug 2004 it shows me 38218. Also when dates are too many I was hoping that it can show them as vertical labels rather than horizontal labels (hopefull I am making this clear :D) following is the code.

Code: Select all

procedure TQGridProc.ShowChart(aParent: TComponent);
var
  iIndex: Integer;
  iLoop: Integer;
  iXSeriesColumnID: Integer;
  iYSeriesColumnID: Integer;
  aYSeriesList: TStringList;
  oFooter: TStringList;
  oReverseFooter: TStringList;
  aLineSeries: TLineSeries;
  aFieldDef: TFieldDef;

  function GetColumnID(aColumnName: String): Integer;
  var
    iCount: Integer;
  begin
    Result := -1;
    for iCount := 0 to Pred(fGridView.ColumnCount) do
    begin
      if fGridView.Columns[iCount].Caption = aColumnName then
      begin
        Result := iCount;
        Break;
      end;
    end;
  end;
begin
  InitGraph(TWinControl(aParent), ChartInterface);

  //Chart Title which would be same as Setting Type at this moment
  ChartInterface.FormCaption := fChartSettingType;

  //General Chart Properties
  ChartInterface.Chart.AllowZoom := True;
  ChartInterface.Chart.Zoom.Pen.Color := clBlack;

  //Header
  ChartInterface.Title.Text.Clear;
  ChartInterface.Title.Font.Color := clBlack;
  ChartInterface.Title.Font.Size  := 12;
  ChartInterface.Title.Font.Style := [fsBold];

  ChartInterface.Title.Text.QuoteChar := '^';
  ChartInterface.Title.Text.Delimiter := '~';
  ChartInterface.Title.Text.DelimitedText := fHeaderText;

  ChartInterface.Title.Alignment := TAlignment(fHeaderAlignment);

  //Footer
  ChartInterface.Foot.Text.Clear;
  ChartInterface.Foot.Font.Color := clBlack;
  ChartInterface.Foot.Font.Size  := 10;
  ChartInterface.Foot.Font.Style := [];

  oFooter := TStringList.Create;
  try
    oFooter.QuoteChar := '^';
    oFooter.Delimiter := '~';
    oFooter.DelimitedText := fFooterText;

    oReverseFooter := TStringList.Create;
    try
      for iLoop := Pred(oFooter.Count) downto 0 do
      begin
        oReverseFooter.Add(oFooter.Strings[iLoop]);
      end;

      ChartInterface.Foot.Text.Assign(oReverseFooter);
      ChartInterface.Foot.Alignment := TAlignment(fFooterAlignment);
    finally
      FreeAndNil(oReverseFooter);
    end;
  finally
    FreeAndNil(oFooter);
  end;

  //Legend
  ChartInterface.Legend.Visible := True;
  chartInterface.Legend.Alignment := TLegendAlignment(fLegendPosition);

  //Build Series
  ChartInterface.Chart.OnBeforeDrawSeries := nil;
  ChartInterface.BottomAxis.Visible := True;
  ChartInterface.LeftAxis.Visible := True;
  ChartInterface.RightAxis.Visible := False;

  //X Axis General Property
  ChartInterface.BottomAxis.Title.Caption := fXSeriesCaption;
  ChartInterface.BottomAxis.Automatic := False;
  ChartInterface.BottomAxis.AutomaticMaximum := True;
  ChartInterface.BottomAxis.AutomaticMinimum := False;
  ChartInterface.BottomAxis.Minimum := 0;

  //y Axis General Property
  ChartInterface.LeftAxis.Title.Caption := fYSeriesCaption;
  ChartInterface.LeftAxis.Automatic := False;
  ChartInterface.LeftAxis.AutomaticMaximum := True;
  ChartInterface.LeftAxis.AutomaticMinimum := False;
  ChartInterface.LeftAxis.Minimum := 0;

  aYSeriesList := TStringList.Create;
  try
    aYSeriesList.QuoteChar := '^';
    aYSeriesList.Delimiter := '~';
    aYSeriesList.DelimitedText := fYSeries;

    for iIndex := 0 to pred(aYSeriesList.Count) do
    begin
      fSeriesDataSet.Add(TClientDataSet.Create(nil));

      //X Axis
      aFieldDef := TClientDataSet(fSeriesDataSet.Items[iIndex]).FieldDefs.AddFieldDef;
      aFieldDef.DataType := TFieldType(Integer(fChartColumns.Objects[fChartColumns.IndexOf(fXSeries)]));
      aFieldDef.Name := fXSeries;

      //Y Axis
      aFieldDef := TClientDataSet(fSeriesDataSet.Items[iIndex]).FieldDefs.AddFieldDef;
      aFieldDef.DataType := TFieldType(Integer(fChartColumns.Objects[fChartColumns.IndexOf(aYSeriesList.Strings[iIndex])]));
      aFieldDef.Name := aYSeriesList.Strings[iIndex];

      //Load X Axis and Y Axis data into the Client DataSet
      TClientDataSet(fSeriesDataSet.Items[iIndex]).CreateDataSet;
      TClientDataSet(fSeriesDataSet.Items[iIndex]).Open;

      //Retrieve the Grid Column ID by it's name
      iXSeriesColumnID := GetColumnID(fXSeries);
      iYSeriesColumnID := GetColumnID(aYSeriesList.Strings[iIndex]);

      //Loop through the grid and load the data
      for iLoop := 0 to Pred(fGridView.DataController.RecordCount) do
      begin
        TClientDataSet(fSeriesDataSet.Items[iIndex]).Append;
        TClientDataSet(fSeriesDataSet.Items[iIndex]).FieldByName(fXSeries).Value :=
          fGridView.DataController.GetDisplayText(iLoop, iXSeriesColumnID);
        TClientDataSet(fSeriesDataSet.Items[iIndex]).FieldByName(aYSeriesList.Strings[iIndex]).Value :=
          fGridView.DataController.GetDisplayText(iLoop, iYSeriesColumnID);
        TClientDataSet(fSeriesDataSet.Items[iIndex]).Post;
      end;

      aLineSeries := ChartInterface.AddLineSeries(TClientDataSet(fSeriesDataSet.Items[iIndex]),
        aYSeriesList.Strings[iIndex], fXSeries, aYSeriesList.Strings[iIndex]);

      //Making sure that Reference to the Line is visible in legend
      aLineSeries.ShowInLegend := True;

      //Show Points for a LineSeries
      aLineSeries.Pointer.Visible := True;
      aLineSeries.Pointer.HorizSize := 2;
      aLineSeries.Pointer.VertSize := 2;
      aLineSeries.Pointer.Style := psRectangle;
      aLineSeries.Pointer.Color := aLineSeries.Color;
      alineSeries.Pointer.Pen.Style := psSolid;
      alineSeries.Pointer.Pen.Color := aLineSeries.Color;
    end;
  finally
    FreeAndNil(aYSeriesList);
  end;

  ChartInterface.Chart.MarginTop := 5;
  ChartInterface.Chart.MarginBottom := 5;
  ChartInterface.BottomAxis.DateTimeFormat := 'd mmm yyyy';

  for iIndex := 0 to Pred(ChartInterFace.Chart.SeriesList.Count) do
  begin
    ChartInterFace.Chart.SeriesList.Items[iIndex].CheckDataSource;
  end;

  try
    ShowGraph;
  finally
    fSeriesDataSet.Clear;
  end;
end;
Cheers :)

Posted: Thu Aug 19, 2004 12:16 pm
by Marjan
Hi.

It might help if you declare the XVlaues as datetime values. then TeeChart will automatically internally transform double to datetime value. In your case, add the following line:

Code: Select all

aLineSeries.XValues.Datetime := True;
as vertical labels rather than horizontal labels
This can be done by rotating bottom axis labels. Something like this:

Code: Select all

Chart1.Axes.Left. LabelsAngle := 90;