public BoxplotChart(String title, String xAxisTitle, String yAxisTitle) {
    super();
    dataset = new DefaultBoxAndWhiskerCategoryDataset();

    CategoryAxis xAxis = new CategoryAxis("Type");
    NumberAxis yAxis = new NumberAxis("Value");
    BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
    renderer.setFillBox(false);
    CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
    chart = new JFreeChart(title, plot);

    Font font = new Font("Helvetica", Font.BOLD, 12);
    xAxis.setLabelFont(font);
    xAxis.setLabel(xAxisTitle);
    yAxis.setLabelFont(font);
    yAxis.setLabel(yAxisTitle);

    // Making it pretty
    plot.setBackgroundPaint(Color.white);
    plot.setDomainGridlinePaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.lightGray);
    chart.setBackgroundPaint(Color.white);
    chart.setTextAntiAlias(true);
    yAxis.setAutoRange(true);
    colorGenerator = new ColorGenerator();
    datasetIndex = 0;

    chart.getLegend().setPosition(RectangleEdge.BOTTOM);
    chart.getLegend().setBorder(1, 1, 1, 1);
  }
  /**
   * create the chart for the original time series
   *
   * @param tsData the data to plot.
   * @return a JFreeChart object of the chart
   */
  private void paintTheChart(double[] tsData) {

    // making the data
    //
    XYSeries dataset = new XYSeries("Series");
    for (int i = 0; i < tsData.length; i++) {
      dataset.add(i, (float) tsData[i]);
    }
    chartXYSeriesCollection = new XYSeriesCollection(dataset);

    // set the renderer
    //
    XYLineAndShapeRenderer xyRenderer = new XYLineAndShapeRenderer(true, false);
    xyRenderer.setSeriesPaint(0, new Color(0, 0, 0));
    xyRenderer.setBaseStroke(new BasicStroke(3));

    // X - the time axis
    //
    NumberAxis timeAxis = new NumberAxis();
    timeAxis.setLabel("Time");

    // Y axis
    //
    NumberAxis valueAxis = new NumberAxis("Values");
    valueAxis.setAutoRangeIncludesZero(false);
    valueAxis.setLabel("Values");

    // put these into collection of dots
    //
    this.timeseriesPlot = new XYPlot(chartXYSeriesCollection, timeAxis, valueAxis, xyRenderer);

    // finally, create the chart
    this.chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, timeseriesPlot, false);

    // set the progress listener to react to mouse clicks in the chart
    this.chart.addProgressListener(this);
  }
Esempio n. 3
0
  private static BufferedImage getChart(double[] tsData, double redDot) {
    try {

      // making the data
      //
      XYSeries dataset = new XYSeries("Series");
      for (int i = 0; i < tsData.length; i++) {
        dataset.add(i, (float) tsData[i]);
      }
      XYSeriesCollection chartXYSeriesCollection = new XYSeriesCollection(dataset);

      XYSeries dot = new XYSeries("Dot");
      dot.add((float) redDot, 0.0f);
      chartXYSeriesCollection.addSeries(dot);

      // set the renderer
      //
      XYLineAndShapeRenderer xyRenderer = new XYLineAndShapeRenderer(true, false);
      xyRenderer.setBaseStroke(new BasicStroke(3));

      xyRenderer.setSeriesPaint(0, new Color(0, 0, 0));
      xyRenderer.setSeriesLinesVisible(0, true);
      xyRenderer.setSeriesShapesVisible(0, false);

      xyRenderer.setSeriesPaint(1, Color.RED);
      xyRenderer.setSeriesLinesVisible(1, false);
      xyRenderer.setSeriesShapesVisible(1, true);

      // X - the time axis
      //
      NumberAxis timeAxis = new NumberAxis();
      timeAxis.setLabel("Time");

      // Y axis
      //
      NumberAxis valueAxis = new NumberAxis("Values");
      valueAxis.setAutoRangeIncludesZero(false);
      valueAxis.setLabel("Values");

      // put these into collection of dots
      //
      XYPlot timeseriesPlot = new XYPlot(chartXYSeriesCollection, timeAxis, valueAxis, xyRenderer);

      // finally, create the chart
      JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, timeseriesPlot, false);

      BufferedImage objBufferedImage = chart.createBufferedImage(800, 400);
      ByteArrayOutputStream bas = new ByteArrayOutputStream();
      try {
        ImageIO.write(objBufferedImage, "png", bas);
      } catch (IOException e) {
        e.printStackTrace();
      }
      byte[] byteArray = bas.toByteArray();

      InputStream in = new ByteArrayInputStream(byteArray);
      BufferedImage image = ImageIO.read(in);

      return image;
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }
Esempio n. 4
0
  static JFreeChart createChart(
      NavigableMap<TimeAxisKey, DiffStat> aggregatedDiffstats,
      DiffStatConfiguration configuration) {

    boolean legend = false;
    boolean tooltips = false;
    boolean urls = false;
    Font helvetica = new Font("Helvetica", Font.PLAIN, 11 * configuration.multiplierInt());

    XYDatasetMinMax datasetMinMax =
        createDeltaDataset("Additions and Delections", aggregatedDiffstats);
    XYDataset dataset = datasetMinMax.dataset;
    JFreeChart chart =
        ChartFactory.createTimeSeriesChart("", "", "", dataset, legend, tooltips, urls);

    chart.setBackgroundPaint(WHITE);
    chart.setBorderVisible(false);

    float strokeWidth = 1.2f * configuration.multiplierFloat();

    XYPlot plot = chart.getXYPlot();
    plot.setOrientation(VERTICAL);
    plot.setBackgroundPaint(WHITE);
    plot.setDomainGridlinesVisible(true);
    plot.setDomainGridlinePaint(AXIS_LINE_COLOR);
    plot.setDomainGridlineStroke(new BasicStroke(1.0f * configuration.multiplierFloat()));
    plot.setRangeGridlinesVisible(false);

    plot.setOutlineVisible(false);

    DateAxis dateAxis = (DateAxis) plot.getDomainAxis();
    dateAxis.setDateFormatOverride(new SimpleDateFormat("MM/yy"));
    dateAxis.setTickLabelFont(helvetica);
    dateAxis.setAxisLineVisible(false);
    dateAxis.setTickUnit(computeDateTickUnit(aggregatedDiffstats));
    RectangleInsets insets =
        new RectangleInsets(
            8.0d * configuration.multiplierDouble(),
            4.0d * configuration.multiplierDouble(),
            4.0d * configuration.multiplierDouble(),
            4.0d * configuration.multiplierDouble());
    dateAxis.setTickLabelInsets(insets);

    NumberAxis additionDeletionAxis = (NumberAxis) plot.getRangeAxis(0);
    additionDeletionAxis.setAxisLineVisible(false);
    additionDeletionAxis.setLabel("Additions and Deletions");
    additionDeletionAxis.setLabelFont(helvetica);
    additionDeletionAxis.setTickLabelFont(helvetica);
    additionDeletionAxis.setRangeType(RangeType.FULL);
    int lowerBound = datasetMinMax.min + (int) (datasetMinMax.min * 0.1d);
    additionDeletionAxis.setLowerBound(lowerBound);
    int upperBound = datasetMinMax.max + (int) (datasetMinMax.max * 0.1d);
    additionDeletionAxis.setUpperBound(upperBound);
    additionDeletionAxis.setNumberFormatOverride(new AbbreviatingNumberFormat());
    additionDeletionAxis.setMinorTickMarksVisible(false);
    additionDeletionAxis.setTickMarkInsideLength(5.0f * configuration.multiplierFloat());
    additionDeletionAxis.setTickMarkOutsideLength(0.0f);
    additionDeletionAxis.setTickMarkStroke(new BasicStroke(2.0f * configuration.multiplierFloat()));
    additionDeletionAxis.setTickUnit(
        new NumberTickUnit(computeTickUnitSize(datasetMinMax.max + abs(datasetMinMax.min))));

    XYAreaRenderer areaRenderer = new XYAreaRenderer(XYAreaRenderer.AREA);
    areaRenderer.setOutline(true);
    areaRenderer.setSeriesOutlinePaint(0, ADDED_STROKE);
    areaRenderer.setSeriesOutlineStroke(0, new BasicStroke(strokeWidth));
    areaRenderer.setSeriesPaint(0, ADDED_FILL);
    areaRenderer.setSeriesOutlinePaint(1, REMOVED_STROKE);
    areaRenderer.setSeriesOutlineStroke(1, new BasicStroke(strokeWidth));
    areaRenderer.setSeriesPaint(1, REMOVED_FILL);
    plot.setRenderer(0, areaRenderer);

    // Total Axis
    NumberAxis totalAxis = new NumberAxis("Total Lines");
    totalAxis.setAxisLineVisible(false);
    totalAxis.setLabelPaint(VALUE_LABEL);
    totalAxis.setTickLabelPaint(TOTAL_LABEL);
    totalAxis.setLabelFont(helvetica);
    totalAxis.setTickLabelFont(helvetica);
    totalAxis.setNumberFormatOverride(new AbbreviatingNumberFormat());
    totalAxis.setMinorTickMarksVisible(false);
    totalAxis.setTickMarkInsideLength(5.0f * configuration.multiplierFloat());
    totalAxis.setTickMarkOutsideLength(0.0f);
    totalAxis.setTickMarkStroke(new BasicStroke(2.0f * configuration.multiplierFloat()));
    totalAxis.setTickMarkPaint(TOTAL_LABEL);
    plot.setRangeAxis(1, totalAxis);
    plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);

    XYDatasetAndTotal datasetAndTotal = createTotalDataset("Total Lines", aggregatedDiffstats);
    XYDataset totalDataSet = datasetAndTotal.dataset;
    plot.setDataset(1, totalDataSet);
    plot.mapDatasetToRangeAxis(1, 1);
    //        XYItemRenderer totalRenderer = new XYSplineRenderer();
    XYItemRenderer totalRenderer = new StandardXYItemRenderer();
    totalRenderer.setSeriesPaint(0, TOTAL_FILL);
    totalRenderer.setSeriesStroke(
        0,
        new BasicStroke(
            strokeWidth,
            CAP_ROUND,
            JOIN_ROUND,
            10.0f * configuration.multiplierFloat(),
            new float[] {
              6.0f * configuration.multiplierFloat(), 3.0f * configuration.multiplierFloat()
            },
            0.0f));
    plot.setRenderer(1, totalRenderer);

    totalAxis.setTickUnit(new NumberTickUnit(computeTickUnitSize(datasetAndTotal.total)));

    return chart;
  }
  public Drawable createChart(ADCDataset dataset, Dimension dimension) {
    JFreeChart chart =
        ChartFactory.createBarChart(
            "", // chart title
            "", // domain axis label
            "", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // the plot orientation
            false, // legend
            false, // tooltips
            false // urls
            );
    TextTitle textTitle = new TextTitle(dataset.get(Attribute.TITLE), TITLE_FONT);
    textTitle.setPadding(new RectangleInsets(10, 0, 0, 0));
    chart.setTitle(textTitle);

    chart.addLegend(createLegend(dataset.getRowKey(0).toString(), dataset.getRowKey(1).toString()));
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setOutlineVisible(false);
    plot.setAxisOffset(new RectangleInsets(0, 0, 0, 0));
    plot.setDomainGridlinesVisible(false);
    plot.setRangeGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.gray);
    plot.setRangeGridlineStroke(new BasicStroke(2));

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setAutoTickUnitSelection(true);
    rangeAxis.setTickUnit(new NumberTickUnit(0.2, percentFormatter()));
    rangeAxis.setAxisLineVisible(true);
    rangeAxis.setLabel(dataset.get(Attribute.Y_AXIS_LABEL));
    rangeAxis.setAxisLineStroke(new BasicStroke(2));
    rangeAxis.setAxisLinePaint(Color.black);
    rangeAxis.setTickMarksVisible(false);
    rangeAxis.setLabelPaint(AXIS_LABEL_COLOR);
    rangeAxis.setLabelFont(AXIS_LABEL_FONT);
    rangeAxis.setLabelInsets(new RectangleInsets(0, 0, 0, 0));
    rangeAxis.setUpperMargin(0);
    rangeAxis.setAutoRange(false);
    rangeAxis.setRange(0, 1);

    CategoryAxis cAxis = plot.getDomainAxis();
    cAxis.setTickMarksVisible(false);
    cAxis.setAxisLinePaint(Color.black);
    cAxis.setAxisLineStroke(new BasicStroke(2));
    cAxis.setLabel(dataset.get(Attribute.X_AXIS_LABEL));
    cAxis.setTickLabelsVisible(true);
    cAxis.setUpperMargin(0.05);
    cAxis.setLowerMargin(0.05);
    cAxis.setTickLabelFont(CAXIS_LABEL_FONT);
    cAxis.setTickLabelPaint(Color.black);
    CustomBarRenderer renderer = new CustomBarRenderer();
    plot.setRenderer(renderer);
    renderer.setDrawBarOutline(false);
    renderer.setBaseItemLabelsVisible(false);
    renderer.setShadowVisible(false);
    renderer.setBarPainter(new StandardBarPainter());
    renderer.setMaximumBarWidth(0.08);
    renderer.setItemMargin(0.01);
    return new JFreeChartDrawable(chart, dimension);
  }