Skip to content
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
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file.
- Border properties on PathAnnotation to match functionality in TextAnnotation (#1900)
- Expanded `IntervalBarSeries` and `TornadoBarSeries` to allow for varied label positions and angles (#2027)
- VectorSeries (#107)
- PolygonSeries (#1751)

### Changed
- Make consistent BaseValue and BaseLine across BarSeries, LinearBarSeries, and HistogramSeries
Expand Down
84 changes: 84 additions & 0 deletions Source/Examples/ExampleLibrary/Series/PolygonSeriesExamples.cs
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;
}
}
}
165 changes: 165 additions & 0 deletions Source/OxyPlot/Series/PolygonItem.cs
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;
//}
Comment on lines +94 to +120
Copy link
Contributor Author

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).


/// <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);
}
}
}