001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations
015: * under the License.
016: *
017: */
018: package org.apache.jmeter.visualizers;
019:
020: import java.awt.Color;
021: import java.awt.Dimension;
022: import java.awt.Graphics;
023: import java.awt.Graphics2D;
024: import java.awt.LayoutManager;
025: import java.awt.Paint;
026: import java.math.BigDecimal;
027:
028: import javax.swing.JPanel;
029:
030: import org.apache.jorphan.logging.LoggingManager;
031: import org.apache.log.Logger;
032: import org.jCharts.axisChart.AxisChart;
033: import org.jCharts.axisChart.customRenderers.axisValue.renderers.ValueLabelPosition;
034: import org.jCharts.axisChart.customRenderers.axisValue.renderers.ValueLabelRenderer;
035: import org.jCharts.chartData.AxisChartDataSet;
036: import org.jCharts.chartData.ChartDataException;
037: import org.jCharts.chartData.DataSeries;
038: import org.jCharts.properties.AxisProperties;
039: import org.jCharts.properties.BarChartProperties;
040: import org.jCharts.properties.ChartProperties;
041: import org.jCharts.properties.DataAxisProperties;
042: import org.jCharts.properties.LabelAxisProperties;
043: import org.jCharts.properties.LegendProperties;
044: import org.jCharts.properties.PropertyException;
045: import org.jCharts.types.ChartType;
046:
047: /**
048: *
049: * Axis graph is used by StatGraphVisualizer, which generates bar graphs
050: * from the statistical data.
051: */
052: public class AxisGraph extends JPanel {
053:
054: private static final Logger log = LoggingManager
055: .getLoggerForClass();
056:
057: private static final String ELLIPSIS = "..."; //$NON-NLS-1$
058: private static final int ELLIPSIS_LEN = ELLIPSIS.length();
059:
060: protected double[][] data = null;
061: protected String title, xAxisTitle, yAxisTitle, yAxisLabel;
062: protected int maxLength;
063: protected String[] xAxisLabels;
064: protected int width, height;
065:
066: /**
067: *
068: */
069: public AxisGraph() {
070: super ();
071: }
072:
073: /**
074: * @param layout
075: */
076: public AxisGraph(LayoutManager layout) {
077: super (layout);
078: }
079:
080: /**
081: * @param layout
082: * @param isDoubleBuffered
083: */
084: public AxisGraph(LayoutManager layout, boolean isDoubleBuffered) {
085: super (layout, isDoubleBuffered);
086: }
087:
088: public void setData(double[][] data) {
089: this .data = data;
090: }
091:
092: public void setTitle(String title) {
093: this .title = title;
094: }
095:
096: public void setMaxLength(int maxLength) {
097: this .maxLength = maxLength;
098: }
099:
100: public void setXAxisTitle(String title) {
101: this .xAxisTitle = title;
102: }
103:
104: public void setYAxisTitle(String title) {
105: this .yAxisTitle = title;
106: }
107:
108: public void setXAxisLabels(String[] labels) {
109: this .xAxisLabels = labels;
110: }
111:
112: public void setYAxisLabels(String label) {
113: this .yAxisLabel = label;
114: }
115:
116: public void setWidth(int w) {
117: this .width = w;
118: }
119:
120: public void setHeight(int h) {
121: this .height = h;
122: }
123:
124: public void paintComponent(Graphics g) {
125: if (data != null && this .title != null
126: && this .xAxisLabels != null && this .xAxisTitle != null
127: && this .yAxisLabel != null && this .yAxisTitle != null) {
128: drawSample(this .title, this .maxLength, this .xAxisLabels,
129: this .xAxisTitle, this .yAxisTitle, this .data,
130: this .width, this .height, g);
131: }
132: }
133:
134: private double findMax(double _data[][]) {
135: double max = 0;
136: max = _data[0][0];
137: for (int i = 0; i < _data.length; i++) {
138: for (int j = 0; j < _data[i].length; j++) {
139: if (_data[i][j] > max) {
140: max = _data[i][j];
141: }
142: }
143: }
144: return max;
145: }
146:
147: private String squeeze(String input, int _maxLength) {
148: if (input.length() > _maxLength) {
149: String output = input.substring(0, _maxLength
150: - ELLIPSIS_LEN)
151: + ELLIPSIS;
152: return output;
153: }
154: return input;
155: }
156:
157: private void drawSample(String _title, int _maxLength,
158: String[] _xAxisLabels, String _xAxisTitle,
159: String _yAxisTitle, double[][] _data, int _width,
160: int _height, Graphics g) {
161: double max = findMax(_data);
162: try {
163: /** These controls are already done in StatGraphVisualizer
164: if (_width == 0) {
165: _width = 450;
166: }
167: if (_height == 0) {
168: _height = 250;
169: }
170: **/
171: if (_maxLength < 3) {
172: _maxLength = 3;
173: }
174: // if the "Title of Graph" is empty, we can assume some default
175: if (_title.length() == 0) {
176: _title = "Graph";
177: }
178: // if the labels are too long, they'll be "squeezed" to make the chart viewable.
179: for (int i = 0; i < _xAxisLabels.length; i++) {
180: String label = _xAxisLabels[i];
181: _xAxisLabels[i] = squeeze(label, _maxLength);
182: }
183: this .setPreferredSize(new Dimension(_width, _height));
184: DataSeries dataSeries = new DataSeries(_xAxisLabels,
185: _xAxisTitle, _yAxisTitle, _title);
186:
187: String[] legendLabels = { yAxisLabel };
188: Paint[] paints = new Paint[] { Color.yellow };
189: BarChartProperties barChartProperties = new BarChartProperties();
190: ValueLabelRenderer valueLabelRenderer = new ValueLabelRenderer(
191: false, false, true, 0);
192: valueLabelRenderer
193: .setValueLabelPosition(ValueLabelPosition.AT_TOP);
194: valueLabelRenderer.useVerticalLabels(true);
195: barChartProperties
196: .addPostRenderEventListener(valueLabelRenderer);
197: AxisChartDataSet axisChartDataSet = new AxisChartDataSet(
198: _data, legendLabels, paints, ChartType.BAR,
199: barChartProperties);
200: dataSeries.addIAxisPlotDataSet(axisChartDataSet);
201:
202: ChartProperties chartProperties = new ChartProperties();
203: LabelAxisProperties xaxis = new LabelAxisProperties();
204: DataAxisProperties yaxis = new DataAxisProperties();
205:
206: try {
207: BigDecimal round = new BigDecimal(max / 1000d);
208: round = round.setScale(0, BigDecimal.ROUND_UP);
209: double topValue = round.doubleValue() * 1000;
210: yaxis.setUserDefinedScale(0, 500);
211: yaxis.setNumItems((int) (topValue / 500) + 1);
212: yaxis.setShowGridLines(1);
213: } catch (PropertyException e) {
214: log.warn("", e);
215: }
216:
217: AxisProperties axisProperties = new AxisProperties(xaxis,
218: yaxis);
219: axisProperties.setXAxisLabelsAreVertical(true);
220: LegendProperties legendProperties = new LegendProperties();
221: AxisChart axisChart = new AxisChart(dataSeries,
222: chartProperties, axisProperties, legendProperties,
223: _width, _height);
224: axisChart.setGraphics2D((Graphics2D) g);
225: axisChart.render();
226: } catch (ChartDataException e) {
227: log.warn("", e);
228: } catch (PropertyException e) {
229: log.warn("", e);
230: }
231: }
232:
233: }
|