【问题标题】:Drawing pie chart with PDFbox用 PDFbox 绘制饼图
【发布时间】:2021-03-31 17:08:46
【问题描述】:

我正在尝试用 PDFbox 绘制饼图,但是切片之间有白线,有人可以帮我吗?有这个选项吗?

附上我正在使用的绘制圆弧的代码:

while (start < stop) {
    List<Float> smallArc = PdfUtils.createSmallArc(a, b, radius, start, start + 
        2.0944 > stop ? stop : start + 2.0944);
    contentStream.saveGraphicsState();
    contentStream.setNonStrokingColor(components[0], components[1], 
        components[2]);
    contentStream.moveTo(smallArc.get(0), smallArc.get(1));
    contentStream.curveTo(smallArc.get(2), smallArc.get(3), smallArc.get(4), 
        smallArc.get(5), smallArc.get(6), smallArc.get(7));
    contentStream.fill();
    contentStream.restoreGraphicsState();
    start += 2.0944;
}


public static List<Float> createSmallArc(float x, float y, double r, double a1, double a2) {
    double a = (a2 - a1) / 2;
    double x4 = r * Math.cos(a);
    double y4 = r * Math.sin(a);
    double x1 = x4;
    double y1 = -y4;
    double q1 = x1 * x1 + y1 * y1;

    double q2 = q1 + x1 * x4 + y1 * y4;
    double k2 = 4 / 3d * (Math.sqrt(2 * q1 * q2) - q2) / (x1 * y4 - y1 * x4);
    double x2 = x1 - k2 * y1;
    double y2 = y1 + k2 * x1;
    double x3 = x2;
    double y3 = -y2;

    double ar = a + a1;
    double cos_ar = Math.cos(ar);
    double sin_ar = Math.sin(ar);

    List<Float> list = new ArrayList<Float>();
    list.add((float) (r * Math.cos(a1)) + x);
    list.add((float) -(r * Math.sin(a1)) + y);
    list.add((float) (x2 * cos_ar - y2 * sin_ar) + x);
    list.add((float) -(x2 * sin_ar + y2 * cos_ar) + y);
    list.add((float) (x3 * cos_ar - y3 * sin_ar) + x);
    list.add((float) -(x3 * sin_ar + y3 * cos_ar) + y);
    list.add((float) (r * Math.cos(a2)) + x);
    list.add((float) -(r * Math.sin(a2)) + y);
    return list;
}

结果图片:

谢谢

【问题讨论】:

  • 尝试strokeAndFill,并将描边颜色设置为与非描边颜色相同。你得到了什么?
  • startstop 在您的代码中没有更改,因此一旦进入 while 循环,您的程序将永远不会离开它,直到出现一些错误...
  • @mkl 我编辑了问题并添加了缺失的代码。
  • @TilmanHausherr 问题已解决,谢谢!

标签: java pdf pdfbox


【解决方案1】:

不要使用fill(),而是使用fillAndStroke() 方法并使用与setNonStrokingColor() 相同的参数调用setStrokingColor()

【讨论】:

    猜你喜欢
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    • 2019-01-03
    • 1970-01-01
    相关资源
    最近更新 更多