3
$\begingroup$
data = Dataset[{<|"userId" -> 5346, "A3Grades" -> 56.25`, 
 "ColorCode" -> "Red", "totalpointsA3" -> 36, 
 "Passingpoints" -> 38.4`, "AveragePoints" -> 47.285714285714285`,
  "Pointsreqdforpassing" -> 2.3999999999999986`, 
 "pointsforabvavg" -> 0.`|>, <|"userId" -> 5347, 
 "A3Grades" -> 78.125`, "ColorCode" -> "Green", 
 "totalpointsA3" -> 50, "Passingpoints" -> 38.4`, 
 "AveragePoints" -> 47.285714285714285`, 
 "Pointsreqdforpassing" -> 0.`, 
 "pointsforabvavg" -> 0.`|>, <|"userId" -> 5349, 
 "A3Grades" -> 73.4375`, "ColorCode" -> "Yellow", 
 "totalpointsA3" -> 47, "Passingpoints" -> 38.4`, 
 "AveragePoints" -> 47.285714285714285`, 
 "Pointsreqdforpassing" -> 0.`, 
 "pointsforabvavg" -> 0.2857142857142847`|>}];

I want to create a grid, such that the color of the grid background changes according to the ColorCode column of data. I tried the following and few other workarounds, but couldn't figure out how to make the exact association:

Grid[{{"Color", SpanFromLeft, SpanFromLeft}, {"Code", SpanFromLeft, SpanFromLeft}}, Frame -> All, Background -> {{}, data[If[#ColorCode == "Red", Red, LightGray] &]}]

Can anyone help?

$\endgroup$
4
$\begingroup$

If we define a couple of helper functions...

color[row_] := If[row["ColorCode"] === "Red", Red, LightGray]

grid[rows_] :=
  Grid[
    Prepend[Values @ rows, Keys @ First @ rows]
  , Frame -> All
  , Background -> {None, {None, color /@ rows}}
  ]

... then we can write:

data[grid]

data grid with a red and grey colour scheme

We can easily experiment with the colour function:

color[row_] := Symbol @ row["ColorCode"]

data[grid]

data grid with a multiple colour scheme

If so inclined, we could also bundle all this up into a single dataset query operator:

data[
   {Values, Keys @* First, Map[Symbol @ #["ColorCode"] &]}
/* Apply[Grid[Prepend[#1, #2], Frame -> All, Background -> {None, {None, #3}}]&]
]

data grid with a multiple colour scheme

$\endgroup$
  • $\begingroup$ Thanks for your reply. But I was trying to achieve something like this: Manipulate[ Grid[{{performance, SpanFromLeft, SpanFromLeft}, {Switch[Ceiling[performance, 25], 25, StringTemplate["``%"][performance], 50, StringTemplate["``%"][performance], 75, StringTemplate["``%"][performance], 100, StringTemplate["``%"][performance]], SpanFromLeft}}, BaseStyle -> {FontFamily -> "Roboto"}, Background -> {{}, {Switch[Ceiling[performance, 25], 25, Red, 50, Orange, 75, Yellow, 100, Green], LightGray}}], {{performance, 1}, 1, 100}] $\endgroup$ – Annesha Bhoumik Apr 29 at 19:01
  • $\begingroup$ I want to connect the manipulate to this dataset, such that the color changes according to the color code and A3Grades as I slide performance $\endgroup$ – Annesha Bhoumik Apr 29 at 19:06
  • $\begingroup$ Are you imagining that moving the slider will alter the dataset or that it will only alter the visualization? If the former, change data from a variable to a function takes performance as an argument. If the latter, change color to accept performance as an argument. The latter is probably easier, something like color[performance_][row_] := ... and Manipulate[ data[{..., Map[color[performance]]} /* ...], {{performance, 1}, 1, 100}]. $\endgroup$ – WReach Apr 29 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.