条形图快速入门
条形图可以从一系列值中添加。
// add bars
double[] values = { 5, 10, 7, 13 };
WpfPlot1.Plot.Add.Bars(values);
// tell the plot to autoscale with no padding beneath the bars
WpfPlot1.Plot.Axes.Margins(bottom: 0);
WpfPlot1.Refresh();
条形图图例
条形集合可以作为单个项显示在图例中。
double[] xs1 = { 1, 2, 3, 4 };
double[] ys1 = { 5, 10, 7, 13 };
var bars1 = WpfPlot1.Plot.Add.Bars(xs1, ys1);
bars1.LegendText = "Alpha";
double[] xs2 = { 6, 7, 8, 9 };
double[] ys2 = { 7, 12, 9, 15 };
var bars2 = WpfPlot1.Plot.Add.Bars(xs2, ys2);
bars2.LegendText = "Beta";
WpfPlot1.Plot.ShowLegend(Alignment.UpperLeft);
WpfPlot1.Plot.Axes.Margins(bottom: 0);
WpfPlot1.Refresh();
带值标签的条形图
将 bars 的属性设置为在每个 bar 上方显示文本。
double[] values = { 5, 10, 7, 13 };
var barPlot = WpfPlot1.Plot.Add.Bars(values);
// define the content of labels
foreach (var bar in barPlot.Bars)
{
bar.Label = bar.Value.ToString();
}
// customize label style
barPlot.ValueLabelStyle.Bold = true;
barPlot.ValueLabelStyle.FontSize = 18;
WpfPlot1.Plot.Axes.Margins(bottom: 0, top: .2);
WpfPlot1.Refresh();
带值标签的条形图(水平)
将 bars 的属性设置为在每个条形的旁边(左侧或右侧)显示文本。
double[] values = { -20, 10, 7, 13 };
// set the label for each bar
var barPlot = WpfPlot1.Plot.Add.Bars(values);
foreach (var bar in barPlot.Bars)
{
bar.Label = "Label " + bar.Value.ToString();
}
// customize label style
barPlot.ValueLabelStyle.Bold = true;
barPlot.ValueLabelStyle.FontSize = 18;
barPlot.Horizontal = true;
// add extra margin to account for label
WpfPlot1.Plot.Axes.SetLimitsX(-45, 35);
WpfPlot1.Plot.Add.VerticalLine(0, 1, Colors.Black);
WpfPlot1.Refresh();
棒材定位
每个条形的确切位置和大小可以自定义。
ScottPlot.Bar[] bars =
{
new() { Position = 1, Value = 5, ValueBase = 3, FillColor = Colors.Red },
new() { Position = 2, Value = 7, ValueBase = 0, FillColor = Colors.Blue },
new() { Position = 4, Value = 3, ValueBase = 2, FillColor = Colors.Green },
};
WpfPlot1.Plot.Add.Bars(bars);
WpfPlot1.Refresh();
有误差的柱形
条形可以有误差线。
ScottPlot.Bar[] bars =
{
new() { Position = 1, Value = 5, Error = 1, FillColor = Colors.Red },
new() { Position = 2, Value = 7, Error = 2, FillColor = Colors.Orange },
new() { Position = 3, Value = 6, Error = 1, FillColor = Colors.Green },
new() { Position = 4, Value = 8, Error = 2, FillColor = Colors.Blue },
};
WpfPlot1.Plot.Add.Bars(bars);
// tell the plot to autoscale with no padding beneath the bars
WpfPlot1.Plot.Axes.Margins(bottom: 0);
WpfPlot1.Refresh();
带有标记即时报价的柱形
可以通过手动指定轴刻度线位置和标签来标记条形。
WpfPlot1.Plot.Add.Bar(position: 1, value: 5, error: 1);
WpfPlot1.Plot.Add.Bar(position: 2, value: 7, error: 2);
WpfPlot1.Plot.Add.Bar(position: 3, value: 6, error: 1);
WpfPlot1.Plot.Add.Bar(position: 4, value: 8, error: 2);
Tick[] ticks =
{
new(1, "Apple"),
new(2, "Orange"),
new(3, "Pear"),
new(4, "Banana"),
};
WpfPlot1.Plot.Axes.Bottom.TickGenerator = new ScottPlot.TickGenerators.NumericManual(ticks);
WpfPlot1.Plot.Axes.Bottom.MajorTickStyle.Length = 0;
WpfPlot1.Plot.HideGrid();
// tell the plot to autoscale with no padding beneath the bars
WpfPlot1.Plot.Axes.Margins(bottom: 0);
WpfPlot1.Refresh();
条形填充样式
每个条形都可以单独设置样式。
// add bars with sample data
double[] values = { 3, 7, 9 };
var barPlot = WpfPlot1.Plot.Add.Bars(values);
// bars may be styled after they have been added
barPlot.Bars[0].FillColor = Colors.Orange;
barPlot.Bars[1].FillColor = Colors.Green;
barPlot.Bars[2].FillColor = Colors.Navy;
barPlot.Bars[0].FillHatch = new ScottPlot.Hatches.Striped();
barPlot.Bars[1].FillHatch = new ScottPlot.Hatches.Dots();
barPlot.Bars[2].FillHatch = new ScottPlot.Hatches.Checker();
foreach (var bar in barPlot.Bars)
{
bar.LineWidth = 2;
bar.LineColor = bar.FillColor.Darken(0.5);
bar.FillHatchColor = bar.FillColor.Lighten(0.1);
}
// tell the plot to autoscale with no padding beneath the bars
WpfPlot1.Plot.Axes.Margins(bottom: 0);
WpfPlot1.Refresh();