c# 画热力图(一)

 public class HeatMap
    {
        public float BrushStop { get; set; }
        public int Radius { get; set; }
        private struct HeatPoint
        {
            public Point point;
            public int value;
            public HeatPoint(Point point, int value)
            {
                this.point = point;
                this.value = value;
            }
        }
        private List<HeatPoint> _heatPoints = new List<HeatPoint>();


        public void AddPoint(Point point, int value)
        {
            _heatPoints.Add(new HeatPoint(point, value));
        }

        public void Draw(ref Bitmap bitmap)
        {
            Graphics surface = Graphics.FromImage(bitmap);

            foreach (HeatPoint heatPoint in _heatPoints)
                DrawHeatPoint(surface, heatPoint);

            surface.Dispose();
        }

        private System.Drawing.Drawing2D.ColorBlend GetColorBlend(int value)
        {
            System.Drawing.Drawing2D.ColorBlend colors = new System.Drawing.Drawing2D.ColorBlend(3);

            colors.Positions = new float[3] { 0, BrushStop, 1 };

            colors.Colors = new Color[3]
            {
                Color.FromArgb(0, Color.White),
                Color.FromArgb(value, Color.Black),
                Color.FromArgb(value, Color.Black)
            };
            return colors;
        }

        private void DrawHeatPoint(Graphics surface, HeatPoint heatPoint)
        {
            int x = heatPoint.point.X - Radius;
            int y = heatPoint.point.Y - Radius;
            int len = 2 * Radius;

            var ellipsePath = new System.Drawing.Drawing2D.GraphicsPath();
            ellipsePath.AddEllipse(x, y, len, len);

            System.Drawing.Drawing2D.PathGradientBrush brush = new System.Drawing.Drawing2D.PathGradientBrush(ellipsePath);
            System.Drawing.Drawing2D.ColorBlend gradientSpecifications = GetColorBlend(heatPoint.value);
            brush.InterpolationColors = gradientSpecifications;

            surface.FillEllipse(brush, x, y, len, len);
        }

        public void Colorize(ref Bitmap bitmap, string paletteFile)
        {
            int[] palette = LoadPalette(paletteFile);

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                    bitmap.SetPixel(x, y, Color.FromArgb(palette[(byte)~(((uint)(bitmap.GetPixel(x, y).ToArgb())) >> 24)]));
            }
        }

        private int[] LoadPalette(string paletteFile)
        {
            int[] palette = new int[256];
            Bitmap paletteImage = (Bitmap)Bitmap.FromFile(paletteFile);
            for (int i = 0; i < palette.Length - 1; i++)
            {
                palette[i] = paletteImage.GetPixel(i, 0).ToArgb();
            }
            palette[palette.Length - 1] = 0;
            return palette;
        }
    }

以下是使用C#绘制小波包数据热力图的示例代码: ```csharp using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace WaveletPacketHeatmap { public partial class Form1 : Form { private Chart chart; public Form1() { InitializeComponent(); // 创建 Chart 控件 chart = new Chart(); chart.Parent = this; chart.Dock = DockStyle.Fill; chart.BackColor = Color.White; // 添加热力图 AddHeatmap(); } private void AddHeatmap() { // 读取小波包数据 double[,] data = ReadWaveletPacketData("wavelet_packet_data.txt"); // 创建数据序列 List<double> values = new List<double>(); for (int i = 0; i < data.GetLength(0); i++) { for (int j = 0; j < data.GetLength(1); j++) { values.Add(data[i, j]); } } // 计算数据范围 double minValue = values.Min(); double maxValue = values.Max(); // 创建热力图序列 List<DataPoint> points = new List<DataPoint>(); for (int i = 0; i < data.GetLength(0); i++) { for (int j = 0; j < data.GetLength(1); j++) { double value = data[i, j]; double intensity = (value - minValue) / (maxValue - minValue); Color color = Color.FromArgb((int)(255 * intensity), 0, 0); points.Add(new DataPoint(j, i, value) { Color = color }); } } // 创建热力图 Series heatmap = new Series(); heatmap.ChartType = SeriesChartType.Point; heatmap.Points.AddRange(points.ToArray()); chart.Series.Add(heatmap); // 设置坐标轴 chart.ChartAreas.Add(new ChartArea()); chart.ChartAreas[0].AxisX.Minimum = 0; chart.ChartAreas[0].AxisX.Maximum = data.GetLength(1); chart.ChartAreas[0].AxisY.Minimum = 0; chart.ChartAreas[0].AxisY.Maximum = data.GetLength(0); chart.ChartAreas[0].AxisX.Interval = 1; chart.ChartAreas[0].AxisY.Interval = 1; chart.ChartAreas[0].AxisX.MajorGrid.Enabled = false; chart.ChartAreas[0].AxisY.MajorGrid.Enabled = false; chart.ChartAreas[0].AxisX.MajorTickMark.Enabled = false; chart.ChartAreas[0].AxisY.MajorTickMark.Enabled = false; chart.ChartAreas[0].AxisX.LabelStyle.Enabled = false; chart.ChartAreas[0].AxisY.LabelStyle.Enabled = false; } private double[,] ReadWaveletPacketData(string filename) { List<double[]> rows = new List<double[]>(); using (StreamReader reader = new StreamReader(filename)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); string[] values = line.Split('\t'); double[] row = new double[values.Length]; for (int i = 0; i < values.Length; i++) { row[i] = double.Parse(values[i]); } rows.Add(row); } } double[,] data = new double[rows.Count, rows[0].Length]; for (int i = 0; i < rows.Count; i++) { for (int j = 0; j < rows[i].Length; j++) { data[i, j] = rows[i][j]; } } return data; } } } ``` 在上面的示例代码中,`ReadWaveletPacketData` 方法用于从文件中读取小波包数据,`AddHeatmap` 方法用于创建热力图并添加到 Chart 控件中。通过设置数据点的颜色来实现热力图效果。最后需要设置坐标轴的范围、网格线、刻度标记和标签等属性来展示热力图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值