ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  X Axis sync with data issue
Posted by JP on Apr-09-2012 22:32
Hi -- I've been having some challenges getting chartdirector to work like I want it to.  I am new at this.  I am using Chardriector for RealBasic (RealStudio).

I have set up a program to read weather data from my weather station.  I store that data in a listbox which includes the date and time of the read.  Date and Time are in separate columns.  The other columns in the listbox are for the actual weather data.  Each new read adds another line in the listbox.  At midnight I clear the listbox and start fresh.

My first problem was getting the x axis to a full scale (24 hours) at the start.  Using labels(-1) which points to the time stamp in my listbox column 1, upon start up I would get the timestamp (labels) for the read in the center of the xaxis (at the  bottom as a label from the labels(-1)), then it went immediately to the left and the lines appeared.  As new reads came in I can see the data (lines change shape) and a new time would appear on the full right of the graph, then work it's way to the left with each read.  The time (labels) would appear depending on the step I set.

After looking for hints on this forum, I decided to convert the read times to seconds after midnight, then used the setDateScale(0, 86400, label).  Label is the label(-1) which you will see in the code below that shows time as 12:00 AM, 1:00 AM, etc.  I now get the full scale on the graph and the lines start at the left of the graph.

What I am noticing is the data point really does not correspond to the time of the data read (i.e. what is stored in listbox2.Cell(i,1).  I can play with the setDateScale(0, 86400, label) and make it say setDateScale(0, 43200, label).  This makes it a bit better, but of course this is not correct.

What I would like to do is get the data read plotted against the time of the read and show correctly on the graph.  I want the graph to show full scale from midnight to midnight.  When I start the program say at 7:00 PM, I would like for lines to start at 7:00 PM on the graph and not fully to the left.  Now they start to fully to the left whether I started the program at 7:00 PM, or after the listbox clear at midnight (which clears the data and the graph starts over)

I really do not know what I am doing, so any help would be great.  Thanks

JP

Below is the code (attached also):

//
//
//

note: labels(-1) and label(-1) are different.  Not sure what I am doing with labels(-1) at this point.  It does point to the listbox2 column that has the time as seconds after midnight.  label(-1) I use to put the label I want on the bottom of the graph via: c.xAxis.setDateScale(0, 86400, label).

//
//  setting it up
//

  dim AmbT(-1) As Double
  dim DewP(-1) As Double
  dim Hum(-1) As Double
  dim Baro(-1) As Double
  dim labels(-1) as string

  dim label(-1) as string = array("12:00 AM", "1:00 AM", "2:00 AM", "3:00 AM", "4:00 AM", "5:00 AM", "6:00 AM", "7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM", "9:00 PM", "10:00 PM", "11:00 PM", "12:00 AM")

  dim i,u as integer

  //
  // Paint to graph area
  //

  u=Listbox2.ListCount-1
  for i=0 to u

    labels.Append listbox2.Cell(i,1)
    AmbT.Append val(listbox2.Cell(i,2))
    DewP.Append val(listbox2.Cell(i,3))
    Hum.Append val(listbox2.Cell(i,4))
    Baro.Append val(listbox2.Cell(i,5))

  next

  dim c as new CDXYChartMBS(Canvas2.width, Canvas2.Height)

  dim legendBox as CDLegendBoxMBS
  legendBox = c.addLegend(393, 20, false, "arial.ttf", 8)
  legendBox.setAlignment(CDXYChartMBS.kBottomCenter)
  legendBox.setBackground(CDXYChartMBS.kTransparent, CDXYChartMBS.kTransparent)

  call c.setPlotArea(30,20, Canvas2.width-55, Canvas2.height-50).setGridColor(&hcccccc, &hcccccc)

  c.addLineLayer(AmbT, &cFF000000, "Temperature").setLineWidth(2)
  c.addLineLayer(DewP, &c00FF0000, "Dew Point").setLineWidth(2)
  c.addLineLayer(Hum, &c0000FF00, "Humidity").setLineWidth(2)
  c.addLineLayer(Baro, &c66CCFF00, "Pressure").setLineWidth(2)

  c.xAxis.setDateScale(0, 86400, label)


  c.xAxis.setLabelStep 2

  Canvas2.Backdrop = c.makeChartPicture

  //
  //
  //

  Re: X Axis sync with data issue
Posted by Peter Kwan on Apr-10-2012 01:48
Hi JP,

Assuming labels is an array that contains a number from 0 to 86400, representing the seconds of day of your data points. In this case, the code is like:

*** Note: I am not familiar with RealBasic, and are not sure if the following code is correct.

dim layer as CDLayerMBS

layer =  c.addLineLayer(AmbT, &cFF000000, "Temperature")
layer.setLineWidth(2)
layer.setXData(labels)

Basically, you would need to use setXData, so that ChartDirector knows where should each data point be plotted. You would need to apply setXData to all of your layers.

Hope this can help.

Regards
Peter Kwan

  Re: X Axis sync with data issue
Posted by JP on Apr-10-2012 02:25
Peter -- thank you very much.  This works great.

JP

  Re: X Axis sync with data issue
Posted by Peter Kwan on Apr-10-2012 01:51
Hi JP,

Assuming labels is an array that contains a number from 0 to 86400, representing the seconds of day of your data points. In this case, the code is like:

*** Note: I am not familiar with RealBasic, and are not sure if the following code is correct.

dim layer as CDLayerMBS

layer =  c.addLineLayer(AmbT, &cFF000000, "Temperature")
layer.setLineWidth(2)
layer.setXData(labels)

Basically, you would need to use setXData, so that ChartDirector knows where should each data point be plotted. You would need to apply setXData to all of your layers.

Hope this can help.

Regards
Peter Kwan

  Re: X Axis sync with data issue
Posted by JP on Apr-10-2012 13:13
Peter --new question.  The graph worked fine with your suggestion.  But when i reached midnight the graph continued past the right margin.  I was trying to set it up to start over again at the left.  I clear the listbox at midnight which brings in new data.  How do I tell the char to start at the beginning?  Here is the current code:


  dim AmbT(-1) As Double
  dim DewP(-1) As Double
  dim Hum(-1) As Double
  dim Baro(-1) As Double
  'dim UV(-1) As Double
  'dim Solar(-1) As Double
  dim labels(-1) as Double
  dim i,u as integer

  dim label(-1) as string = array("12:00 AM", "1:00 AM", "2:00 AM", "3:00 AM", "4:00 AM", "5:00 AM", "6:00 AM", "7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM", "9:00 PM", "10:00 PM", "11:00 PM", "12:00 AM")

  //
  // Paint to graph area
  //

  u=Listbox2.ListCount-1
  for i=0 to u

    labels.Append val(listbox2.Cell(i,1))
    AmbT.Append val(listbox2.Cell(i,2))
    DewP.Append val(listbox2.Cell(i,3))
    Hum.Append val(listbox2.Cell(i,4))
    Baro.Append val(listbox2.Cell(i,5))
    'UV.Append val(listbox2.Cell(i,7))
    'Solar.Append val(listbox2.Cell(i,7))

  next

  dim c as new CDXYChartMBS(Canvas2.width, Canvas2.Height)

  dim legendBox as CDLegendBoxMBS
  legendBox = c.addLegend(393, 20, false, "arial.ttf", 8)
  legendBox.setAlignment(CDXYChartMBS.kBottomCenter)
  legendBox.setBackground(CDXYChartMBS.kTransparent, CDXYChartMBS.kTransparent)

  'call c.setPlotArea 30,10, 736, 144
  call c.setPlotArea(30,20, Canvas2.width-70, Canvas2.height-50).setGridColor(&hcccccc, &hcccccc)

  dim layer As CDLayerMBS

  layer = c.addLineLayer(AmbT, &cFF000000, "Temperature")
  layer.setLineWidth(2)
  layer.setXData(labels)

  layer = c.addLineLayer(DewP, &c00FF0000, "Dew Point")
  layer.setLineWidth(2)
  layer.setXData(labels)
  layer = c.addLineLayer(Hum, &c0000FF00, "Humidity")

  layer.setLineWidth(2)
  layer.setXData(labels)

  c.yAxis2.setColors(0, &h66aaee)', &c66CCFF00, -1, -1)

  c.yAxis2.setLinearScale(26, 34)

  layer = c.addLineLayer(Baro, &c66CCFF00, "Pressure")
  layer.setLineWidth(2)
  layer.setXData(labels)

  'c.xAxis.setLabelFormat "{value|hh:nn:ss}"
  c.xAxis.setDateScale 0, 86400-1, label

  c.xAxis.setLabelStep 2

  Canvas2.Backdrop = c.makeChartPicture

  //
  //
  //

Thanks
JP

  Re: X Axis sync with data issue
Posted by JP on Apr-10-2012 13:22
Peter --

Note that in the code I just sent I had already changed:

c.xAxis.setDateScale 0, 86400, label

to this:

c.xAxis.setDateScale 0, 86400-1, label

Not sure if that will make a difference -- but we'll see what happens at midnight.

JP

  Re: X Axis sync with data issue
Posted by Peter Kwan on Apr-11-2012 00:27
Hi JP,

Actually, I think "c.xAxis.setDateScale 0, 86400, label" is more correct, as it matches with your labels. Your last label 12:00AM should be at 86400.

If you have a data point at x = 88400 (which is beyond 86400), and you want it to appear at x = 2000 instead, please modify your x coordinate to use 2000 instead of 86400.

One thing to note is that if your data are flowing from left to right (which is the most common case), and then beyond a certain point, it goes back to the left side, this will cause a line to flow backwards to join the right most point to the next point on the left side. To avoid this from happening, when you cross 86400,  you would need to insert a NoValue point to break the line. (***NOTE***: I am not exactly sure what is the syntax of NoValue in RealBasic. I think it is called kNoValue in RealBasic.)

Hope this can help.

Regards
Peter Kwan

  Re: X Axis sync with data issue
Posted by JP on Apr-11-2012 00:51
Thanks Peter --

As soon as I get to 86401i want the graph to start fresh on the left. So do I replace
the 0 with a 1?

Regarding the novalue -- are there any parameters that go with it? Do I call it at the
chart level our the layer level?

Thanks for your quick response
JP

  Re: X Axis sync with data issue
Posted by Peter Kwan on Apr-11-2012 01:48
Hi JP,

It should be replaced with 1.

Personally, I think 86400 should be replaced with 0 as well. So your x-coordinates should really be from 0 to 86399.9999999999999999. Once it reaches 86400, it goes back to 0. You can achieve this by using the modulus operator, which is supported in most programming languages and in SQL as well.

NoValue (or kNoValue ?) is a numeric constant defined in ChartDirector (just like Transparent is a constant in ChartDirector). It is not a function and does not need to be called. The NoValue should be used just like any other number.

The RealBasic edition of ChartDirector is not ported by us, so I am not sure the exact syntax in RealBasic. There is a sample code called "Missing Data Points" that come with ChartDirector. I guess this sample code is also available in the RealBasic edition of ChartDirector as well. The sample code illustrates how to use the NoValue constant. May be you can refer to that sample code to see the exact syntax of NoValue in RealBasic.

Hope this can help.

Regards
Peter Kwan

  Re: X Axis sync with data issue
Posted by JP on Apr-11-2012 05:04
Peter --Hmmm ... Interesting. I'll play around with
this and let you know how it goes.

Thanks for your quick response
JP

  Re: X Axis sync with data issue
Posted by JP on Apr-12-2012 00:46
Hi Peter -- this is the code I am trying now.  Will see how this works tonight.  I am reseting everything at midnight and making all the graph values = novalue ... I think.

In addition to the chartdirector code, I also included the code on what I do at midnight and how I convert to seconds after midnight.

I am still digging into the modulus operator.  That feature is available in realbasic.  I just need to figure out what to do with it and your suggestion.  I am learning not only ChartDierctor but RealBasic at the same time.

I do appreciate your help on this.

Thanks
JP

//
//  Building the graph
//

  dim AmbT(-1) As Double
  dim DewP(-1) As Double
  dim Hum(-1) As Double
  dim Baro(-1) As Double

  dim labels(-1) as Double

  dim label(-1) as string = array("12:00 AM", "1:00 AM", "2:00 AM", "3:00 AM", "4:00 AM", "5:00 AM", "6:00 AM", "7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM", "9:00 PM", "10:00 PM", "11:00 PM", "12:00 AM")

  dim i,u as integer

  //
  // Paint to graph area
  //

  u=Listbox2.ListCount-1
  for i=0 to u

    labels.Append val(listbox2.Cell(i,1))
    AmbT.Append val(listbox2.Cell(i,2))
    DewP.Append val(listbox2.Cell(i,3))
    Hum.Append val(listbox2.Cell(i,4))
    Baro.Append val(listbox2.Cell(i,5))

  next

  dim c as new CDXYChartMBS(Canvas2.width, Canvas2.Height)

  dim legendBox as CDLegendBoxMBS
  legendBox = c.addLegend(393, 20, false, "arial.ttf", 8)
  legendBox.setAlignment(CDXYChartMBS.kBottomCenter)
  legendBox.setBackground(CDXYChartMBS.kTransparent, CDXYChartMBS.kTransparent)

  'call c.setPlotArea 30,10, 736, 144
  call c.setPlotArea(30,20, Canvas2.width-70, Canvas2.height-50).setGridColor(&hcccccc, &hcccccc)

  dim layer As CDLayerMBS

  layer = c.addLineLayer(AmbT, &cFF000000, "Temperature")
  layer.setLineWidth(2)
  layer.setXData(labels)

  layer = c.addLineLayer(DewP, &c00FF0000, "Dew Point")
  layer.setLineWidth(2)
  layer.setXData(labels)
  layer = c.addLineLayer(Hum, &c0000FF00, "Humidity")

  layer.setLineWidth(2)
  layer.setXData(labels)

  c.yAxis2.setColors(0, &h66aaee)', &c66CCFF00, -1, -1)

  c.yAxis2.setLinearScale(28, 32)

  layer = c.addLineLayer(Baro, &c66CCFF00, "Pressure")
  layer.setLineWidth(2)
  layer.setXData(labels)

  'c.xAxis.setLabelFormat "{value|hh:nn:ss}"
  c.xAxis.setDateScale 0, 86400, label

  c.xAxis.setLabelStep 2

  Canvas2.Backdrop = c.makeChartPicture

  //
  //
  //

//
// Then at midnight:
//

  If date.LongTime = "12:00:00 AM"  then 'or date.LongTime = "12:00:01 PM" or date.LongTime = "12:00:02 PM"

    Listbox2.DeleteAllRows

    time = 0

    labels(time) = CDBaseChartMBS.kNoValue

    AmbT(time) = CDBaseChartMBS.kNoValue

    DewP(time) = CDBaseChartMBS.kNoValue

    Hum(time) = CDBaseChartMBS.kNoValue

    Baro(time) = CDBaseChartMBS.kNoValue


  Else

  end

//
//
//

//
// I get time this way:
//

  Dim timeArray As String
  Dim time As Integer

  timeArray = Str(date.LongTime)

  //
  // Convert to seconds after midnight
  //

  If  NthField(timeArray, " ", 2) = "PM" Then

    time = (val(NthField(timeArray, ":", 1)))*3600 + (val(NthField(timeArray, ":", 2)))*60 + (val(NthField(timeArray, ":", 3))) +43200


  Else

    time = (val(NthField(timeArray, ":", 1)))*3600 + (val(NthField(timeArray, ":", 2)))*60 + (val(NthField(timeArray, ":", 3))) - 43200


  end

//
//
//

  Re: X Axis sync with data issue
Posted by JP on Apr-12-2012 02:15
Peter -- Please use this for seconds after midnight.  I think I have this correct -- I've been battling the "When it is "12 PM or "12 AM" issue for a copule of days.

By the way -- my logic for the "knovalue" is not working ...

//
// Convert to seconds after midnight
//

  Dim timeArray As String


  timeArray = Str(date.LongTime)


  If  NthField(timeArray, " ", 2) = "PM" Then

    If val(NthField(timeArray, ":", 1)) = 12 then

      time = (val(NthField(timeArray, ":", 1)))*3600 + (val(NthField(timeArray, ":", 2)))*60 + (val(NthField(timeArray, ":", 3)))


    Else

      time = ((val(NthField(timeArray, ":", 1)))+12)*3600 + (val(NthField(timeArray, ":", 2)))*60 + (val(NthField(timeArray, ":", 3)))


    end

  Else

    If val(NthField(timeArray, ":", 1)) = 12 then

      time = ((val(NthField(timeArray, ":", 1)))-12)*3600 + (val(NthField(timeArray, ":", 2)))*60 + (val(NthField(timeArray, ":", 3)))


    Else

      time = ((val(NthField(timeArray, ":", 1))))*3600 + (val(NthField(timeArray, ":", 2)))*60 + (val(NthField(timeArray, ":", 3)))


    end
  end

  Re: X Axis sync with data issue
Posted by Peter Kwan on Apr-12-2012 02:42
Hi JP,

Whereas I am not familiar with RealBasic, from your code, I think it has not reset everything to kNoValue at midnight. It may have reset at most one element (the element at time = 0) to kNoValue. It may be possible that no element is reset to kNoValue after all, if your code later stores a valid value at time = 0.

In VB6/VBScript syntax, the code to "reset everything to kNoValue" should be like:

For i = 0 To Ubound(labels)
    labels(i) = CDBaseChartMBS.kNoValue
    AmbT(i) = CDBaseChartMBS.kNoValue
    DewP(i) = CDBaseChartMBS.kNoValue
    Hum(i) = CDBaseChartMBS.kNoValue
    Baro(i) = CDBaseChartMBS.kNoValue
Next

(You may need to modify the above code to RealBasic syntax.)

Regards
Peter Kwan

  Re: X Axis sync with data issue
Posted by JP on Apr-12-2012 04:05
Thanks Peter --

I will try your idea next.  It is kind of working with the below code.  I do not get a line across the graph from the last point to the new start at the left.  I do however keep the old data (lines) on the graph.  Is there a way to clear the graph?

JP

PS -- by the way, ct is just a counter that I use when I populate the listbox.  Also I could just use:

ListBox2.Cell(ct,1)=Str(CDXYChartMBS.kNoValue)
...

//
//
//


  If date.LongTime = "12:00:00 AM" then 'or date.LongTime = "12:00:01 PM"  then 'or date.LongTime = "12:00:02 PM"

    Listbox2.DeleteAllRows

    time = 0


    Listbox2.InsertRow(ct, "")

    dataArray(1) = Str(CDXYChartMBS.kNoValue)
    ListBox2.Cell(ct,1)=dataArray(1)
    dataArray(2) = Str(CDXYChartMBS.kNoValue)
    ListBox2.Cell(ct,2)=dataArray(1)
    dataArray(3) = Str(CDXYChartMBS.kNoValue)
    ListBox2.Cell(ct,3)=dataArray(1)
    dataArray(4) = Str(CDXYChartMBS.kNoValue)
    ListBox2.Cell(ct,4)=dataArray(1)
    dataArray(5) = Str(CDXYChartMBS.kNoValue)
    ListBox2.Cell(ct,5)=dataArray(1)

    count = count + 1

    TextField12.Text = str(count)

  Else

  end

//
//
//

  Re: X Axis sync with data issue
Posted by JP on Apr-12-2012 08:57
Peter -- the below appears to be working.  Not sure if I need to clear the graph or if it will happen automatically.

Also -- now I've introduced somewhere a process that is taking CPU cycles.  Not sure how that happened.

JP

//
//
//

  If date.LongTime = "12:00:00 AM" then 'or date.LongTime = "12:00:01 PM"  then 'or date.LongTime = "12:00:02 PM"

    Listbox2.DeleteAllRows

    time = 0

    dim n As  Integer

    For time = 86400 to 86400

      Listbox2.InsertRow(n, "")

      ListBox2.Cell(n,1)=Str(CDXYChartMBS.kNoValue)
      ListBox2.Cell(n,2)=Str(CDXYChartMBS.kNoValue)
      ListBox2.Cell(n,3)=Str(CDXYChartMBS.kNoValue)
      ListBox2.Cell(n,4)=Str(CDXYChartMBS.kNoValue)
      ListBox2.Cell(n,5)=Str(CDXYChartMBS.kNoValue)

    next

    Canvas2.Refresh

    count = count + 1

    TextField12.Text = str(count)

  Else

  end

//
//
//

  Re: X Axis sync with data issue
Posted by Peter Kwan on Apr-12-2012 23:09
Hi JP,

Now I read your code again. I see that in your code, all the data arrays are always created from scratch by copying the data from Listbox2. According to this logic, all update should be applied to the Listbox2 (as any update to the data arrays will be wiped out by the data from the Listbox2). According to this logic, Listbox2.DeleteAllRows will clear the line, as it also clears the data arrays.

However, I am thinking, it there are a lot of data points, appending values to a variable size array may become slow. Text manipulation can be slow as well. (I see that your Listbox2 only supports text, and you need to convert the data values into text to store it in Listbox2, and then convert it back to numbers to be copied the data arrays.) May be this takes some CPU cycles.

Personally, if I am using Visual Basic (I am not familiar with RealBasic, so I use Visual Basic as an example), I would append the data values directly to the data arrays (instead of to the Listbox2, then recreate the data arrays from scratch every time). The data arrays would need to be persistent across method calls similar to the contents of the Listbox2. In VB, it means they should not be "local variables", but should be "Form variables". In this ways, there is no need to recreate the arrays from scratch and append many values to them everytime a new chart is needed. You just need to append one value to them. Also, as the data are directly stored in the arrays, there is no need to convert them to text and then convert them back to numbers.

In the above case, when the time reaches 86400, you may clear the arrays (instead of or in addition to clearing the Listbox2).

If the Listbox2 is not actually used in your code (apart from as a data storage), you may consider to remove it. If you need the Listbox2 to display the data to the user, you may still append the data to Listbox2.

Regards
Peter Kwan

  Re: X Axis sync with data issue
Posted by JP on Apr-13-2012 00:14
Peter -- thanks.  I don't really need the listbox any more.  I was just using that when I started building the program to confirm that I was getting the data, making sure my formulas, etc were correct.  Now that I've added the graphing, I really have no use for the listbox.

I was playing around with the Array approach a little bit this morning, but while I was getting the data in my TextFields (confirming the arrays work), it was not showing on the graph for some reason.  I'll have to dig into this some more.

Thanks for you help.
JP

  Re: X Axis sync with data issue
Posted by JP on Apr-13-2012 02:58
Peter -- well this is strange.  I am using the below code on a much simpler application trying to use an array instead of a listbox.  It works with the listbox.  These are very similar applications and the graph routine is pretty much the same -- so don't get confused by some different names (e.g SkyT now instead of DewP, etc).

So the way the code is right now I get one data point on the graph.  I can use i=0 to 1 or i=0 to 9 and I get one graphed data point.  The funny thing is I do not use "i" in the "for next" loop.  If I take the ".append" outside the loop and not use the loop at all nothing will graph.

The array is:   dataArray(9) As String.

Other than trying to convert from listbox to an array this program worked will including the graphs, and clearing the graph at noon (vice midnight as on the other app)

Your eyes would be a great help.

Thanks
JP

  //
  // Paint to graph area
  //

  dim starttime As Integer
  dim endtime As String
  dim range As String
  dim hour As Integer
  dim minute As Integer

  dim times As Double

  dim AmbT(-1) As Double
  dim SkyT(-1) As Double
  dim SkyC(-1) As Double
  dim SkyL(-1) As Double
  dim SkyR(-1) As Double
  dim labels(-1) as Double

  dim label(-1) as string = array("12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM", "9:00 PM", "10:00 PM", "11:00 PM","12:00 AM", "1:00 AM", "2:00 AM", "3:00 AM", "4:00 AM", "5:00 AM", "6:00 AM", "7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00 PM") ', "0:00 PM")


  dim i,u as integer

  'u=Listbox2.ListCount-1
  'u=val(dataArray(9))

  for i=0 to 1   // I can use to 1 or greater and will get one data point on the graph but I
                    // am not using "i"


    labels.append val(dataArray(1))

    AmbT.Append val(dataArray(3))
    SkyT.Append val(dataArray(4))
    SkyC.Append val(dataArray(5))
    SkyL.Append val(dataArray(6))
    SkyR.Append val(dataArray(7))

    'labels.append val(dataArray(i))  // not using "i" as I can not figure out how to assign
                                                   //  it to the proper array element
    '
    'AmbT.Append val(dataArray(i))
    'SkyT.Append val(dataArray(i))
    'SkyC.Append val(dataArray(i))
    'SkyL.Append val(dataArray(i))
    'SkyR.Append val(dataArray(i))

  next

  'labels.append val(dataArray(1))   //  So if I take the "For next" loop at and use this set
                                                   //  nothing paints
  '
  'AmbT.Append val(dataArray(3))
  'SkyT.Append val(dataArray(4))
  'SkyC.Append val(dataArray(5))
  'SkyL.Append val(dataArray(6))
  'SkyR.Append val(dataArray(7))


  dim c as new CDXYChartMBS(Canvas2.width, Canvas2.Height)

  dim legendBox as CDLegendBoxMBS
  legendBox = c.addLegend(393, 20, false, "arial.ttf", 8)
  legendBox.setAlignment(CDXYChartMBS.kBottomCenter)
  legendBox.setBackground(CDXYChartMBS.kTransparent, CDXYChartMBS.kTransparent)

  'call c.setPlotArea 30,10, 736, 144
  call c.setPlotArea(30,20, Canvas2.width-70, Canvas2.height-50).setGridColor(&hcccccc, &hcccccc)

  dim layer As CDLayerMBS

  layer = c.addLineLayer(AmbT, &cFF0000, "Ambient Temp")
  layer.setLineWidth(2)
  layer.setXData(labels)

  layer = c.addLineLayer(SkyT, &c00FF00, "Sky Temp")
  layer.setLineWidth(2)
  layer.setXData(labels)

  layer = c.addLineLayer(SkyC, &c0000FF00, "Clarity")
  layer.setLineWidth(2)
  layer.setXData(labels)

  layer = c.addLineLayer(SkyL, &cFF800000, "Light")
  layer.setLineWidth(2)
  layer.setXData(labels)

  c.yAxis2.setColors(0, &h66aaee)', &c66CCFF00, -1, -1)

  c.yAxis2.setLinearScale(-5, 30)

  layer = c.addLineLayer(SkyR, &c66CCFF00, "Rain")'.setLineWidth(2)
  layer.setLineWidth(2)
  layer.setXData(labels)
  layer.setUseYAxis2

  layer.setXData(labels)

  ''c.xAxis.setLabelFormat "{value|hh:nn:ss}"
  c.xAxis.setDateScale 12*3600, 36*3600, label

  c.xAxis.setLabelStep 2

  Canvas2.Backdrop = c.makeChartPicture

  //
  //
  //

  Re: X Axis sync with data issue
Posted by JP on Apr-13-2012 23:40
Peter -- sorry for the repost, but I think I see what is going on converting to the array approach.  I'm going to repeat some of the last note to give you context and let you read only this note.

I am using the below code on a much simpler application trying to use an array instead of a listbox.  It works with the listbox.  These are very similar applications. This one reads sky conditions via a cloud sensor, the first app we discussed is getting weather data from my weather station. The graph routine is pretty much the same -- so don't get confused by some different names (e.g SkyT now instead of DewP, etc).

So the way the code is right now I get one data point on the graph.  The data point changes, but there is no line from data point to data point.  I can use i=0 to 1 or i=0 to 9 and I get one graphed data point.  The data points are changing.  So I know we are getting the data in, but just losing the line.

The funny thing is I do not use "i" in the "for next" loop.  If I take the ".append" outside the loop and not use the loop nothing will graph.

The array is:   dataArray(9) As String.

Other than trying to convert from listbox to an array this program worked will including the graphs, and clearing the graph at noon (vice midnight as on the other app)

Thanks for your help
JP

  //
  // Paint to graph area
  //

  dim AmbT(-1) As Double
  dim SkyT(-1) As Double
  dim SkyC(-1) As Double
  dim SkyL(-1) As Double
  dim SkyR(-1) As Double

  dim labels(-1) as Double


  dim label(-1) as string = array("12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM", "9:00 PM", "10:00 PM", "11:00 PM","12:00 AM", "1:00 AM", "2:00 AM", "3:00 AM", "4:00 AM", "5:00 AM", "6:00 AM", "7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00 PM") ', "0:00 PM")


  dim i,u as integer

  for i=0 to 1  // not sure what I am doing here, but the data points graph, just no line.

    labels.append val(dataArray(1))

    AmbT.Append val(dataArray(3))
    SkyT.Append val(dataArray(4))
    SkyC.Append val(dataArray(5))
    SkyL.Append val(dataArray(6))
    SkyR.Append val(dataArray(7))

  next

    'labels.append val(dataArray(1))  // if I use this outside of the "for next" nothing will graph.

    'AmbT.Append val(dataArray(3))
    'SkyT.Append val(dataArray(4))
    'SkyC.Append val(dataArray(5))
    'SkyL.Append val(dataArray(6))
    'SkyR.Append val(dataArray(7))

  dim c as new CDXYChartMBS(Canvas2.width, Canvas2.Height)

  dim legendBox as CDLegendBoxMBS
  legendBox = c.addLegend(393, 20, false, "arial.ttf", 8)
  legendBox.setAlignment(CDXYChartMBS.kBottomCenter)
  legendBox.setBackground(CDXYChartMBS.kTransparent, CDXYChartMBS.kTransparent)

  call c.setPlotArea(30,20, Canvas2.width-70, Canvas2.height-50).setGridColor(&hcccccc, &hcccccc)

  dim layer As CDLayerMBS

  layer = c.addLineLayer(AmbT, &cFF0000, "Ambient Temp")
  layer.setLineWidth(2)
  layer.setXData(labels)

  layer = c.addLineLayer(SkyT, &c00FF00, "Sky Temp")
  layer.setLineWidth(2)
  layer.setXData(labels)

  layer = c.addLineLayer(SkyC, &c0000FF00, "Clarity")
  layer.setLineWidth(2)
  layer.setXData(labels)

  layer = c.addLineLayer(SkyL, &cFF800000, "Light")
  layer.setLineWidth(2)
  layer.setXData(labels)

  c.yAxis2.setColors(0, &h66aaee)', &c66CCFF00, -1, -1)

  c.yAxis2.setLinearScale(-5, 30)

  layer = c.addLineLayer(SkyR, &c66CCFF00, "Rain")
  layer.setLineWidth(2)
  layer.setXData(labels)
  layer.setUseYAxis2

  layer.setXData(labels)

  ''c.xAxis.setLabelFormat "{value|hh:nn:ss}"
  c.xAxis.setDateScale 12*3600, 36*3600, label

  c.xAxis.setLabelStep 2

  Canvas2.Backdrop = c.makeChartPicture

  //
  //
  //

  Re: X Axis sync with data issue
Posted by Peter Kwan on Apr-13-2012 23:41
Hi JP,

I see that your code contains the lines:

  dim AmbT(-1) As Double
  dim SkyT(-1) As Double
  dim SkyC(-1) As Double
  dim SkyL(-1) As Double
  dim SkyR(-1) As Double
  dim labels(-1) as Double

The above lines clears the arrays, so the arrays become empty. Your code then re-populate the arrays with data from the listbox.

If you would like to directly append the data to the arrays, you should not want to clear the arrays. If you include the above code in the subroutine that appends data to the array, the array will be cleared first before you append the data. As a result, there is always only one data point in the array, because the previous points are already cleared. To plot a line, at least 2 points are needed. So you would not see anything in a line chart. (If you plot a scatter chart, you should see a single symbol.)

That means the above code should not exist in the subroutine that appends data to the array. I am not familiar with RealBasic. In Visual Basic, the above lines would be in the Form variable declaration section or declare as global variables. (The Form variable is executed once when the Form is loaded. It is never executed again unless the Form is closed and then reloaded.) The key is, the above code should only be executed once. It should not be executed when you add data to the array.

Hope this can help.

Regards
Peter Kwan

  Re: X Axis sync with data issue
Posted by JP on Apr-14-2012 00:22
Peter -- fantastic.  This did the trick.  Also -- no need for the "for next" -- just the straight append.

So -- just to help me understand.  I populate the dataArray with each read of the TCPSocket (it comes in via a comma delimitated text string).  So what am I doing with the "append" in the graph routine?

Also -- is there a reference somewhere for the standard colors for ChartDirector?

I really appreciate your timely help.  I could not have got to this point without your advice.

Thanks a bunch
JP

  Re: X Axis sync with data issue
Posted by Peter Kwan on Apr-14-2012 00:53
Hi JP,

In RealBasic, I suppose the Append method appends a value to the end of the array. As the chart is a visualization of the data arrays, if you append a value to the end of the array, it will be visualized as appending a point to the end of the line.

The coloring system in ChartDirector is the same as in HTML, except that ChartDirector also supports semi-transparent colors. If you search for HTML color using Google, you can see a lot of resources on HTML colors.

An HTML color is like ABCDEF, where AB, CD and EF are the hex representation of the red, green and blue channels. Different programming languages may require different syntax for hex numbers. In VB, we use "&H" (like &HABCDEF). In C#,C++ and Java, the "0x" syntax is used (like 0xABCDEF). From your code, I suppose in RealBasic, the "&c" syntax is used (like &cABCDEF).

So if you need to choose a color, you may go to a web site that has an HTML color picker or palette, and look for the HTML code for the color, and change it to RealBasic syntax (by append &c in front).

Hope this can help.

Regards
Peter Kwan

  Re: X Axis sync with data issue
Posted by JP on Apr-14-2012 01:58
Thanks Peter -- great explanation.  Now all I have to do is figure out a good way to clear the full array at midnight, not just the elements I am using for the graph.

I have both applications graphing very well at the moment using only arrays :)

Really appreciate the help.

JP

  Re: X Axis sync with data issue
Posted by JP on Apr-14-2012 12:55
Well Peter ... drat ...

At midnight while I have no lines on the graph from the last data point at the right to the new data point on the left -- the original lines stay on the graph.  The good news is I am using a less memory and the cpu usage is down.  Anyway -- the code is below.

Here is the graph routine:


  //
  // Paint to graph area
  //

  dim label(-1) as string = array("12:00 AM", "1:00 AM", "2:00 AM", "3:00 AM", "4:00 AM", "5:00 AM", "6:00 AM", "7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM", "9:00 PM", "10:00 PM", "11:00 PM", "12:00 AM")

  labels.Append val(dataArray(1))
  AmbT.Append val(dataArray(2))
  DewP.Append val(dataArray(3))
  Hum.Append val(dataArray(4))
  Baro.Append val(dataArray(5))


  dim c as new CDXYChartMBS(Canvas2.width, Canvas2.Height)

  dim legendBox as CDLegendBoxMBS
  legendBox = c.addLegend(393, 20, false, "arial.ttf", 8)
  legendBox.setAlignment(CDXYChartMBS.kBottomCenter)
  legendBox.setBackground(CDXYChartMBS.kTransparent, CDXYChartMBS.kTransparent)

  call c.setPlotArea(30,20, Canvas2.width-70, Canvas2.height-50).setGridColor(&hcccccc, &hcccccc)

  dim layer As CDLayerMBS

  layer = c.addLineLayer(AmbT, &cFF000000, "Temperature")
  layer.setLineWidth(2)
  layer.setXData(labels)

  layer = c.addLineLayer(DewP, &c00FF0000, "Dew Point")
  layer.setLineWidth(2)
  layer.setXData(labels)

  layer = c.addLineLayer(Hum, &c0000FF00, "Humidity")
  layer.setLineWidth(2)
  layer.setXData(labels)

  c.yAxis2.setColors(0, &h66aaee)
  c.yAxis2.setLinearScale(29, 31)

  layer = c.addLineLayer(Baro, &c66CCFF00, "Pressure")
  layer.setLineWidth(2)
  layer.setXData(labels)
  layer.setUseYAxis2

  'c.xAxis.setLabelFormat "{value|hh:nn:ss}"
  c.xAxis.setDateScale 0, 86400, label

  c.xAxis.setLabelStep 2

  Canvas2.Backdrop = c.makeChartPicture

  //
  //
  //


Here is how I am clearing the graph:

  //
  //  Clear at midnight
  //

  Static count As Integer

  If date.LongTime = "12:00:00 AM"  then 'or date.LongTime = "12:00:01 PM" or date.LongTime = "12:00:02 PM"

    Listbox2.DeleteAllRows

     // Clean ArrayInfo

    ReDim dataArray(-1)
    ReDim dataArray(100)

    time = 0

    dim n As  Integer

    For time = 0 to 1

      dataArray(1)=Str(CDXYChartMBS.kNoValue)
      dataArray(2)=Str(CDXYChartMBS.kNoValue)
      dataArray(3)=Str(CDXYChartMBS.kNoValue)
      dataArray(4)=Str(CDXYChartMBS.kNoValue)
      dataArray(5)=Str(CDXYChartMBS.kNoValue)

    next

    Canvas2.Refresh

    count = count + 1

    TextField12.Text = str(count)

  Else

  end

  //
  //
  //

  Re: X Axis sync with data issue
Posted by JP on Apr-15-2012 13:55
Peter -- also it looks like I am getting lines from the last data point on the right going all the way over to the first data point on the left.

Hmmm -- this worked when I was using the listbox approach which deleted the entire listbox, then put the kNoValue in the first cells of the listbox.

Something must not be working with the ReDim of my array??

JP

  Re: X Axis sync with data issue
Posted by JP on Apr-16-2012 10:55
Peter -- I figured out the reason I am getting the lines across the graph.  I had a code error:

If date.LongTime = "12:00:00 AM"  then 'or date.LongTime = "12:00:01 PM" or date.LongTime = "12:00:02 PM"

Should be:

If date.LongTime = "12:00:00 AM"  then 'or date.LongTime = "12:00:01 AM" or date.LongTime = "12:00:02 AM"

I also had to add:

    dim AmbT(-1) As Double
    dim DewP(-1) As Double
    dim Hum(-1) As Double
    dim Baro(-1) As Double

    dim labels(-1) as Double

in front of:

    ReDim dataArray(-1)
    ReDim dataArray(100)

for the midnight routine.   Not sure why, but it works.  So here is the midnight routine:

  //
  //  Clear at midnight
  //

  Static count As Integer

  If date.LongTime = "12:00:00 AM" or date.LongTime = "12:00:01 AM" or date.LongTime = "12:00:02 AM" then

    'Listbox2.DeleteAllRows

     // Clean ArrayInfo

    dim AmbT(-1) As Double
    dim DewP(-1) As Double
    dim Hum(-1) As Double
    dim Baro(-1) As Double
    'dim UV(-1) As Double
    'dim Solar(-1) As Double
    dim labels(-1) as Double

    ReDim dataArray(-1)
    ReDim dataArray(100)

    time = 0

    For time = 86400 to 86401

      dataArray(1)=Str(CDXYChartMBS.kNoValue)
      dataArray(2)=Str(CDXYChartMBS.kNoValue)
      dataArray(3)=Str(CDXYChartMBS.kNoValue)
      dataArray(4)=Str(CDXYChartMBS.kNoValue)
      dataArray(5)=Str(CDXYChartMBS.kNoValue)

    next

    Canvas2.Refresh

    count = count + 1

    TextField12.Text = str(count)

  Else
    'If date.LongTime = "12:00:00 PM"  then 'or date.LongTime = "3:00:01 AM" or date.LongTime = "3:00:02 AM"
    '
    'Listbox2.DeleteAllRows
    '
    ''CleanGraphicsBeforeDrawing
    '
    'count = count + 1
    '
    'TextField12.Text = str(count)
    '
    '
    'Else
    'end
  end

  //
  //
  //

  Re: X Axis sync with data issue
Posted by JP on Apr-16-2012 10:59
Peter -- so you do not have to dig through all these notes, here is repeat of my latest issue:

At midnight while I have no lines on the graph from the last data point at the right to the new data point on the left (see note in front of this one) -- the original lines stay on the graph.

Here is the graph routine:


  //
  // Paint to graph area
  //

  dim label(-1) as string = array("12:00 AM", "1:00 AM", "2:00 AM", "3:00 AM", "4:00 AM", "5:00 AM", "6:00 AM", "7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM", "9:00 PM", "10:00 PM", "11:00 PM", "12:00 AM")

  labels.Append val(dataArray(1))
  AmbT.Append val(dataArray(2))
  DewP.Append val(dataArray(3))
  Hum.Append val(dataArray(4))
  Baro.Append val(dataArray(5))


  dim c as new CDXYChartMBS(Canvas2.width, Canvas2.Height)

  dim legendBox as CDLegendBoxMBS
  legendBox = c.addLegend(393, 20, false, "arial.ttf", 8)
  legendBox.setAlignment(CDXYChartMBS.kBottomCenter)
  legendBox.setBackground(CDXYChartMBS.kTransparent, CDXYChartMBS.kTransparent)

  call c.setPlotArea(30,20, Canvas2.width-70, Canvas2.height-50).setGridColor(&hcccccc, &hcccccc)

  dim layer As CDLayerMBS

  layer = c.addLineLayer(AmbT, &cFF000000, "Temperature")
  layer.setLineWidth(2)
  layer.setXData(labels)

  layer = c.addLineLayer(DewP, &c00FF0000, "Dew Point")
  layer.setLineWidth(2)
  layer.setXData(labels)

  layer = c.addLineLayer(Hum, &c0000FF00, "Humidity")
  layer.setLineWidth(2)
  layer.setXData(labels)

  c.yAxis2.setColors(0, &h66aaee)
  c.yAxis2.setLinearScale(29, 31)

  layer = c.addLineLayer(Baro, &c66CCFF00, "Pressure")
  layer.setLineWidth(2)
  layer.setXData(labels)
  layer.setUseYAxis2

  'c.xAxis.setLabelFormat "{value|hh:nn:ss}"
  c.xAxis.setDateScale 0, 86400, label

  c.xAxis.setLabelStep 2

  Canvas2.Backdrop = c.makeChartPicture

  //
  //
  //


Appreciate any help you can provide.

Thanks
JP

  Re: X Axis sync with data issue
Posted by Peter Kwan on Apr-17-2012 00:10
Hi JP,

Again, I am not familiar with RealBasic. By searching the Internet, I think in RealBasic, ReDim can be used to clear an array in RealBasic. I suppose the code should be like:


If ..... data pass midnight .... Then

  Redim AmbT(-1)
  Redim SkyT(-1)
  Redim SkyC(-1)
  Redim SkyL(-1)
  Redim SkyR(-1)
  Redim labels(-1)

End If


Regards
Peter Kwan

  Re: X Axis sync with data issue
Posted by JP on Apr-17-2012 02:47
Peter -- thanks.

I didn't think to redim the individual graph
elements. I was doing the entire array. Will try
this tonight.

Thanks
JP

  Re: X Axis sync with data issue
Posted by JP on Apr-17-2012 09:25
Peter -- it worked ... Yeee Haaa!!!  Thank you very much.

I'll start a new thread with you in a few days after I dig into the realtime example that comes with ChartDirector.  I want to turn one of these programs into a continuous scroll graph complete with the label evenly spaced and scrolling with the data.

Thanks again -- you have been extremely helpful.
JP