Domanda

Voglio cambiare il colore dei "pezzi" di torta nella mia jfreechart PieChart3D, questo è il codice che rende il piechart:

<% 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);
%>

Ogni aiuto è molto apprezzato.

È stato utile?

Soluzione

Il colore per ogni sezione è normalmente popolato da DrawingSupplier della trama. È possibile sovrascrivere le impostazioni di default, anche se, chiamando

PiePlot.setSectionPaint(Comparable key, Paint paint);

Con, questo, però, dovrete impostare manualmente ogni sezione. Se si desidera solo un diverso insieme di colori, sembra che si potrebbe implementare DrawingSupplier.

Altri suggerimenti

È possibile utilizzare

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

e come una classe interna:

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]); 
            } 
        } 
    } 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top