Recent

Author Topic: Move the chart area  (Read 251 times)

Davidous

  • Jr. Member
  • **
  • Posts: 67
Move the chart area
« on: July 06, 2019, 01:47:28 am »
Hello!

The question for this evening is the following:

I would like to make the graph to step forward with a button click for example by 100.
I tried to change the CurrentExtent (Chart.CurrentExtent.a.X:=Chart.CurrentExtent.a.X+100), but then I got the "Argument cannot be assigned to" error.
What would be the easiest way to do that?
Thank you in advance!  :)
« Last Edit: August 05, 2019, 12:44:03 pm by Davidous »

wp

  • Hero Member
  • *****
  • Posts: 6225
Re: Move tha chart area
« Reply #1 on: July 06, 2019, 12:00:36 pm »
There are two errors in your idea:
  • CurrentExtent is a read-only property. It refers to the range between the axis lines which is usually a bit wider than the range defined by the data due to the chart's Margins and series labels. The range defined by the data points is called LogicalExtent. Since the CurrentExtent is calculated from the LogicalExtent and the Margins/label sizes it cannot be modified by the user directly.
  • Although LogicalExtent is a read/write property still you cannot say: Chart1.LogicalExtent.a.x := 100. The type of LogicalExtent is a TDoubleRect (in unit TACharUtils), i.e. a record of two TDoublePoints, a and b, each of them with elements x and y. Since all these are no classes and since there is no setter procedure for them you are forced to assign a complete TDoubleRect record to the chart's LogicalExtent. Something like this:
Code: Pascal  [Select]
  1. procedure TForm1.MoveChartBy(dx, dy: Double);
  2. var
  3.   ex: TDoubleRect;
  4. begin
  5.   ex := Chart1.LogicalExtent;
  6.   ex.a.x := ex.a.x + dx;
  7.   ex.b.x := ex.b.x + dx;
  8.   ex.a.y := ex.a.y + dy;
  9.   ex.b.y := ex.b.y + dy;
  10.   Chart1.LogicalExtent := ex;
  11. end;

Instead of coding it yourself (which is always a good idea!) you could try a TPanClickTool. It moves the chart to the right when the user clicks somewhere within the right margin of the chart (the margin is set by the equally named property, 4 pixels by default).  Similarly with left, top, bottom sides.

Since there is no tutorial on this tool here are the steps how to use it:
  • Add a TChartTools component to the chart. Link it to the "Tools" property of the chart (IMPORTANT - I always forget this)
  • Double-click on the ChartTools component, click "+ Add" and select "Panning by click". This adds a TPanClickTool to the tools component.
  • In the object inspector, make sure that the TPanClickTool is selected. Go to property "Shift" and check "ssLeft" - this assigns the left mouse button to activate the tool.
  • Check the corresponding elements of the "LimitToExtent" property if you want to suppress moving in particular directions. For example, check "pdDown" and "pdUp" if you want to allow only horizontal movement.
  • In property "Interval" you can specify the milliseconds until the next panning step is executed automatically while the mouse button is pressed. Keep it at 0 if you don't want this.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Davidous

  • Jr. Member
  • **
  • Posts: 67
Re: Move tha chart area
« Reply #2 on: July 06, 2019, 12:36:34 pm »
Thank you again wp, your example works perfectly! :)