OxyPlot使用经验
OxyPlot C#中的使用经验
不同坐标轴移动的问题
【解决方法】在坐标轴上点击,移动即可实现。
类似Surfer的彩虹色标
// 色标,图形中的色标,色标条
linearColorAxis = new LinearColorAxis
{
Minimum = minVal,
Maximum = maxVal,
Position = AxisPosition.Right,
Palette = ConstHelper.myRainbow,
InvalidNumberColor = OxyColors.White
};
// 本来在创建色标后,显示完成之后,可以通过 GetPaletteIndex 传入数值,获取到调色板的索引号的。但是还没显示出来,无法这样操作,那么就直接写代码来进行计算即可。
var paletteIndex = 1 + (int)((val - minVal) / (maxVal - minVal)
* linearColorAxis.Palette.Colors.Count);
var color = linearColorAxis.GetColor(paletteIndex);
类似Surfer的彩虹色标
// 彩虹色标
OxyColor[] rainbowColors = new OxyColor[7];
rainbowColors[0] = OxyColor.FromRgb(255, 0, 0); //darkRed
rainbowColors[1] = OxyColor.FromRgb(255, 102, 0); //red
rainbowColors[2] = OxyColor.FromRgb(255, 255, 0);
rainbowColors[3] = OxyColor.FromRgb(0, 255, 0);
rainbowColors[4] = OxyColor.FromRgb(0, 0, 255);
rainbowColors[5] = OxyColor.FromRgb(153, 102, 255);
rainbowColors[6] = OxyColor.FromRgb(153, 102, 255);
// 反向
// rainbowColors = rainbowColors.Reverse().ToArray();
var numberOfColors = 200;
var myRainbow = OxyPalette.Interpolate(
numberOfColors,
rainbowColors);
// oxyplot 绘图色标添加,热力图
var hms = new HeatMapSeries
{
X0 = xvalues.Min(),
X1 = xvalues.Max(),
Y0 = yvalues.Min(),
Y1 = yvalues.Max(),
Data = data,
};
model.Series.Add(hms);
model.Axes.Add(new LinearColorAxis
{
Position = AxisPosition.Right,
Palette = ConstHelper.myRainbow,
InvalidNumberColor = OxyColors.White
});
// 等值线成图效果
var maxData = terrainData.Max2D();
var minData = terrainData.Min2D();
var coutourLevelCount = 12;
// 加上等值线的显示
var cs = new ContourSeries
{
Color = OxyColors.Black,
FontSize = 0,
// 【注意】level 一定要用二维数组中的 最小值 到 最大值
ContourLevels = ArrayBuilder.CreateVector(minData, maxData, coutourLevelCount),
LabelBackground = OxyColors.Undefined,
ColumnCoordinates = terrainXValues,
RowCoordinates = terrainYValues,
Data = terrainData
};
model.Series.Add(cs);
图形注解
点注解:
var pointAnnotation = (new PointAnnotation
{
X = x,
Y = y,
Size = 5,
Fill = OxyColors.Red,
Shape = MarkerType.Triangle,
});
this.plotView1.Model.Annotations.Add(pointAnnotation);
折线注解:
// 线的情况
PolylineAnnotation polylineAnnotation = new PolylineAnnotation { };
/*
// 也可指定 线宽 和颜色, 线型 和 相交 的形状等等
new PolylineAnnotation {
StrokeThickness = 5,
LineStyle = LineStyle.Solid,
LineJoin = LineJoin.Round,
Color = OxyColors.Gold
};
*/
listGridPoint.ForEach(gridPoint =>
{
var x = gridPoint.x;
var y = gridPoint.y;
DataPoint dp = new DataPoint(x, y);
polylineAnnotation.Points.Add(dp);
});
this.plotView1.Model.Annotations.Add(polylineAnnotation);
底图注解:
注意,仅仅只支持加载 png 类型的图片,不支持加载 jpg 的图片,加载这种图片无效。
而且要注意放在线上,否则,等值线图等二维平面图是不会显示的。
Layer = AnnotationLayer.AboveSeries
if (basemapPath == null)
{
return;
}
// 加载底图
byte[] data = File.ReadAllBytes(basemapPath);
OxyImage image = new OxyImage(data);
var opacity = trackOpacity.Value / 100;
var widthUnitCount = maxX - minX;
var heightUnitCount = maxY - minY;
baseImage = new ImageAnnotation
{
Layer = AnnotationLayer.AboveSeries,
ImageSource = image,
Opacity = opacity,
Interpolate = false,
X = new PlotLength(minX, PlotLengthUnit.Data),
Y = new PlotLength(minY, PlotLengthUnit.Data),
Width = new PlotLength(widthUnitCount, PlotLengthUnit.Data),
Height = new PlotLength(heightUnitCount, PlotLengthUnit.Data),
HorizontalAlignment = OxyPlot.HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Bottom
};
plotView.Model.Annotations.Add(baseImage);
plotView.InvalidatePlot(false);
底图透明度的更新
private void trackOpacity_ValueChanged(object sender, EventArgs e)
{
if (baseImage == null)
{
return;
}
var plotView = this.plotView1;
var opacity = trackOpacity.Value / 100.0;
var txt = trackOpacity.Value + "%";
txtOpacity.Text = txt;
baseImage.Opacity = opacity;
plotView.InvalidatePlot(false);
}
热力图四周多余的图形
先指定坐标轴
var yaxis = new LinearAxis { Position = AxisPosition.Left, Minimum = yValues.Min(), Maximum = yValues.Max() };
var xaxis = new LinearAxis { Position = AxisPosition.Bottom, Minimum = xValues.Min(), Maximum = xValues.Max() };
model.Axes.Add(yaxis);
model.Axes.Add(xaxis);
热力图四周多余的图形
在控件内部已经做了边界处理了,所以会出现边界结果
if (this.XAxis.IsLogarithmic())
{
double gx = Math.Log(this.X1 / this.X0) / (m - 1);
left *= Math.Exp(gx / -2);
right *= Math.Exp(gx / 2);
}
else
{
left -= dx / 2;
right += dx / 2;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤