-
Notifications
You must be signed in to change notification settings - Fork 943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PolygonSeries #2033
Draft
VisualMelon
wants to merge
3
commits into
oxyplot:develop
Choose a base branch
from
VisualMelon:polygonseries
base: develop
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add PolygonSeries #2033
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
Source/Examples/ExampleLibrary/Series/PolygonSeriesExamples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace ExampleLibrary.Series | ||
{ | ||
using OxyPlot; | ||
using OxyPlot.Series; | ||
using OxyPlot.Legends; | ||
using OxyPlot.Axes; | ||
|
||
[Examples("PolygonSeries"), Tags("Series")] | ||
public static class PolygonSeriesExamples | ||
{ | ||
[Example("PolygonSeries")] | ||
[DocumentationExample("Series/PolygonSeries")] | ||
public static PlotModel PolygonSeries() | ||
{ | ||
var model = new PlotModel { Title = "Polygon Series", PlotType = PlotType.Cartesian }; | ||
|
||
model.Axes.Add(new LinearColorAxis()); | ||
|
||
var ps = new PolygonSeries(); | ||
var outlines = new List<DataPoint[]>(); | ||
for (int i = 0; i < 5; i++) | ||
{ | ||
ps.Items.Add(new PolygonItem(RegularPolygon(new DataPoint(i * 5, 0), 2, 3 + i), i)); | ||
outlines.Add(RegularPolygon(new DataPoint(i * 5, 5), 2, 3 + i)); | ||
} | ||
ps.Items.Add(new PolygonItem(outlines, 10)); | ||
model.Series.Add(ps); | ||
|
||
return model; | ||
} | ||
|
||
[Example("Hexagonal Grid")] | ||
[DocumentationExample("Series/PolygonSeries")] | ||
public static PlotModel HexGrid() | ||
{ | ||
var model = new PlotModel { Title = "Hexagonal Grid" }; | ||
|
||
model.Axes.Add(new LinearColorAxis() { Position = AxisPosition.Right }); | ||
|
||
double eval(DataPoint p) => Math.Sin(p.X / 5) + Math.Sqrt(p.Y); | ||
|
||
var dim = 1.0; | ||
var w = dim * 2 - Math.Cos(Math.PI / 3) * dim; | ||
var h = Math.Sin(Math.PI / 3) * dim * 2; | ||
|
||
var ps = new PolygonSeries() { Stroke = OxyColors.Black, StrokeThickness = 1 }; | ||
for (int x = 0; x < 40; x++) | ||
{ | ||
for (int y = 0; y < 40; y++) | ||
{ | ||
var oy = (x % 2 == 0) ? 0 : h / 2; | ||
|
||
var p = new DataPoint(x * w, y * h + oy); | ||
|
||
ps.Items.Add(new PolygonItem(RegularPolygon(p, dim, 6), eval(p))); | ||
} | ||
} | ||
model.Series.Add(ps); | ||
|
||
return model; | ||
} | ||
|
||
private static DataPoint[] RegularPolygon(DataPoint center, double dimension, int polyCount) | ||
{ | ||
var res = new DataPoint[polyCount]; | ||
|
||
for (int i = 0; i < res.Length; i++) | ||
{ | ||
var angle = Math.PI * 2 * i / res.Length; | ||
|
||
var x = Math.Cos(angle) * dimension; | ||
var y = Math.Sin(angle) * dimension; | ||
|
||
res[i] = new DataPoint(center.X + x, center.Y + y); | ||
} | ||
|
||
return res; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright file="PolygonItem.cs" company="OxyPlot"> | ||
// Copyright (c) 2014 OxyPlot contributors | ||
// </copyright> | ||
// <summary> | ||
// Represents an item in a PolygonSeries. | ||
// </summary> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
namespace OxyPlot.Series | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
/// <summary> | ||
/// Represents an item in a <see cref="PolygonSeries" />. | ||
/// </summary> | ||
public class PolygonItem : ICodeGenerating | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PolygonItem" /> class. | ||
/// </summary> | ||
/// <param name="outline">The outline of the polygons.</param> | ||
/// <param name="value">The value of the data polygon.</param> | ||
public PolygonItem(IEnumerable<DataPoint> outline, double value) | ||
: this(new[] { outline }, value) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PolygonItem" /> class. | ||
/// </summary> | ||
/// <param name="outlines">The outlines of the sub-polygons.</param> | ||
/// <param name="value">The value of the data polygon.</param> | ||
public PolygonItem(IEnumerable<IEnumerable<DataPoint>> outlines, double value) | ||
{ | ||
if (outlines == null) | ||
{ | ||
throw new ArgumentNullException(nameof(outlines)); | ||
} | ||
|
||
var outlineArrs = outlines.Select(outline => outline.ToArray()).ToArray(); | ||
|
||
foreach (var outline in outlineArrs) | ||
{ | ||
foreach (var p in outline) | ||
{ | ||
if (!p.IsDefined() || (double.IsInfinity(p.X) || double.IsInfinity(p.Y))) | ||
{ | ||
throw new ArgumentException("Outline contains non-finite elements."); | ||
} | ||
} | ||
} | ||
|
||
if (outlineArrs.Length < 1) | ||
{ | ||
throw new ArgumentException($"Outlines does not contain any sub-polygons."); | ||
} | ||
|
||
this.Outlines = outlineArrs; | ||
this.Value = value; | ||
this.Bounds = outlineArrs.Select(outline => ComputeBounds(outline)).ToArray(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the outlines of each sub-polygon in the item. | ||
/// </summary> | ||
/// <value>The outlines of each sub-polygon in the item..</value> | ||
public IReadOnlyList<IReadOnlyList<DataPoint>> Outlines { get; } | ||
|
||
/// <summary> | ||
/// Gets the value of the item. | ||
/// </summary> | ||
/// <value>The value can be used to color-code the Polygon.</value> | ||
public double Value { get; } | ||
|
||
/// <summary> | ||
/// Gets the bounds of the item. | ||
/// </summary> | ||
public IReadOnlyList<OxyRect> Bounds { get; } | ||
|
||
///// <summary> | ||
///// Determines whether the specified point lies within the boundary of the Polygon. | ||
///// </summary> | ||
///// <returns><c>true</c> if the value of the <param name="p"/> parameter is inside the bounds of this instance.</returns> | ||
//public bool Contains(DataPoint p) | ||
//{ | ||
// if (!this.Bounds.Contains(p.X, p.Y)) | ||
// return false; | ||
|
||
// int intersectionCount = 0; | ||
|
||
// DataPoint cur = outline[0]; | ||
// DataPoint prev; | ||
// for (int i = 1; i < outline.Length; i++) | ||
// { | ||
// prev = cur; | ||
// cur = outline[i]; | ||
|
||
// if (cur.Equals(prev)) | ||
// { | ||
// continue; | ||
// } | ||
|
||
// var y0 = Math.Min(cur.Y, prev.Y); | ||
// var y1 = Math.Max(cur.Y, prev.Y); | ||
|
||
// if (y0 > p.Y || y1 <= p.Y) | ||
// continue; | ||
|
||
// var c = (p.Y - prev.Y) / (cur.Y - prev.Y); | ||
// var x = prev.Y + c * (cur.X - prev.Y); | ||
|
||
// if (x <= p.X) | ||
// intersectionCount++; | ||
// } | ||
|
||
// return intersectionCount % 2 == 0; | ||
//} | ||
|
||
/// <summary> | ||
/// Returns C# code that generates this instance. | ||
/// </summary> | ||
/// <returns>The to code.</returns> | ||
public string ToCode() | ||
{ | ||
// TODO: teach FormatConstructor to translate arrays | ||
var outlineCode = string.Format("new DataPoint[][] {0}", string.Join(",", this.Outlines.Select(outline => string.Format("new DataPoint[] {0}", outline.Select(p => p.ToCode()))))); | ||
return CodeGenerator.FormatConstructor(this.GetType(), "{0},{2}", outlineCode, this.Value); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a <see cref="System.String" /> that represents this instance. | ||
/// </summary> | ||
/// <returns>A <see cref="System.String" /> that represents this instance.</returns> | ||
public override string ToString() | ||
{ | ||
var outlineString = string.Join("; ", this.Outlines.Select(outline => string.Join(", ", outline))); | ||
return $"{outlineString} {this.Value}"; | ||
} | ||
|
||
/// <summary> | ||
/// Computes the bounds of the polygon in data space. | ||
/// </summary> | ||
/// <returns></returns> | ||
private static OxyRect ComputeBounds(IReadOnlyList<DataPoint> outline) | ||
{ | ||
var minx = outline[0].X; | ||
var miny = outline[0].Y; | ||
var maxx = outline[0].X; | ||
var maxy = outline[0].Y; | ||
|
||
for (int i = 1; i < outline.Count; i++) | ||
{ | ||
minx = Math.Min(minx, outline[i].X); | ||
miny = Math.Min(miny, outline[i].Y); | ||
maxx = Math.Max(maxx, outline[i].X); | ||
maxy = Math.Max(maxy, outline[i].Y); | ||
} | ||
|
||
return new OxyRect(minx, miny, maxx - minx, maxy - miny); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea where this code came from or if it works for why it's commented out. Probably don't need it at all (tracker is done in screen space).