TExportChartXY

TExportDrawingTExportChart → [...] → TExportChartXY

Description

TExportChartXY is the XY plot.

Please note that Excel has some problems reading ODS charts (it can read most features, but not all)! ODS charts are correctly displayed in Calc and XLSX charts are correctly displayed in Excel.

Properties

NameTypeDescription
LineTypeTExportChartXYLineTypeLine type of the chart:
  • eclStraight: straight lines between points.
  • eclSmooth: smooth curve - the curve is interpolated automatically between points.
Please see TExportChart for inherited properties.

Code snippet (1)

This code generates an XY plot:

procedure TForm1.BtnTestClick(Sender: TObject);
var
  xExport: TOExport;
  I, L: Integer;
  xTime: Double;
begin
  xExport := TOExport.Create;
  try
    with xExport.AddWorkSheet('XY chart') do begin
      AddRow.AddCellString('Velocity in km/h').SetFontSize(20);

      with AddRow do begin
        AddCellString('Time');
        AddCellString('Car 1');
        AddCellString('Car 2');
        AddCellString('Car 3');

        AddCellString('');
        AddCellString('');
        with AddCellString('').AddChart(TExportChartXY, 0, 0, 500, 300) do
        with TExportChartXY(Drawing) do begin
          Title := 'Velocity in km/h';
          Fill.Color := clWhite;
          Border.Color := clBlue;
          Shadow.FillStyle := edfColor;
          LineType := eclSmooth;//TRY DIFFERENT SETTINGS
          Legend := eclRight;//TRY DIFFERENT SETTINGS
          XAxis.Caption := 'Time [h]';
          YAxis.Caption := 'Velocity [km/h]';
          for I := 1 to 3 do
          with AddData(I, Rows.Count, 1, 10) do begin//y-axis data
            XRange.SetRange(0, Rows.Count, 1, 10);//x-axis data
            DataTitle.SetRange(I, Rows.Count-1, 1, 1);//legend entry title
            ShowLabels := False;
            Border.Size := 2;
            Markers.Size := 8;
          end;
        end;
      end;

      Randomize;
      xTime := 0;
      with AddRow do//zero values
      for L := 0 to 3 do
        AddCellNumber(0, 2);

      for I := 1 to 10 do
      with AddRow do begin
        xTime := xTime + RandomRange(10, 40)/10;
        AddCellNumber(xTime, 2);
        for L := 1 to 3 do
          AddCellNumber(Random(12000) / 100, 2);
      end;
    end;

    xExport.SaveToFileWithDialog;
  finally
    xExport.Free;
  end;
end;

Code snippet (2)

It is possible to generate charts from data that is in a different sheet. This is how you do it:

procedure TForm1.BtnDifferentGraphClick(Sender: TObject);
  procedure _FillData(const bWS: TExportWorkSheet; const aColTitle: String; var bFirstRow, bLastRow: Integer);
  var
    I: Integer;
    xTime: Double;
  begin
    //FILL DATA FOR Worksheet 1
    bWS.AddRow.AddCellString('Velocity in km/h - '+aColTitle).SetFontSize(20);

    with bWS.AddRow do begin
      AddCellString('Time');
      AddCellString(aColTitle);
    end;

    Randomize;
    xTime := 0;
    with bWS.AddRow do begin//zero values
      AddCellNumber(0, 2);
      AddCellNumber(xTime, 2);
    end;
    bFirstRow := bWS.Rows.Count-1;

    for I := 1 to 10 do begin
      with bWS.AddRow do begin
        xTime := xTime + RandomRange(10, 40)/10;
        AddCellNumber(xTime, 2);
        AddCellNumber(Random(12000) / 100, 2);
      end;
    end;
    bLastRow := bWS.Rows.Count-1;
  end;

  procedure _AddData(const bChart: TExportChartXY; const bWS: TExportWorkSheet;
    const bFirstRow, bLastRow: Integer);
  begin
    with bChart.AddData(0, 0, 0, 0) do begin//Y-Range will be set afterwards
      XRange.SetRange(bWS, 0, bFirstRow, 1, bLastRow-bFirstRow+1);//x-axis data
      YRange.SetRange(bWS, 1, bFirstRow, 1, bLastRow-bFirstRow+1);//y-axis data
      DataTitle.SetRange(bWS, 1, bFirstRow-1, 1, 1);//legend entry title
      ShowLabels := False;
      Border.Size := 2;
      Markers.Size := 8;
    end;
  end;
var
  xExport: TOExport;
  xWSData1, xWSData2, xWSChart: TExportWorkSheet;
  xWSData1FirstRow, xWSData1LastRow: Integer;
  xWSData2FirstRow, xWSData2LastRow: Integer;
  xChart: TExportChartXY;
begin
  xExport := TOExport.Create;
  try
    xWSData1 := xExport.AddWorkSheet('Data1');
    xWSData2 := xExport.AddWorkSheet('Data2');
    xWSChart := xExport.AddWorkSheet('Chart');

    //FILL DATA FOR Worksheet 1
    _FillData(xWSData1, 'Car 1', xWSData1FirstRow, xWSData1LastRow);

    //FILL DATA FOR Worksheet 2
    _FillData(xWSData2, 'Car 2', xWSData2FirstRow, xWSData2LastRow);

    //CREATE CHART
    xWSChart.AddRow.AddCellString('Velocity in km/h - Chart').SetFontSize(20);

    with xWSChart.AddRow.AddCellString('').AddChart(TExportChartXY, 0, 0, 500, 300) do begin
      xChart := TExportChartXY(Drawing);
      with xChart do begin
        Title := 'Velocity in km/h';
        Fill.Color := clWhite;
        Border.Color := clBlue;
        Shadow.FillStyle := edfColor;
        LineType := eclSmooth;//TRY DIFFERENT SETTINGS
        Legend := eclRight;//TRY DIFFERENT SETTINGS
        XAxis.Caption := 'Time [h]';
        YAxis.Caption := 'Velocity [km/h]';
      end;
      //Data from Worksheet 1
      _AddData(xChart, xWSData1, xWSData1FirstRow, xWSData1LastRow);
      //Data from Worksheet 2
      _AddData(xChart, xWSData2, xWSData2FirstRow, xWSData2LastRow);
    end;

    xExport.SaveToFile('extra-chart.xlsx', True);
  finally
    xExport.Free;
  end;
end;
Categories