# Represent data as a table with diagonally split color-coded cells

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:

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

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}]]


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.

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}}},
{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]


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]


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

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]