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,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.jmeter.testelement;
019:
020: import java.awt.BasicStroke;
021: import java.awt.Dimension;
022: import java.awt.Shape;
023: import java.awt.Stroke;
024: import java.awt.image.BufferedImage;
025: import java.util.ArrayList;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: import javax.swing.JComponent;
030:
031: import org.apache.jmeter.report.DataSet;
032: import org.apache.jmeter.visualizers.LineGraph;
033: import org.apache.jmeter.visualizers.SamplingStatCalculator;
034: import org.jCharts.properties.PointChartProperties;
035:
036: public class LineChart extends AbstractChart {
037:
038: private static final String URL_DELIM = ","; //$NON-NLS-1$
039: private static final String REPORT_CHART_URLS = "ReportChart.chart.urls"; //$NON-NLS-1$
040: private static final Shape[] SHAPE_ARRAY = {
041: PointChartProperties.SHAPE_CIRCLE,
042: PointChartProperties.SHAPE_DIAMOND,
043: PointChartProperties.SHAPE_SQUARE,
044: PointChartProperties.SHAPE_TRIANGLE };
045:
046: protected int width = 350;
047: protected int height = 250;
048:
049: protected int shape_counter = 0;
050:
051: public LineChart() {
052: super ();
053: }
054:
055: public String getURLs() {
056: return getPropertyAsString(REPORT_CHART_URLS);
057: }
058:
059: public void setURLs(String urls) {
060: setProperty(REPORT_CHART_URLS, urls);
061: }
062:
063: public double[][] convertToDouble(List data) {
064: String[] urls = this .getURLs().split(URL_DELIM);
065: double[][] dataset = new double[urls.length][data.size()];
066: for (int idx = 0; idx < urls.length; idx++) {
067: for (int idz = 0; idz < data.size(); idz++) {
068: DataSet dset = (DataSet) data.get(idz);
069: SamplingStatCalculator ss = dset
070: .getStatistics(urls[idx]);
071: dataset[idx][idz] = getValue(ss);
072: }
073: }
074: return dataset;
075: }
076:
077: public JComponent renderChart(List dataset) {
078: ArrayList dset = new ArrayList();
079: ArrayList xlabels = new ArrayList();
080: Iterator itr = dataset.iterator();
081: while (itr.hasNext()) {
082: DataSet item = (DataSet) itr.next();
083: if (item != null) {
084: // we add the entry
085: dset.add(item);
086: if (getXLabel().equals(X_DATA_FILENAME_LABEL)) {
087: xlabels.add(item.getDataSourceName());
088: } else {
089: xlabels.add(item.getMonthDayYearDate());
090: }
091: }
092: }
093: double[][] dbset = convertToDouble(dset);
094: return renderGraphics(dbset, (String[]) xlabels
095: .toArray(new String[xlabels.size()]));
096: }
097:
098: public JComponent renderGraphics(double[][] data,
099: String[] xAxisLabels) {
100: LineGraph panel = new LineGraph();
101: panel.setTitle(this .getTitle());
102: panel.setData(data);
103: panel.setXAxisLabels(xAxisLabels);
104: panel.setYAxisLabels(this .getURLs().split(URL_DELIM));
105: panel.setXAxisTitle(this .getFormattedXAxis());
106: panel.setYAxisTitle(this .getYAxis());
107: // we should make this configurable eventually
108: int _width = getWidth();
109: int _height = getHeight();
110: panel.setPreferredSize(new Dimension(_width, _height));
111: panel.setSize(new Dimension(_width, _height));
112: panel.setWidth(_width);
113: panel.setHeight(_width);
114: setBufferedImage(new BufferedImage(_width, _height,
115: BufferedImage.TYPE_INT_RGB));
116: panel.paintComponent(this .getBufferedImage().createGraphics());
117: return panel;
118: }
119:
120: /**
121: * Since we only have 4 shapes, the method will start with the
122: * first shape and keep cycling through the shapes in order.
123: * @param count
124: * @return
125: */
126: public Shape[] createShapes(int count) {
127: Shape[] shapes = new Shape[count];
128: for (int idx = 0; idx < count; idx++) {
129: shapes[idx] = nextShape();
130: }
131: return shapes;
132: }
133:
134: /**
135: * Return the next shape
136: * @return
137: */
138: public Shape nextShape() {
139: if (shape_counter >= (SHAPE_ARRAY.length - 1)) {
140: shape_counter = 0;
141: }
142: return SHAPE_ARRAY[shape_counter];
143: }
144:
145: /**
146: *
147: * @param count
148: * @return
149: */
150: public Stroke[] createStrokes(int count) {
151: Stroke[] str = new Stroke[count];
152: for (int idx = 0; idx < count; idx++) {
153: str[idx] = nextStroke();
154: }
155: return str;
156: }
157:
158: public Stroke nextStroke() {
159: return new BasicStroke(1.5f);
160: }
161: }
|