1 /// 2 module plot2d.axis; 3 4 import plot2d.drawable; 5 6 /// 7 class Axis : Drawable, Stylized 8 { 9 mixin StylizedHelper!"axis"; 10 11 bool vertical = true; 12 bool horisontal = true; 13 14 double[] dash = []; 15 double dashOffset = 0.0; 16 17 /// 18 this(Style root) { setRootStyle(root); } 19 20 override void draw(Ctx cr, Trtor tr) 21 { 22 setupStyle(cr); 23 drawLines(cr, tr); 24 cr.stroke(); 25 } 26 27 /// 28 void setupStyle(Ctx cr) 29 { 30 cr.setLineWidth(style.number.get("linewidth", 1)); 31 cr.setColor(style.color.get("color", Color.mono(0, 0.8))); 32 cr.setDash(dash, dashOffset); 33 } 34 35 abstract void drawLines(Ctx, Trtor); 36 } 37 38 /// 39 class LBAxis : Axis 40 { 41 /// 42 this(Style root) { super(root); } 43 44 override void drawLines(Ctx cr, Trtor tr) 45 { 46 auto im = tr.inMargin; 47 48 if (vertical) cr.lineP2P(im.lt, im.lb); 49 if (horisontal) cr.lineP2P(im.lb, im.rb); 50 } 51 } 52 53 /// 54 class ZeroAxis : Axis 55 { 56 /// 57 this(Style root) { super(root); } 58 59 override void drawLines(Ctx cr, Trtor tr) 60 { 61 auto im = tr.inMargin; 62 auto z = tr.toDA(0,0); 63 if (vertical) cr.lineP2P(z.x, im.h.min, z.x, im.h.max); 64 if (horisontal) cr.lineP2P(im.w.min, z.y, im.w.max, z.y); 65 } 66 } 67 68 /// 69 class Grid : Axis 70 { 71 /// 72 this(Style root) { super(root); } 73 74 override string styleName() @property { return "grid"; } 75 76 override void drawLines(Ctx cr, Trtor tr) 77 { 78 auto im = tr.inMargin; 79 auto s = tr.gridStep; 80 auto o = tr.gridOffset; 81 82 if (vertical) 83 for (double i = im.w.min + o.x; i <= im.w.max; i += s.x) 84 cr.lineP2P(i, im.h.max, i, im.h.min); 85 86 if (horisontal) 87 for (double i = im.h.max - o.y; i > im.h.min; i -= s.y) 88 cr.lineP2P(im.w.min, i, im.w.max, i); 89 } 90 }