public class BarChart extends JComponent implements Serializable
{
int width;
int height;
String chartName;
String chartMark;
Calculation calc;
float temp=0;
float []array;
float heightdia;
float widthdia;
float x0p;
float y0p;
float spacex;
float spacey;
float balkwidth;
float spacenumbx;
float spacenumby;
public BarChart()
{
}
public BarChart(int width,int height,String chartName,String chartMark,Calculation calc)
{
this.chartName=chartName;
this.chartMark=chartMark;
this.width=width;
this.height=height;
this.calc=calc;
heightdia=(height-(height/10)); // H�he des Diagramms
widthdia=(width-(width/10)); // Breite des Diagramms
x0p=((width/10)); // x des Nullpunktes
y0p=(heightdia); // y des Nullpunktes
spacex=widthdia/10; // Abstand der Zahlen auf der x Achse
spacey=heightdia/11; // Abstand der Prozente auf der y Achse
balkwidth=widthdia/15; // Breite des Balkens
spacenumbx=((height/20)); // Abstand der Zahlen zur x Achse
spacenumby=((width/12)); // Abstand der Zahlen zur y Achse
array=calc.getBarHeight(chartName,chartMark); // Balkenh�he berrechnen
for(int i=0;i<9;i++)
{
array[i]=array[i]*(spacey/10); // Umrechnen der H�he von % in Pixel , (spacey/10)entspricht der H�he von 1 Prozent
}
}
@Override
public void paintComponent(Graphics g)
{
Font font = new Font("Serif",Font.PLAIN,10);
g.setFont(font);
g.setColor(Color.black);
//y Achse zeichnen
g.drawLine((int)x0p,(int)y0p,(int)x0p,(int)(y0p-heightdia));
//x Achse zeichnen
g.drawLine((int)x0p,(int)y0p,(int)(x0p+widthdia),(int)y0p);
// Zahlen zeichnen unter der x Achse
/* for(int i=1;i<=9;i++)
{
g.drawString(Integer.toString(i),(int)(x0p+(spacex*i)),(int)(y0p+spacenumbx));
}*/
// Prozente zeichnen neberen der y Achse
for(int i=10;i<=100;i=i+10)
{
g.drawString(Integer.toString(i)+"%",(int)(x0p-spacenumby),(int)(y0p-(spacey*(i/10))));
g.drawLine((int)(x0p-5),(int)(y0p-(spacey*(i/10))),(int)(x0p+5),(int)(y0p-(spacey*(i/10))));
}
for(int i=0;i<9;i++)
{
// Farbe wechseln f�r die eigenen Zahlen
if(chartName.equals("Einsteiger"))
{
if(calc.userData.einst==i+1)
{
g.setColor(Color.red);
}
}
if(chartName.equals("Rückrat"))
{
if(calc.userData.rück==i+1)
{
g.setColor(Color.red);
}
}
if(chartName.equals("Aussteiger"))
{
if(calc.userData.ausst==i+1)
{
g.setColor(Color.red);
}
}
if(chartName.equals("Mischfeld1"))
{
if(calc.userData.misch1==i+1)
{
g.setColor(Color.red);
}
}
if(chartName.equals("Mischfeld2"))
{
if(calc.userData.misch2==i+1)
{
g.setColor(Color.red);
}
}
if(chartName.equals("Carrot"))
{
if(calc.userData.carrot==i+1)
{
g.setColor(Color.red);
}
}
g.drawString(Integer.toString(i+1),(int)(x0p+(spacex*(i+1))),(int)(y0p+spacenumbx)); // Zahlen zeichnen unter der x Achse
g.drawRect((int)((x0p+(spacex*(i+1)))-balkwidth/2),(int)(y0p)-(int)(array[i]),(int)balkwidth,(int)array[i]); // Balken zeichnen
g.setColor(Color.black);
}
}
}