Algorithm for coloring DataGridView to see visually a whole distribution of cell values
I have a matrix of numbers. A DataGridView is used for displaying it. I need an algorithm which for each cell calculates its BackColor, based on the value of the ce开发者_如何学编程ll. So, when looking on the DataGridView I will be able to see visually a whole distribution of values. So, cells with close values will have similar colors.
Find the Max and Min of the Matrix and set the Hue of the Background to
Hue(a(i,j)) = (a(i,j) - min)/(max - min))
You may get for example (code in Mathematica,don't mind, just as an example):
a = Table[x + Sin[3 x + y^2], {x, -3, 3, 0.1}, {y, -3, 3, 0.1}];
ArrayPlot[a, ColorFunction -> (Hue[(# - min)/(max - min), 1, 1] &)],
ColorFunctionScaling->False ]
Edit
Just as a reference, the 3D plot of this function is
There's a lot of literature on that (e.g. Tufte, Ware, Bertin)
Basically, you have to decide what you want to see:
- If you want to recognize similar values, a colourmap (like the one @belisarius showed) is good. Looking at the color distribution, you can easily see "ok, the point in the middle has about the same value as the one at the bottom right". But looking at the colors, you can't see which value is higher than the other, or where the maximal/minimal values are. Hue has no intuitive order.
If you want to display order information, a greyscale is ideal.
Here you can immediately see the local and global maximal/minimal values. Downside: yon can only compare brightness values very well, when they're close together. You can't visually compare the color in the center and the color at the bottom right. Lots of optical illusions are based on that.
- If you want to highlight small differences between adjacent cells, using a derivative for the brightness might be better (similar to topographic maps)
Finally, if you're target users are average males, you'll have to think about color-blind people. Even non-colorblind people are a lot better at comparing lengths than we are at comparing colors.
You want to look into a space-filling-curve or a spatial index or a treemap. It reduces the 2d complexity to a 1d complexity. It uses in a lot of heat maps and maps application too. A sfc is a data structure similar to a quadtree. You want to look into Nick's hilbert curve spatial index quadtree blog. There is also the posibility to reduce a 3d complexity.
精彩评论