private void drawLegend(Context2D context) {
    if (legend != null && drawLegend) {
      legend.setContext(context);
      double tX = 0;
      double tY = 0;
      double offsetTop = calculateOffsetCausedByTitleAndSubtitle(context);
      Legend.LegendPosition position = legend.getPosition();

      if (position.equals(Legend.LegendPosition.LEGEND_POSITION_LEFT)) {
        tX = paddingLeft;
        tY = offsetTop;
      } else if (position.equals(Legend.LegendPosition.LEGEND_POSITION_RIGHT)) {
        tX = width - paddingRight - legend.getWidth();
        tY = offsetTop;
      } else if (position.equals(Legend.LegendPosition.LEGEND_POSITION_TOP)) {
        tX = paddingLeft;
        tY = offsetTop;
      } else if (position.equals(Legend.LegendPosition.LEGEND_POSITION_BOTTOM)) {
        tX = paddingLeft;
        tY = height - paddingBottom - legend.getHeight();
      }
      context.save();
      context.translate(tX, tY);
      legend.draw();
      context.restore();
    }
  }
 private void drawOuterBorder(
     Context2D context, Paint borderPaint, BasicStroke borderStroke, double width, double height) {
   float borderWidth = borderStroke.getLineWidth();
   context.setStroke(borderStroke);
   context.setPaint(borderPaint);
   context.draw(
       new Rectangle2D.Double(
           borderWidth / 2, borderWidth / 2, width - borderWidth, height - borderWidth));
 }
  private void drawTitleAndSubtitle(Context2D context) {
    double widthWithoutPadding = width - paddingLeft - paddingRight;
    context.save();

    context.translate(0, paddingTop);

    // Draw title:
    if (title != null) {
      drawCenteredText(context, title, width, titleFont, titleColor, widthWithoutPadding);
      context.translate(0, context.getLineHeight(titleFont, title));
    }

    // Draw subtitle:
    if (subtitle != null) {
      context.translate(0, subtitleMargin);
      drawCenteredText(context, subtitle, width, subtitleFont, subtitleColor, widthWithoutPadding);
      context.translate(0, context.getLineHeight(subtitleFont, subtitle));
    }

    if (title != null || subtitle != null) {
      context.translate(0, titleMargin);
    }

    context.restore();
  }
  private double calculateOffsetCausedByTitleAndSubtitle(Context2D context) {
    double offsetTop = paddingTop;
    if (title != null) {
      offsetTop += context.getLineHeight(titleFont, title);
    }
    if (subtitle != null) {
      offsetTop += context.getLineHeight(subtitleFont, subtitle) + subtitleMargin;
    }

    if (title != null || subtitle != null) {
      offsetTop += titleMargin;
    }
    return offsetTop;
  }
 private void drawCenteredText(
     Context2D context,
     String text,
     double width,
     Font textFont,
     Color textColor,
     double maxWidth) {
   context.setFont(textFont);
   context.setPaint(textColor);
   context.drawText(
       text,
       width / 2,
       0,
       Context2D.HORIZONTAL_ALIGN_CENTER,
       Context2D.VERTICAL_ALIGN_TOP,
       maxWidth);
 }
  private void drawChart(Context2D context) {
    if (renderer != null) {
      double offsetTop = calculateOffsetCausedByTitleAndSubtitle(context);
      double chartWidth = width - paddingLeft - paddingRight;
      double chartHeight = height - offsetTop - paddingBottom;

      context.save();
      context.translate(paddingLeft, offsetTop);

      if (drawLegend && legend != null) {
        Legend.LegendPosition position = legend.getPosition();
        if (position.equals(Legend.LegendPosition.LEGEND_POSITION_LEFT)
            || position.equals(Legend.LegendPosition.LEGEND_POSITION_RIGHT)) {

          chartWidth -= legend.getWidth() + legendMargin;

          if (position.equals(Legend.LegendPosition.LEGEND_POSITION_LEFT)) {
            context.translate(legend.getWidth() + legendMargin, 0);
          }

        } else {
          chartHeight -= legend.getHeight() + legendMargin;
          if (position.equals(Legend.LegendPosition.LEGEND_POSITION_TOP)) {
            context.translate(0, legend.getHeight() + legendMargin);
          }
        }
      }

      context.resize(chartWidth, chartHeight);
      renderer.draw(context, dataseries, labels);
      context.restore();
    }
  }
 private void fillBackground(
     Context2D context, Paint backgroundPaint, double width, double height) {
   context.setPaint(backgroundPaint);
   context.fill(new Rectangle2D.Double(0, 0, width, height));
 }