Forum > TAChart

[SOLVED] Bottom axis disappears

(1/2) > >>

artem101:
Hello all!

I want to display some data with TChart divided by years. I add TDateTimeIntervalChartSource and set Steps to dtsYears.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---object Form1: TForm1  Left = 324  Height = 325  Top = 171  Width = 477  Caption = 'Form1'  ClientHeight = 325  ClientWidth = 477  LCLVersion = '2.0.12.0'  object Chart1: TChart    Left = 0    Height = 325    Top = 0    Width = 477    AxisList = <          item        Marks.LabelBrush.Style = bsClear        Minors = <>        Title.LabelFont.Orientation = 900        Title.LabelBrush.Style = bsClear      end          item        Alignment = calBottom        Marks.Format = '%2:s'        Marks.LabelBrush.Style = bsClear        Marks.Source = DateTimeIntervalChartSource1        Marks.Style = smsLabel        Minors = <>        Title.LabelBrush.Style = bsClear      end>    Foot.Brush.Color = clBtnFace    Foot.Font.Color = clBlue    Title.Brush.Color = clBtnFace    Title.Font.Color = clBlue    Title.Text.Strings = (      'TAChart'    )    Align = alClient    object RedBarSeries: TBarSeries      Title = 'Red'      BarBrush.Color = clRed      BarOffsetPercent = -25      BarWidthPercent = 25      BarWidthStyle = bwPercentMin      Source = RedRandomChartSource    end    object YellowBarSeries: TBarSeries      Title = 'Yellow'      BarBrush.Color = clYellow      BarWidthPercent = 25      BarWidthStyle = bwPercentMin      Source = YellowRandomChartSource    end    object BlueBarSeries: TBarSeries      Title = 'Blue'      BarBrush.Color = clBlue      BarOffsetPercent = 25      BarWidthPercent = 25      BarWidthStyle = bwPercentMin      Source = BlueRandomChartSource    end  end  object RedRandomChartSource: TRandomChartSource    PointsNumber = 6    RandSeed = 935630190    XMax = 38353    XMin = 36526    YMax = 100    Left = 358    Top = 62  end  object YellowRandomChartSource: TRandomChartSource    PointsNumber = 6    RandSeed = 935695833    XMax = 38353    XMin = 36526    YMax = 100    Left = 358    Top = 128  end  object BlueRandomChartSource: TRandomChartSource    PointsNumber = 6    RandSeed = 935750986    XMax = 38353    XMin = 36526    YMax = 100    Left = 358    Top = 200  end  object DateTimeIntervalChartSource1: TDateTimeIntervalChartSource    Steps = [dtsYear]    Left = 224    Top = 40  endend
Looks fine, but if I make window a bit wider bottom axis disappears. How to make it always visible?

Thanks.

wp:
To be honest, I don't know why the Steps property has been published, I don't see any good usage for it. It works like this: The labelling algorithm decides which labels to put to the axes, depending on the axis length in pixels and constraints due to allowed labels count, label distance etc. In case of date/time data it changes the date/time units for optimimum label selection, and these units are stored internally. Steps means: "Display labels only when these internally stored units are among the selected Steps elements". When you set Steps to dtsYears, you get labels only when the internal units are years. When you increase the chart width, the labelling algorithm may switch to dtsMonths, and your year labels will be gone. You could add dtsMonths to the Steps property, but now you will have intermediate labels for x values that are meaningless in a bar chart at yearly intervals.

If I understand this correctly I'd recommand to replace the TDateTimeIntervalChartSource by an ordinary TListChartSource, and populate it with the year values manually:
- Delete the TDateTimeIntervalChartSource
- Add a TListChartSource and assign it to the Marks.Source of the bottom axis. Set the BottomAxis.Marks.Style to smsXValue.
- Click on the '...' next to its DataPoints property to open the DataPoints editor
- In the X column enter the year values that you want to see. You can add extra values if you can expect that your "real data range" might be larger than anticipated now. Or you could generate the labels by code:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.CreateYearLabels;var  year1, year2, year: Integer;begin  year1 := trunc(Chart1Barseries1.MinXValue);  year2 := trunc(Chart1Barseries1.MaxXValue) + 1;  ListChartSource1.Clear;  for year := year1 to year2 do    ListChartSource1.Add(year, 0);end; 

artem101:
By the way, what SuppressPrevUnit propery does? I thought it disable switching to more detailed unit, but I didn't see any difference.

wp:
Maybe the naming of the property is confusing. But the idea is as follows: Suppose that the internal steps are dtsMonths, then labels, by default when "SuppressPrevUnit" is true, are for example "2022/1", "2", "3", ... "12", "2023/1", "2", i.e. the year is only displayed after the year has changed, or: the "previous" unit is suppressed. When, on the other hand, "SuppressPrevUnit" is false, the year is always shown: "2022/1", "2022/2", "2022/3", ...

artem101:

--- Quote from: wp on July 28, 2022, 12:45:28 pm ---I'd recommand to replace the TDateTimeIntervalChartSource by an ordinary TListChartSource, and populate it with the year values manually

--- End quote ---

Thanks for advice. This is what I need, but there is small problem - if choosed interval very big many labels overlaps each other. Is it possible to autohide some of them (for example: 2000 - 2005 - 2010 - 2015 - 2020)?

Navigation

[0] Message Index

[#] Next page

Go to full version