Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to generate heat maps given the points

Tags:

c#

.net

I want to generate a heat map in windows form. I have a set of points as the input. How to go about doing this in the simplest way? Thanks.

like image 210
olive Avatar asked Mar 18 '11 09:03

olive


People also ask

How are heat maps calculated?

You can think of a heat map as a data-driven “paint by numbers” canvas overlaid on top of an image. In short, an image is divided into a grid and within each square, the heat map shows the relative intensity of values captured by your eye tracker by assigning each value a color representation.

Can I create a heat map in Google Maps?

Creating a Google Maps heatmap takes 3 simple steps: Location data selection, data download and using a location history visualizer.


2 Answers

Here is a simple method that will generate a color based on the relative position of a value between min and max. Values closer to min will be greener, while values closer to max will be redder.
To use this method, generate your list of values and calculate the min and max values. If you are building a grid you can handle the RowDataBound event or something similar and call the HeatMap method from there. Get a reference to the cell and set the background color to the color returned by the HeatMap method.

public Color HeatMap(decimal value, decimal min, decimal max)
{
   decimal val = (value - min) / (max-min);
   return new Color
   {
       A = 255,
       R = Convert.ToByte(255 * val),
       G = Convert.ToByte(255 * (1-val)),
       B = 0
   };
}
like image 127
Sam Avatar answered Sep 30 '22 01:09

Sam


Building on the answers already here, this method allows you to specify the Colors you wish to use as the max and min colours.

private Color HeatMapColor(double value, double min, double max)
{
    Color firstColour = Color.RoyalBlue;
    Color secondColour = Color.LightSkyBlue;

    // Example: Take the RGB
    //135-206-250 // Light Sky Blue
    // 65-105-225 // Royal Blue
    // 70-101-25 // Delta

    int rOffset = Math.Max(firstColour.R, secondColour.R);
    int gOffset = Math.Max(firstColour.G, secondColour.G);
    int bOffset = Math.Max(firstColour.B, secondColour.B);

    int deltaR = Math.Abs(firstColour.R - secondColour.R);
    int deltaG = Math.Abs(firstColour.G - secondColour.G);
    int deltaB = Math.Abs(firstColour.B - secondColour.B);

    double val = (value - min) / (max - min);
    int r = rOffset - Convert.ToByte(deltaR * (1 - val));
    int g = gOffset - Convert.ToByte(deltaG * (1 - val));
    int b = bOffset - Convert.ToByte(deltaB * (1 - val));        

    return Color.FromArgb(255, r, g, b);
}

The results look like this for a test DataGrid with some sample data.

enter image description here

like image 42
Matt Avatar answered Sep 30 '22 01:09

Matt