java - JFreeChart:如何为蜘蛛图中的系列设置渐变绘制

标签 java jfreechart

我有一个包含此演示文稿的图表:

Current chart

但我需要这样做:

Expected chart

如何正确设置系列的渐变绘画?这是我所拥有的:

public class SpiderWebChartDemo1 extends ApplicationFrame {

    public SpiderWebChartDemo1(String s) {
        super(s);
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(jpanel);
    }

    private static CategoryDataset createDataset() {
        String s = "First";
        String s3 = "Self leadership";
        String s4 = "Organization leadership";
        String s5 = "Team leadership";
        
        DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
        defaultcategorydataset.addValue(1.0D, s, s3);
        defaultcategorydataset.addValue(4D, s, s4);
        defaultcategorydataset.addValue(3D, s, s5);
        return defaultcategorydataset;
    }

    private static JFreeChart createChart(CategoryDataset categorydataset) {
        Color bckColor1 = Color.decode("#4282CE"); //Light blue
        Color bckColor2 = Color.decode("#9BC1FF"); //Dark blue
        Color axisColor = Color.decode("#DD0010"); //Red
        
        SpiderWebPlot plot = new SpiderWebPlot(categorydataset);
        Paint p = new GradientPaint(0,0,bckColor1,0,0,bckColor2);
        
        plot.setSeriesPaint(p);
        plot.setAxisLinePaint(axisColor);
        
        JFreeChart chart = new JFreeChart("Spider Web Chart Demo 1"
                , TextTitle.DEFAULT_FONT, plot, false);

        LegendTitle legendtitle = new LegendTitle(plot);
        legendtitle.setPosition(RectangleEdge.BOTTOM);
        chart.addSubtitle(legendtitle);
        return chart;
    }

    public static JPanel createDemoPanel() {
        JFreeChart jfreechart = createChart(createDataset());
        return new ChartPanel(jfreechart);
    }

    public static void main(String args[]) {
        SpiderWebChartDemo1 spiderwebchartdemo1 = new SpiderWebChartDemo1("SpiderWebChartDemo1");
        spiderwebchartdemo1.pack();
        RefineryUtilities.centerFrameOnScreen(spiderwebchartdemo1);
        spiderwebchartdemo1.setVisible(true);
    }
}

我在条形图中看到了渐变绘制,但在蜘蛛图中没有看到。我得到的只是透明系列。

谢谢。

最佳答案

您正确设置了油漆,但是您应该意识到两件事。

  • Java 中的渐变绘画声明起点和终点。第一种颜色将从点 1 开始,并在点 2 处转换为颜色 2。如果使用它来绘制多边形,则这些点与多边形尺寸无关。这是要显示的图片,图片中的 pt1 和 pt2 是定义渐变起点和终点的位置。

enter image description here

  • 在理想的世界中,每个设置都可以在库中进行编辑,但很多时候情况并非如此。我们可以通过覆盖子类中的方法来克服这个问题。您将需要重写 SpiderWebPlot 类并实现一些绘画方法。这是我写的一个快速类(class),它就是这样做的。

看看它实际绘制多边形的最末端。我直接从 SpiderWebPlot 源中获取此内容并更改了最后部分。要在您的程序中使用它,请像这样调用它 GradientSpiderWebPlot 图 = new GradientSpiderWebPlot(categorydataset, Color.decode("#4282CE"), Color.decode("#9BC1FF"), .8f);

这是结果

enter image description here

public class GradientSpiderWebPlot extends SpiderWebPlot {

    private Color startColor, endColor;
    private float alpha;

    public GradientSpiderWebPlot(CategoryDataset data, Color startColor, Color endColor, float alpha) {
        // TODO Auto-generated constructor stub
        super(data);
        this.startColor = startColor;
        this.endColor = endColor;
        this.alpha = alpha;
    }

    @Override
    protected void drawRadarPoly(Graphics2D g2,
                                 Rectangle2D plotArea,
                                 Point2D centre,
                                 PlotRenderingInfo info,
                                 int series, int catCount,
                                 double headH, double headW) {

        Polygon polygon = new Polygon();

        EntityCollection entities = null;
        if (info != null) {
            entities = info.getOwner().getEntityCollection();
        }

        // plot the data...
        for (int cat = 0; cat < catCount; cat++) {

            Number dataValue = getPlotValue(series, cat);

            if (dataValue != null) {
                double value = dataValue.doubleValue();

                if (value >= 0) { // draw the polygon series...

                    // Finds our starting angle from the centre for this axis

                    double angle = getStartAngle()
                        + (getDirection().getFactor() * cat * 360 / catCount);

                    // The following angle calc will ensure there isn't a top
                    // vertical axis - this may be useful if you don't want any
                    // given criteria to 'appear' move important than the
                    // others..
                    //  + (getDirection().getFactor()
                    //        * (cat + 0.5) * 360 / catCount);

                    // find the point at the appropriate distance end point
                    // along the axis/angle identified above and add it to the
                    // polygon

                    Point2D point = getWebPoint(plotArea, angle,
                            value / this.getMaxValue());
                    polygon.addPoint((int) point.getX(), (int) point.getY());

                    // put an elipse at the point being plotted..

                    Paint paint = getSeriesPaint(series);
                    Paint outlinePaint = getSeriesOutlinePaint(series);
                    Stroke outlineStroke = getSeriesOutlineStroke(series);

                    Ellipse2D head = new Ellipse2D.Double(point.getX()
                            - headW / 2, point.getY() - headH / 2, headW,
                            headH);
                    g2.setPaint(paint);
                    g2.fill(head);
                    g2.setStroke(outlineStroke);
                    g2.setPaint(outlinePaint);
                    g2.draw(head);

                    if (entities != null) {
                        int row = 0; int col = 0;
                        if (this.getDataExtractOrder() == TableOrder.BY_ROW) {
                            row = series;
                            col = cat;
                        }
                        else {
                            row = cat;
                            col = series;
                        }
                        String tip = null;
                        if (this.getToolTipGenerator() != null) {
                            tip = this.getToolTipGenerator().generateToolTip(
                                    this.getDataset(), row, col);
                        }

                        String url = null;
                        if (this.getURLGenerator() != null) {
                            url = this.getURLGenerator().generateURL(this.getDataset(),
                                   row, col);
                        }

                        Shape area = new Rectangle(
                                (int) (point.getX() - headW),
                                (int) (point.getY() - headH),
                                (int) (headW * 2), (int) (headH * 2));
                        CategoryItemEntity entity = new CategoryItemEntity(
                                area, tip, url, this.getDataset(),
                                this.getDataset().getRowKey(row),
                                this.getDataset().getColumnKey(col));
                        entities.add(entity);
                    }

                }
            }
        }
        // Plot the polygon

        // Lastly, fill the web polygon if this is required

        Rectangle2D rec = polygon.getBounds2D();

        //Paint paint = getSeriesPaint(series);
        // create linear vertical gradient based upon the bounds of the polygon.
        Paint paint = new GradientPaint(new Point2D.Double(rec.getCenterX(),rec.getMinY()), startColor,
                new Point2D.Double(rec.getCenterX(),rec.getMaxY()), endColor);

        g2.setPaint(paint);
        g2.setStroke(getSeriesOutlineStroke(series));
        g2.draw(polygon);


        if (this.isWebFilled()) {
            // made this the variable alpha instead of the fixed .1f
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                    alpha));
            g2.fill(polygon);
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                    getForegroundAlpha()));
        }
    }
}

关于java - JFreeChart:如何为蜘蛛图中的系列设置渐变绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21996872/

相关文章:

java - 如何将 JFreechart 添加到面板?

charts - jfreechart 中的线上点

java - 为什么我可以导入 org.jfree.ui,但不能导入 org.jfree.chart?

java - 如何在我的 java 文件中实现旋转器?

java - 将位图保存到 SD 卡后图像质量下降

java - 是否可以让我的自定义验证注释在持久化时被忽略?

java - 信号量 - 为什么我的线程一个接一个地运行而不是并发运行?

java - 在映射键集流上应用带有 anyMatch() 的谓词,并在谓词为假时返回一些结果

java - JFreeChart 简单绘图(抛物线)

java - 将 JFreeChart 添加到 JPanel