Skip to content

Commit 9176183

Browse files
committed
Adjustable vector positioning
1 parent fbfd864 commit 9176183

File tree

2 files changed

+56
-53
lines changed

2 files changed

+56
-53
lines changed

Source/Examples/ExampleLibrary/Series/VectorSeriesExamples.cs

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,33 @@ public static class VectorSeriesExamples
1414
[DocumentationExample("Series/VectorSeries")]
1515
public static PlotModel FromItems()
1616
{
17-
const int NumberOfItems = 100;
18-
var model = new PlotModel { Title = "VectorSeries" };
19-
20-
var rand = new Random(1);
21-
var w = 100.0;
22-
var h = 100.0;
23-
var max = 10.0;
24-
25-
model.Axes.Add(new LinearColorAxis
26-
{
27-
Position = AxisPosition.Right,
28-
Palette = new OxyPalette(OxyPalettes.Cool(10).Colors.Select(c => OxyColor.FromAColor(100, c))),
29-
Minimum = 0.0,
30-
Maximum = max,
31-
});
32-
33-
model.Axes.Add(new LinearAxis()
34-
{
35-
Position = AxisPosition.Bottom,
36-
Minimum = -max,
37-
Maximum = w + max,
38-
});
39-
model.Axes.Add(new LinearAxis()
40-
{
41-
Position = AxisPosition.Left,
42-
Minimum = -max,
43-
Maximum = h + max,
44-
});
45-
46-
var s = new VectorSeries() { LabelFontSize = 12 };
47-
for (int i = NumberOfItems - 1; i >= 0; i--)
48-
{
49-
var ang = rand.NextDouble() * Math.PI * 2.0;
50-
var mag = rand.NextDouble() * max;
51-
52-
var origin = new DataPoint(rand.NextDouble() * w, rand.NextDouble() * h);
53-
var direction = new DataVector(Math.Cos(ang) * mag, Math.Sin(ang) * mag);
54-
s.Items.Add(new VectorItem(origin, direction, mag));
55-
}
56-
57-
model.Series.Add(s);
17+
var model = GetModel(out _);
18+
return model;
19+
}
5820

21+
[Example("VectorSeries (Veeness = 2)")]
22+
[DocumentationExample("Series/VectorSeries")]
23+
public static PlotModel FromItemsVeeness()
24+
{
25+
var model = GetModel(out var series);
26+
series.Veeness = 2;
5927
return model;
6028
}
6129

62-
[Example("VectorSeriesV")]
30+
[Example("VectorSeries (Vector Origin and Label position)")]
6331
[DocumentationExample("Series/VectorSeries")]
64-
public static PlotModel FromItemsv()
32+
public static PlotModel FromItemsVectorOriginAndLabelPosition()
33+
{
34+
var model = GetModel(out var series);
35+
series.VectorLabelPosition = 0.25;
36+
series.VectorOriginPosition = 0.5;
37+
return model;
38+
}
39+
40+
private static PlotModel GetModel(out VectorSeries series)
6541
{
6642
const int NumberOfItems = 100;
67-
var model = new PlotModel { Title = "VectorSeries" };
43+
var model = new PlotModel { Title = "VectorSeries (Veeness = 2)" };
6844

6945
var rand = new Random(1);
7046
var w = 100.0;
@@ -92,18 +68,18 @@ public static PlotModel FromItemsv()
9268
Maximum = h + max,
9369
});
9470

95-
var s = new VectorSeries() { LabelFontSize = 12, Veeness = 2 };
71+
series = new VectorSeries() { LabelFontSize = 12, Veeness = 2 };
9672
for (int i = NumberOfItems - 1; i >= 0; i--)
9773
{
9874
var ang = rand.NextDouble() * Math.PI * 2.0;
9975
var mag = rand.NextDouble() * max;
10076

10177
var origin = new DataPoint(rand.NextDouble() * w, rand.NextDouble() * h);
10278
var direction = new DataVector(Math.Cos(ang) * mag, Math.Sin(ang) * mag);
103-
s.Items.Add(new VectorItem(origin, direction, mag));
79+
series.Items.Add(new VectorItem(origin, direction, mag));
10480
}
10581

106-
model.Series.Add(s);
82+
model.Series.Add(series);
10783

10884
return model;
10985
}

Source/OxyPlot/Series/VectorSeries.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class VectorSeries : XYAxisSeries
2626
private List<VectorItem> actualItems;
2727

2828
/// <summary>
29-
/// Specifies if the <see cref="actualItems" /> list can be modified.\
29+
/// Specifies if the <see cref="actualItems" /> list can be modified.
3030
/// </summary>
3131
private bool ownsActualItems;
3232

@@ -53,6 +53,10 @@ public VectorSeries()
5353
this.LineStyle = LineStyle.Solid;
5454
this.LineJoin = LineJoin.Miter;
5555

56+
this.Veeness = 0;
57+
this.VectorOriginPosition = 0;
58+
this.VectorLabelPosition = 0;
59+
5660
this.TrackerFormatString = DefaultTrackerFormatString;
5761
this.LabelFormatString = "0.00";
5862
this.LabelFontSize = 0;
@@ -123,6 +127,25 @@ public VectorSeries()
123127
/// <value>The 'veeness'.</value>
124128
public double Veeness { get; set; }
125129

130+
/// <summary>
131+
/// Gets the position of the origin of the arrow for each vector relative to the length of the vector (the default value is 0).
132+
/// </summary>
133+
/// <remarks>
134+
/// A value of 0 indicates the arrow will start at the <see cref="VectorItem.Origin"/>.
135+
/// A value of 1 indicates the arrow will terminate at the <see cref="VectorItem.Origin"/>.
136+
/// Value between 0 and 1 may produce arrows that do not pass through the <see cref="VectorItem.Origin"/> on non-linear axes.
137+
/// </remarks>
138+
public double VectorOriginPosition { get; set; }
139+
140+
/// <summary>
141+
/// Gets the psoitions of the label for each vector along the vector (the default value is 0).
142+
/// </summary>
143+
/// <remarks>
144+
/// A value of 0 indicates the label will be positioned at the start of the vector arrow.
145+
/// A value of 1 indicates the label will be positioned at the end of the vector arrow.
146+
/// </remarks>
147+
public double VectorLabelPosition { get; set; }
148+
126149
/// <summary>
127150
/// Gets or sets the color axis.
128151
/// </summary>
@@ -287,17 +310,21 @@ protected void RenderVectors(IRenderContext rc, ICollection<VectorItem> items)
287310

288311
vectorColor = this.GetSelectableColor(vectorColor, i);
289312

290-
this.DrawClippedVector(
313+
var vector = item.Direction;
314+
var origin = item.Origin - vector * this.VectorOriginPosition;
315+
var textOrigin = origin + vector * this.VectorLabelPosition;
316+
317+
this.DrawVector(
291318
rc,
292-
item.Origin,
293-
item.Direction,
319+
origin,
320+
vector,
294321
vectorColor
295322
);
296323

297324
if (this.LabelFontSize > 0)
298325
{
299326
rc.DrawText(
300-
this.Transform(item.Origin),
327+
this.Transform(textOrigin),
301328
item.Value.ToString(this.LabelFormatString),
302329
this.ActualTextColor,
303330
this.ActualFont,
@@ -312,7 +339,7 @@ protected void RenderVectors(IRenderContext rc, ICollection<VectorItem> items)
312339
}
313340
}
314341

315-
private void DrawClippedVector(IRenderContext rc, DataPoint point, DataVector vector, OxyColor color)
342+
private void DrawVector(IRenderContext rc, DataPoint point, DataVector vector, OxyColor color)
316343
{
317344
var points = new List<DataPoint>() { point, point + vector };
318345
var screenPoints = new List<ScreenPoint>();

0 commit comments

Comments
 (0)