JFreeChart:如何在蜘蛛图中为系列设置渐变绘画

时间:2014-02-24 19:17:04

标签: 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);
    }
}

我在条形图中看过渐变色,但不是蜘蛛图。我所得到的只是透明系列。

感谢。

1 个答案:

答案 0 :(得分:5)

您正在正确设置绘画,但有两件事你应该意识到。

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

enter image description here

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

看看实际绘制多边形的最末端。我直接从SpiderWebPlot源获取并改变了最终结果。要在程序中使用它,请像这样调用它 GradientSpiderWebPlot plot = 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()));
        }
    }
}