(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

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

(Conditional) font color in specific column of Datagrid

Post by redfield » Mon Aug 22, 2022 5:12 pm

Hi everybody,

I have started playing with a Datagrid and am wondering if it is possible to change the font color in one specific column - at all - or even based on a condition? E. g. positive cell values = green font, negative cell values = red font

Thanks
redfield

stam
Posts: 2776
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

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

Post by stam » Mon Aug 22, 2022 7:06 pm

redfield wrote:
Mon Aug 22, 2022 5:12 pm
Hi everybody,

I have started playing with a Datagrid and am wondering if it is possible to change the font color in one specific column - at all - or even based on a condition? E. g. positive cell values = green font, negative cell values = red font

Thanks
redfield
Not sure you can control the colour of individual cells directly, but you can edit the cell template, add a rect and colour that based on a condition.
You would probably want to resize the rect to fill the 'cell' in the layoutControl handler of the script for the cell and set the colour in the fillInData handler.

Not sure how familiar you are with the data grid - apologies if you're already experienced with using this. If not - you would need to add a column behaviour for the column in question in the 'column' tab of the properties inspector and edit that code. Once you've done that it will also bring up the column template (where you can add the rect) but you'd need to ungroup several times to get the the level where it needs to be. You can re-edit the column template from the 'data grid' tab of the properties inspector.

A whole lot more info can be found here: https://lessons.livecode.com/m/datagrid

HTH
Stam

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 22, 2022 7:36 pm

I'm not familiar at all with Datagrids, this is my first try. So it seems like what I'm aiming at regarding colors is not easily or quickly done, at least for a beginner. Thank you anyway, maybe I'll bring this up again some other time, but for now I give up on this :) .

stam
Posts: 2776
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

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

Post by stam » Mon Aug 22, 2022 7:45 pm

There is a learning curve for the data grid, but once you “get” it, it’s very powerful and very quick to do stuff.
It’s definitely worth spending the time getting familiar with it, but you’re right, you can’t really just pick it up instantly.
What I described might take an experienced user a minute or two to set up but no longer.

IMHO it’s worth spending some time learning the data grid and the lessons linked above do cover lots of stuff.

Of course if you invested in the summer megabundle there are a couple of other alternatives to this (polyList and polyGrid) although documentation on these is as yet a bit sparse. These have a simpler API but also not as many features as the data grid.

But depending on your needs the simpler table field may suffice and that may be worth exploring as well.

No doubt, if you working a table of some kind and get stuck, posting a question in the forum will help you get unstuck fairly quickly, so don’t hesitate to ask…

stam
Posts: 2776
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

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

Post by stam » Mon Aug 22, 2022 8:06 pm

As a quick proof of concept i just threw this together in a minute or so.
Doesn't look pretty but it shows how you can do this. Have a look at the attached stack... if you change the values in the 'contents' tab of the data grid (tab separated text) the color background should change automatically to reflect a value of > 30 or not.
conditional.jpg
Attachments
dg colour test.livecode.zip
(7.69 KiB) Downloaded 87 times

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9867
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

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

Post by FourthWorld » Tue Aug 23, 2022 12:35 am

redfield wrote:
Mon Aug 22, 2022 7:36 pm
I'm not familiar at all with Datagrids, this is my first try. So it seems like what I'm aiming at regarding colors is not easily or quickly done, at least for a beginner. Thank you anyway, maybe I'll bring this up again some other time, but for now I give up on this :) .
Colorizing specific text isn't that hard once you've got to the point that you can put data into the grid row.

When you have that working please post it and I'll show you how to add conditional font color to it.

It also occurs to me from your use of "column" in your description that you may not need a DataGrid at all. Have you considered a table field?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

stam
Posts: 2776
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

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

Post by stam » Tue Aug 23, 2022 7:35 am

I know people say tablefield is simpler - but in truth it took a grand total of 6 lines of quite simple code (per column) to set up conditional cell colouring in a data grid (one conditional “if/then/else” block and one “set the rect” statement).
Not sure how much simpler a tablefield would be?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9785
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Tue Aug 23, 2022 5:12 pm

I'm not familiar at all with Datagrids
Me neither, but I do use them.

Richard and Stam are both correct in that there is a learning curve with DG's. I am only writing to confirm something Richard said, that you may not need one; a table field will do.

New users are often attracted to the DG early on. But a table field should often be at least considered first. That said, know that a DG is far more powerful and flexible than a table field, and much better looking. Know also that if you do become proficient with them, your current project notwithstanding, you have a powerful tool indeed in your repertoire.

Craig

stam
Posts: 2776
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

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

Post by stam » Tue Aug 23, 2022 8:40 pm

dunbarx wrote:
Tue Aug 23, 2022 5:12 pm
But a table field should often be at least considered first.
My old boss used to say 'use the right tool for the job'.
Just not sure which jobs would actually make me choose the tableField over the DG ;)

Even for simpler tables, the DG just makes life easier with it's very rich API - but that is also the difficulty, as learning the API is a modest learning curve. But for new users, there is also a learning curve for tableField that is not straightforward either.

Just to illustrate the counterpoint view - again influenced by familiarity (but in the opposite direction):
I personally feel the TableField is like a hack that cobbles stuff together to emulate a table that doesn't look that good by default, but it also doesn't make life easier (other than in very simple instances).
Yes, you can get it looking nice and you can get it to do more complex stuff, but that involves a whole lot of code, and for stuff that is given 'for free' in the data grid. I have yet to use a tableField and feel "ah, this was the right thing to use and it's so easy". I have used it on occasion, but each time it involved a whole lot of head-scratching, searching online and the API on how to bend it to my will... tabStops: ughhh...
Which is probably what others feel about the data grid :D


Case in point: I'm sure it's doable, but could someone post an example where a cell (note: not the backgroundColor of the cell's text, but the whole cell) is coloured conditionally? It would be interesting (to me anyway) to compare code...


dunbarx wrote:
Tue Aug 23, 2022 5:12 pm
New users are often attracted to the DG early on.
This is an often repeated view here.
My take on this, as a 'newcomer' of two years (never mind when the forum says i joined, i only really started using LC in lockdown ;) ), is that this is the view often expressed by those who were using LC long before the data grid came along.
Familiarity with the tableField probably influences these comparisons quite heavily - much in the same way i would favour the DG.

But remember: For new users there is a learning curve for both - and yes the DG has a more extensive API but is also better documented with more examples online. And once learned, the API gives you access to many conveniences. What we really need to be asking is how much more difficult it is using the data grid than a tableField for a new user who is unfamiliar with both.

The tableField basically draws lines around tab-separated text; but then so does the data grid in the default table mode. For basic usage there is actually little difference between the two, except you already get draggable and sortable column headers with the DG, which you'd need to code for the tableField. Beyond (very) basic usage, tableField makes me work harder to achieve my goals and cramps my style ;)

Having said that i'm rather enjoying the new grids as well that came with the megabundle; but both polyList and polyGrid have fewer features than the DG but are very good in what the do. In particular, polyList has a couple of sweet features not available in the data grid as seen on the blog: https://livecode.com/building-an-airbnb ... -livecode/

So for me, it's now a choice between DG, PL and PG - i have no love for the TF!
Obviously this is my preference and does and should not apply to anyone else - whatever makes you happy and productive is the right way to do it. Just sharing my views...

S.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9785
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Tue Aug 23, 2022 10:15 pm

Stam

All you said is true and valid.

I think you were over-defending the DG if it was my post that started all this. I only meant that a TF should be at least considered, because it often is simply overlooked, not that it should be forced into contortions that a DG does in its sleep. :wink:

Craig
Last edited by dunbarx on Wed Aug 24, 2022 1:51 pm, edited 1 time in total.

stam
Posts: 2776
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

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

Post by stam » Wed Aug 24, 2022 12:13 am

dunbarx wrote:
Tue Aug 23, 2022 10:15 pm
Stam

All you said is true and valid.

I think you were over-defending the DG if it was my post that started all this. I only meant that it should be at least considered, because it often is simply overlooked, not that it should be forced into contortions that a DG does in its sleep. :wink:

Craig
Hi Craig - wasn't really your post, it was Richard's ;)
I think probably more 'over-hating' the tableField rather than overdefending the data grid lol - i really don't like using it (in case you couldn't tell!!)

Joking aside though: I'm wondering how you would do what he OP requested with a tableField... Can you colour an entire cell? (ie not just the backgroundColor of the text?)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9785
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Wed Aug 24, 2022 12:55 am

Stam.
Can you colour an entire cell?
No, I mean, yes, but you have to kludge it, something I like doing. An overlay of a control with its backColor and blendlevel set to something nice. Coloring a cell or an entire column is trivial, but is so only as a kludge. I agree with you that a TF is just a regular field with some tab-based wrappers. That has an appeal at a certain level. Low, that is, like me.

Craig

stam
Posts: 2776
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

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

Post by stam » Wed Aug 24, 2022 2:06 am

dunbarx wrote:
Wed Aug 24, 2022 12:55 am
No, I mean, yes, but you have to kludge it, something I like doing. An overlay of a control with its backColor and blendlevel set to something nice. Coloring a cell or an entire column is trivial, but is so only as a kludge. I agree with you that a TF is just a regular field with some tab-based wrappers. That has an appeal at a certain level. Low, that is, like me.

Craig
Hi Craig,
It's fascinating to read what other people do in code... and this is what makes this forum fun. Whatever makes each developer happy and productive is the right way... there is no better way! Kludge away!

Personally i think i would struggle if i had to manage variable overlays for conditional colouring of each cell for multiple columns and especially if cells are resizeable, eg on resizeStack (did i mention my tabStop allergy?)

The point being - by the time you've managed to kludge this all together, you'll end up with a much more complex solution than just adding a 6 lines of code to a datagrid to both conditionally colour and cater for resizing the cell. Hence for new users i wouldn't recommend this other than an exercise in kludgery ;)
Change My Mind.jpg

I'm too lazy to kludge, i just want to get it done quickly and as painlessly as possible... I guess that makes me 'high level' then 8)

Stam

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9867
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

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

Post by FourthWorld » Wed Aug 24, 2022 7:27 am

I missed the post where the OP defined the full range of features required.

But Bernd's headers do most of what's needed for simple lists.

And I've used a great many list fields with no need to resize headers at all.

The DG is unbeatable for what it was designed for: form lists primarily. Columnar lists were an easy addition needed at the time for numeric displays, because LC's list fields didn't have independent column alignment until the tabAlign property was added in v7-ish.

If you like DGs you're in good company. Many do, including myself.

But in this thread where the OP expressed difficulty working with them, it seemed helpful to note that a native single-object alternative is available.

People like what they like, and struggle with what they struggle with. I have no interest in telling anyone what they enjoy, just making sure they see the scope of what's available.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4036
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

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

Post by bn » Wed Aug 24, 2022 9:40 pm

Hi Stam,
stam wrote:
Wed Aug 24, 2022 2:06 am
(did i mention my tabStop allergy?)
Have a look at "tabWidth", it is easier to grasp than "tabStops"

If you have used tinyDictionary then you have looked at a "modifiedTableField" by yours truly.

I wrote it for myself and a couple of other people because the dataGrid made my head spin. But of course with adding features and the API it grew complex by necessity.

Without DataGridHelper from Zryip theSlug I would not even had an idea of the possibilities of the dataGrid.

Kind regards
Bernd

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”