List Info

Thread: Use colours to represent state of data




Use colours to represent state of data
user name
2006-07-26 11:12:32

Hello all

I want to query a dataset and then represent the nature or state of that data by one of three different colours (essentially red if a field contains data in all rows, amber if a field contains data in some rows but not others, and green if a field contains data in none of the rows).

The following code shows what I did in a rough and ready solution using the OnDrawCell of a TStringGrid, which is not a workable solution since the process starts from scratch each time the Grid is repainted (so, for example, if you return to the Grid's form after focusing on another application).
There will be, of course, many ways of achieving the objective, but what might be the most efficient (using any components - Grid does not have to feature) ?

Thanks for any suggestions

Regards
Steve
----------------------------------------------------------

procedure TOverviewForm.SGDrawCell(Sender: TObject; ACol, ARow: Integer;Rect: TRect; State: TGridDrawState);
var i, j, count: Byte; colour: TColor; theDate: TDateTime;
begin
for j:= 1 to max do
begin
theDate:= StrToDateTime(inttostr(j) + '/' + inttostr(mnth) + '/' + yr);
getData(theDate); // Gets data for a single day
for i:= 2 to Q_Overview.FieldCount-1 do
begin
Q_Overview.First;
count:=0;
while not Q_Overview.Eof do // Iterates over each column in turn and counts the cells containing data
begin
if Q_Overview.Fields[i].AsString > '' then
inc(count);
Q_Overview.Next;
end;
if count > 0 then // Sets variable colour according to value of variable count
begin
if count = 27 then
colour:= $001111FF
else
colour:= $001BBFF
end
else
colour:= $0044DE44;
if (ACol = i-1) and (ARow = j) then
begin
SG.Canvas.Brush.Color := colour;
SG.Canvas.FillRect(Rect);
end;
end;
end;
end;

procedure TOverviewForm.Button1Click(Sender: TObject);
var i: Byte;
begin
//starttime:= gettickcount;
mnth:= cb_Month.ItemIndex3;1;
yr:= cb_Year.Text;
case mnth of
1,3,5,7,8,10,12: max:= 31;
4,6,9,11: max:= 30;
2: if isLeapYear(strtoint(yr)) then max:= 29 else max:= 28;
end;

for i:= 1 to max do
SG.Cells[0,i]:= FormatDateTime('ddd d',StrToDateTime(inttostr(i) + '/' + inttostr(mnth) + '/' + yr));
SG.Repaint; // have grid repaint itself
{endtime:= gettickcount;
secondstaken:= (endtime-starttime)/1000;
showmessage('Time taken = ' + floattostr(secondstaken) + ' secs'); } // Takes about 45 secs from start to finish with 14 data fields and 28 - 31 rows
end;

***************************************************************************
This e-mail and any files transmitted with it are confidential. If you are not the intended recipient, any reading, printing, storage, disclosure, copying or any other action taken in respect of this e-mail is prohibited and may be unlawful. If you are not the intended recipient, please notify the sender immediately by using the reply function and then permanently delete what you have received.
Content of emails received by this Trust will be subject to disclosure under the Freedom of Information Act 2000, subject to the specified exemptions, including the Data Protection Act 1998 and Caldicott Guardian principles.
This footnote also confirms that this email message has been swept by MIMESweeper and Sophos Anti-virus for the presence of computer viruses.
***************************************************************************

__._,_.___
.

__,_._,___
Use colours to represent state of data
user name
2006-07-26 14:20:32

If I understood what do you want I prefer another approach.
In the button1 click event I would fetch the data and I would fill not the cells but the objects of the cells
with the a flag about the color I want, like

SG.Objects[col,row]:= TObject(n)
where n could be an integer that denotes the index in a collor array e.g. MyArrayOfColors[1..3] of TColor

Then in the
procedure TOverviewForm.SGDrawCell(Sender: TObject; ACol, ARow:Integer;Rect: TRect; State: TGridDrawState);

i would have
idx := Integer(SG.Objects[Acol, Arow);
if idx>0 then
begin
SG.Canvas.Brush.Color := MyArrayOfColors[Integer(SG.Objects[Acol, Arow)]
SG.Canvas.FillRect(Rect);
end;
==========
So there is not need to fetch all the time the data
The code is by mind so just catch the idea
Sorry for my english
Antonis Tsourinakis

*********** REPLY SEPARATOR ***********

On 26/07/2006 at 12:12 Wilson, Stephen wrote:

>Hello all
>
>I want to query a dataset and then represent the nature or state of that
>data by one of three different colours (essentially red if a field
>;contains data in all rows, amber if a field contains data in some rows but
>not others, and green if a field contains data in none of the rows).
>
>The following code shows what I did in a rough and ready solution using
>;the OnDrawCell of a TStringGrid, which is not a workable solution since
>;the process starts from scratch each time the Grid is repainted (so, for
>example, if you return to the Grid's form after focusing on another
>application).
>; There will be, of course, many ways of achieving the objective, but what
>might be the most efficient (using any components - Grid does not have to
>feature) ?
>
>Thanks for any suggestions
>
>Regards
>Steve
>
>----------------------------------------------------------
>
>procedure TOverviewForm.SGDrawCell(Sender: TObject; ACol, ARow:
>;Integer;Rect: TRect; State: TGridDrawState);
>var i, j, count: Byte; colour: TColor; theDate: TDateTime;
>begin
>for j:= 1 to max do
> begin
>; theDate:= StrToDateTime(inttostr(j) + '/' + inttostr(mnth) + '/' + yr);
> getData(theDate); // Gets data for a single day
> for i:= 2 to Q_Overview.FieldCount-1 do
> begin
>; Q_Overview.First;
> count:=0;
> while not Q_Overview.Eof do // Iterates over each column in turn
>and counts the cells containing data
> begin
>; if Q_Overview.Fields[i].AsString > '' then
> inc(count);
> Q_Overview.Next;
>; end;
> if count > 0 then // Sets variable colour according
>to value of variable count
>; begin
>; if count = 27 then
> colour:= $001111FF
> else
> colour:= $001BBFF
> end
> else
> colour:= $0044DE44;
> if (ACol = i-1) and (ARow = j) then
> begin
>; SG.Canvas.Brush.Color := colour;
> SG.Canvas.FillRect(Rect);
> end;
> end;
> end;
>end;
>
>
>
>procedure TOverviewForm.Button1Click(Sender: TObject);
>var i: Byte;
>;begin
>;//starttime:= gettickcount;
>mnth:= cb_Month.ItemIndex3;1;
>yr:= cb_Year.Text;
> case mnth of
> 1,3,5,7,8,10,12: max:= 31;
> 4,6,9,11: max:= 30;
> 2: if isLeapYear(strtoint(yr)) then max:= 29 else max:= 28;
> end;
>
>for i:= 1 to max do
> SG.Cells[0,i]:= FormatDateTime('ddd d',StrToDateTime(inttostr(i) + '/' +
>inttostr(mnth) + '/' + yr));
>;SG.Repaint; // have grid repaint itself
>{endtime:= gettickcount;
>secondstaken:= (endtime-starttime)/1000;
>showmessage('Time taken = ' + floattostr(secondstaken) + ' secs'); } //
>Takes about 45 secs from start to finish with 14 data fields and 28 - 31
>rows
>end;
>
>;***************************************************************************
>This e-mail and any files transmitted with it are confidential. If you are
>not the intended recipient, any reading, printing, storage, disclosure,
>copying or any other action taken in respect of this e-mail is prohibited
>and may be unlawful. If you are not the intended recipient, please notify
>the sender immediately by using the reply function and then permanently
>delete what you have received.
>Content of emails received by this Trust will be subject to disclosure
>under the Freedom of Information Act 2000, subject to the specified
>exemptions, including the Data Protection Act 1998 and Caldicott Guardian
>principles.
>This footnote also confirms that this email message has been swept by
>MIMESweeper and Sophos Anti-virus for the presence of computer viruses.
>***************************************************************************
>
>
>
>;
>-----------------------------------------------------
>Home page: http://groups.yahoo.com/group/delphi-en/
>To unsubscribe: delphi-en-unsubscribeyahoogroups.com
>Yahoo! Groups Links
>;
>
>

__._,_.___
.

__,_._,___
Use colours to represent state of data
user name
2006-07-26 17:27:01

Your OnDraw event is flawed. First of all, do NOT get the data from within the onDraw event! Your only putting code in to handle custom drawing of the control.

You did say "field" so I assume you want to color the columns?. Get rid of the for loops since it already loops through each cell. It starts at the top left and ends up at the bottom right. Just have it check the column property as it goes and color the cell based on that. You could put the color for that column in the column tag property. The code to set the color for each column is put in the appropriate place outside of this event (button click or whatever). You'd have to give each column tag a default property in order for it not to be black with ($000000) until your code executes.
(Tip: defined your cutom colors as a constant at top of form):

constants
clAmber: TColor($0011FF); <-- only 6 digits for a color!!!

procedure TOverviewForm.SGDrawCell(Sender: TObject; ACol, ARow: Integer;Rect: TRect; State: TGridDrawState);
begin
Case ACol Of
0: color := SG.Column[0].Tag;
1: color := SG.Columns[1].Tag;
etc.
end;



"Wilson, Stephen&quot; <stephen.wilsonipswichhospital.nhs.uk> wrote:
New Message Search
Find the message you want faster. Visit your group to try out the improved message search.


Share feedback on the new changes to Groups

Recent Activity

9
New Members

Visit Your Group
SPONSORED LINKS

C programming language
Computer programming languages
Java programming language
The c programming language
C programming language

.


---------------------------------
Groups are talking. We&acute;re listening. Check out the handy changes to Yahoo! Groups.

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )