package kg.apc.jmeter.vizualizers;

import kg.apc.jmeter.charting.GraphRowAverages;
import kg.apc.jmeter.charting.GraphRowOverallAverages;
import kg.apc.jmeter.charting.AbstractGraphRow;
import java.awt.Color;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jorphan.gui.RateRenderer;

/**
 * 
 * @author apc
 */
public class ThroughputVsThreadsGui
        extends AbstractGraphPanelVisualizer
{

    /**
     *
     */
    public ThroughputVsThreadsGui()
    {
        super();
        graphPanel.getGraphObject().setDrawCurrentX(true);
        graphPanel.getGraphObject().setyAxisLabelRenderer(new RateRenderer("#.0"));
        graphPanel.getGraphObject().setForcedMinX(0);
    }

    public String getLabelResource()
    {
        return this.getClass().getSimpleName();
    }

    @Override
    public String getStaticLabel()
    {
        return "Transaction Throughput vs Threads";
    }

    public void add(SampleResult res)
    {
        long time = res.getTime();
        if (time < 1)
        {
            return;
        }

        String label = res.getSampleLabel();
        String averageLabel = "Average " + res.getSampleLabel();
        GraphRowAverages row;
        GraphRowOverallAverages avgRow;
        if (!model.containsKey(label) || !model.containsKey(averageLabel))
        {
            row = getNewRow(label);
            avgRow = getNewAveragesRow(averageLabel, row.getColor());
        } else
        {
            row = (GraphRowAverages) model.get(label);
            avgRow = (GraphRowOverallAverages) model.get(averageLabel);
        }

        int allThreads = res.getAllThreads();
        double throughput = (double) allThreads * 1000.0d / time;
        row.add(allThreads, throughput);
        avgRow.add(allThreads, throughput);
        graphPanel.getGraphObject().setCurrentX(allThreads);
        updateGui(null);
    }

    private synchronized GraphRowOverallAverages getNewAveragesRow(String averageLabel, Color color)
    {
        GraphRowOverallAverages avgRow = null;
        if (!model.containsKey(averageLabel))
        {
            avgRow = new GraphRowOverallAverages();
            avgRow.setLabel(averageLabel);
            avgRow.setColor(color);
            avgRow.setMarkerSize(AbstractGraphRow.MARKER_SIZE_BIG);
            avgRow.setDrawValueLabel(true);
            avgRow.setShowInLegend(false);
            model.put(averageLabel, avgRow);
            graphPanel.addRow(avgRow);
        } else
        {
            avgRow = (GraphRowOverallAverages) model.get(averageLabel);
        }
        return avgRow;
    }

    private synchronized GraphRowAverages getNewRow(String label)
    {
        GraphRowAverages row = null;
        if (!model.containsKey(label))
        {
            row = new GraphRowAverages();
            row.setLabel(label);
            row.setColor(colors.getNextColor());
            row.setDrawLine(true);
            row.setMarkerSize(AbstractGraphRow.MARKER_SIZE_SMALL);
            model.put(label, row);
            graphPanel.addRow(row);
        } else
        {
            row = (GraphRowAverages) model.get(label);
        }
        return row;
    }
}

@@ -254,52 +254,8 @@
 ult;
-%0Aimport org.apache.jorphan.gui.RateRenderer;
 %0A%0A/*
@@ -566,16 +566,22 @@
 rer(new 
+Custom
 RateRend
@@ -3120,11 +3120,530 @@
 w;%0A    %7D
+%0A%0A    private class CustomRateRenderer%0A            extends org.apache.jorphan.gui.RateRenderer%0A    %7B%0A%0A        public CustomRateRenderer(String format)%0A        %7B%0A            super(format);%0A        %7D%0A%0A        @Override%0A        public void setValue(Object value)%0A        %7B%0A            if (value != null && (value instanceof Double) && ((Double)value).doubleValue() == 0)%0A            %7B%0A                setText(%220.0/sec%22);%0A            %7D else%0A            %7B%0A                super.setValue(value);%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Fix Y axis 0 value