3
$\begingroup$

I have two rows of values that I'd like to represent as a table, where cells are diagonally divided showing color-coded values with column A and column B values for each row such as below:

enter image description here

Any neat idea how produce such design? The hardest part for me is dividing the cells diagonally.

$\endgroup$

2 Answers 2

2
$\begingroup$

I'd start with a generic utility:

SplitBox[{upColor_, downColor_}, {upValue_, downValue_}] :=
Graphics[
  {{upColor, Triangle[{{0, 0}, {0, 1}, {1, 1}}]}, 
   {downColor, Triangle[{{0, 0}, {1, 0}, {1, 1}}]}, 
   Text[upValue, {.3, .7}], 
   Text[downValue, {.7, .3}]}, 
  ImageSize -> 100]

Demo:

GraphicsGrid[
  Apply[
    SplitBox, 
    Table[{{RandomColor[], RandomColor[]}, {RandomReal[500], RandomReal[100]}}, {4}, {3}], 
    {2}]]

enter image description here

Now you'll need to add any styling and presentation details you want.

Presumably the colors are determined by the values, so you'll need some helper functions that return a color for a given argument.

$\endgroup$
2
$\begingroup$
ClearAll[labeledSplitRectangle]

labeledSplitRectangle[{topcolor_, bottomcolor_}, 
    {toplabel_, bottomlabel_}, dir_ : 1] := 
 Module[{c = Transpose @ CoordinateBounds[Rectangle @ ##], 
    w = {{3/4, 1/4}, (dir /. {-1 -> Reverse, 1 -> Identity}) @ {1/3, 2/3}}}, 
  {{LinearGradientFilling[ 
      {0, 1/2, 1/2, 1} -> {topcolor, topcolor, bottomcolor, bottomcolor}, 
      dir ArcTan @@ N @ Cross[Subtract @@ c], "Fixed"], Rectangle @@ c}, 
    Text[toplabel, Dot @@@ Thread[{w, Transpose @ c}]], 
    Text[bottomlabel,  Dot @@@ Thread[{1 - w, Transpose @ c}]]}] &

Examples:

SeedRandom[7];

Graphics[{labeledSplitRectangle[{Red, Cyan}, 
   Style[#, FontSize -> Scaled[.03]] & /@ 
     {"top label", "bottom label"}][{1, 1}, {3, 2}], 
 labeledSplitRectangle[{RandomColor[], RandomColor[]}, 
   Style[#, FontSize -> Scaled[.03]] & /@ 
     {"lbl 1", "lbl 2"}][{2, 0}, {3, 1}], 
 labeledSplitRectangle[{RandomColor[], RandomColor[]}, 
   Style[#, FontSize -> Scaled[.03]] & /@ 
     {"A", "B"}, -1][{3.5, .5}, {5, 3}]}, 
  ImageSize -> 400]

enter image description here

SeedRandom[1];

tbl = Round[RandomReal[1, {7, 4, 2}], .01];

Graphics[
 {MapIndexed[
  labeledSplitRectangle[
     Hue /@ #, 
     Style[#, 16, ColorNegate @ Hue @ #] & /@ #] @ #2 &, 
   tbl, {2}]}, 
 Method -> {"GridLinesInFront" -> True}, 
 GridLines -> {Range[7], Range[4]}, 
 GridLinesStyle -> Directive[AbsoluteThickness[5], White], 
 PlotRangePadding -> 0, ImageSize -> 1 -> 100]

enter image description here

Replace labeledSplitRectangle[...] with labeledSplitRectangle[..., -1] to get

enter image description here

Graphics[{MapIndexed[
   labeledSplitRectangle[
      {ColorData["DeepSeaColors"] @ First @ #, 
       ColorData["SolarColors"] @ Last @ #}, 
      Style[#, 16, If[# < .5, White, Black]] & /@ #] @ #2 &, tbl, {2}]},
 Method -> {"GridLinesInFront" -> True}, 
 GridLines -> {Range[7], Range[4]}, 
 GridLinesStyle -> Directive[AbsoluteThickness[5], White], 
 PlotRangePadding -> 0, ImageSize -> 1 -> 100]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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