@Override
    public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {
      g2.setPaint(Color.blue);
      g2.setStroke(this.outlineStroke);
      Rectangle2D lengthRect = DialPlot.rectangleByRadius(frame, getRadius(), getRadius());
      Rectangle2D widthRect = DialPlot.rectangleByRadius(frame, getWidthRadius(), getWidthRadius());
      double value = plot.getValue(getDatasetIndex());
      DialScale scale = plot.getScaleForDataset(getDatasetIndex());
      double angle = scale.valueToAngle(value);

      Arc2D arc1 = new Arc2D.Double(lengthRect, angle, 0, Arc2D.OPEN);
      Point2D pt1 = arc1.getEndPoint();
      Arc2D arc2 = new Arc2D.Double(widthRect, angle - 90.0, 180.0, Arc2D.OPEN);
      Point2D pt2 = arc2.getStartPoint();
      Point2D pt3 = arc2.getEndPoint();
      Arc2D arc3 = new Arc2D.Double(widthRect, angle - 180.0, 0.0, Arc2D.OPEN);
      Point2D pt4 = arc3.getStartPoint();

      GeneralPath gp = new GeneralPath();
      gp.moveTo((float) pt1.getX(), (float) pt1.getY());
      gp.lineTo((float) pt2.getX(), (float) pt2.getY());
      gp.lineTo((float) pt4.getX(), (float) pt4.getY());
      gp.lineTo((float) pt3.getX(), (float) pt3.getY());
      gp.closePath();
      g2.setPaint(getFillPaint());
      g2.fill(gp);

      g2.setPaint(getOutlinePaint());
      Line2D line =
          new Line2D.Double(frame.getCenterX(), frame.getCenterY(), pt1.getX(), pt1.getY());
      g2.draw(line);

      line.setLine(pt2, pt3);
      g2.draw(line);

      line.setLine(pt3, pt1);
      g2.draw(line);

      line.setLine(pt2, pt1);
      g2.draw(line);

      line.setLine(pt2, pt4);
      g2.draw(line);

      line.setLine(pt3, pt4);
      g2.draw(line);
    }
Exemplo n.º 2
0
  /** @param arc the Arc2D object to be converted */
  public Element toSVG(Arc2D arc) {
    double ext = arc.getAngleExtent();
    double width = arc.getWidth();
    double height = arc.getHeight();

    if (width == 0 || height == 0) {
      Line2D line =
          new Line2D.Double(arc.getX(), arc.getY(), arc.getX() + width, arc.getY() + height);
      if (svgLine == null) {
        svgLine = new SVGLine(generatorContext);
      }
      return svgLine.toSVG(line);
    }

    if (ext >= 360 || ext <= -360) {
      Ellipse2D ellipse = new Ellipse2D.Double(arc.getX(), arc.getY(), width, height);
      if (svgEllipse == null) {
        svgEllipse = new SVGEllipse(generatorContext);
      }
      return svgEllipse.toSVG(ellipse);
    }

    Element svgPath = generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI, SVG_PATH_TAG);
    StringBuffer d = new StringBuffer("");

    Point2D startPt = arc.getStartPoint();
    Point2D endPt = arc.getEndPoint();
    int type = arc.getArcType();

    d.append(PATH_MOVE);
    d.append(doubleString(startPt.getX()));
    d.append(SPACE);
    d.append(doubleString(startPt.getY()));
    d.append(SPACE);

    d.append(PATH_ARC);
    d.append(doubleString(width / 2));
    d.append(SPACE);
    d.append(doubleString(height / 2));
    d.append(SPACE);
    d.append("0"); // no rotation with J2D arc.
    d.append(SPACE);
    if (ext > 0) {
      // CCW sweep case, ext > 0
      if (ext > 180) d.append("1"); // use large arc.
      else d.append("0"); // use small arc.
      d.append(SPACE);
      d.append("0"); // sweep ccw
    } else {
      // CW sweep case, ext < 0
      if (ext < -180) d.append("1"); // use large arc.
      else d.append("0"); // use small arc.
      d.append(SPACE);
      d.append("1"); // sweep cw
    }

    d.append(SPACE);
    d.append(doubleString(endPt.getX()));
    d.append(SPACE);
    d.append(doubleString(endPt.getY()));

    if (type == Arc2D.CHORD) {
      d.append(PATH_CLOSE);
    } else if (type == Arc2D.PIE) {
      double cx = arc.getX() + width / 2;
      double cy = arc.getY() + height / 2;
      d.append(PATH_LINE_TO);
      d.append(SPACE);
      d.append(doubleString(cx));
      d.append(SPACE);
      d.append(doubleString(cy));
      d.append(SPACE);
      d.append(PATH_CLOSE);
    }
    svgPath.setAttributeNS(null, SVG_D_ATTRIBUTE, d.toString());
    return svgPath;
  }
Exemplo n.º 3
0
  /**
   * Draws the scale on the dial plot.
   *
   * @param g2 the graphics target (<code>null</code> not permitted).
   * @param plot the dial plot (<code>null</code> not permitted).
   * @param frame the reference frame that is used to construct the geometry of the plot (<code>null
   *     </code> not permitted).
   * @param view the visible part of the plot (<code>null</code> not permitted).
   */
  public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {

    Rectangle2D arcRect =
        DialPlot.rectangleByRadius(frame, this.getTickRadius(), this.getTickRadius());
    Rectangle2D arcRectMajor =
        DialPlot.rectangleByRadius(
            frame,
            this.getTickRadius() - this.getMajorTickLength(),
            this.getTickRadius() - this.getMajorTickLength());
    Rectangle2D arcRectMinor = arcRect;
    if (this.getMinorTickCount() > 0 && this.getMinorTickLength() > 0.0) {
      arcRectMinor =
          DialPlot.rectangleByRadius(
              frame,
              this.getTickRadius() - this.getMinorTickLength(),
              this.getTickRadius() - this.getMinorTickLength());
    }
    Rectangle2D arcRectForLabels =
        DialPlot.rectangleByRadius(
            frame,
            this.getTickRadius() - this.getTickLabelOffset(),
            this.getTickRadius() - this.getTickLabelOffset());

    boolean firstLabel = true;

    Arc2D arc = new Arc2D.Double();
    Line2D workingLine = new Line2D.Double();
    Stroke arcStroke = new BasicStroke(0.75f);

    for (double v = this.getLowerBound();
        v <= this.getUpperBound();
        v += this.getMajorTickIncrement()) {
      arc.setArc(arcRect, this.getStartAngle(), valueToAngle(v) - this.getStartAngle(), Arc2D.OPEN);
      g2.setPaint(this.getMajorTickPaint());
      g2.setStroke(arcStroke);
      g2.draw(arc);

      Point2D pt0 = arc.getEndPoint();
      arc.setArc(
          arcRectMajor, this.getStartAngle(), valueToAngle(v) - this.getStartAngle(), Arc2D.OPEN);
      Point2D pt1 = arc.getEndPoint();
      g2.setPaint(this.getMajorTickPaint());
      g2.setStroke(this.getMajorTickStroke());
      workingLine.setLine(pt0, pt1);
      g2.draw(workingLine);
      arc.setArc(
          arcRectForLabels,
          this.getStartAngle(),
          valueToAngle(v) - this.getStartAngle(),
          Arc2D.OPEN);
      Point2D pt2 = arc.getEndPoint();

      if (this.getTickLabelsVisible()) {
        if (!firstLabel || this.getFirstTickLabelVisible()) {
          g2.setFont(this.getTickLabelFont());
          TextUtilities.drawAlignedString(
              this.getTickLabelFormatter().format(v),
              g2,
              (float) pt2.getX(),
              (float) pt2.getY(),
              TextAnchor.CENTER);
        }
      }
      firstLabel = false;

      // now do the minor tick marks
      if (this.getMinorTickCount() > 0 && this.getMinorTickLength() > 0.0) {
        double minorTickIncrement = this.getMajorTickIncrement() / (this.getMinorTickCount() + 1);
        for (int i = 0; i < this.getMinorTickCount(); i++) {
          double vv = v + ((i + 1) * minorTickIncrement);
          if (vv >= this.getUpperBound()) {
            break;
          }
          double angle = valueToAngle(vv);

          arc.setArc(arcRect, this.getStartAngle(), angle - this.getStartAngle(), Arc2D.OPEN);
          pt0 = arc.getEndPoint();
          arc.setArc(arcRectMinor, this.getStartAngle(), angle - this.getStartAngle(), Arc2D.OPEN);
          Point2D pt3 = arc.getEndPoint();
          g2.setStroke(this.getMinorTickStroke());
          g2.setPaint(this.getMinorTickPaint());
          workingLine.setLine(pt0, pt3);
          g2.draw(workingLine);
        }
      }
    }
  }