How to: plot data by TAChart? lazarus / 55-TAChart ---------------------------------------------------------------------- madepabloh · 2011-08-08 10:05 Hi, I have different values (temperature and humidity) on different edits on my form. Those values refresh after a short time. I would like to plot each one of them by the use of a TACHart plot. How could i do that? Previously i take a look to the samples of the component, but it not clear to me how they works. I neither locted a tutorial on internet, so any help will be wellcome. Thanks! ---------------------------------------------------------------------- BigChimp · 2011-08-08 10:12 There's a huge amount of documentation for TAChart on the wiki... ---------------------------------------------------------------------- madepabloh · 2011-08-08 10:15 I only see this one on the wiki: http://wiki.lazarus.freepascal.org/TAChart and this: http://wiki.lazarus.freepascal.org/TAChart_documentation ---------------------------------------------------------------------- Ask · 2011-08-08 10:18 Please, can you be more specific about the part(s) you do not understand? ---------------------------------------------------------------------- madepabloh · 2011-08-08 10:26 ok, I have four edits components whate refresh (for example) every minute and they have values of Date, time, temperature, humidity. I would like to plot them (temp vs date&time; hum vs date@time). Then, i included a graph on the form, and a  TDateTimeIntervalChartSource component, following the TAChart documentation on the wiki. And here start my problem... how do i assign a new value for each plot, where x axis is date-time and y the temperature or humidity? Thanks! It is the first time i try to use this component i am so lost, and i could not see any tutorial on internet... ---------------------------------------------------------------------- Ask · 2011-08-08 11:05 Still not very specific, but I'll try to guess the missing parts. You want to draw a single plot (we call it chart), containing two graphs (we call them series). I guess you want to draw your graphs as lines. You should put a TChart component on the form, double-click it and add two Line series. Then, you should edit properties of Chart and series using Object Inspector (it is a gray table usually located to the left of the source code editor). There you can set colors, line styles, etc. It is a good programming practice to set the Name property of your chart/series to something descriptive. Perhaps you have already did all this, but you did not tell ;-) If the X axis of your chart will display date/time values, and you want to allow user to zoom/pan your chart, then you have correctly decided to use TDateTimeIntervalChartSource. You should assign it to the Marks.Source property of the Bottom axis. To add new point, call AddXY procedure of each series, for example TemperatureSeries.AddXY(vTime, vTemp); Where vTime is a variable (of type TDateTime) containing the date/time, and vTemp is a variable (of type Double) containing the temperature. ---------------------------------------------------------------------- madepabloh · 2011-08-08 11:13 Thanks Ask!! Yes, you where more detailed. Sorry for not to be as specific as you report here. Ok, i see how to start. I this that this is all what i will need. I suposse that i only need to assign to the vTemp variable the value of the edit containing this data, and change include a new point each time the edit value changes. Great Ask! Thaks for your really detailed information. I will try this afternoon (i do not have lazarus here).  Just few seconds ago i find this, what is more o less what i want, but playing with time on X axis, and more than one parameter: http://mucrofe.blogspot.com/2011/03/starduino-in-action- lm35-temperature.html By the way, how could i have two different Y axis and only one X axis? I mean, both plots have different units (ºC for temperature and % for humidity) vs the date-time what appear on the x axis. Thanks! ---------------------------------------------------------------------- Ask · 2011-08-08 11:30 Quote from: madepabloh on August 08, 2011, 01:13:38 pmBy the way, how could i have two different Y axis and only one X axis? I mean, both plots have different units (ºC for temperature and % for humidity) vs the date-time what appear on the x axis. Thanks! You can add axises by editing AxisList property. You can assign axis to series by editing AxisIndexX/AxisIndexY properties. You can control axis transformations by using TChartTransformations component, as described in the manual and shown in axistransf demo. ---------------------------------------------------------------------- madepabloh · 2011-08-08 11:46 ok Ask, My last question before to try what you proposed along this post: where is the manual? I didn´t see. It could help to me to solve my questions and to learn to use it without disturb to anybody in this forum. Thanks! ---------------------------------------------------------------------- Ask · 2011-08-08 11:53 Quotewhere is the manual?http://wiki.lazarus.freepascal.org/TAChart_documentation In particular, here it talks about axis transformations: http://wiki.lazarus.freepascal.org/TAChart_documentation#Axis_transfor mations ---------------------------------------------------------------------- madepabloh · 2011-08-08 11:55 Ok, i believe there was other manual different than the wiki. Thanks one more time! ---------------------------------------------------------------------- hallenberger · 2011-08-08 12:49 Quote from: madepabloh on August 08, 2011, 12:05:48 pmHi, I have different values (temperature and humidity) on different edits on my form. Those values refresh after a short time. I would like to plot each one of them by the use of a TACHart plot. How could i do that? Previously i take a look to the samples of the component, but it not clear to me how they works. I neither locted a tutorial on internet, so any help will be wellcome. Thanks! I had the same problem - I think - and missed an example for timeaxis too. So I made an example (see attachment). Hope it helps. Thomas ---------------------------------------------------------------------- madepabloh · 2011-08-08 21:37 Thanks hallenberger for the code with an example. I will study it to learn what i need. Cheers! ---------------------------------------------------------------------- madepabloh · 2011-08-08 23:10 I tried to plot something following your ideas.... and it works! Is my first plot!! Thanks dudes! This is the code that i have working: Code: [Select]procedure TForm1.PlotData(); var   moment : double; begin   moment := now;   Edit1.Text:= FloatToStrF(moment,ffFixed,3,4);   DateTimeIntervalChartSource1.DateTimeFormat := 'hh:mm:ss';   AirTemp.AddXY(moment, StrtoFloat(Edit3.Text));   AirHumidity.AddXY(moment, StrtoFloat(Edit4.Text));   DewPoint.AddXY(moment, StrtoFloat(Edit5.Text));   Irradiance.AddXY(moment, StrtoFloat(Edit6.Text));   Light.AddXY(moment, StrtoFloat(Edit7.Text));   WindSpeed.AddXY(moment, StrtoFloat(Edit17.Text)); end; I still must to work a lot on this to have the plot such as i want. But there is something that do not run and that seems that do not work: the format of datetime...  the plot show a number like this 4567.01234 what is the unix time if i am right. Although i formated the datetime component such as proposed Thomas in his example, the format do not change at all. I will need to learn more about plotting data on lazarus, but thans to your ideas and examples i had my first plot. So thanks so much for your inputs! Cheers ---------------------------------------------------------------------- Ask · 2011-08-09 01:50 Quote from: Ask on August 08, 2011, 01:05:56 pmuse TDateTimeIntervalChartSource. You should assign it to the Marks.Source property of the Bottom axis. Quote from: madepabloh on August 09, 2011, 01:10:27 amBut there is something that do not run and that seems that do not work: the format of datetime...  the plot show a number like this 4567.01234 what is the unix time if i am right. Although i formated the datetime component such as proposed Thomas in his example, the format do not change at all. Did you assign the component as suggested above? ---------------------------------------------------------------------- madepabloh · 2011-08-09 08:09 Yes Ask, I did by the using Object Inspector,and it works, but it does not show the time in other form than the  unix number of date. may be is this not the valid way to format the datetime? Code: [Select]DateTimeIntervalChartSource1.DateTimeFormat := 'hh:mm:ss'; In fact, when i move the mouse over the 'hh:mm:ss' part of the code, a yellow marker appear with this text: Quotehh=? and the same when the mouse is over mm or ss. I changed it to YYYYMM, with the same results. It compiles, but nothing changes when i plot data there... EDITED: By the way, what are the possible formats for date and time? i mean, if i want to show both date and time, the format should be "YYYY-MM-DD hh:mm:ss"? Thanks Ask ---------------------------------------------------------------------- Ask · 2011-08-09 10:51 QuoteI did by the using Object Inspector,and it works, but it does not show the time in other form than the  unix number of date.That is very strange, I can not reproduce the problem. Can you post a simple project demonstrating this issue? Quotemay be is this not the valid way to format the datetime? As you can see from the docs: http://www.freepascal.org/docs-html/rtl/sysutils/formatchars.html Your format is valid, but somewhat strange (I think you heed "hh:nn:ss"). But anyway, even for wrong format, *something* should change when setting it. So please post your test project. ---------------------------------------------------------------------- madepabloh · 2011-08-09 11:42 Thanks Ask. You are right, there is an error with minutes format (must be nn), however, it does not solve the problem. Here i attach an example project. Thanks! ---------------------------------------------------------------------- Ask · 2011-08-09 12:35 I see now. You need to set Marks.Style to smsLabel. I clarified this in the docs: http://wiki.lazarus.freepascal.org/TAChart_documentation#Date- time_interval_source ---------------------------------------------------------------------- madepabloh · 2011-08-09 12:43 I see.... it works now!! Thanks for your patience!! ---------------------------------------------------------------------- mas steindorff · 2011-08-12 23:42 Quote from: Ask on August 09, 2011, 02:35:52 pm... I clarified this in the docs: http://wiki.lazarus.freepascal.org/TAChart_documentation#Date- time_interval_source Nice start to the refferance document Ask!  Can I assume later referances to "TChart" can be replace with "TAChart"? ---------------------------------------------------------------------- Ask · 2011-08-13 01:28 Sorry, I do not understand you at all. What references, and why, do you think should be replaced? ---------------------------------------------------------------------- mas steindorff · 2011-08-14 19:51 I see your latest update has removed  the references to tchart. Good work on thepage. ---------------------------------------------------------------------- Pi · 2015-04-21 11:05 I'm trying to show date & times in the bottom axis of the chart too, but it's not working. Do you know what I'm doing wrong? Code: [Select]   the_date_time_source:=TDateTimeIntervalChartSource.Create(self);    the_date_time_source.DateTimeFormat := 'dd/mm/yy hh:nn'; // nn=minutes (mm=month)    chart1.BottomAxis.Marks.Source:=the_date_time_source;    chart1.BottomAxis.Marks.Style:=TSeriesMarksStyle(smsLabel);    the_date_time:=encodedatetime(2014,10,30,9,30,01,00);    chart1lineseries4.addxy(the_date_time,50.99,FormatDateTime('dd/mm/y yyy hh:nn',the_date_time));    the_date_time:=encodedatetime(2014,10,31,10,40,33,00);    chart1lineseries4.addxy(the_date_time,4.86,FormatDateTime('dd/mm/yyyy hh:nn',the_date_time));    the_date_time_source.Free;     It's adding labels (though technically I don't really need them, I just want the dates and approx times - also it's cutting the part of the date/time off the labels - though I don't need them if I can have the dates/times showing on the bottom axis). On the bottom axis with the above code, it's just blank. On chart properties that weren't done in code, the mark's style is set to "smsLabel" If I take that "chart1.BottomAxis.Marks.Style:=TSeriesMarksStyle(smsLabel);" line out, it's also blank and with it as "smsValue" it's a number (whereas I want date & time). ---------------------------------------------------------------------- wp · 2015-04-21 13:43 You must not free the_date_time_source at the end of the procedure. It is needed through-out the entire lifetime of the chart. As you are manually creating the chartsource watch out that you create it only once! There is no need to cast the smsLabel to TSeriesMarksStyle. Just add TAChartUtils to "uses". I don't understand your final paragraph ---------------------------------------------------------------------- Pi · 2015-04-21 18:24 Quote from: wp on April 21, 2015, 03:43:12 pmYou must not free the_date_time_source at the end of the procedure. It is needed through-out the entire lifetime of the chart. As you are manually creating the chartsource watch out that you create it only once! There is no need to cast the smsLabel to TSeriesMarksStyle. Just add TAChartUtils to "uses". I don't understand your final paragraph Thanks. I took the "the_date_time_source.Free" out and it now shows the dates and times on the bottom of the graph (but crammed so much together that they are unreadable currently). I had it in because I was testing this code in a button1 procedure run from the form and I wanted to make sure I freed things that I needed to (so I don't have memory leaks or errors if I called the procedure again). I've attached a picture of the chart. I'd mostly like the dates and times at the bottom of the graph to be readable (show less of them so it's readable - and if possible have the dates below the times). Also I don't really need the labels (shown in yellow) - as long as the dates and times at the bottom are readable. It doesn't really need to show any more than 4 times on the bottom of the graph (the person seeing it should be able to guess approx what the times/dates in between would be - and would have the option of altering the date/time range). ---------------------------------------------------------------------- wp · 2015-04-21 19:55 Yes, a horizontal date/time axis is always a bit crammed. Here are some ideas what you can do against this: Rotate the labels by 90 degress (set BottomAxis.Marks.LabelFont.Orientation to 900 - this number is in tenths of degrees)Don't use a DateTimeFormat for the DateTimeIntervalChartSource. This way some internal logics comes into play which shortens the strings depending on the axis range. Unfortunately, you do have little control of this kind of labelling.Play with the Params property of the DateTimeIntervalChartSource and/or the Intervals of the BottomAxis (I don't know if both of them are effective, though). Increasing the MinLength, e.g., makes the labels less dense. Sometime better labels are achieved by giving the Tolerance a small positive value (say, 5). The way how these settings work, however, are still a bit mysterious to me, but you can get quite decent labels with this method. (I guess if increasing the MinLength you should also increase the Maxlength, not sure what happens if MinLength becomes greater than MaxLength...)The axis marks also have an OverlapPolicy property: if set to opHideNeighbors then overlapping is mostly reduded, but labels are spaced irregularly.Insert a line-break into the format string of the DateTimeIntervalChartSource. You can't do this in the Object Inspector, but certainly by code: DateTimeChartSource.DateTimeFormat := 'dd/mm/yy' + LineEnding + 'hh:nn' - this creates one line with the date and another one with the time.If you still do not get happy with these possibilities use a TListChartSource instead of the TDateTimeIntervalChartSource. This way you have full control over positioning of the marks. Just call ListChartSource.Add(x, y, label_text) for each point where you want a label; x is the coordinate on the x axis, you don't need y it would be needed for the y axis (just use any value here), and label_test is the text that will be shown at these axis points. One big disadvantage though: the labels do not rearrange when you rescale or zoom the chart. Recently, there was a lengthy discussion of a rather complex x axis in the forum (http://forum.lazarus.freepascal.org/index.php/topic,27334.0.html) ---------------------------------------------------------------------- Pi · 2015-04-22 23:20 Thanks a lot wp. I should have posted earlier. But your suggestion of changing MinLength was what made it right (MinLength in DateTimeIntervalChartSource - I think I'd tried changing it in the normal chart properties but I don't think that had worked). So this is what mostly fixed it: the_date_time_source.Params.MinLength:=40;  Plus I made the chart wider but didn't need to rotate the lables (I think unrotated are easier to read). I also used your suggestion for the line break method to put dates under the times (though it does show times like 18:19 then 19:01 when I'd prefer times like 18:20, 19:00 but that's minor), and your suggestion of MinLength (as described above) fixed the problem thanks . ---------------------------------------------------------------------- wp · 2015-04-23 09:48 Quotethough it does show times like 18:19 then 19:01 when I'd prefer times like 18:20, 19:00Maybe you can tune this with the "Tolerance" parameter. ---------------------------------------------------------------------- Pi · 2015-04-24 14:23 Quote from: wp on April 23, 2015, 11:48:42 amQuotethough it does show times like 18:19 then 19:01 when I'd prefer times like 18:20, 19:00Maybe you can tune this with the "Tolerance" parameter. Thanks. I tried that. I with Tolerance 5 the times were "18:19, 19:01, 19:43, 20:25..." with tolerance 6, no change to times with tolerance 10 no change tolerance 1 no change tolerance 100 no change tolerance 1000 no change tolerance 10,000 no change setting use nice steps=true no change ---------------------------------------------------------------------- wp · 2015-04-24 15:55 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 · 2015-04-26 22:10 Quote from: wp on April 24, 2015, 05:55:25 pmBad 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. ---------------------------------------------------------------------- wp · 2015-04-27 14:52 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 · 2015-05-02 02:24 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.