Recent

Author Topic: multiple y-axes, again  (Read 4158 times)

mtanner

  • Sr. Member
  • ****
  • Posts: 287
multiple y-axes, again
« on: March 21, 2017, 10:53:50 pm »
I am returning to this topic again after trying to understand it some while ago.  What I am trying to do is quite straightfoward. I have a chart with two series, assume they are line series. One series uses the left axis, the other series uses the right axis. I want to control the range plotted on each axis.

For a single Y axis I can easily do this using MyChart.Extent, YMin,YMax, UseYMin,UseYMax. I must admit that trying to make this work with two y axes just left me totally baffled, despite some lengthy replies to the posts I submitted then. I gave up through lack of time and redesigned my application to use charts with just a single y-axis.

Now I have another application that could really benefit from separate y-axes. Is it possible for someone to just give me the code to do what I need? The code snippets supplied before, and the info in the various tutorials, does not actually supply complete code for what I am sure must be a common requirement, and I suspect I'm not the only one to have difficulty with this. Once I've got something that works, then maybe I will be better able to understand why it works. But the pressures of development mean that I need to just get things working first.

For single y-axis I use:

With MyChart.Extent do begin
  set YMin,YMax UseYMin, UseYMax in an obvious way
end;

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: multiple y-axes, again
« Reply #1 on: March 21, 2017, 11:43:49 pm »
No, Chart.Extent is not good for multiple axes because it works on the invisible coordinate system shared by all axes ("graph coordinates"). Instead, use the Range property of each axis. It has a Min and Max value to define its limits, as well as boolean UseMin and UseMax properties to activate them. Min and Max are specified in "graph units" -- these are the units in which you add the data, so, nothing to worry about.

You may also want to set axis.Marks.AtDataOnly to true to draw axis labels only between this min and max, and also to set axis.Title.PositionOnMarks to true to center the title between min and max.

As for the steps required to create multiple axes see the tutorials http://wiki.lazarus.freepascal.org/TAChart_Tutorial:_Dual_y_axis,_Legend and http://wiki.lazarus.freepascal.org/TAChart_Tutorial:_Multiple_Panes_in_one_Chart, and the demos in the folders lazarus/components/tachart/demo/panes, panes-2 and pane-3. In Lazarus trunk there is also a runtime-only demo in the folder runtime/dualaxes of the tachart demo directory.

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: multiple y-axes, again
« Reply #2 on: March 22, 2017, 01:52:59 pm »
Thanks for that. I will try and get this to work. Problem is I fall at the first hurdle. Just using a single y-axis chart I try to control the x range with the following code.

With MyChart.BottomAxis.Range do begin
   Min:=<my low value>;
   Max:=<my high value>;
   UseMin:=true;
   UseMax:=true;
end;

bit this has no effect, TAGraph just uses the maximum x range for the data submitted.

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: multiple y-axes, again
« Reply #3 on: March 22, 2017, 03:34:38 pm »
Setting the axis Range is one of the last steps to apply. And I guess it will not work for single-axis charts at all.

Please read the tutorial, I don't want to repeat details: You must add TChartAxisTransformations and an AutoScaleAxisTransform for each of the dual axes. And don't forget to assign the AxisIndex of the series to indicate to which axis it belongs.
« Last Edit: March 22, 2017, 04:09:59 pm by wp »

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: multiple y-axes, again
« Reply #4 on: March 23, 2017, 07:39:13 am »
I will go and do my homework. TAGraph does seem very good, but has some design concepts rather different to TeeChart, so anything other tham straightforward charts requires some investment in learning. It took me a while to get my head around stacked bar charts, but the results are excellent.

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: multiple y-axes, again
« Reply #5 on: March 24, 2017, 11:28:14 am »
Well I've now got dual y-axes working ok at least as far as I need, after following the tutorial. The multiple axes mechanism seems a bit elaborate for what I am doing, because it's obviously a very flexible facility of which I am just scratching the surface.

One small confusing point. I can control the left/right y-axes fine using Range, after setting up the transforms. But when I try and use the same technique to control the range of the (single) x-axis it doesn't work. So I went back to using Extent just for the x-axis and that works ok.

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: multiple y-axes, again
« Reply #6 on: March 24, 2017, 12:56:56 pm »
I think this "equations" are correct (not tested)
  single axis = use Chart.Extent
  axis with transformations = use Axis.Range

You probably can add another ChartAxisTransformation and AutoScaleAxisTransform, and assign it to the bottom axis of the chart -- and don't forget to set the AxisIndexX of the series properly. Then there is a transformation for the x axis, and range should work for it as well.
« Last Edit: March 24, 2017, 06:22:26 pm by wp »

 

TinyPortal © 2005-2018