(Conditional) font color in specific column of Datagrid

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

stam
Posts: 3061
Joined: Sun Jun 04, 2006 9:39 pm

Re: (Conditional) font color in specific column of Datagrid

Post by stam » Wed Aug 24, 2022 11:43 pm

I'm sure i downloaded a stack with modfiedTableField some time ago and was very impressed by what could be done with the tablefield... but ultimately it was a lot of hard work and not practical for me - but that's my mileage.

Keeping in mind the new user: tableField is only simple for experienced users because they are experienced users.
The DG seems scary but is good and holding your hand and guiding you as almost all functionality you may be looking for has been implemented.

The OP had asked how to conditionally colour a cell in a data grid (not the text). Then people posted that we should be considering the tableField "first". For me this doesn't seem like the right tool for the task at hand.

For my own learning i asked if conditional cell (not text) colouring can be done easily for a tableField column, and was answered with vague positive responses of ease and simplicity, which seem dubious to me.

i still hope someone would be kind enough post an example of a tableField where a column will automatically do conditional cell colouring... for comparison if nothing else.

Stam

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: (Conditional) font color in specific column of Datagrid

Post by FourthWorld » Thu Aug 25, 2022 1:29 am

stam wrote:
Wed Aug 24, 2022 11:43 pm
I'm sure i downloaded a stack with modfiedTableField some time ago and was very impressed by what could be done with the tablefield... but ultimately it was a lot of hard work and not practical for me - but that's my mileage.

Keeping in mind the new user: tableField is only simple for experienced users because they are experienced users.
The DG seems scary but is good and holding your hand and guiding you as almost all functionality you may be looking for has been implemented.
I suppose one can download all sorts of things, but I'm referring to LC's native field object: Click a few property settings in the Inspector, drop tab-delimited text into it, enjoy.

Whether the OP needs anything beyond that remains unknown. May we please let the OP tell us what's needed?
The OP had asked how to conditionally colour a cell in a data grid (not the text).
What is the title of this thread, and where is the post from the OP contradicting it?

It wouldn't be the first time I let the most prominent detail override discovering some detailed buried in subsequent posts. In my reading the goal was to color text in ways commonly done in accounting packages and other business tools.

Unless I've missed something already written by redfield, I wonder if we might allow redfield to clarify.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Re: (Conditional) font color in specific column of Datagrid

Post by redfield » Mon Aug 29, 2022 10:47 am

Hello everybody,

I have not yet had the time to go deeper into this, only tried a little here and there. Thank you @stam for the example, very impressive but I get it hard to understand those handlers (e. g. LayoutControl). I did copy the code to my Stack but I get an error because the object graphic "colourRect" is not found. And I don't know what the heck those "rects" are for. Does a "rect" represent a table cell :oops: ?

I was indeed thinking of using the "simple" table field, but in terms of formatting it doesn't seem much simpler to me.

Then I thought maybe I should create text fields programmatically and build something like a table with them, but the whole thing would not be scrollable, I guess.

The aim is to list stocks and their returns and color the negative returns red and the positive ones green. Either the text or the whole cell - why not if the latter is easier to do. Here's a screenshot:
Screenshot_2022-08-29.png

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: (Conditional) font color in specific column of Datagrid

Post by FourthWorld » Mon Aug 29, 2022 4:16 pm

There are several ways to do that, some more readable and others faster.

Is that sample representative of the number of lines typically displayed?

What is the he maximum number of lines you can expect?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: (Conditional) font color in specific column of Datagrid

Post by jacque » Mon Aug 29, 2022 5:17 pm

A regular LC field with tabstops could do that easily, including colored text.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: (Conditional) font color in specific column of Datagrid

Post by FourthWorld » Mon Aug 29, 2022 6:07 pm

jacque wrote:
Mon Aug 29, 2022 5:17 pm
A regular LC field with tabstops could do that easily, including colored text.
Excellent idea. :)

Now I'm looking forward to hearing back from his to see if it's worth explaining htmlText (which I'd do for long lists) or just setting the text color in place (which would be fast enough if it's just a few lines as shown).
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

stam
Posts: 3061
Joined: Sun Jun 04, 2006 9:39 pm

Re: (Conditional) font color in specific column of Datagrid

Post by stam » Mon Aug 29, 2022 6:25 pm

redfield wrote:
Mon Aug 29, 2022 10:47 am
Hello everybody,

I have not yet had the time to go deeper into this, only tried a little here and there. Thank you @stam for the example, very impressive but I get it hard to understand those handlers (e. g. LayoutControl). I did copy the code to my Stack but I get an error because the object graphic "colourRect" is not found. And I don't know what the heck those "rects" are for. Does a "rect" represent a table cell :oops: ?
Hey @redfield - sorry I should have clarified - the “colourRect” is a rectangular graphic (from the tools palette). I just dropped a rectangular graphic into the column template and named it “colourRect” and made sure the text field was in front of it (to get to this, edit the group of the template that contains all the column templates then select the desired column and edit that group as well). This is resized in LayoutControl and coloured in fillInData.

LayoutControl gives you the size of a cell in left/top/right/bottom coordinates (the “rect” of the cell), so all that handler does is set the size of the coloured graphic to the size (“rect”) of the cell, and does this for all cells in the column as this is the template for the column. Any resizing of the cell (eg dynamic layouts or even if you just change your mind about dimensions) will trigger this and ensure the colour always fills the cell.

The fillInData is hopefully easier to grasp - if you don’t add code it just adds the the field of the tap-separated text. As this is passed to the handler of this column for each row, it’s an easy place to enter a formula that applies formatting based on the value. In this case a simple conditional if/then/else block to colour the graphic above.

As others have said there are many ways to do this.

“Simpler” ways probably mean (possibly a lot) more code, but whatever suits your needs and appeals to you is the the best way :)

S.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: (Conditional) font color in specific column of Datagrid

Post by FourthWorld » Mon Aug 29, 2022 6:54 pm

FourthWorld wrote:
Mon Aug 29, 2022 4:16 pm
There are several ways to do that, some more readable and others faster.

Is that sample representative of the number of lines typically displayed?

What is the he maximum number of lines you can expect?
One more question: Do you obtain the data in tab-delimited format? If not, what format is the data in before being displayed?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Re: (Conditional) font color in specific column of Datagrid

Post by redfield » Mon Aug 29, 2022 7:42 pm

FourthWorld wrote:
Mon Aug 29, 2022 4:16 pm
What is the he maximum number of lines you can expect?
Theoretically open end, but probably up to 30 the most.

jacque wrote:
Mon Aug 29, 2022 5:17 pm
A regular LC field with tabstops could do that easily, including colored text.

:shock: okay. And are the lines then clickable? With the Datagrid I open a Substack when clicking on a line.

FourthWorld wrote:
Mon Aug 29, 2022 6:54 pm
One more question: Do you obtain the data in tab-delimited format? If not, what format is the data in before being displayed?
The data comes from a database and I do a

Code: Select all

set linedelimiter to "#"

Thanks @stam for the explanation, I will look into it again.

stam
Posts: 3061
Joined: Sun Jun 04, 2006 9:39 pm

Re: (Conditional) font color in specific column of Datagrid

Post by stam » Mon Aug 29, 2022 8:28 pm

redfield wrote:
Mon Aug 29, 2022 7:42 pm
:shock: okay. And are the lines then clickable? With the Datagrid I open a Substack when clicking on a line.
No reason you can't with a tableField - LC incudes many functions to work with text, the clickLine and the clickText (check them out in the dictionary). But as mentioned you then need to code the expected behaviours such as highlighting etc.

redfield wrote:
Mon Aug 29, 2022 7:42 pm
The data comes from a database and I do a

Code: Select all

set linedelimiter to "#"
Probably only if you receive your data as #-delimited text and in that case you'd probably just want to substitute # with a tab, so not sure there is a reason to set the itemDelimiter to # -- but clearly this depends on your data and what you want to do.

Most controls expect tab-delimited text in LC (or other than the tableField you can usually use an array as well). In many cases TSV text is fine.

S.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: (Conditional) font color in specific column of Datagrid

Post by jacque » Mon Aug 29, 2022 9:03 pm

Yes, the lines can be clickable.

Create a standard LC field with these properties:

Locktext: true
Listbehavior: true
Autohilite: true
Vgrid: true
tabstops: (whatever works, start with 120, you can adjust later)

Get the database data and make it tab-delimited with normal CR line endings.

If you only have a few lines then manipulating the field text itself it a bit easier than managing the htmlText, though htmlText is much faster. For the first method, put the tab-delimited text into the field and then run this handler:

Code: Select all

on parseFld pData
  put 1 into tCount
  set the itemDel to tab
  lock screen
  repeat for each line L in pData
    if last item of l begins with "-" then
      set the textcolor of last item of line tCount of fld 1 to "red"
    else
      set the textcolor of last item of line tCount of fld 1 to "green"
    end if
    add 1 to tCount
  end repeat
  unlock screen
end parseFld
Call the handler by passing the text of the field:

Code: Select all

parseFld the text of fld 1
When the user clicks a line, a mouseUp handler in the field should get the clicktext, which will be the text of the whole line. Then you can do something with it.

If you want to try setting the htmlText instead, let us know.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply