基于C#实现的医疗体温单线条显示源代码,支持动态图形显示与高度可配置化,结合GDI+绘图技术与XML配置,符合医疗行业规范。
一、核心功能设计
-
多体征绘制
- 体温(三色曲线)、脉搏、呼吸、大便次数等
- 特殊标记:物理降温(❌)、体温不升(↓)、脉搏短绌(连线)
-
动态配置
- 通过XML定义坐标轴范围、颜色方案、刻度间隔
-
数据驱动
- 绑定
List<TemperatureRecord>数据源,实时刷新
- 绑定
二、代码实现
1. 体温单绘制核心类
public class MedicalTemperatureChart : UserControl
{
// 配置属性
public Color NormalTempColor { get; set; } = Color.Green; // 正常体温色
public Color HighTempColor { get; set; } = Color.Red; // 发热色
public Color LowTempColor { get; set; } = Color.Blue; // 低体温色
public float GridStepX { get; set; } = 40f; // 横向网格步长(像素)
public float GridStepY { get; set; } = 20f; // 纵向网格步长(像素)
// 数据绑定
public List<TemperatureRecord> Records { get; set; } = new List<TemperatureRecord>();
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
// 1. 绘制网格
DrawGrid(g);
// 2. 绘制三色体温曲线
DrawTemperatureCurve(g);
// 3. 绘制脉搏/呼吸点
DrawPulseAndRespiration(g);
// 4. 绘制特殊标记
DrawSpecialMarkers(g);
}
private void DrawGrid(Graphics g)
{
using (Pen gridPen = new Pen(Color.LightGray, 1))
{
// 横向网格(每1℃一条)
for (float y = 0; y < Height; y += GridStepY)
{
g.DrawLine(gridPen, 0, y, Width, y);
}
// 纵向网格(每4小时一条)
for (float x = 0; x < Width; x += GridStepX * 4)
{
g.DrawLine(gridPen, x, 0, x, Height);
}
}
}
private void DrawTemperatureCurve(Graphics g)
{
if (Records.Count < 2) return;
// 按时间排序
var sortedRecords = Records.OrderBy(r => r.Time).ToList();
for (int i = 0; i < sortedRecords.Count - 1; i++)
{
TemperatureRecord r1 = sortedRecords[i];
TemperatureRecord r2 = sortedRecords[i + 1];
// 计算坐标
Point p1 = new Point((int)(r1.Time.TotalHours * GridStepX),
(int)((42 - r1.Temperature) * GridStepY));
Point p2 = new Point((int)(r2.Time.TotalHours * GridStepX),
(int)((42 - r2.Temperature) * GridStepY));
// 选择颜色
Color lineColor = r1.Temperature switch
{
> 37.3f => HighTempColor, // 发热
< 36.0f => LowTempColor, // 低体温
_ => NormalTempColor // 正常
};
// 绘制线段
using (Pen tempPen = new Pen(lineColor, 2))
{
g.DrawLine(tempPen, p1, p2);
}
// 绘制数据点(口温=实心圆)
g.FillEllipse(Brushes.Red, p1.X - 3, p1.Y - 3, 6, 6);
}
}
}
2. 数据结构
public class TemperatureRecord
{
public DateTime Time { get; set; } // 测量时间
public float Temperature { get; set; } // 体温值
public int Pulse { get; set; } // 脉搏
public int Respiration { get; set; } // 呼吸
public bool IsPhysicalCooling { get; set; } // 是否物理降温
}
3. 动态配置(XML示例)
<!-- TemperatureChartConfig.xml -->
<Configuration>
<Axis>
<YMin>35</YMin>
<YMax>42</YMax>
<YStep>0.5</YStep>
</Axis>
<Colors>
<Normal>#2ECC71</Normal> <!-- 绿色 -->
<High>#E74C3C</High> <!-- 红色 -->
<Low>#3498DB</Low> <!-- 蓝色 -->
</Colors>
</Configuration>
4. 配置加载
public void LoadConfig(string xmlPath)
{
XDocument doc = XDocument.Load(xmlPath);
YMin = float.Parse(doc.Root.Element("Axis").Element("YMin").Value);
YMax = float.Parse(doc.Root.Element("Axis").Element("YMax").Value);
NormalTempColor = ColorTranslator.FromHtml(doc.Root.Element("Colors").Element("Normal").Value);
// ...其他配置加载
}
三、关键特性实现
1. 三色体温曲线逻辑
// 根据体温值动态切换画笔颜色
private Pen GetTemperaturePen(float temp)
{
return temp switch
{
> 37.3f => new Pen(HighTempColor, 2), // 发热红色
< 36.0f => new Pen(LowTempColor, 2), // 低体温蓝色
_ => new Pen(NormalTempColor, 2) // 正常绿色
};
}
2. 特殊标记绘制
private void DrawSpecialMarkers(Graphics g)
{
foreach (var record in Records)
{
Point p = CalculatePoint(record);
// 物理降温标记(红色❌)
if (record.IsPhysicalCooling)
{
g.DrawString("❌", Font, Brushes.Red, p.X - 8, p.Y - 8);
}
// 脉搏短绌连线(虚线连接脉搏点)
if (record.Pulse < 60)
{
g.DrawLine(new Pen(Color.Purple, 1) { DashStyle = DashStyle.Dash }, p, previousPulsePoint);
}
}
}
3. 数据绑定与刷新
// 动态更新数据
public void UpdateData(List<TemperatureRecord> newRecords)
{
Records = newRecords;
Invalidate(); // 触发重绘
}
参考代码 医疗体温单线条显示源代码 www.youwenfan.com/contentzhd/111871.html
四、扩展功能
1. 打印支持
public void PrintChart()
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += (sender, e) =>
{
Bitmap bmp = new Bitmap(Width, Height);
DrawToBitmap(bmp, new Rectangle(0, 0, Width, Height));
e.Graphics.DrawImage(bmp, e.MarginBounds);
};
pd.Print();
}
2. 交互功能
// 鼠标悬停显示详细数据
protected override void OnMouseMove(MouseEventArgs e)
{
var record = FindNearestRecord(e.Location);
if (record != null)
{
ShowTooltip($"时间: {record.Time:t}\n体温: {record.Temperature}℃");
}
}
五、使用示例
// 创建控件并配置
var tempChart = new MedicalTemperatureChart();
tempChart.LoadConfig("config.xml");
tempChart.Size = new Size(800, 600);
// 绑定数据
var records = new List<TemperatureRecord>
{
new TemperatureRecord { Time = DateTime.Today.AddHours(8), Temperature = 36.5f },
new TemperatureRecord { Time = DateTime.Today.AddHours(12), Temperature = 37.8f, IsPhysicalCooling = true }
};
tempChart.UpdateData(records);
// 添加到窗体
this.Controls.Add(tempChart);