001: //$Header$
002: /*
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */
019: package org.apache.jmeter.testelement;
020:
021: import java.awt.image.BufferedImage;
022: import java.util.List;
023:
024: import javax.swing.JComponent;
025:
026: import org.apache.jmeter.report.ReportChart;
027: import org.apache.jmeter.visualizers.SamplingStatCalculator;
028: import org.apache.jorphan.util.JOrphanUtils;
029:
030: /**
031: * The general idea of the chart graphs information for a table.
032: * A chart can only be generated from a specific table, though more
033: * than one chart can be generated from a single table.
034: * @author Peter Lin
035: *
036: */
037: public abstract class AbstractChart extends AbstractTestElement
038: implements ReportChart {
039:
040: public static final String REPORT_CHART_X_AXIS = "ReportChart.chart.x.axis";
041: public static final String REPORT_CHART_Y_AXIS = "ReportChart.chart.y.axis";
042: public static final String REPORT_CHART_X_LABEL = "ReportChart.chart.x.label";
043: public static final String REPORT_CHART_Y_LABEL = "ReportChart.chart.y.label";
044: public static final String REPORT_CHART_TITLE = "ReportChart.chart.title";
045: public static final String REPORT_CHART_CAPTION = "ReportChart.chart.caption";
046: public static final String REPORT_CHART_WIDTH = "ReportChart.chart.width";
047: public static final String REPORT_CHART_HEIGHT = "ReportChart.chart.height";
048:
049: public static final int DEFAULT_WIDTH = 350;
050: public static final int DEFAULT_HEIGHT = 350;
051:
052: public static final String X_DATA_FILENAME_LABEL = "Filename";
053: public static final String X_DATA_DATE_LABEL = "Date";
054: public static final String[] X_LABELS = { X_DATA_FILENAME_LABEL,
055: X_DATA_DATE_LABEL };
056: protected BufferedImage image = null;
057:
058: public AbstractChart() {
059: super ();
060: }
061:
062: public String getXAxis() {
063: return getPropertyAsString(REPORT_CHART_X_AXIS);
064: }
065:
066: public String getFormattedXAxis() {
067: String text = getXAxis();
068: if (text.indexOf('.') > -1) {
069: text = text.substring(text.indexOf('.') + 1);
070: text = JOrphanUtils.replaceAllChars(text, '_', " ");
071: }
072: return text;
073: }
074:
075: public void setXAxis(String field) {
076: setProperty(REPORT_CHART_X_AXIS, field);
077: }
078:
079: public String getYAxis() {
080: return getPropertyAsString(REPORT_CHART_Y_AXIS);
081: }
082:
083: public void setYAxis(String scale) {
084: setProperty(REPORT_CHART_Y_AXIS, scale);
085: }
086:
087: public String getXLabel() {
088: return getPropertyAsString(REPORT_CHART_X_LABEL);
089: }
090:
091: /**
092: * The X data labels should be either the filename, date or some
093: * other series of values
094: * @param label
095: */
096: public void setXLabel(String label) {
097: setProperty(REPORT_CHART_X_LABEL, label);
098: }
099:
100: public String getYLabel() {
101: return getPropertyAsString(REPORT_CHART_Y_LABEL);
102: }
103:
104: public void setYLabel(String label) {
105: setProperty(REPORT_CHART_Y_LABEL, label);
106: }
107:
108: /**
109: * The title is a the name for the chart. A page link will
110: * be generated using the title. The title will also be
111: * used for a page index.
112: * @return
113: */
114: public String getTitle() {
115: return getPropertyAsString(REPORT_CHART_TITLE);
116: }
117:
118: /**
119: * The title is a the name for the chart. A page link will
120: * be generated using the title. The title will also be
121: * used for a page index.
122: * @param title
123: */
124: public void setTitle(String title) {
125: setProperty(REPORT_CHART_TITLE, title);
126: }
127:
128: /**
129: * The caption is a description for the chart explaining
130: * what the chart means.
131: * @return
132: */
133: public String getCaption() {
134: return getPropertyAsString(REPORT_CHART_CAPTION);
135: }
136:
137: /**
138: * The caption is a description for the chart explaining
139: * what the chart means.
140: * @param caption
141: */
142: public void setCaption(String caption) {
143: setProperty(REPORT_CHART_CAPTION, caption);
144: }
145:
146: /**
147: * if no width is set, the default is returned
148: * @return
149: */
150: public int getWidth() {
151: int w = getPropertyAsInt(REPORT_CHART_WIDTH);
152: if (w <= 0) {
153: return DEFAULT_WIDTH;
154: } else {
155: return w;
156: }
157: }
158:
159: /**
160: * set the width of the graph
161: * @param width
162: */
163: public void setWidth(String width) {
164: setProperty(REPORT_CHART_WIDTH, String.valueOf(width));
165: }
166:
167: /**
168: * if the height is not set, the default is returned
169: * @return
170: */
171: public int getHeight() {
172: int h = getPropertyAsInt(REPORT_CHART_HEIGHT);
173: if (h <= 0) {
174: return DEFAULT_HEIGHT;
175: } else {
176: return h;
177: }
178: }
179:
180: /**
181: * set the height of the graph
182: * @param height
183: */
184: public void setHeight(String height) {
185: setProperty(REPORT_CHART_HEIGHT, String.valueOf(height));
186: }
187:
188: /**
189: * Subclasses will need to implement the method by doing the following:
190: * 1. get the x and y axis
191: * 2. filter the table data
192: * 3. pass the data to the chart library
193: * 4. return the generated chart
194: */
195: public abstract JComponent renderChart(List data);
196:
197: /**
198: * this makes it easy to get the bufferedImage
199: * @return
200: */
201: public BufferedImage getBufferedImage() {
202: return this .image;
203: }
204:
205: /**
206: * in case an user wants set the bufferdImage
207: * @param img
208: */
209: public void setBufferedImage(BufferedImage img) {
210: this .image = img;
211: }
212:
213: /**
214: * convienance method for getting the selected value. Rather than use
215: * Method.invoke(Object,Object[]), it's simpler to just check which
216: * column is selected and call the method directly.
217: * @param stat
218: * @return
219: */
220: public double getValue(SamplingStatCalculator stat) {
221: if (this .getXAxis().equals(
222: AbstractTable.REPORT_TABLE_50_PERCENT)) {
223: return stat.getPercentPoint(.50).doubleValue();
224: } else if (this .getXAxis().equals(
225: AbstractTable.REPORT_TABLE_90_PERCENT)) {
226: return stat.getPercentPoint(.90).doubleValue();
227: } else if (this .getXAxis().equals(
228: AbstractTable.REPORT_TABLE_ERROR_RATE)) {
229: return stat.getErrorPercentage();
230: } else if (this .getXAxis().equals(
231: AbstractTable.REPORT_TABLE_MAX)) {
232: return stat.getMax().doubleValue();
233: } else if (this .getXAxis().equals(
234: AbstractTable.REPORT_TABLE_MEAN)) {
235: return stat.getMean();
236: } else if (this .getXAxis().equals(
237: AbstractTable.REPORT_TABLE_MEDIAN)) {
238: return stat.getMedian().doubleValue();
239: } else if (this .getXAxis().equals(
240: AbstractTable.REPORT_TABLE_MIN)) {
241: return stat.getMin().doubleValue();
242: } else if (this .getXAxis().equals(
243: AbstractTable.REPORT_TABLE_RESPONSE_RATE)) {
244: return stat.getRate();
245: } else if (this .getXAxis().equals(
246: AbstractTable.REPORT_TABLE_TRANSFER_RATE)) {
247: // return the pagesize divided by 1024 to get kilobytes
248: return stat.getKBPerSecond();
249: } else {
250: return Double.NaN;
251: }
252: }
253: }
|