001: // ymageChart.java
002: // ---------------------------
003: // (C) by Michael Peter Christen; mc@anomic.de
004: // first published on http://www.anomic.de
005: // Frankfurt, Germany, 2005
006: // created: 26.10.2005
007: //
008: // This program is free software; you can redistribute it and/or modify
009: // it under the terms of the GNU General Public License as published by
010: // the Free Software Foundation; either version 2 of the License, or
011: // (at your option) any later version.
012: //
013: // This program is distributed in the hope that it will be useful,
014: // but WITHOUT ANY WARRANTY; without even the implied warranty of
015: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: // GNU General Public License for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // along with this program; if not, write to the Free Software
020: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: //
022: // Using this software in any meaning (reading, learning, copying, compiling,
023: // running) means that you agree that the Author(s) is (are) not responsible
024: // for cost, loss of data or any harm that may be caused directly or indirectly
025: // by usage of this softare or this documentation. The usage of this software
026: // is on your own risk. The installation and usage (starting/running) of this
027: // software may allow other people or application to access your computer and
028: // any attached devices and is highly dependent on the configuration of the
029: // software which must be done by the user of the software; the author(s) is
030: // (are) also not responsible for proper configuration and usage of the
031: // software, even if provoked by documentation provided together with
032: // the software.
033: //
034: // Any changes to this file according to the GPL as documented in the file
035: // gpl.txt aside this file in the shipment you received can be done to the
036: // lines that follows this copyright notice here, but changes must not be
037: // done inside the copyright notive above. A re-distribution must contain
038: // the intact and unchanged copyright notice.
039: // Contributions and changes to the program code must be marked as such.
040:
041: package de.anomic.ymage;
042:
043: import java.io.File;
044: import java.io.FileOutputStream;
045: import java.io.IOException;
046:
047: import javax.imageio.ImageIO;
048:
049: public class ymageChart extends ymageMatrix {
050:
051: public static final int DIMENSION_RIGHT = 0;
052: public static final int DIMENSION_TOP = 1;
053: public static final int DIMENSION_LEFT = 2;
054: public static final int DIMENSION_BOTTOM = 3;
055:
056: int leftborder;
057: int rightborder;
058: int topborder;
059: int bottomborder;
060: int[] scales = new int[] { 0, 0, 0, 0 };
061: int[] pixels = new int[] { 0, 0, 0, 0 };
062: int[] offsets = new int[] { 0, 0, 0, 0 };
063: String[] colnames = new String[] { "FFFFFF", "FFFFFF", "FFFFFF",
064: "FFFFFF" };
065: String[] colscale = new String[] { null, null, null, null };
066: String[] tablenames = new String[] { "", "", "", "" };
067: String name;
068: String backgroundColor, foregroundColor;
069:
070: public ymageChart(int width, int height, String backgroundColor,
071: String foregroundColor, int leftborder, int rightborder,
072: int topborder, int bottomborder, String name) {
073: super (width, height, ymageMatrix.MODE_REPLACE, backgroundColor);
074: this .leftborder = leftborder;
075: this .rightborder = rightborder;
076: this .topborder = topborder;
077: this .bottomborder = bottomborder;
078: this .name = name;
079: this .backgroundColor = backgroundColor;
080: this .foregroundColor = foregroundColor;
081: if (name != null) {
082: this .setColor(foregroundColor);
083: ymageToolPrint.print(this , width / 2 - name.length() * 3,
084: 6, 0, name, -1);
085: }
086: }
087:
088: public void declareDimension(int dimensionType, int scale,
089: int pixelperscale, int offset, String colorNaming,
090: String colorScale, String name) {
091: if ((dimensionType == DIMENSION_LEFT)
092: || (dimensionType == DIMENSION_RIGHT)) {
093: drawVerticalScale((dimensionType == DIMENSION_LEFT), scale,
094: pixelperscale, offset, colorNaming, colorScale,
095: name);
096: }
097: if ((dimensionType == DIMENSION_TOP)
098: || (dimensionType == DIMENSION_BOTTOM)) {
099: drawHorizontalScale((dimensionType == DIMENSION_TOP),
100: scale, pixelperscale, offset, colorNaming,
101: colorScale, name);
102: }
103: scales[dimensionType] = scale;
104: pixels[dimensionType] = pixelperscale;
105: offsets[dimensionType] = offset;
106: colnames[dimensionType] = colorNaming;
107: colscale[dimensionType] = colorScale;
108: tablenames[dimensionType] = name;
109: }
110:
111: public void chartDot(int dimension_x, int dimension_y, int coord_x,
112: int coord_y, int dotsize) {
113: int x = (coord_x - offsets[dimension_x]) * pixels[dimension_x]
114: / scales[dimension_x];
115: int y = (coord_y - offsets[dimension_y]) * pixels[dimension_y]
116: / scales[dimension_y];
117: if (dotsize == 1)
118: plot(leftborder + x, height - bottomborder - y);
119: else
120: dot(leftborder + x, height - bottomborder - y, dotsize,
121: true);
122: }
123:
124: public void chartLine(int dimension_x, int dimension_y,
125: int coord_x1, int coord_y1, int coord_x2, int coord_y2) {
126: int x1 = (coord_x1 - offsets[dimension_x])
127: * pixels[dimension_x] / scales[dimension_x];
128: int y1 = (coord_y1 - offsets[dimension_y])
129: * pixels[dimension_y] / scales[dimension_y];
130: int x2 = (coord_x2 - offsets[dimension_x])
131: * pixels[dimension_x] / scales[dimension_x];
132: int y2 = (coord_y2 - offsets[dimension_y])
133: * pixels[dimension_y] / scales[dimension_y];
134: line(leftborder + x1, height - bottomborder - y1, leftborder
135: + x2, height - bottomborder - y2);
136: }
137:
138: private void drawHorizontalScale(boolean top, int scale,
139: int pixelperscale, int offset, String colorNaming,
140: String colorScale, String name) {
141: int y = (top) ? topborder : height - bottomborder;
142: int x = leftborder;
143: int s = offset;
144: while (x < width - rightborder) {
145: if ((colorScale != null) && (x > leftborder)
146: && (x < (width - rightborder))) {
147: setColor(colorScale);
148: line(x, topborder, x, height - bottomborder);
149: }
150: setColor(colorNaming);
151: line(x, y - 3, x, y + 3);
152: ymageToolPrint.print(this , x, (top) ? y - 3 : y + 9, 0,
153: Integer.toString(s), -1);
154: x += pixelperscale;
155: s += scale;
156: }
157: setColor(colorNaming);
158: ymageToolPrint.print(this , width - rightborder, (top) ? y - 9
159: : y + 15, 0, name, 1);
160: line(leftborder - 4, y, width - rightborder + 4, y);
161: }
162:
163: private void drawVerticalScale(boolean left, int scale,
164: int pixelperscale, int offset, String colorNaming,
165: String colorScale, String name) {
166: int x = (left) ? leftborder : width - rightborder;
167: int y = height - bottomborder;
168: int s = offset;
169: String s1;
170: int s1max = 0;
171: while (y > topborder) {
172: if ((colorScale != null) && (y > topborder)
173: && (y < (height - bottomborder))) {
174: setColor(colorScale);
175: line(leftborder, y, width - rightborder, y);
176: }
177: setColor(colorNaming);
178: line(x - 3, y, x + 3, y);
179: s1 = Integer.toString(s);
180: if (s1.length() > s1max)
181: s1max = s1.length();
182: ymageToolPrint.print(this , (left) ? leftborder - 4 : width
183: - rightborder + 4, y, 0, s1, (left) ? 1 : -1);
184: y -= pixelperscale;
185: s += scale;
186: }
187: setColor(colorNaming);
188: ymageToolPrint.print(this , (left) ? x - s1max * 6 - 6 : x
189: + s1max * 6 + 9, topborder, 90, name, 1);
190: line(x, topborder - 4, x, height - bottomborder + 4);
191: }
192:
193: public static void main(String[] args) {
194: System.setProperty("java.awt.headless", "true");
195: boolean invers = false;
196: String bg = (invers) ? "000000" : "FFFFFF";
197: String fg = (invers) ? "FFFFFF" : "000000";
198: String scale = (invers) ? "333333" : "CCCCCC";
199: String green = (invers) ? "008800" : "008800";
200: String blue = (invers) ? "0000FF" : "0000FF";
201: ymageChart ip = new ymageChart(660, 240, bg, fg, 30, 30, 20,
202: 20,
203: "PEER PERFORMANCE GRAPH: PAGES/MINUTE and USED MEMORY");
204: ip.declareDimension(DIMENSION_BOTTOM, 60, 60, -600, fg, scale,
205: "TIME/SECONDS");
206: //ip.declareDimension(DIMENSION_TOP, 10, 40, "000000", null, "count");
207: ip.declareDimension(DIMENSION_LEFT, 50, 40, 0, green, scale,
208: "PPM [PAGES/MINUTE]");
209: ip.declareDimension(DIMENSION_RIGHT, 100, 20, 0, blue, scale,
210: "MEMORY/MEGABYTE");
211: ip.setColor(green);
212: ip.chartDot(DIMENSION_BOTTOM, DIMENSION_LEFT, -160, 100, 5);
213: ip.chartLine(DIMENSION_BOTTOM, DIMENSION_LEFT, -160, 100, -130,
214: 200);
215: ip.setColor(blue);
216: ip.chartDot(DIMENSION_BOTTOM, DIMENSION_RIGHT, -50, 300, 2);
217: ip.chartLine(DIMENSION_BOTTOM, DIMENSION_RIGHT, -80, 100, -50,
218: 300);
219: //ip.print(100, 100, 0, "TEXT", true);
220: //ip.print(100, 100, 0, "1234", false);
221: //ip.print(100, 100, 90, "TEXT", true);
222: //ip.print(100, 100, 90, "1234", false);
223: File file = new File("/Users/admin/Desktop/testimage.png");
224: try {
225: FileOutputStream fos = new FileOutputStream(file);
226: ImageIO.write(ip.getImage(), "png", fos);
227: fos.close();
228: } catch (IOException e) {
229: }
230:
231: }
232:
233: }
|