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.Dimension;
021: import java.awt.image.BufferedImage;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.swing.JComponent;
027:
028: import org.apache.jmeter.report.DataSet;
029: import org.apache.jmeter.visualizers.AxisGraph;
030: import org.apache.jmeter.visualizers.SamplingStatCalculator;
031:
032: /**
033: * The class is reponsible for returning
034: * @author pete
035: *
036: */
037: public class BarChart extends AbstractChart {
038:
039: public static final String REPORT_BAR_CHART_URL = "ReportChart.bar.chart.url";
040:
041: public BarChart() {
042: super ();
043: }
044:
045: public String getURL() {
046: return getPropertyAsString(REPORT_BAR_CHART_URL);
047: }
048:
049: public void setURL(String url) {
050: setProperty(REPORT_BAR_CHART_URL, url);
051: }
052:
053: /**
054: * Convert the data from SamplingStatCalculator to double array of array
055: * @param data
056: * @return
057: */
058: public double[][] convertToDouble(List data) {
059: double[][] dataset = new double[1][data.size()];
060: //Iterator itr = data.iterator();
061: for (int idx = 0; idx < data.size(); idx++) {
062: SamplingStatCalculator stat = (SamplingStatCalculator) data
063: .get(idx);
064: dataset[0][idx] = getValue(stat);
065: }
066: return dataset;
067: }
068:
069: public JComponent renderChart(List data) {
070: ArrayList dset = new ArrayList();
071: ArrayList xlabels = new ArrayList();
072: Iterator itr = data.iterator();
073: while (itr.hasNext()) {
074: DataSet item = (DataSet) itr.next();
075: SamplingStatCalculator ss = item.getStatistics(this
076: .getURL());
077: if (ss != null) {
078: // we add the entry
079: dset.add(ss);
080: if (getXLabel().equals(X_DATA_FILENAME_LABEL)) {
081: xlabels.add(item.getDataSourceName());
082: } else {
083: xlabels.add(item.getMonthDayYearDate());
084: }
085: }
086: }
087: double[][] dbset = convertToDouble(dset);
088: return renderGraphics(dbset, (String[]) xlabels
089: .toArray(new String[xlabels.size()]));
090: }
091:
092: public JComponent renderGraphics(double[][] data,
093: String[] xAxisLabels) {
094: AxisGraph panel = new AxisGraph();
095: panel.setTitle(this .getTitle());
096: panel.setData(data);
097: panel.setXAxisLabels(xAxisLabels);
098: panel.setYAxisLabels(this .getYLabel());
099: panel.setXAxisTitle(this .getFormattedXAxis());
100: panel.setYAxisTitle(this .getYAxis());
101: // we should make this configurable eventually
102: int width = getWidth();
103: int height = getHeight();
104: panel.setPreferredSize(new Dimension(width, height));
105: panel.setSize(new Dimension(width, height));
106: panel.setWidth(width);
107: panel.setHeight(width);
108: setBufferedImage(new BufferedImage(width, height,
109: BufferedImage.TYPE_INT_RGB));
110: panel.paintComponent(this.getBufferedImage().createGraphics());
111: return panel;
112: }
113:
114: }
|