|
| 1 | +package com.sin.java.plot; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import com.sin.java.plot.model.DrawableObject; |
| 7 | +import com.sin.java.plot.util.XLog; |
| 8 | + |
| 9 | +/*** |
| 10 | + * UI方法,提供图形绘制接口 |
| 11 | + * |
| 12 | + * @author RobinTang |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +final public class Plot { |
| 17 | + private static List<PlotFrame> plotFrameStack = null; |
| 18 | + private static PlotFrame currenPlotFrame = null; |
| 19 | + private static int plotFrameConter = 1; |
| 20 | + private static boolean autoFigure = false; // 自动创建图形窗口 |
| 21 | + |
| 22 | + /** |
| 23 | + * 初始化图形窗口栈 |
| 24 | + */ |
| 25 | + private static void initPlotFrameStack() { |
| 26 | + if (plotFrameStack == null) { |
| 27 | + plotFrameStack = new ArrayList<PlotFrame>(); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * 初始化当前图形窗口 |
| 33 | + */ |
| 34 | + private static void initCurrentPlotFrame() { |
| 35 | + if (currenPlotFrame == null) { |
| 36 | + figrue(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // like matlab |
| 41 | + |
| 42 | + /** |
| 43 | + * 开图形保存 |
| 44 | + */ |
| 45 | + public static void hold_on() { |
| 46 | + initCurrentPlotFrame(); |
| 47 | + currenPlotFrame.setHoldOn(true); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * 关图形保持 |
| 52 | + */ |
| 53 | + public static void hold_off() { |
| 54 | + initCurrentPlotFrame(); |
| 55 | + currenPlotFrame.setHoldOn(false); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * 开自动创建图形窗口 |
| 60 | + */ |
| 61 | + public static void autofigure_on() { |
| 62 | + autoFigure = true; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * 关自动创建图形窗口 |
| 67 | + */ |
| 68 | + public static void autofigure_off() { |
| 69 | + autoFigure = false; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * 打开图形窗口 |
| 74 | + */ |
| 75 | + public static PlotFrame figrue() { |
| 76 | + return figrue(plotFrameConter); |
| 77 | + } |
| 78 | + |
| 79 | + public static PlotFrame figrue(String title) { |
| 80 | + initPlotFrameStack(); |
| 81 | + currenPlotFrame = null; |
| 82 | + for (PlotFrame fm : plotFrameStack) { |
| 83 | + if (title.equals(fm.getTitle())) { |
| 84 | + currenPlotFrame = fm; |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + if (currenPlotFrame == null) |
| 89 | + addPlotFrame(new PlotFrame(title)); |
| 90 | + return currenPlotFrame; |
| 91 | + } |
| 92 | + |
| 93 | + public static PlotFrame figrue(int index) { |
| 94 | + return figrue("Figure " + index); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * 绘图 |
| 99 | + */ |
| 100 | + public static PlotFrame plot(DrawableObject drawableObject) { |
| 101 | + if (autoFigure) |
| 102 | + figrue(); |
| 103 | + else |
| 104 | + initCurrentPlotFrame(); |
| 105 | + currenPlotFrame.plot(drawableObject); |
| 106 | + Plot.suit(); |
| 107 | + return currenPlotFrame; |
| 108 | + } |
| 109 | + |
| 110 | + public static PlotFrame plot(double[] y) { |
| 111 | + return plot(new DrawableObject(y)); |
| 112 | + } |
| 113 | + |
| 114 | + public static PlotFrame plot(double[] x, double[] y) { |
| 115 | + return plot(new DrawableObject(x, y)); |
| 116 | + } |
| 117 | + |
| 118 | + public static PlotFrame plot(double[][] pts) { |
| 119 | + return plot(new DrawableObject(pts[0], pts[1])); |
| 120 | + } |
| 121 | + |
| 122 | + public static PlotFrame plot(double[] x, String pams) { |
| 123 | + return plot(new DrawableObject(x, pams)); |
| 124 | + } |
| 125 | + |
| 126 | + public static PlotFrame plot(double[] x, double[] y, String pams) { |
| 127 | + return plot(new DrawableObject(x, y, pams)); |
| 128 | + } |
| 129 | + |
| 130 | + public static PlotFrame plot(double[][] pts, String pams) { |
| 131 | + return plot(new DrawableObject(pts[0], pts[1], pams)); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * 设置显示范围 |
| 136 | + */ |
| 137 | + public static void axis(double sx, double ex, double sy, double ey) { |
| 138 | + initCurrentPlotFrame(); |
| 139 | + currenPlotFrame.axis(sx, ex, sy, ey); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * 最佳视野 |
| 144 | + */ |
| 145 | + public static void suit() { |
| 146 | + initCurrentPlotFrame(); |
| 147 | + currenPlotFrame.suit(); |
| 148 | + } |
| 149 | + |
| 150 | + // internal |
| 151 | + public static void addPlotFrame(PlotFrame frame) { |
| 152 | + XLog.log("add plotframe %s", frame.getTitle()); |
| 153 | + initPlotFrameStack(); |
| 154 | + ++plotFrameConter; |
| 155 | + plotFrameStack.add(frame); |
| 156 | + currenPlotFrame = frame; |
| 157 | + } |
| 158 | + |
| 159 | + public static boolean removePlotFrame(PlotFrame frame) { |
| 160 | + XLog.log("remove plotframe %s", frame.getTitle()); |
| 161 | + plotFrameStack.remove(frame); |
| 162 | + return plotFrameStack.size() != 0; |
| 163 | + } |
| 164 | +} |
0 commit comments