public void DrawHeat(string filePath, string sheetName)
{
string ParamPalettePath = @"D:\Ventuz5\01\Images\HeatMap/palette.png";//颜色板
DataTable dt = GetExcelTable(filePath, sheetName,2);
List<double> lon = new List<double>();
List<double> lat = new List<double>();
List<double> weight = new List<double>();
for (int i = 0; i < dt.Rows.Count; i++)
{
string[] lonAndLat = dt.Rows[i]["坐标值"].ToString().Split(',');
double lonStr = Convert.ToDouble(lonAndLat[0]);
double latStr = Convert.ToDouble(lonAndLat[1]);
double weightStr = Convert.ToDouble(dt.Rows[i]["权重值"].ToString());
lon.Add(lonStr);lat.Add(latStr);weight.Add(weightStr);
}
double latRange = Math.Abs(lat.Max() - lat.Min());
double lngRange = Math.Abs(lon.Max() - lon.Min());
int width = 0;
int height = 0;
double scale = 1;
if (lngRange > latRange)
{
width = 500;
scale = lngRange / 500;
height = (int)(latRange / lngRange * 500);
}
else
{
height =500;
scale = latRange / 500;
width = (int)(lngRange / latRange * 500);
}
Bitmap bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
RayHeatMap heatMap = new RayHeatMap();
heatMap.BrushStop = 0.3f;
heatMap.Radius = 30;
for (int i = 0; i < lat.Count; i++)
{
int y = (int)((lat[i] - lat.Min()) / scale);
int x = (int)((lon[i] - lon.Min()) / scale);
int weightInt = (int)((double)weight[i] / weight.Max() * 255);
heatMap.AddPoint(new Point(x, y), weightInt);
}
heatMap.Draw(ref bitmap);
heatMap.Colorize(ref bitmap, ParamPalettePath);
DirectoryInfo dir = new DirectoryInfo(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(ParamPalettePath), "ShenZhen"));
if (!dir.Exists)
dir.Create();
string name = DateTime.Now.ToString("yyyyMMddHHmm") + ".png";
string filePaths = System.IO.Path.Combine(dir.ToString(), name);
FileStream fs = File.Open(filePaths, FileMode.Create);
bitmap.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
fs.Close();
}