⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rrdgraph.java

📁 jrobin,使用纯java实现的RRD数据库,使用RRD数据库来统计数据.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			im.xgif = PADDING_RIGHT + im.xsize + im.xorigin;
			im.ygif = im.yorigin + (int) (PADDING_PLOT * getSmallFontHeight());
		}
	}

	private void removeOutOfRangeRules() {
		for (PlotElement plotElement : gdef.plotElements) {
			if (plotElement instanceof HRule) {
				((HRule) plotElement).setLegendVisibility(im.minval, im.maxval, gdef.forceRulesLegend);
			}
			else if (plotElement instanceof VRule) {
				((VRule) plotElement).setLegendVisibility(im.start, im.end, gdef.forceRulesLegend);
			}
		}
	}

	private void expandValueRange() {
		im.ygridstep = (gdef.valueAxisSetting != null) ? gdef.valueAxisSetting.gridStep : Double.NaN;
		im.ylabfact = (gdef.valueAxisSetting != null) ? gdef.valueAxisSetting.labelFactor : 0;
		if (!gdef.rigid && !gdef.logarithmic) {
			double sensiblevalues[] = {
					1000.0, 900.0, 800.0, 750.0, 700.0, 600.0, 500.0, 400.0, 300.0, 250.0, 200.0, 125.0, 100.0,
					90.0, 80.0, 75.0, 70.0, 60.0, 50.0, 40.0, 30.0, 25.0, 20.0, 10.0,
					9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.5, 3.0, 2.5, 2.0, 1.8, 1.5, 1.2, 1.0,
					0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0, -1
			};
			double scaled_min, scaled_max, adj;
			if (Double.isNaN(im.ygridstep)) {
				if (gdef.altYMrtg) { /* mrtg */
					im.decimals = Math.ceil(Math.log10(Math.max(Math.abs(im.maxval), Math.abs(im.minval))));
					im.quadrant = 0;
					if (im.minval < 0) {
						im.quadrant = 2;
						if (im.maxval <= 0) {
							im.quadrant = 4;
						}
					}
					switch (im.quadrant) {
						case 2:
							im.scaledstep = Math.ceil(50 * Math.pow(10, -(im.decimals)) * Math.max(Math.abs(im.maxval),
									Math.abs(im.minval))) * Math.pow(10, im.decimals - 2);
							scaled_min = -2 * im.scaledstep;
							scaled_max = 2 * im.scaledstep;
							break;
						case 4:
							im.scaledstep = Math.ceil(25 * Math.pow(10,
									-(im.decimals)) * Math.abs(im.minval)) * Math.pow(10, im.decimals - 2);
							scaled_min = -4 * im.scaledstep;
							scaled_max = 0;
							break;
						default: /* quadrant 0 */
							im.scaledstep = Math.ceil(25 * Math.pow(10, -(im.decimals)) * im.maxval) *
									Math.pow(10, im.decimals - 2);
							scaled_min = 0;
							scaled_max = 4 * im.scaledstep;
							break;
					}
					im.minval = scaled_min;
					im.maxval = scaled_max;
				}
				else if (gdef.altAutoscale) {
					/* measure the amplitude of the function. Make sure that
					   graph boundaries are slightly higher then max/min vals
					   so we can see amplitude on the graph */
					double delt, fact;

					delt = im.maxval - im.minval;
					adj = delt * 0.1;
					fact = 2.0 * Math.pow(10.0,
							Math.floor(Math.log10(Math.max(Math.abs(im.minval), Math.abs(im.maxval)))) - 2);
					if (delt < fact) {
						adj = (fact - delt) * 0.55;
					}
					im.minval -= adj;
					im.maxval += adj;
				}
				else if (gdef.altAutoscaleMax) {
					/* measure the amplitude of the function. Make sure that
					   graph boundaries are slightly higher than max vals
					   so we can see amplitude on the graph */
					adj = (im.maxval - im.minval) * 0.1;
					im.maxval += adj;
				}
				else {
					scaled_min = im.minval / im.magfact;
					scaled_max = im.maxval / im.magfact;
					for (int i = 1; sensiblevalues[i] > 0; i++) {
						if (sensiblevalues[i - 1] >= scaled_min && sensiblevalues[i] <= scaled_min) {
							im.minval = sensiblevalues[i] * im.magfact;
						}
						if (-sensiblevalues[i - 1] <= scaled_min && -sensiblevalues[i] >= scaled_min) {
							im.minval = -sensiblevalues[i - 1] * im.magfact;
						}
						if (sensiblevalues[i - 1] >= scaled_max && sensiblevalues[i] <= scaled_max) {
							im.maxval = sensiblevalues[i - 1] * im.magfact;
						}
						if (-sensiblevalues[i - 1] <= scaled_max && -sensiblevalues[i] >= scaled_max) {
							im.maxval = -sensiblevalues[i] * im.magfact;
						}
					}
				}
			}
			else {
				im.minval = (double) im.ylabfact * im.ygridstep *
						Math.floor(im.minval / ((double) im.ylabfact * im.ygridstep));
				im.maxval = (double) im.ylabfact * im.ygridstep *
						Math.ceil(im.maxval / ((double) im.ylabfact * im.ygridstep));
			}

		}
	}

	private void identifySiUnit() {
		im.unitsexponent = gdef.unitsExponent;
		im.base = gdef.base;
		if (!gdef.logarithmic) {
			final char symbol[] = {'a', 'f', 'p', 'n', 'u', 'm', ' ', 'k', 'M', 'G', 'T', 'P', 'E'};
			int symbcenter = 6;
			double digits;
			if (im.unitsexponent != Integer.MAX_VALUE) {
				digits = Math.floor(im.unitsexponent / 3);
			}
			else {
				digits = Math.floor(Math.log(Math.max(Math.abs(im.minval), Math.abs(im.maxval))) / Math.log(im.base));
			}
			im.magfact = Math.pow(im.base, digits);
			if (((digits + symbcenter) < symbol.length) && ((digits + symbcenter) >= 0)) {
				im.symbol = symbol[(int) digits + symbcenter];
			}
			else {
				im.symbol = '?';
			}
		}
	}

	private void findMinMaxValues() {
		double minval = Double.NaN, maxval = Double.NaN;
		for (PlotElement pe : gdef.plotElements) {
			if (pe instanceof SourcedPlotElement) {
				minval = Util.min(((SourcedPlotElement) pe).getMinValue(), minval);
				maxval = Util.max(((SourcedPlotElement) pe).getMaxValue(), maxval);
			}
		}
		if (Double.isNaN(minval)) {
			minval = 0D;
		}
		if (Double.isNaN(maxval)) {
			maxval = 1D;
		}
		im.minval = gdef.minValue;
		im.maxval = gdef.maxValue;
		/* adjust min and max values */
		if (Double.isNaN(im.minval) || ((!gdef.logarithmic && !gdef.rigid) && im.minval > minval)) {
			im.minval = minval;
		}
		if (Double.isNaN(im.maxval) || (!gdef.rigid && im.maxval < maxval)) {
			if (gdef.logarithmic) {
				im.maxval = maxval * 1.1;
			}
			else {
				im.maxval = maxval;
			}
		}
		/* make sure min is smaller than max */
		if (im.minval > im.maxval) {
			im.minval = 0.99 * im.maxval;
		}
		/* make sure min and max are not equal */
		if (im.minval == im.maxval) {
			im.maxval *= 1.01;
			if (!gdef.logarithmic) {
				im.minval *= 0.99;
			}
			/* make sure min and max are not both zero */
			if (im.maxval == 0.0) {
				im.maxval = 1.0;
			}
		}
	}

	private void calculatePlotValues() throws RrdException {
		for (PlotElement pe : gdef.plotElements) {
			if (pe instanceof SourcedPlotElement) {
				((SourcedPlotElement) pe).assignValues(dproc);
			}
		}
	}

	private void resolveTextElements() throws RrdException {
		ValueScaler valueScaler = new ValueScaler(gdef.base);
		for (CommentText comment : gdef.comments) {
			comment.resolveText(dproc, valueScaler);
		}
	}

	private void fetchData() throws RrdException, IOException {
		dproc = new DataProcessor(gdef.startTime, gdef.endTime);
		dproc.setPoolUsed(gdef.poolUsed);
		if (gdef.step > 0) {
			dproc.setStep(gdef.step);
		}
		for (Source src : gdef.sources) {
			src.requestData(dproc);
		}
		dproc.processData();
		//long[] t = dproc.getTimestamps();
		//im.start = t[0];
		//im.end = t[t.length - 1];
		im.start = gdef.startTime;
		im.end = gdef.endTime;
	}

	private boolean lazyCheck() {
		// redraw if lazy option is not set or file does not exist
		if (!gdef.lazy || !Util.fileExists(gdef.filename)) {
			return false; // 'false' means 'redraw'
		}
		// redraw if not enough time has passed
		long secPerPixel = (gdef.endTime - gdef.startTime) / gdef.width;
		long elapsed = Util.getTimestamp() - Util.getLastModified(gdef.filename);
		return elapsed <= secPerPixel;
	}

	private void drawLegend() {
		if (!gdef.onlyGraph && !gdef.noLegend) {
			int ascent = (int) worker.getFontAscent(gdef.smallFont);
			int box = (int) getBox(), boxSpace = (int) (getBoxSpace());
			for (CommentText c : gdef.comments) {
				if (c.isValidGraphElement()) {
					int x = c.x, y = c.y + ascent;
					if (c instanceof LegendText) {
						// draw with BOX
						worker.fillRect(x, y - box, box, box, gdef.colors[COLOR_FRAME]);
						worker.fillRect(x + 1, y - box + 1, box - 2, box - 2, ((LegendText) c).legendColor);
						worker.drawString(c.resolvedText, x + boxSpace, y, gdef.smallFont, gdef.colors[COLOR_FONT]);
					}
					else {
						worker.drawString(c.resolvedText, x, y, gdef.smallFont, gdef.colors[COLOR_FONT]);
					}
				}
			}
		}
	}

	// helper methods

	double getSmallFontHeight() {
		return worker.getFontHeight(gdef.smallFont);
	}

	private double getLargeFontHeight() {
		return worker.getFontHeight(gdef.largeFont);
	}

	private double getSmallFontCharWidth() {
		return worker.getStringWidth("a", gdef.smallFont);
	}

	double getInterlegendSpace() {
		return getSmallFontCharWidth() * LEGEND_INTERSPACING;
	}

	double getLeading() {
		return getSmallFontHeight() * LEGEND_LEADING;
	}

	double getSmallLeading() {
		return getSmallFontHeight() * LEGEND_LEADING_SMALL;
	}

	double getBoxSpace() {
		return Math.ceil(getSmallFontHeight() * LEGEND_BOX_SPACE);
	}

	private double getBox() {
		return getSmallFontHeight() * LEGEND_BOX;
	}

	double[] xtr(long[] timestamps) {
		/*
		double[] timestampsDev = new double[timestamps.length];
		for (int i = 0; i < timestamps.length; i++) {
			timestampsDev[i] = mapper.xtr(timestamps[i]);
		}
		return timestampsDev;
		*/
		double[] timestampsDev = new double[2 * timestamps.length - 1];
		for (int i = 0, j = 0; i < timestamps.length; i += 1, j += 2) {
			timestampsDev[j] = mapper.xtr(timestamps[i]);
			if (i < timestamps.length - 1) {
				timestampsDev[j + 1] = timestampsDev[j];
			}
		}
		return timestampsDev;
	}

	double[] ytr(double[] values) {
		/*
		double[] valuesDev = new double[values.length];
		for (int i = 0; i < values.length; i++) {
			if (Double.isNaN(values[i])) {
				valuesDev[i] = Double.NaN;
			}
			else {
				valuesDev[i] = mapper.ytr(values[i]);
			}
		}
		return valuesDev;
		*/
		double[] valuesDev = new double[2 * values.length - 1];
		for (int i = 0, j = 0; i < values.length; i += 1, j += 2) {
			if (Double.isNaN(values[i])) {
				valuesDev[j] = Double.NaN;
			}
			else {
				valuesDev[j] = mapper.ytr(values[i]);
			}
			if (j > 0) {
				valuesDev[j - 1] = valuesDev[j];
			}
		}
		return valuesDev;
	}

	/**
	 * Renders this graph onto graphing device
	 *
	 * @param g Graphics handle
	 */
	public void render(Graphics g) {
		byte[] imageData = getRrdGraphInfo().getBytes();
		ImageIcon image = new ImageIcon(imageData);
		image.paintIcon(null, g, 0, 0);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -