|
Dynamic data and conditional formating |
Posted by Jason on Jun-30-2012 03:08 |
|
I am migrating some old TeeCharts to our Chartdirector and the way TeeChart functioned is that you built each data point through a loop, giving attributes to the X and Y. Using a database (MS SQL)
I am able to sort/order/filter the data and then loop through the records. Because of this I am also able to write conditions for example I have a records with a value and date I could write and if then
that would see how old the previous record was to the next and if it was more than 30 days it would be considered a data gap (Missing data) and then I could create a transparent line connecting the
point to show the missing data. OR in some cases I have a date, but no data so I would draw a transparent line etc.. I have some more conditional formatting based on the data for that record. like
change the point size if a value was over a set amount.
The problem is that it seems that the only way data points get entered into ChartDirector is by Arrays, which make it *impossible* to do any of these conditional formatting.
The missing data point is a good but that only work when both arrays (Data and Label are predefined in the array as a cd.NoValue I can't seem to see a way to be able to migrate these conditional formatted charts over unless there is some way to enter each point condition through a loop.
I have included my old TeeChart code of one of these charts so you can see what I am talking about
Thanks in Advance!
Jason
OLD TeeChart Code:
rst.movefirst
dim PreviousDate, PreviousLevel ' These variables hold the last date and observed Level
while isnull(rst("min")) or rst("min")=0 ' This loop finds the first valid date and Level at the query result
PreviousDate=rst("date")
PreviousLevel=rst("min")
rst.movenext
wend
' GRAPH STARTS HERE
while not rst.eof
if isnull(rst("min")) or rst("min")=0 then 'there is a date but not a valid rst("min")
chart.series(0).addxy rst("date"),PreviousLevel,"",RGB(255,255,255) ' draw it white
else
if DateDiff("d",PreviousDate,rst("date")) > 45 then ' date difference between sequential data is more than 1 there must be a gap
chart.series(0).addxy (rst("date")-0.25),rst("min"),"",RGB(255,255,255) 'if there is a gap between dates, draw white line
chart.series(0).addxy (rst("date")+0.25),rst("min"),"",RGB(34,150,123) 'attached to the white line draw a line for half a day
else ' if dates come one after another draw the line
chart.series(0).addxy rst("date"),rst("min"),"",RGB(34,150,123) '
end if
PreviousLevel=rst("min") ' getting last observed Level
showLow = rst("min")
end if
PreviousDate=rst("date") ' getting last date
rst.movenext |
Re: Dynamic data and conditional formating |
Posted by Peter Kwan on Jul-03-2012 00:57 |
|
Hi Jason,
You can use exact the same method to build your data. Instead of adding the data to the TeeChart object, you just need to add the data to the array. You can add them conditionally if you like.
For example, based on your original TeeChart code, it would be like:
'Use arrays to store your data
ReDim myXData(1000)
ReDim myYData(1000)
Dim size
size = 0
' GRAPH STARTS HERE
while not rst.eof
'Resize arrays if necessary
If size > UBound(myXData) + 10 Then
ReDim myXData(UBound(myXData) * 2)
ReDim myYData(UBound(myYData) * 2)
End If
'Initialize to NoValue
myXData(size) = rst("date")
myYData(size) = cd.NoValue
'Overwrite the NoValue point if there is valid data
if not (isnull(rst("min")) or rst("min")=0) then
if size > 0 then
if DateDiff("d", myXData(size - 1), myXData(size)) > 45 then
'there must be a gap, so keep the NoValue point and add a new point
size = size + 1
myXData(size) = myXData(size - 1)
end if
end if
myYData(size) = rst("min")
end if
size = size + 1
rst.movenext
wend
'Trim the array to the actual size and add the line layer if size > 0
If size > 0 Then
ReDim myXData(size - 1)
ReDim myYData(size - 1)
End If
The advantages of using arrays are that the data manipulation code can be separated from the charting code (eg. in the above code, there is no need to use ChartDirector). Also, using arrays are more flexible than adding data to the TeeChart object. For example, you can easily overwrite a data value. In the above code, I initialize the data point to NoValue, and overwrite it later if there is a valid value. You can also reference earlier points easily. In the above code, PreviousDate is not necessary, as we can obtain it as myXData(size - 1).
Hope this can help.
Regards
Peter Kwan |
|