Displaying HeatMap in DataGridView from List<List> in C# -



Displaying HeatMap in DataGridView from List<List<T>> in C# -

i've got info in list of list of touples. task create heatmap that. beingness newbie in c#, i've searched net , found way of solving task painting elements in datagridview, don't understand, how it. so,i've got list of touples:

using spikedatapacket = list<tuple<double, double>>;

this way load info within grid:

public heatform(list<spikedatapacket> list) { spikelist = list; initializecomponent(); var bindstim = new bindinglist<spikedatapacket>(spikelist); var stimsource = new bindingsource(bindstim, null); heatmap.datasource = stimsource; }

but displays table "capacity" , "count" within datagridview, not data. also, i've found way count color, don't know, how apply it:

private color heatmapcolor(double value, double min, double max) { color firstcolour = color.royalblue; color secondcolour = color.lightskyblue; // example: take rgb //135-206-250 // lite sky bluish // 65-105-225 // royal bluish // 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)); homecoming color.fromargb(255, r, g, b); }

thank in advance!

i think tackle problem differently.

i start without using databinding. neither list of list of tuple construction nor mapping of double color lends databinding. i'm not sure color mapping algorithm..

to fill info datagridview dgv utilize simple routine, first prepares dgv , paints cells:

void filldata() { int maxrow = data.count; int maxcol = data[0].count; double factor = 1.0; dgv.rowheadersvisible = false; dgv.columnheadersvisible = false; dgv.allowusertoaddrows = false; dgv.allowusertoordercolumns = false; dgv.cellborderstyle = datagridviewcellborderstyle.none; //.. int rowheight = dgv.clientsize.height / maxrow - 1; int colwidth = dgv.clientsize.width / maxcol - 1; (int c = 0; c < maxrow; c++) dgv.columns.add(c.tostring(), ""); (int c = 0; c < maxrow; c++) dgv.columns[c].width = colwidth; dgv.rows.add(maxrow); (int r = 0; r < maxrow; r++) dgv.rows[r].height = rowheight; list<color> basecolors = new list<color>(); // create color list basecolors.add(color.royalblue); basecolors.add(color.lightskyblue); basecolors.add(color.lightgreen); basecolors.add(color.yellow); basecolors.add(color.orange); basecolors.add(color.red); list<color> colors = interpolatecolors(basecolors, 1000); (int r = 0; r < maxrow; r++) { (int c = 0; c < maxrow; c++) { dgv[r,c].style.backcolor = colors[ convert.toint16( data[r][c].item2 * factor)]; } } }

you wnat alter few things, base of operations colors , number of colors want get, depending on values mapping double value integer index!

here function create list of interpolated colors. takes few base of operations colors , length n , returns n interpolated colors. makes mapping simple , flexible..

list<color> interpolatecolors(list<color> stopcolors, int count) { sorteddictionary<float, color> gradient = new sorteddictionary<float, color>(); (int = 0; < stopcolors.count; i++) gradient.add(1f * / (stopcolors.count-1), stopcolors[i]); list<color> colorlist = new list<color>(); using (bitmap bmp = new bitmap(count, 1)) using (graphics g = graphics.fromimage(bmp)) { rectangle bmpcrect = new rectangle(point.empty, bmp.size); lineargradientbrush br = new lineargradientbrush (bmpcrect, color.empty, color.empty, 0, false); colorblend cb = new colorblend(); cb.positions = new float[gradient.count]; (int = 0; < gradient.count; i++) cb.positions[i] = gradient.elementat(i).key; cb.colors = gradient.values.toarray(); br.interpolationcolors = cb; g.fillrectangle(br, bmpcrect); (int = 0; < count; i++) colorlist.add(bmp.getpixel(i, 0)); br.dispose(); } homecoming colorlist; }

my test info created this:

list<list<tuple<double,double>>> info = new list<list<tuple<double,double>>>(); random r = new random(); void createdata(int maxrow, int maxcol) { (int c = 0; c < maxrow; c++) { data.add(new list<tuple<double, double>>()); (int r = 0; r < maxrow; r++) { data[c].add(new tuple<double, double>(c, math.min(999, r.next(r*c)))); } } }

and used this:

private void form1_load(object sender, eventargs e) { createdata(40, 40); filldata(); }

here (rather boring) screenshot:

once got display want it, may want decide going solution with databinding. believe need utilize ownerdrawing cells. 1 time have values in cells can utilize same mapping this:

private void dgv_cellpainting(object sender, datagridviewcellpaintingeventargs e) { color thecolor = ..... e.graphics.clear(colors[thecolor]); }

c# heatmap

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -