Page 1 of 1

Plot Histogram with vertical axis in Percentage

Posted: Mon Aug 06, 2012 2:47 pm
by 16462785
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

Re: Plot Histogram with vertical axis in Percentage

Posted: Tue Aug 07, 2012 9:08 am
by yeray
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".

Re: Plot Histogram with vertical axis in Percentage

Posted: Tue Aug 07, 2012 12:33 pm
by 16462785
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

Re: Plot Histogram with vertical axis in Percentage

Posted: Wed Aug 08, 2012 3:43 pm
by yeray
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.

Re: Plot Histogram with vertical axis in Percentage

Posted: Thu Aug 23, 2012 6:24 pm
by 16462785
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

Re: Plot Histogram with vertical axis in Percentage

Posted: Fri Aug 24, 2012 3:17 pm
by yeray
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).