D3Sharp [Deprecated]
D3.js 的C#实现,使用 .Net Standard 2.1 .
可用
- D3Sharp.QuadTree for d3-quadtree.
- D3Sharp.Force for d3-force.
示例
D3Sharp.QuadTree
定义:
c#public class QuadTree<TData, TNode> where TData : IQuadData where TNode : QuadNode<TData>, new()
默认使用 QuadNode
:
c#... using D3Sharp.QuadTree; public class CustomData : IQuadData { public double X { get; set; } = double.NaN; public double Y { get; set; } = double.NaN; public string Location => X + "," + Y; public int Id { get; set; } } var q = new QuadTree<CustomData, QuadNode<CustomData>>(); q.Extent(0, 0, 2, 2).Add(new CustomData{X=3,Y=1,CustomField="Custom"}); Console.WriteLine($"Bounds = {q.Extents}"); //output: new double[,] { { 0, 0 }, { 4, 4 } } Console.WriteLine($"Node type = {q.Root.GetType().Name}"); //output: QuadNode`1
使用自定义节点:
c#... public class CustomNode<T> : QuadNode<T> where T : CustomData { public int Index { get; set; } public override T Data { get => base.Data; set { base.Data = value; this.Index = value.Id; } } } var datas = new List<CustomData> { new CustomData{X=0,Y=0,Id=0}, new CustomData{X=0.9,Y=0.9,Id=1}, }; var q = new QuadTree<CustomData, CustomNode<CustomData>>(datas); Console.WriteLine($"Bounds = {q.Extents}"); //output: new double[,] { { 0, 0 }, { 1, 1 } } Console.WriteLine($"Node type = {q.Root.GetType().Name}"); //output: CustomNode`1
D3Sharp.Force
定义:
csharppublic class Link public interface INode : QuadTree.IQuadData public abstract class Force<TNode> : IDisposable where TNode : INode public class Simulation<TNode> : IDisposable where TNode : INode
示例:
csharp... using D3Sharp.Force; ... var simulation = new Simulation<Node>(Nodes) .AddForce("Links", new ForceLink<Node,Link>(Links) .AddForce("Centering", new ForceCenter<Node>(300, 300).SetStrength(1)) .AddForce("Collision", new ForceCollide<Node>(50, 0.3, 5) .AddForce("Many-Body", new ForceManyBody<Node>().SetStrength(-100); simulation.Start();
截图: