【问题标题】:Change the color of jfreechart piechart改变jfreechart饼图的颜色
【发布时间】:2009-12-07 11:02:29
【问题描述】:

我想在我的 jfreechart PieChart3D 中更改饼“块”的颜色,这是呈现饼图的代码:

<% response.setContentType("image/png"); %><%@page import="org.jfree.data.general.*"%><%@page import="org.jfree.chart.*"%><%@page import="org.jfree.chart.plot.*"%><%@page import="java.awt.Color" %><%

        DefaultPieDataset ds = (DefaultPieDataset)session.getAttribute("usagePieOutputDataset");

  JFreeChart chart = ChartFactory.createPieChart3D
  (
   null,  // Title
   ds,  // Dataset
   false,  // Show legend
   false,  // Use tooltips
   false  // Configure chart to generate URLs?
  );

     chart.setBackgroundPaint(Color.WHITE);
     chart.setBorderVisible(false);

  PiePlot3D plot = ( PiePlot3D )chart.getPlot();
  plot.setDepthFactor(0.0);
  plot.setLabelGenerator(null); //null means no labels

  plot.setLabelOutlinePaint(Color.LIGHT_GRAY);
  plot.setLabelFont(new java.awt.Font("Arial",  java.awt.Font.BOLD, 10));


  ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 150, 144);
%>

非常感谢任何帮助。

【问题讨论】:

    标签: java jfreechart


    【解决方案1】:

    每个部分的颜色通常由绘图的 DrawingSupplier 填充。不过,您可以通过调用

    来覆盖默认值
    PiePlot.setSectionPaint(Comparable key, Paint paint);
    

    不过,您需要手动设置每个部分。如果你只是想要一组不同的颜色,看起来你可以实现 DrawingSupplier。

    【讨论】:

    • 如何找回Comparable key ?是否有来自 plotPie 对象的方法?我试过plot.getSectionKey(index) 但它受到保护...
    【解决方案2】:

    你可以使用

     Color[] colors = {Color.green, Color.red, Color.yellow .. /* size of data set */}; 
     PieRenderer renderer = new PieRenderer(colors); 
     renderer.setColor(plot, ds);
    

    作为一个内部类:

    static class PieRenderer 
        { 
            private Color[] color; 
    
            public PieRenderer(Color[] color) 
            { 
                this.color = color; 
            }        
    
            public void setColor(PiePlot plot, DefaultPieDataset dataset) 
            { 
                List <Comparable> keys = dataset.getKeys(); 
                int aInt; 
    
                for (int i = 0; i < keys.size(); i++) 
                { 
                    aInt = i % this.color.length; 
                    plot.setSectionPaint(keys.get(i), this.color[aInt]); 
                } 
            } 
        } 
    

    【讨论】:

      猜你喜欢
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      • 2021-07-04
      相关资源
      最近更新 更多