绘图仪:十字线
 

十字线

十字线打印类型绘制在打印上某一点相交的垂直线和水平线,这些线的坐标显示在轴刻度的顶部。此打印类型通常在MouseMove事件后更新以跟踪鼠标

var plt = new ScottPlot.Plot(600, 400);

plt.AddSignal(ScottPlot.DataGen.Sin(51));
plt.AddSignal(ScottPlot.DataGen.Cos(51));
plt.AddCrosshair(42, 0.48);

plt.Title("Crosshair Demo");
plt.XLabel("Horizontal Axis");
plt.YLabel("Vertical Axis");

plt.SaveFig("crosshair_quickstart.png");
 

十字线定制

可以通过访问公共字段自定义十字线样式和标签格式。

var plt = new ScottPlot.Plot(600, 400);

plt.Title("Customized Crosshair");
plt.XLabel("Horizontal Axis");
plt.YLabel("Vertical Axis");

plt.AddSignal(ScottPlot.DataGen.Sin(51));
plt.AddSignal(ScottPlot.DataGen.Cos(51));

var ch = plt.AddCrosshair(42, 0.48);

// you can style both lines at the same time
ch.LineStyle = LineStyle.Dash;
ch.Color = System.Drawing.Color.Blue;
ch.LineWidth = 2;

// or reach in and style lines individually
ch.HorizontalLine.PositionLabelFont.Size = 16;
ch.VerticalLine.PositionLabelFont.Size = 8;
ch.VerticalLine.LineStyle = LineStyle.Dot;

plt.SaveFig("crosshair_customize.png");
 

日期时间轴标签

默认情况下,十字线标签显示数字标签,但公共字段允许在生成轴标签时将位置转换为日期时间(FromOATime)。

var plt = new ScottPlot.Plot(600, 400);

plt.Title("Crosshair with DateTime Axis");
plt.XLabel("Horizontal Axis");
plt.YLabel("Vertical Axis");

// plot DateTime data
int pointCount = 100;
Random rand = new Random(0);
double[] values = ScottPlot.DataGen.RandomWalk(rand, pointCount);
DateTime[] dates = Enumerable.Range(0, pointCount)
      .Select(x => new DateTime(2016, 06, 27).AddDays(x))
      .ToArray();
double[] xs = dates.Select(x => x.ToOADate()).ToArray();
plt.AddScatter(xs, values);

// add a crosshair
var ch = plt.AddCrosshair(xs[50], values[50]);

// indicaite horizontal axis is DateTime and give a proper DateTime format string
// https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
ch.VerticalLine.PositionFormatter = pos => DateTime.FromOADate(pos).ToString("d");

// use a numeric vertical axis but customize the format string
// https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
ch.VerticalLine.PositionFormatter = pos => pos.ToString("F4");

plt.SaveFig("crosshair_formatting.png");
 

自定义标签格式

对于十字线标签格式的最终控制,您可以创建一个自定义格式化程序函数,并使用该函数将位置转换为标签。这允许使用逻辑来格式化十字线标签。

var plt = new ScottPlot.Plot(600, 400);

plt.AddSignal(ScottPlot.DataGen.Sin(51));
plt.AddSignal(ScottPlot.DataGen.Cos(51));

var ch = plt.AddCrosshair(20, 0);

// create a custom formatter as a static class
static string customFormatter(double position)
{
    if (position == 0)
        return "zero";
    else if (position > 0)
        return $"+{position:F2}";
    else
        return $"({Math.Abs(position):F2})";
}

// use the custom formatter for X and Y crosshair labels
ch.HorizontalLine.PositionFormatter = customFormatter;
ch.VerticalLine.PositionFormatter = customFormatter;

// style the plot
plt.Title("Crosshair with Custom Label Formmater");
plt.XLabel("Horizontal Axis");
plt.YLabel("Vertical Axis");

plt.SaveFig("crosshair_customlabelformatting.png");
 

多轴十字准星

默认情况下,十字光标标记主轴上的坐标,但可以更改轴索引,允许多个十字光标标记多个轴。

var plt = new ScottPlot.Plot(600, 400);

// add a signal and crosshair to the primary X and Y axis (index 0)
var signal1 = plt.AddSignal(DataGen.RandomWalk(null, 100));
var cross1 = plt.AddCrosshair(24, 5.29);
cross1.LineWidth = 2;

// add a signal and crosshair to the secondary X and Y axis (index 1)
var signal2 = plt.AddSignal(DataGen.RandomWalk(null, 50));
signal2.YAxisIndex = 1;
signal2.XAxisIndex = 1;

var cross2 = plt.AddCrosshair(33, 5.1);
cross2.HorizontalLine.PositionLabelOppositeAxis = true;
cross2.VerticalLine.PositionLabelOppositeAxis = true;
cross2.YAxisIndex = signal2.YAxisIndex;
cross2.XAxisIndex = signal2.XAxisIndex;
cross2.LineStyle = LineStyle.Dot;
cross2.LineWidth = 2;

// apply signal colors to the crosshairs
cross1.Color = signal1.Color;
cross2.Color = signal2.Color;

// add axis labels
plt.Title("Multiple Crosshairs for different Axes");
plt.XLabel("Horizontal Axis");
plt.YLabel("Vertical Axis");
plt.YAxis2.Label("Signal2 Value");

// show ticks for axes where ticks are hidden by default
plt.YAxis2.Ticks(true);
plt.XAxis2.Ticks(true);

plt.SaveFig("crosshair_multiple_different_axes.png");