| 1 |
using System; |
|---|
| 2 |
using System.Collections; |
|---|
| 3 |
using System.Collections.Generic; |
|---|
| 4 |
using System.Text; |
|---|
| 5 |
using System.Drawing; |
|---|
| 6 |
|
|---|
| 7 |
namespace Pybrary.Plot |
|---|
| 8 |
{ |
|---|
| 9 |
public delegate void AxisCollectionChangedHandler(); |
|---|
| 10 |
|
|---|
| 11 |
public class AxisCollection : IEnumerable<KeyValuePair<string, NumericYAxis>> |
|---|
| 12 |
{ |
|---|
| 13 |
public event AxisCollectionChangedHandler OnAxisCollectionChanged; |
|---|
| 14 |
|
|---|
| 15 |
private Dictionary<string, NumericYAxis> axesByName = new Dictionary<string, NumericYAxis>(); |
|---|
| 16 |
private List<NumericYAxis> leftAxes = new List<NumericYAxis>(); |
|---|
| 17 |
private List<NumericYAxis> rightAxes = new List<NumericYAxis>(); |
|---|
| 18 |
private float axisSpacing = 0.1f; // spacing between multiple axis on one side, in inches |
|---|
| 19 |
|
|---|
| 20 |
private Plot parent; |
|---|
| 21 |
private float[] axisWidthLeft; |
|---|
| 22 |
private float[] axisWidthRight; |
|---|
| 23 |
|
|---|
| 24 |
public AxisCollection(Plot parent) |
|---|
| 25 |
{ |
|---|
| 26 |
this.parent = parent; |
|---|
| 27 |
} |
|---|
| 28 |
|
|---|
| 29 |
public NumericAxis this[string name] |
|---|
| 30 |
{ |
|---|
| 31 |
get |
|---|
| 32 |
{ |
|---|
| 33 |
return axesByName[name]; |
|---|
| 34 |
} |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
public NumericYAxis AddLeft(string name) |
|---|
| 38 |
{ |
|---|
| 39 |
NumericYAxis axis = new NumericYAxis(name, parent.Series, false, parent.BorderPen); |
|---|
| 40 |
axis.OnChanged += delegate(object sender, EventArgs args) { raiseChanged(); }; |
|---|
| 41 |
leftAxes.Add(axis); |
|---|
| 42 |
axesByName[axis.Name] = axis; |
|---|
| 43 |
return axis; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
public NumericYAxis AddRight(string name) |
|---|
| 47 |
{ |
|---|
| 48 |
NumericYAxis axis = new NumericYAxis(name, parent.Series, true, parent.BorderPen); |
|---|
| 49 |
axis.OnChanged += delegate(object sender, EventArgs args) { raiseChanged(); }; |
|---|
| 50 |
rightAxes.Add(axis); |
|---|
| 51 |
axesByName[axis.Name] = axis; |
|---|
| 52 |
return axis; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
public float CalculateWidthLeft(Graphics g) |
|---|
| 56 |
{ |
|---|
| 57 |
return calculateWidth(g, leftAxes, ref axisWidthLeft); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
public float CalculateWidthRight(Graphics g) |
|---|
| 61 |
{ |
|---|
| 62 |
return calculateWidth(g, rightAxes, ref axisWidthRight); |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
private float calculateWidth(Graphics g, List<NumericYAxis> axes, ref float[] axisWidth) |
|---|
| 66 |
{ |
|---|
| 67 |
axisWidth = new float[axes.Count]; |
|---|
| 68 |
float total = 0; |
|---|
| 69 |
for (int i = 0; i < axes.Count; i++) |
|---|
| 70 |
{ |
|---|
| 71 |
float f; |
|---|
| 72 |
if (axes[i].Visible) |
|---|
| 73 |
f = axes[i].CalculateWidth(g); |
|---|
| 74 |
else |
|---|
| 75 |
f = 0; |
|---|
| 76 |
axisWidth[i] = f; |
|---|
| 77 |
total += f; |
|---|
| 78 |
} |
|---|
| 79 |
// Add spacing between multiple axes |
|---|
| 80 |
if (axes.Count != 0) |
|---|
| 81 |
total += axisSpacing * (axes.Count - 1); |
|---|
| 82 |
return total; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
public void Draw(Graphics g, AdvancedRect dataArea) |
|---|
| 86 |
{ |
|---|
| 87 |
AdvancedRect area = dataArea.Clone(); |
|---|
| 88 |
for (int i = 0; i < leftAxes.Count; i++) |
|---|
| 89 |
{ |
|---|
| 90 |
if (!leftAxes[i].Visible) |
|---|
| 91 |
continue; |
|---|
| 92 |
area.BottomRight.X = area.TopLeft.X; |
|---|
| 93 |
area.TopLeft.X -= axisWidthLeft[i]; |
|---|
| 94 |
leftAxes[i].DrawY(g, area, dataArea); |
|---|
| 95 |
area.TopLeft.X -= axisSpacing; |
|---|
| 96 |
} |
|---|
| 97 |
area = dataArea.Clone(); |
|---|
| 98 |
for (int i = 0; i < rightAxes.Count; i++) |
|---|
| 99 |
{ |
|---|
| 100 |
if (!rightAxes[i].Visible) |
|---|
| 101 |
continue; |
|---|
| 102 |
area.TopLeft.X = area.BottomRight.X; |
|---|
| 103 |
area.BottomRight.X += axisWidthRight[i]; |
|---|
| 104 |
rightAxes[i].DrawY(g, area, dataArea); |
|---|
| 105 |
area.BottomRight.X += axisSpacing; |
|---|
| 106 |
} |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
public YAxis FindAxisAt(PointF pt) |
|---|
| 110 |
{ |
|---|
| 111 |
for (int i = 0; i < leftAxes.Count; i++) |
|---|
| 112 |
if (leftAxes[i].DrawArea.HasValue && leftAxes[i].DrawArea.Value.Rect.Contains(pt)) |
|---|
| 113 |
return leftAxes[i]; |
|---|
| 114 |
for (int i = 0; i < rightAxes.Count; i++) |
|---|
| 115 |
if (rightAxes[i].DrawArea.HasValue && rightAxes[i].DrawArea.Value.Rect.Contains(pt)) |
|---|
| 116 |
return rightAxes[i]; |
|---|
| 117 |
return null; |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
public IEnumerator<KeyValuePair<string, NumericYAxis>> GetEnumerator() |
|---|
| 121 |
{ |
|---|
| 122 |
return axesByName.GetEnumerator(); |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
IEnumerator IEnumerable.GetEnumerator() |
|---|
| 126 |
{ |
|---|
| 127 |
return axesByName.GetEnumerator(); |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
private void raiseChanged() |
|---|
| 131 |
{ |
|---|
| 132 |
AxisCollectionChangedHandler tmp = OnAxisCollectionChanged; |
|---|
| 133 |
if (tmp != null) |
|---|
| 134 |
tmp(); |
|---|
| 135 |
} |
|---|
| 136 |
} |
|---|
| 137 |
} |
|---|