Plot Histogram with vertical axis in Percentage

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
rimaqui
Newbie
Newbie
Posts: 5
Joined: Wed Jun 20, 2012 12:00 am

Plot Histogram with vertical axis in Percentage

Post by rimaqui » Mon Aug 06, 2012 2:47 pm

Hi,

This is my first post to this forum and I'm also new to Delphi and TeeChart, so, I'm probably missing some obvious function or property.

Is it possible to plot the histogram alternatively in percentage or count, just by changing some property ? I can draw the percentage in the marks, but not in the vertical axis. I have 7 histograms series in the same chart. When I have only one, if I set the vertical axis labelStyle to talMark it will work, although the ticks won't be evenly spaced. If I have more than one series it will be superimposed one scale to another.

Thanks in advance

Ricardo Aquino

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Plot Histogram with vertical axis in Percentage

Post by Yeray » Tue Aug 07, 2012 9:08 am

Hello and welcome Ricardo,

I'm afraid there isn't a Percent axis labels style, as for the series marks. However, you can use the OnGetAxisLabel event to manually change the axis labels texts for the according ones (calculating what percentage corresponds to the current value). Ie:

Code: Select all

procedure TForm1.Chart1GetAxisLabel(Sender: TChartAxis;
  Series: TChartSeries; ValueIndex: Integer; var LabelText: String);
var tmpValue, tmpTotal: Double;
    i: Integer;
begin
  if (Sender=Chart1.Axes.Left) then
  begin
    //get the current value
    tmpValue:=StrToFloat(StringReplace(LabelText, ThousandSeparator, '', []));

    //calculate the Total value used to get each percentage. ie. the max value of all the series
    tmpTotal:=0;
    for i:=0 to Chart1.SeriesCount-1 do
      tmpTotal:=Max(tmpTotal, Chart1[i].YValues.MaxValue);

    //use the current value to calculate the current percentage related tot he Total calculated
    tmpValue:=(tmpValue*100)/tmpTotal;
    LabelText:=FormatFloat('#0.##', tmpValue) + '%';
  end;
end;
Note since this event is called each time a label is going to be drawn, it may be more efficient to calculate the tmpTotal somewhere else. For example, after adding all the values to the series.

Alternatively, you can use custom labels to specify both the texts and positions. You can find an example of it in the features demo program included with the installation, at "All features\Welcome !\Axes\Labels\Custom Labels".
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

rimaqui
Newbie
Newbie
Posts: 5
Joined: Wed Jun 20, 2012 12:00 am

Re: Plot Histogram with vertical axis in Percentage

Post by rimaqui » Tue Aug 07, 2012 12:33 pm

Hi Yeray,
Thank you for your answer.

I have solved the problem with another workaround that is much more, let say, "complex", but it's working fine: I created another chart with the same number of series of the first chart. The series of the second chart are "manual" entered values that I calculate the percentage for each series. When I switch between absolute or percentage, I just make one or another chart visible. The bad think of that arrangement is that I have to make changes to two charts every time I want to make some cosmetic changes.

I have some other issues to post to you:
1. It seems to me that would be very simple to implement such a solution of the above issue, once you have all values already calculated, isn't it? The Histogram function could have an option of percentage or absolute values.

2.The position of the Bars of the Histogram are being placed out of the correct position. I have tried to change the property periodAlign to paFirst and paCenter, but it makes no difference. Could you give me some hint on that ?

3. Botton axis datetime format: the botton axis format checkbox of the first histogram series is always checked when I run the program. I had to force it by code. I think this is a bug.

If you need some screen shot of the plots, please let me know.

Thank you again

Ricardo Aquino

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Plot Histogram with vertical axis in Percentage

Post by Yeray » Wed Aug 08, 2012 3:43 pm

Hi Ricardo,
rimaqui wrote:1. It seems to me that would be very simple to implement such a solution of the above issue, once you have all values already calculated, isn't it? The Histogram function could have an option of percentage or absolute values.
I'm not sure if it would be so easy... I understand it should be an extra TAxisLabelStyle but this would take effect for all the series types, so it should be studied if would be also meaningful with the affected series types.
Have you tried the alternatives suggested?
rimaqui wrote:2.The position of the Bars of the Histogram are being placed out of the correct position. I have tried to change the property periodAlign to paFirst and paCenter, but it makes no difference. Could you give me some hint on that ?
If the values are different, the space taken to draw the labels may be also different, and thus the chart rectangle exact position will also be different.
You can try setting a manual left/top margin (I'd change the margin units to mupixels before) to take the same space with both charts.
But I still think the suggested workaround in my first reply would be easier to implement.
rimaqui wrote:3. Botton axis datetime format: the botton axis format checkbox of the first histogram series is always checked when I run the program. I had to force it by code. I think this is a bug.
Does it happen in a new simple example for you? I create a new form, I place a new chart on it, I create a THistogramSeries and I see the values in the bottom axis aren't DateTime.
If you still have problems with it, please, try to arrange a simpleexample project we can run as-is to reproduce the problem here.

Thanks in advance.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

rimaqui
Newbie
Newbie
Posts: 5
Joined: Wed Jun 20, 2012 12:00 am

Re: Plot Histogram with vertical axis in Percentage

Post by rimaqui » Thu Aug 23, 2012 6:24 pm

Hi Yeray,
2.The position of the Bars of the Histogram are being placed out of the correct position. I have tried to change the property periodAlign to paFirst and paCenter, but it makes no difference. Could you give me some hint on that ?
Now I've returned to the histogram bar positions and I did a very simple example to show you what I'm talking about. The two series of the first plot are simple manual entered fixed values at zero and five respectively. The other plot is the histogram of the two first series. You can see that the bars are located out of the correct position, regardless if I set the properties PeriodAlign and PeriodStyle to whatever values. Is there another property that should be changed?

Thanks
Ricardo Mayer de Aquino
Attachments
Histogram.7z
Simple project for histogram bar placing position
(1.36 KiB) Downloaded 350 times

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Plot Histogram with vertical axis in Percentage

Post by Yeray » Fri Aug 24, 2012 3:17 pm

Hi Ricardo,

I see what you meant now. Thanks for the explanation and the example.
I've added it to the defect list to be revised asap (TV52016316).
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Post Reply