delphiteechart

TChart is not utilizing the X parameter for the Horizontal Axis Labels


I am using Delphi 11 VCL TChart to create a graph. The X value is not being considered for the Horizontal Axis. The bars are not at expected positions neither are the Labels. I have the entire code here. Output with Title. Output without title . TChart has been simply dropped on the Form1 without any adjustments done.

procedure TForm1.FormShow(Sender: TObject);
  procedure AddOneDay(nDay,nVal1,nVal2,nVal3:Integer;wTitle:String) ;
  var
    SuccessSeries: TBarSeries;
    AlertsSeries: TBarSeries;
    ErrorsSeries: TBarSeries;
  begin
    SuccessSeries:=TBarSeries.Create(FChart);
    AlertsSeries:=TBarSeries.Create(FChart);
    ErrorsSeries:=TBarSeries.Create(FChart);

    SuccessSeries.ParentChart:=FChart ;
    AlertsSeries.ParentChart:=FChart ;
    ErrorsSeries.ParentChart:=FChart ;

    SuccessSeries.BarStyle := bsRectGradient;
    AlertsSeries.BarStyle := bsRectGradient;
    ErrorsSeries.BarStyle := bsRectGradient;

    SuccessSeries.Marks.Visible := False;
    AlertsSeries.Marks.Visible := False;
    ErrorsSeries.Marks.Visible := False;

    if nDay<>1 then
    begin  // Need just one set of Legends
      SuccessSeries.Legend.Visible:=False;
      AlertsSeries.Legend.Visible:=False;
      ErrorsSeries.Legend.Visible:=False;
    end
    else
    begin
      SuccessSeries.Legend.Text:='Errors' ;
      AlertsSeries.Legend.Text:='Alerts' ;
      ErrorsSeries.Legend.Text:='Success' ;
    end;

    SuccessSeries.StackGroup:=nDay ;
    AlertsSeries.StackGroup:=nDay ;
    ErrorsSeries.StackGroup:=nDay ;

    SuccessSeries.Transparency:=30 ;
    AlertsSeries.Transparency:=30 ;
    ErrorsSeries.Transparency:=30 ;

//  SuccessSeries.AddXY((nDay-1)*10,nVal1,wTitle,$00615CFC);
//  AlertsSeries.AddXy((nDay-1)*10,nVal2,wTitle,$00ECC2AA);
//  ErrorsSeries.AddXy((nDay-1)*10,nVal3,wTitle,$0086F76F);

    SuccessSeries.AddXY((nDay-1)*10,nVal1,'',$00615CFC);
    AlertsSeries.AddXy((nDay-1)*10,nVal2,'',$00ECC2AA);
    ErrorsSeries.AddXy((nDay-1)*10,nVal3,'',$0086F76F);
  end;
begin
  FChart.SeriesList.Clear;
  with FChart.Title do
  begin
    Font.Size := 15 ;
    Font.Color := clBlack;
    Text.Text:='Performance Chart';
  end;

  AddOneDay(1,10,20,30,'Day-1');
  AddOneDay(2,10,20,30,'Day-2');
  AddOneDay(3,10,20,30,'Day-3');
  AddOneDay(4,10,20,30,'Day-4');
  AddOneDay(5,10,20,30,'Day-5');
  AddOneDay(6,10,20,30,'Day-6');
  AddOneDay(7,10,20,30,'Day-7');
end;


Solution

  • Your code is overcomplex and is adding far too many series. Run the code below against an empty TChart dropped on a form. The X axis labels will be the text passed in by the .Add.

    begin
        Chart1.AddSeries(TBarSeries.create(Chart1));        
        Chart1.AddSeries(TBarSeries.create(Chart1));
    
        Chart1.Series[0].Add(10,'Day-1');
        Chart1.Series[1].Add(20,'Day-1');
        Chart1.Series[0].Add(15,'Day-2');
        Chart1.Series[1].Add(25,'Day-2');
    end;
    

    Bar chart produced by code in answer.

    A rewrite of your code to give desired result:

    procedure TForm1.Button1Click(Sender: TObject);
      var
        SuccessSeries: TChartSeries;
        AlertsSeries: TChartSeries;
        ErrorsSeries: TChartSeries;
    
      procedure AddOneDay(nDay,nVal1,nVal2,nVal3:Integer;wTitle:String) ;
      begin
        SuccessSeries.Add(nVal1,wTitle,$00615CFC);
        AlertsSeries.Add(nVal2,wTitle,$00ECC2AA);
        ErrorsSeries.Add(nVal3,wTitle,$0086F76F);
      end;
    
    begin
      with FChart.Title do
      begin
        Font.Size := 15 ;
        Font.Color := clBlack;
        Text.Text:='Performance Chart';
      end;
      SuccessSeries := FChart.AddSeries(TBarSeries.Create(FChart));
      AlertsSeries := FChart.AddSeries(TBarSeries.Create(FChart));
      ErrorsSeries := FChart.AddSeries(TBarSeries.Create(FChart));
    
      TBarSeries(SuccessSeries).BarStyle := bsRectGradient;
      TBarSeries(AlertsSeries).BarStyle := bsRectGradient;
      TBarSeries(ErrorsSeries).BarStyle := bsRectGradient;
    
      SuccessSeries.Legend.Text:='Errors' ;
      AlertsSeries.Legend.Text:='Alerts' ;
      ErrorsSeries.Legend.Text:='Success' ;
    
      SuccessSeries.Marks.Visible := False;
      AlertsSeries.Marks.Visible := False;
      ErrorsSeries.Marks.Visible := False;
    
      SuccessSeries.Transparency:=30 ;
      AlertsSeries.Transparency:=30 ;
      ErrorsSeries.Transparency:=30 ;
    
      AddOneDay(1,10,20,30,'Day-1');
      AddOneDay(2,10,20,30,'Day-2');
      AddOneDay(3,10,20,30,'Day-3');
      AddOneDay(4,10,20,30,'Day-4');
      AddOneDay(5,10,20,30,'Day-5');
      AddOneDay(6,10,20,30,'Day-6');
      AddOneDay(7,10,20,30,'Day-7');
    end;