|
| 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 | +} |
0 commit comments