Skip to content

Instantly share code, notes, and snippets.

@MikeJansen
Created August 29, 2011 18:07
Show Gist options
  • Save MikeJansen/1178973 to your computer and use it in GitHub Desktop.
Save MikeJansen/1178973 to your computer and use it in GitHub Desktop.
JFreeChart - Test Phase Report
package com.necam.eadd.jira.plugin.report.magicchart;
import java.awt.BasicStroke;
import java.awt.Color;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StackedXYBarRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeTableXYDataset;
import org.jfree.ui.RectangleEdge;
public class MagicChartJFreeChart {
private static final Logger log = Logger.getLogger(MagicChartJFreeChart.class);
public MagicChartJFreeChart() {
log.setLevel(Level.ALL);
}
public JFreeChart getMagicChart(final MagicChartModel model, final String label) {
final TimeTableXYDataset retestData = new TimeTableXYDataset();
final TimeTableXYDataset closedData = new TimeTableXYDataset();
final TimeTableXYDataset testActualData = new TimeTableXYDataset();
final TimeTableXYDataset testPlanData = new TimeTableXYDataset();
final TimeTableXYDataset openData = new TimeTableXYDataset();
log.debug(String.format("Today: %1%tc", model.getToday()));
for (final MagicChartModel.Daily daily : model.getDaily()) {
final Day day = new Day(daily.getDate());
openData.add(day, daily.getOpenP1(), "Open(E/P1)");
openData.add(day, daily.getOpenP2(), "Open(P2)");
openData.add(day, daily.getOpenP3(), "Open(P3)");
openData.add(day, daily.getOpenP4(), "Open(P4)");
closedData.add(day, daily.getTestsPassed(), "Tests Passed");
log.debug(String.format("Current %1$tc vs. today = %2$d", daily.getDate(), daily.getDate().compareTo(model.getToday())));
if (daily.getDate().compareTo(model.getToday()) <= 0) {
testPlanData.add(day, daily.getTargetTestsRun(), "Target Test Cases");
testActualData.add(day, 0, "");
retestData.add(day, 0, "");
}
}
return getMagicChart(retestData, closedData, testActualData, testPlanData, openData, label);
}
private JFreeChart getMagicChart(final TimeTableXYDataset retestData, final TimeTableXYDataset closedData,
final TimeTableXYDataset testActualData, final TimeTableXYDataset testPlanData,
final TimeTableXYDataset openData, final String label) {
// Graph
final XYPlot plot = new XYPlot();
final DateAxis dateAxis = new DateAxis("Date");
final NumberAxis valueAxis1 = new NumberAxis();
final NumberAxis valueAxis2 = new NumberAxis();
final BasicStroke dashedStroke =
new BasicStroke(2.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 0.0f, new float[] {7.0f, 2.0f}, 0.0f );
final BasicStroke normalStroke = new BasicStroke(2.0f);
plot.setBackgroundAlpha(0.0f);
plot.setBackgroundImageAlpha(0.0f);
plot.setBackgroundPaint(new Color(255, 255, 255, 0));
plot.setDomainAxis(dateAxis);
plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
plot.setRangeAxis(0, valueAxis1);
plot.setRangeAxisLocation(0, AxisLocation.TOP_OR_RIGHT);
plot.setRangeAxis(1, valueAxis2);
plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
int dataIndex = 0;
/*
// Data: RETEST
final XYLineAndShapeRenderer retestRenderer = new XYLineAndShapeRenderer();
retestRenderer.setSeriesPaint(0, new Color(0, 143, 0));
retestRenderer.setSeriesStroke(0, dashedStroke);
retestRenderer.setSeriesShapesVisible(0, false);
plot.setDataset(dataIndex, retestData);
plot.setRenderer(dataIndex, retestRenderer);
plot.mapDatasetToRangeAxis(dataIndex, 0);
plot.mapDatasetToDomainAxis(dataIndex, 0);
dataIndex++;
*/
// Data: CLOSED
final XYLineAndShapeRenderer closedRenderer = new XYLineAndShapeRenderer();
plot.setDataset(dataIndex, closedData);
plot.setRenderer(dataIndex, closedRenderer);
closedRenderer.setSeriesPaint(0, Color.BLUE);
closedRenderer.setSeriesStroke(0, dashedStroke);
closedRenderer.setSeriesShapesVisible(0, false);
plot.mapDatasetToRangeAxis(dataIndex, 1);
plot.mapDatasetToDomainAxis(dataIndex, 0);
dataIndex++;
// Data: TEST PLAN
final XYLineAndShapeRenderer testPlanRenderer = new XYLineAndShapeRenderer();
testPlanRenderer.setSeriesPaint(0, Color.YELLOW);
testPlanRenderer.setSeriesShapesVisible(0, false);
testPlanRenderer.setSeriesStroke(0, normalStroke);
plot.setDataset(dataIndex, testPlanData);
plot.setRenderer(dataIndex, testPlanRenderer);
plot.mapDatasetToRangeAxis(dataIndex, 1);
plot.mapDatasetToDomainAxis(dataIndex, 0);
dataIndex++;
/*
// Data: TEST ACTUAL
final XYLineAndShapeRenderer testActualRenderer = new XYLineAndShapeRenderer();
testActualRenderer.setSeriesPaint(0, Color.RED);
testActualRenderer.setSeriesShapesVisible(0, false);
testActualRenderer.setSeriesStroke(0, normalStroke);
plot.setDataset(dataIndex, testActualData);
plot.setRenderer(dataIndex, testActualRenderer);
plot.mapDatasetToRangeAxis(dataIndex, 1);
plot.mapDatasetToDomainAxis(dataIndex, 0);
dataIndex++;
*/
// Data: OPEN
final StackedXYBarRenderer openRenderer = new StackedXYBarRenderer();
plot.setDataset(dataIndex, openData);
plot.setRenderer(dataIndex, openRenderer);
openRenderer.setMargin(0.0);
openRenderer.setSeriesPaint(0, new Color(172, 64, 64));
openRenderer.setSeriesPaint(1, new Color(172, 172, 64));
openRenderer.setSeriesPaint(2, new Color(64, 64, 172));
openRenderer.setSeriesPaint(3, new Color(64, 172, 64));
openRenderer.setBaseOutlinePaint(Color.GRAY);
openRenderer.setDrawBarOutline(true);
plot.mapDatasetToRangeAxis(dataIndex, 0);
plot.mapDatasetToDomainAxis(dataIndex, 0);
dataIndex++;
// Create the chart
final JFreeChart chart = new JFreeChart(label, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.getLegend().setPosition(RectangleEdge.RIGHT);
chart.setBackgroundImageAlpha(0.0f);
chart.setBackgroundPaint(new Color(255, 255, 255, 0));
return chart;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment