Page 1 of 1

SeriesGroups / TBarSeries label issue

Posted: Mon Sep 24, 2018 11:59 am
by 16884335
Greetings,

we have an issue with SeriesGroups option. X bar (Axis.Bottom) date time labels are located quite randomly.
How could we adjust your code so that group dates would appear correctly (centred) under group bars?

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var
bar: TBarSeries;
begin
  chart1.ClearChart;
  chart1.Legend.Hide;

  (chart1.SeriesGroups.Add as TSeriesGroup).Name:='A';
  (chart1.SeriesGroups.Add as TSeriesGroup).Name:='B';
  (chart1.SeriesGroups.Add as TSeriesGroup).Name:='C';
 //   chart1.BottomAxis.Automatic := false;
//    chart1.BottomAxis.SetMinMax(strtodate('21.05.2018')-3,strtodate('30.05.2018')+3);
    chart1.Axes.Bottom.LabelStyle :=  talPointValue;
//    chart1.Axes.Bottom.LabelsSeparation := 0;
    chart1.BottomAxis.DateTimeFormat := 'dd.mm.yyyy';

    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.AddXY(strtodate('21.05.2018'), 85, 'test1');
    chart1.SeriesGroups.Items[0].Add(bar);

    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.AddXY(strtodate('21.05.2018'), 19, 'test2');
    chart1.SeriesGroups.Items[0].Add(bar);

    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.AddXY(strtodate('21.05.2018'), 15, 'test3');
    chart1.SeriesGroups.Items[0].Add(bar);



    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.AddXY(strtodate('29.05.2018'), 151, 'test 4');
    chart1.SeriesGroups.Items[1].Add(bar);

    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.AddXY(strtodate('29.05.2018'), 195, 'test 5');
    chart1.SeriesGroups.Items[1].Add(bar);

    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.AddXY(strtodate('29.05.2018'), 101, 'test 6');
    chart1.SeriesGroups.Items[1].Add(bar);



    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.AddXY(strtodate('30.05.2018'), 11, 'test 7');
    chart1.SeriesGroups.Items[2].Add(bar);

    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.AddXY(strtodate('30.05.2018'), 105, 'test 8');
    chart1.SeriesGroups.Items[2].Add(bar);

    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.AddXY(strtodate('30.05.2018'), 21, 'test 9');
    chart1.SeriesGroups.Items[2].Add(bar);
end;

Re: SeriesGroups / TBarSeries label issue

Posted: Thu Sep 27, 2018 7:48 am
by yeray
Hello,

Having 9 TBarSeries, the axis label is placed giving space for all the 9 bars. I've edited your screenshot, moving the bars from the right to the left so they are all together, and you can see how the label is placed correctly:
projection.png
projection.png (13.89 KiB) Viewed 5978 times
I would create as many series as bars you want to be placed at the same date. In this case I would create 3 bar series. Ie:

Code: Select all

  chart1.ClearChart;
  chart1.Legend.Hide;

  (chart1.SeriesGroups.Add as TSeriesGroup).Name:='A';
  (chart1.SeriesGroups.Add as TSeriesGroup).Name:='B';
  (chart1.SeriesGroups.Add as TSeriesGroup).Name:='C';
 //   chart1.BottomAxis.Automatic := false;
//    chart1.BottomAxis.SetMinMax(strtodate('21.05.2018')-3,strtodate('30.05.2018')+3);
    chart1.Axes.Bottom.LabelStyle :=  talPointValue;
//    chart1.Axes.Bottom.LabelsSeparation := 0;
    chart1.BottomAxis.DateTimeFormat := 'dd.mm.yyyy';

    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.AddXY(strtodate('21.05.2018'), 85, 'test1');
    bar.AddXY(strtodate('29.05.2018'), 151, 'test 4');
    bar.AddXY(strtodate('30.05.2018'), 11, 'test 7');
    chart1.SeriesGroups.Items[0].Add(bar);

    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.AddXY(strtodate('21.05.2018'), 19, 'test2');
    bar.AddXY(strtodate('29.05.2018'), 195, 'test 5');
    bar.AddXY(strtodate('30.05.2018'), 105, 'test 8');
    chart1.SeriesGroups.Items[0].Add(bar);

    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.AddXY(strtodate('21.05.2018'), 15, 'test3');
    bar.AddXY(strtodate('29.05.2018'), 101, 'test 6');
    bar.AddXY(strtodate('30.05.2018'), 21, 'test 9');
    chart1.SeriesGroups.Items[0].Add(bar);
Project3_2018-09-27_09-40-01.png
Project3_2018-09-27_09-40-01.png (25.95 KiB) Viewed 5978 times
Now the problem is the distance between the days 21 and 29 is very different to the distance between the days 29 and 30. The result is correct but the bars overlap.
I see two options to solve this:
- You could make the 3 bar series thinner:

Code: Select all

bar.BarWidthPercent:=20;
Project3_2018-09-27_09-43-21.png
Project3_2018-09-27_09-43-21.png (25.47 KiB) Viewed 5978 times
- You could fake the xvalues and use the dates for the labels:

Code: Select all

    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.Add(85, 'test1');
    bar.Add(151, 'test 4');
    bar.Add(11, 'test 7');
    chart1.SeriesGroups.Items[0].Add(bar);

    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.Add(19, 'test2');
    bar.Add(195, 'test 5');
    bar.Add(105, 'test 8');
    chart1.SeriesGroups.Items[0].Add(bar);

    bar := chart1.AddSeries(TBarSeries) as TBarSeries;
    bar.XValues.DateTime := true;
    bar.Add(15, 'test3');
    bar.Add(101, 'test 6');
    bar.Add(21, 'test 9');
    chart1.SeriesGroups.Items[0].Add(bar);

    with Chart1.Axes.Bottom.Items do
    begin
      Clear;
      Add(0, '21.05.2018');
      Add(1, '29.05.2018');
      Add(2, '30.05.2018');
    end;
Project3_2018-09-27_09-46-54.png
Project3_2018-09-27_09-46-54.png (28.6 KiB) Viewed 5978 times