Skip to content

Commit 88bfe5f

Browse files
authored
VectorSeries (#1672)
1 parent 2c90857 commit 88bfe5f

11 files changed

Lines changed: 1217 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file.
1212
- Support HiDPI for WinForms examples (#1597)
1313
- Border properties on PathAnnotation to match functionality in TextAnnotation (#1900)
1414
- Expanded `IntervalBarSeries` and `TornadoBarSeries` to allow for varied label positions and angles (#2027)
15+
- VectorSeries (#107)
1516

1617
### Changed
1718
- Make consistent BaseValue and BaseLine across BarSeries, LinearBarSeries, and HistogramSeries
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
namespace ExampleLibrary
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using OxyPlot;
7+
using OxyPlot.Axes;
8+
using OxyPlot.Series;
9+
10+
[Examples("VectorSeries"), Tags("Series")]
11+
public static class VectorSeriesExamples
12+
{
13+
private static readonly Func<double, double, double> dpeaksdx = (x, y) =>
14+
-10 * ((1 / 5 - 3 * Math.Pow(x, 2)) * Math.Exp(-Math.Pow(x, 2) - Math.Pow(y, 2)) - 2 * x * (x / 5 - Math.Pow(x, 3) - Math.Pow(y, 5)) * Math.Exp(-Math.Pow(x, 2) - Math.Pow(y, 2))) + 0.6666666666666666 * (1 + x) * Math.Exp(-Math.Pow(1 + x, 2) - Math.Pow(y, 2)) + 3 * (-2 * (1 - x) * Math.Exp(-Math.Pow(x, 2) - Math.Pow(1 + y, 2)) - 2 * x * Math.Pow(1 - x, 2) * Math.Exp(-Math.Pow(x, 2) - Math.Pow(1 + y, 2)));
15+
16+
private static readonly Func<double, double, double> dpeaksdy = (x, y) =>
17+
-10 * (-5 * Math.Pow(y, 4) * Math.Exp(-Math.Pow(x, 2) - Math.Pow(y, 2)) - 2 * y * (x / 5 - Math.Pow(x, 3) - Math.Pow(y, 5)) * Math.Exp(-Math.Pow(x, 2) - Math.Pow(y, 2))) + 0.6666666666666666 * y * Math.Exp(-Math.Pow(1 + x, 2) - Math.Pow(y, 2)) - 6 * Math.Pow(1 - x, 2) * (1 + y) * Math.Exp(-Math.Pow(x, 2) - Math.Pow(1 + y, 2));
18+
19+
[Example("VectorSeries")]
20+
public static PlotModel FromItems()
21+
{
22+
var model = GetModel(true, out _);
23+
return model;
24+
}
25+
26+
[Example("VectorSeries (Veeness = 2)")]
27+
public static PlotModel FromItemsVeeness()
28+
{
29+
var model = GetModel(true, out var series);
30+
series.ArrowVeeness = 2;
31+
return model;
32+
}
33+
34+
[Example("VectorSeries (Vector Origin and Label position)")]
35+
public static PlotModel FromItemsVectorOriginAndLabelPosition()
36+
{
37+
var model = GetModel(true, out var series);
38+
series.ArrowLabelPosition = 0.25;
39+
series.ArrowStartPosition = 0.5;
40+
return model;
41+
}
42+
43+
[Example("VectorSeries (without ColorAxis)")]
44+
public static PlotModel FromItemsWithoutColorAxis()
45+
{
46+
var model = GetModel(false, out _);
47+
return model;
48+
}
49+
50+
[Example("Vector Field")]
51+
[DocumentationExample("Series/VectorSeries")]
52+
public static PlotModel VectorField()
53+
{
54+
var model = new PlotModel { Title = "Peaks (Gradient)" };
55+
var vs = new VectorSeries();
56+
var columnCoordinates = ArrayBuilder.CreateVector(-3, 3, 0.25);
57+
var rowCoordinates = ArrayBuilder.CreateVector(-3.1, 3.1, 0.25);
58+
vs.ArrowVeeness = 1;
59+
vs.ArrowStartPosition = 0.5;
60+
vs.ItemsSource = columnCoordinates.SelectMany(x => rowCoordinates.Select(y => new VectorItem(new DataPoint(x, y), new DataVector(dpeaksdx(x, y) / 40, dpeaksdy(x, y) / 40), double.NaN))).ToList();
61+
model.Series.Add(vs);
62+
return model;
63+
}
64+
65+
private static PlotModel GetModel(bool includeColorAxis, out VectorSeries series)
66+
{
67+
const int NumberOfItems = 100;
68+
var model = new PlotModel { Title = "VectorSeries (Veeness = 2)" };
69+
70+
var rand = new Random(1);
71+
var w = 100.0;
72+
var h = 100.0;
73+
var max = 10.0;
74+
75+
if (includeColorAxis)
76+
{
77+
model.Axes.Add(new LinearColorAxis
78+
{
79+
Position = AxisPosition.Right,
80+
Palette = new OxyPalette(OxyPalettes.Cool(10).Colors.Select(c => OxyColor.FromAColor(100, c))),
81+
Minimum = 0.0,
82+
Maximum = max,
83+
});
84+
}
85+
86+
model.Axes.Add(new LinearAxis()
87+
{
88+
Position = AxisPosition.Bottom,
89+
Minimum = -max,
90+
Maximum = w + max,
91+
});
92+
93+
model.Axes.Add(new LinearAxis()
94+
{
95+
Position = AxisPosition.Left,
96+
Minimum = -max,
97+
Maximum = h + max,
98+
});
99+
100+
series = new VectorSeries() { LabelFontSize = 12 };
101+
for (int i = NumberOfItems - 1; i >= 0; i--)
102+
{
103+
var ang = rand.NextDouble() * Math.PI * 2.0;
104+
var mag = rand.NextDouble() * max;
105+
106+
var origin = new DataPoint(rand.NextDouble() * w, rand.NextDouble() * h);
107+
var direction = new DataVector(Math.Cos(ang) * mag, Math.Sin(ang) * mag);
108+
series.Items.Add(new VectorItem(origin, direction, mag));
109+
}
110+
111+
model.Series.Add(series);
112+
113+
return model;
114+
}
115+
116+
[Example("VectorSeries on Log Axis")]
117+
[DocumentationExample("Series/VectorSeries")]
118+
public static PlotModel LogarithmicYAxis()
119+
{
120+
const int NumberOfItems = 100;
121+
var model = new PlotModel { Title = "VectorSeries" };
122+
123+
var rand = new Random(1);
124+
var w = 100.0;
125+
var h = 100.0;
126+
var max = 50.0;
127+
128+
model.Axes.Add(new LinearColorAxis
129+
{
130+
Position = AxisPosition.Right,
131+
Palette = OxyPalettes.Cool(10),
132+
Minimum = 0.0,
133+
Maximum = max,
134+
});
135+
136+
model.Axes.Add(new LinearAxis()
137+
{
138+
Position = AxisPosition.Bottom,
139+
Minimum = -max,
140+
Maximum = w + max,
141+
});
142+
model.Axes.Add(new LogarithmicAxis()
143+
{
144+
Position = AxisPosition.Left,
145+
Minimum = 1,
146+
Maximum = h + max,
147+
});
148+
149+
var s = new VectorSeries() { LabelFontSize = 12 };
150+
for (int i = NumberOfItems - 1; i >= 0; i--)
151+
{
152+
var ang = rand.NextDouble() * Math.PI * 2.0;
153+
var mag = rand.NextDouble() * max;
154+
155+
var origin = new DataPoint(rand.NextDouble() * w, rand.NextDouble() * h + 1);
156+
var direction = new DataVector(Math.Cos(ang) * mag, Math.Sin(ang) * mag);
157+
s.Items.Add(new VectorItem(origin, direction, mag));
158+
}
159+
160+
model.Series.Add(s);
161+
162+
return model;
163+
}
164+
}
165+
}

Source/OxyPlot/Foundation/DataPoint.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public string ToCode()
8484
}
8585

8686
/// <summary>
87-
/// Determines whether this instance and another specified <see cref="T:DataPoint" /> object have the same value.
87+
/// Determines whether this and another specified <see cref="T:DataPoint" /> have the same value.
8888
/// </summary>
8989
/// <param name="other">The point to compare to this instance.</param>
9090
/// <returns><c>true</c> if the value of the <paramref name="other" /> parameter is the same as the value of this instance; otherwise, <c>false</c>.</returns>
@@ -93,6 +93,41 @@ public bool Equals(DataPoint other)
9393
return this.x.Equals(other.x) && this.y.Equals(other.y);
9494
}
9595

96+
/// <summary>
97+
/// Translates a <see cref="DataPoint" /> by a <see cref="DataVector" />.
98+
/// </summary>
99+
/// <param name="p1">The point.</param>
100+
/// <param name="p2">The vector.</param>
101+
/// <returns>The translated point.</returns>
102+
public static DataPoint operator +(DataPoint p1, DataVector p2)
103+
{
104+
return new DataPoint(p1.x + p2.x, p1.y + p2.y);
105+
}
106+
107+
/// <summary>
108+
/// Subtracts a <see cref="DataPoint" /> from a <see cref="DataPoint" />
109+
/// and returns the result as a <see cref="DataVector" />.
110+
/// </summary>
111+
/// <param name="p1">The point on which to perform the subtraction.</param>
112+
/// <param name="p2">The point to subtract from p1.</param>
113+
/// <returns>A <see cref="DataVector" /> structure that represents the difference between p1 and p2.</returns>
114+
public static DataVector operator -(DataPoint p1, DataPoint p2)
115+
{
116+
return new DataVector(p1.x - p2.x, p1.y - p2.y);
117+
}
118+
119+
/// <summary>
120+
/// Subtracts a <see cref="DataVector" /> from a <see cref="DataPoint" />
121+
/// and returns the result as a <see cref="DataPoint" />.
122+
/// </summary>
123+
/// <param name="point">The point on which to perform the subtraction.</param>
124+
/// <param name="vector">The vector to subtract from p1.</param>
125+
/// <returns>A <see cref="DataPoint" /> that represents point translated by the negative vector.</returns>
126+
public static DataPoint operator -(DataPoint point, DataVector vector)
127+
{
128+
return new DataPoint(point.x - vector.x, point.y - vector.y);
129+
}
130+
96131
/// <summary>
97132
/// Returns a <see cref="System.String" /> that represents this instance.
98133
/// </summary>

0 commit comments

Comments
 (0)