Recent

Author Topic: How to: plot data by TAChart?  (Read 30907 times)

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: How to: plot data by TAChart?
« Reply #30 on: April 24, 2015, 05:55:25 pm »
Bad advice - sorry. Could you create a little test project which shows the issue and which I can compile and play with myself? (*.lpr, *.lpi, *.pas and *lfm files only, packed into a single zip which you can upload, no ppu, exe etc).

Pi

  • Jr. Member
  • **
  • Posts: 75
Re: How to: plot data by TAChart?
« Reply #31 on: April 27, 2015, 12:10:10 am »
Bad advice - sorry. Could you create a little test project which shows the issue and which I can compile and play with myself? (*.lpr, *.lpi, *.pas and *lfm files only, packed into a single zip which you can upload, no ppu, exe etc).
I've created a little test program (so figures are a bit different, and only uses 1 series of the 4 in the chart but shows the issue):

Click on button1 to view chart.

ChartTest figures:

with BottomAxis->Intervals->Tolerance=1

21:33
22:06
22:39
23:12
23:45
00:18

Manually change
BottomAxis->Intervals->Tolerance to 100 in the properties,
then re-run the program.  All figures above stay the same.
--
Also, a separate but related thing - if the user wanted to click on a particular date and time (anywhere across the x axis (bottom axis) of the chart. Is there a simple way of the program knowing what date & time the user selected? eg. say the chart was showing 00:00 on the left and 00:00 the next day on the right, and the user clicks in the middle of the chart (x axis), the program would then know the user had selected 12:00 on the first day and could also draw a line for the user's selected date & time.
« Last Edit: April 27, 2015, 12:14:48 am by Pi »
Lazarus version 0.9.30.4 + Lazarus 32 bit 1.2.6

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: How to: plot data by TAChart?
« Reply #32 on: April 27, 2015, 04:52:17 pm »
I am not 100% sure, but I fear this cannot be achieved by TDateTimeIntervalChartSource. I'd prefer to use a TListChartSource instead which gives you full control of the label positioning process. You just have to populate the ListSource with the coordinates and texts for the points at which you want to have a label.

Some extra code is required if you want the labels to adjust themselves according to the space available. Please see the attached modified version of your demo - there's plenty of comments to explain what's happening. The code starts with 1-minute intervals and increases the interval until adjacent labels do no longer overlap. For simplicity, I am only checking the first two labels - this is ok in your case where labels have the same width approximately. The chart is aligned with the form, therefore, if you reduce the width of the form at runtime you will come to a point where the next interval is selected, but there are still nice labels. You can also zoom in (by dragging a rectangle with the mouse), and you will get nice labels again.

As for your second question: Do you know the ChartTools? If not, see for example this tutorial: http://wiki.lazarus.freepascal.org/TAChart_Tutorial:_Chart_Tools

Chart tools are designed for what you mention. Since you want to read values also between data points I used a TUserDefinedTool; the vertical line is added as a TConstantLine series to the chart. It is normally turned off (Active = false). In the Mousedown event of the ChartTool, the line is turned on and moved to the position of the mouse cursor (use Chart.ImageToGraph and Chart.GraphToImage methods to convert between pixels and plotted coordinates). In the MouseUp, the line is hidden again. And in the OnDraw event of the chart I added code which displays a box at the upper end of the vertical line showing the currently selected time.

A comment on your code:
Don't create the ChartSource as a local variable in the ButtonClick procedure. When this procedure is exited the variable disappears, but the ChartSource is still living - there is no easy way to access it again. Define the ChartSource as an element of the Form, or add it to the form at design time using the object inspector.

Pi

  • Jr. Member
  • **
  • Posts: 75
Re: How to: plot data by TAChart?
« Reply #33 on: May 02, 2015, 04:24:06 am »
Thanks a lot wp. That has the things I needed. Including the "selected time" bar thing and better (simpler to the user) time formatting, plus zoom function. I'll also use your suggestion about ChartSource.

The label thing especially is quite a bit of code. I'd imagine these types of thing would be wanted by quite a lot of people using tChart (eg. where on of the axis has dates & times) - so maybe something like these could be added as a standard part of the TChart component (or similarly named component). But thanks again for your help.
Lazarus version 0.9.30.4 + Lazarus 32 bit 1.2.6

 

TinyPortal © 2005-2018