Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFreeChart:如何在蜘蛛图中为系列设置渐变绘制_Java_Jfreechart - Fatal编程技术网

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

Java JFreeChart:如何在蜘蛛图中为系列设置渐变绘制,java,jfreechart,Java,Jfreechart,我有一张与此演示相关的图表: 但我需要这样做: 如何正确设置系列的渐变色?。以下是我所拥有的: public class SpiderWebChartDemo1 extends ApplicationFrame { public SpiderWebChartDemo1(String s) { super(s); JPanel jpanel = createDemoPanel(); jpanel.setPreferredSize(new

我有一张与此演示相关的图表:

但我需要这样做:

如何正确设置系列的渐变色?。以下是我所拥有的:

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是定义渐变起点和终点的位置

  • 在理想情况下,库中的每个设置都是可编辑的,但很多时候情况并非如此。我们可以通过覆盖子类中的方法来克服这个问题。您将需要重写SpiderWebPlot类并实现一些绘制方法。这是我写的一节速成课,就是这样
看看它实际绘制多边形的那一端。我直接从SpiderWebPlot源代码中获取了这一点,并更改了最末端。要在程序中使用它,请这样调用它
GradientSpiderWebPlot=新的GradientSpiderWebPlot(类别数据集,Color.decode(“#4282CE”)、Color.decode(“#9BC1FF”)、0.8f)

以下是结果

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()));
        }
    }
}

公共类GradientSpiderWebPlot扩展了SpiderWebPlot{
私有颜色开始颜色,结束颜色;
私人浮动阿尔法;
公共渐变SpiderWebPlot(类别数据集数据、颜色起始颜色、颜色结束颜色、浮点alpha){
//TODO自动生成的构造函数存根
超级(数据);
this.startColor=startColor;
this.endColor=endColor;
这个α=α;
}
@凌驾
受保护的空图纸(图2D g2,
矩形二维绘图区域,
Point2D中心,
PlotRenderingInfo,
int系列,int catCount,
双头,双头(W){
多边形=新多边形();
EntityCollection实体=空;
如果(信息!=null){
实体=info.getOwner().getEntityCollection();
}
//绘制数据。。。
对于(int cat=0;cat=0){//绘制多边形序列。。。
//找到从该轴中心开始的角度
双角度=getStartAngle()
+(getDirection().getFactor()*cat*360/catCount);
//以下角度计算将确保没有顶部
//垂直轴-如果不需要,这可能很有用
//给定的“出现”条件比
//其他。。
//+(getDirection().getFactor())
//*(cat+0.5)*360/catCount);
//在适当的距离端点处找到该点
//沿着上面确定的轴/角度,并将其添加到
//多边形
Point2D point=getWebPoint(绘图区域、角度、,
value/this.getMaxValue());
polygon.addPoint((int)point.getX(),(int)point.getY());
//在正在绘制的点处放置一个省略号。。
油漆=getSeriesPaint(系列);
Paint outlinePaint=getSeriesOutlinePaint(系列);
笔划大纲笔划=getSeriesOutlineStroke(系列);
Ellipse2D head=新的Ellipse2D.Double(point.getX())
-headW/2,point.getY()-headH/2,headW,
海德);
g2.设置油漆(油漆);
g2.填充(头部);
g2.设定行程(外行程);
g2.设置油漆(概述油漆);
g2.牵引(头部);
if(实体!=null){
整行=0;整列=0;
if(this.getDataExtractOrder()==TableOrder.BY_行){
行=系列;
col=猫;
}
否则{
行=猫;
col=系列;
}
字符串提示=null;
if(this.getToolTipGenerator()!=null){
tip=this.getToolTipGenerator().generateToolTip(
this.getDataset(),行,列);
}
字符串url=null;
if(this.getURLGenerator()!=null){
url=this.getURLGenerator().generateURL(this.getDataset(),
行、列);
}
形状区域=新矩形(
(int)(点getX()-headW),
(int)(点getY()-headH),
(内部)(标题W*2)、(内部)(标题H*2));
CategoryItemEntity=新的CategoryItemEntity(
区域、提示、url、this.getDataset(),
此.getDataset().getRowKey(行),
this.getDataset().getColumnKey(col));
实体。添加(实体);
}
}
}
}
//图th