I have a chart on which I want to plot a heat map; the only data I have is humidity and temperature, which represent a point in the chart.
How do I get the rectangular type of heat map on the chart in c#?
What I want is similar to picture below :
What I really want is a rectangular region in the chart which is plotted in different color based on the point that i get from the list of points and form the colorful section in the chart.
You have a choice of at least three ways to create a chart with colored rectangles that make up a heat map.
Here is one example that uses/abuses a
DataGridView
. While I would not suggest this, the post contains a useful function that creates nice color lists to use in your task.Then there is the option to draw the chart using GDI+ methods, namely
Graphics.FillRectangle
. This not hard at all but once you want to get those nice extras a Chart control offers, like scaling, axes, tooltips etc the work adds up.. See below!So let's have a look at option three: Using the
Chart
control from theDataVisualization
namespace.Let's first assume that you have created a list of colors:
And that you have managed to project your data onto a 2D array of int indices that point into the color list:
Next you have to pick a
ChartType
for yourSeries S1
There really is only one I can think of that will help:Points are displayed by
Markers
. We want theDataPoints
not really displayed as one of the standard MarkerTypes.Square
would be ok, if we wanted to display squares; but for rectangles it will not work well: Even if we let them overlap there will still be points at the borders that have a different size because they don't fully overlap..So we use a custom marker by setting the
MarkerImage
of each point to a bitmap of a suitable size and color.Here is a loop that adds the
DataPoints
to ourSeries
and sets each to have aMarkerImage
:This takes some explaining: To set a
MarkerImage
that is not at a path on the disk, it has to reside in theChart's Images
collection. This means is needs to be of typeNamedImage
. Any image will do, but it has to have a unique name string added to identify it in theNamedImagesCollection
. I chose the names to be 'NI1', 'NI2'..Obviously we need to create all those images; here is a function to do that:
We want all markers to have at least roughly the right size; so whenever that size changes we set it again:
This doesn't care much about details like the
InnerPlotPosition
, i.e. the actual area to draw to; so here is some room for refinement..!We call this when we set up the chart but also upon resizing:
Let's have a look at the result using some cheap testdata:
As you can see resizing works ok..
Here is the full code that set up my example:
A few notes on limitations:
The point will always sit on top of any gridlines. If you really needs those you will have to draw them on top in one of the the
Paint
events.The labels as shown are referring to the integers indices of the data array. If you want to show the original data, one way would be to add
CustomLabels
to the axes.. See here for an example!This should give you an idea of what you can do with a
Chart
control; to complete your confusion here is how to draw those rectangles in GDI+ using the same colors and data:The resulting Bitmap looks familiar:
That was simple; but to add all the extras into the space reserved by the padding will not be so easy..