Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
JENKINS-16580: crop plots to fit screen with extra margin
Browse files Browse the repository at this point in the history
  • Loading branch information
mheinzerling committed Dec 8, 2014
1 parent a30e961 commit 047dfb6
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
Binary file added resources/test/crop100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/test/crop5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions src/main/java/hudson/plugins/jacoco/model/CoverageGraphLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,14 @@ public Number getValue(Coverage c)
static class Axis
{
private String label = null;
private int crop = -1;
private boolean skipZero = false;

public boolean isCrop()
{
return crop != -1;
}

public boolean isSkipZero()
{
return skipZero;
Expand All @@ -168,6 +174,11 @@ public String getLabel()
{
return label;
}

public int getCrop()
{
return crop;
}
}

static class Plot
Expand Down Expand Up @@ -225,6 +236,18 @@ private void assureAxis()
if (axes.isEmpty()) axis();
}

public CoverageGraphLayout crop()
{
return crop(5);
}

public CoverageGraphLayout crop(int marginInPercent)
{
assureAxis();
axes.peek().crop = marginInPercent;
return this;
}

public CoverageGraphLayout label(String label)
{
assureAxis();
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/hudson/plugins/jacoco/model/CoverageObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
Expand Down Expand Up @@ -393,6 +394,7 @@ protected Map<Axis, DataSetBuilder<String, NumberOnlyBuildLabel>> createDataSetB
for (Axis axis : layout.getAxes())
{
builders.put(axis, new DataSetBuilder<String, NumberOnlyBuildLabel>());
if (axis.isCrop()) bounds.put(axis, new Bounds());
}

Map<Plot, Number> last = new HashMap<Plot, Number>();
Expand All @@ -406,6 +408,7 @@ protected Map<Axis, DataSetBuilder<String, NumberOnlyBuildLabel>> createDataSetB
if (axis.isSkipZero() && (value == null || value.floatValue() == 0f)) value = null;
if (value != null)
{
if (axis.isCrop()) bounds.get(axis).update(value);
last.put(plot, value);
}
else
Expand All @@ -428,6 +431,20 @@ abstract class GraphImpl extends Graph {

private CoverageObject<SELF> obj;
private CoverageGraphLayout layout;
protected Map<Axis,Bounds> bounds=new HashMap<Axis, Bounds>();

protected class Bounds
{
float min=Float.MAX_VALUE;
float max=Float.MIN_VALUE;

public void update(Number value)
{
float v=value.floatValue();
if (min>v) min=v;
if (max<v) max=v+1;
}
}

public GraphImpl(CoverageObject<SELF> obj, Calendar timestamp, int defaultW, int defaultH, CoverageGraphLayout layout) {
super(timestamp, defaultW, defaultH);
Expand Down Expand Up @@ -481,12 +498,22 @@ protected JFreeChart createGraph() {
NumberAxis numberAxis = new NumberAxis(axis.getLabel());
plot.setRangeAxis(axisId, numberAxis);
numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); //TODO
setBounds(axis, numberAxis);
axisId++;
}

layout.apply(chart);
return chart;
}

private void setBounds(Axis a, ValueAxis axis)
{
if (!a.isCrop()) return;
Bounds bounds = this.bounds.get(a);
float border = (bounds.max - bounds.min) / 100 * a.getCrop();
axis.setUpperBound(bounds.max + border);
axis.setLowerBound(Math.max(0, bounds.min - border));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,30 @@ public void multipleAccessAndDifferentCoverageType() throws IOException
assertGraph(chart, "multiple.png");
}

@Test
public void crop5() throws IOException
{
CoverageGraphLayout layout = new CoverageGraphLayout()
.baseStroke(2f)
.axis().crop(5).skipZero()
.plot().type(CoverageType.BRANCH).value(CoverageValue.PERCENTAGE).color(Color.RED);

JFreeChart chart = createTestCoverage().createGraph(new GregorianCalendar(), WIDTH, HEIGHT, layout).getGraph();
assertGraph(chart, "crop5.png");
}

@Test
public void crop100() throws IOException
{
CoverageGraphLayout layout = new CoverageGraphLayout()
.baseStroke(2f)
.axis().crop(100).skipZero()
.plot().type(CoverageType.BRANCH).value(CoverageValue.PERCENTAGE).color(Color.RED);

JFreeChart chart = createTestCoverage().createGraph(new GregorianCalendar(), WIDTH, HEIGHT, layout).getGraph();
assertGraph(chart, "crop100.png");
}

@Test
public void skipZero() throws IOException
{
Expand Down

0 comments on commit 047dfb6

Please sign in to comment.