package Principal;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.CharBuffer;
import java.util.HashMap;
import java.util.Map;
public class TestaGráfico {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO Auto-generated method stub
Map<String,Double> mapa = new HashMap<String, Double>();
mapa.put("Ásia", 4.57);
mapa.put("África", 1.18);
mapa.put("Europa", 0.705);
mapa.put("America", 1.03);
mapa.put("Oceania", 0.038);
ChartBuilder g = new ChartBuilder(mapa)
.criaGrafico("População Mundial en 2020")
.salvar(new FileOutputStream("grafico.png"));
}
}
[Ajuda] - Fluent Interface
11 Respostas
Entschuldigung - há algum import para a classe ChartBuilder? Ela está no mesmo package?
Ela esta no mesmo package não precisa de import.
Como é que é a definição dos métodos “criaGrafico” e “salvar”? Eles têm de ter o tipo de retorno “ChartBuilder” e devem retornar “this”.
@SuppressWarnings("deprecation")
public void criaGrafico(String titulo){
this.grafico = ChartFactory.createPieChart3D(titulo, this.dataSet, true, false, true);
this.grafico.getPlot().setForegroundAlpha(0.5f);
this.grafico.setBackgroundPaint(Color.cyan);
PiePlot plotagem = (PiePlot) this.grafico.getPlot();
plotagem.setSectionPaint(0, Color.black);
}
public void salvar(OutputStream out) throws IOException{
ChartUtilities.writeChartAsPNG(out, this.grafico, 500, 400);
}
public void processChart(Object obj, Map map) {
JFreeChart chart = (JFreeChart)obj;
//Altero as cores das series de meus itens
CategoryPlot categoryPlot = chart.getCategoryPlot();
CategoryItemRenderer renderer = categoryPlot.getRenderer();
renderer.setSeriesPaint( 0, Color.BLUE );
renderer.setSeriesPaint( 1, Color.RED );
}
}
estes são os metodos que comentou
Pois é, fluent interface não pode usar métodos "void". Eles sempre devem retornar o próprio objeto. OK?
public ChartBuilder criaGrafico(String titulo){
this.grafico = ChartFactory.createPieChart3D(titulo, this.dataSet, true, false, true);
this.grafico.getPlot().setForegroundAlpha(0.5f);
this.grafico.setBackgroundPaint(Color.cyan);
PiePlot plotagem = (PiePlot) this.grafico.getPlot();
plotagem.setSectionPaint(0, Color.black);
return this; // <----
}
agora sim matou todas certinhu, deu bem certinhu como tu falou! valeow!
Ola thingol,
Gostaria de mais dicas sobre fluent Interfaces, se possivel…
Percebi no topico que os metodos devem retornar o mesmo objeto, existem outras regras?
Valeu!
OK , obrigado.
Estou com uma dúvida , existe diferença entre:
ChartBuilder g = new ChartBuilder(mapa).criaGrafico("População Mundial en 2020").salvar(new FileOutputStream("grafico.png"));
e partindo do new direto como este:
new ChartBuilder(mapa).criaGrafico("População Mundial en 2020").salvar(new FileOutputStream("grafico.png"));
Valeu!
A diferença é que você não grava o resultado numa variável g do tipo ChartBuilder. Talvez nem precise.
Ah sim entendi.
obrigado. 