Sto lavorando a un grafico a torta con 4 elementi diversi. Posso far comparire gli elementi in JFrame ma non riesco a completare il cerchio del grafico a torta. Questi sono gli elementi:impossibile completare il cerchio di un grafico a torta
public static class PieChart extends JComponent {
IAPieChart[] pieValue = {new IAPieChart(5, Color.green),
new IAPieChart(4, Color.orange),
new IAPieChart(3, Color.blue),
new IAPieChart(2, Color.red)
};
Ora che gli elementi sono istanziati, e caricati nella matrice, io uso questo metodo per dipingerli:
public void paintComponent(Graphics g) {
drawPie((Graphics2D) g, getBounds(), pieValue);
}
Questo metodo mette insieme gli elementi sul JFrame ma mi dà solo circa 120 gradi del cerchio. :
void drawPie(Graphics2D g, Rectangle area, IAPieChart[] pieValue){
double sum = 0.0;
for (int i = 0; i < pieValue.length; i++) {
sum += pieValue[i].arcValue;
}
double endPoint = 0.0D;
int arcStart = 0;
for (int i = 0; i < pieValue.length; i++){
endPoint = (int) (endPoint * 360/sum);
int radius = (int) (pieValue[i].arcValue * 360/ sum);
g.setColor(pieValue[i].color);
g.fillArc(area.x, area.y, area.width, area.height, arcStart , radius);
radius += pieValue[i].arcValue;
}
}
}
Sono in perdita. Ho circa 10 settimane di esperienza con Java, quindi questo è principalmente per tentativi ed errori. Sto cercando un modo per completare il cerchio. Ho ridotto i valori al punto più basso che avrebbe mostrato tutti i colori. Qualunque cosa più grande eliminerà un colore o l'altro.
Spero che tu possa aiutare. Qui è la piena IAPieChart programma:
package iapiechart;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.JFrame;
class IAPieChart{
double arcValue; // passes a value for the calculation of the arc.
Color color; // holds value for color (expressed as an integer
public IAPieChart(double value, Color color){
this.arcValue = value;
this.color = color;
}
public static class PieChart extends JComponent {
IAPieChart[] pieValue = {new IAPieChart(5, Color.green),
new IAPieChart(4, Color.orange),
new IAPieChart(3, Color.blue),
new IAPieChart(2, Color.red)
};
public void paintComponent(Graphics g) {
drawPie((Graphics2D) g, getBounds(), pieValue);
}
void drawPie(Graphics2D g, Rectangle area, IAPieChart[] pieValue){
double sum = 0.0;
for (int i = 0; i < pieValue.length; i++) {
sum += pieValue[i].arcValue;
}
double endPoint = 0.0D;
int arcStart = 0;
for (int i = 0; i < pieValue.length; i++){
endPoint = (int) (endPoint * 360/sum);
int radius = (int) (pieValue[i].arcValue * 360/ sum);
g.setColor(pieValue[i].color);
g.fillArc(area.x, area.y, area.width, area.height, arcStart , radius);
radius += pieValue[i].arcValue;
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new PieChart());
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
cosa si intende per risultato? – mishadoff
+1 per [sscce] (http://sscce.org/). Vedi anche [tag: jfreechart], che include ['PiePlot'] (http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/plot/PiePlot.html) con [source] (http : //www.jfree.org/jfreechart/api/javadoc/src-html/org/jfree/chart/plot/PiePlot.html). – trashgod