2013-03-04 38 views
0

我想用java和Swing製作條形圖。我看了下面鏈接的代碼 - http://www.java2s.com/Code/Java/2D-Graphics-GUI/Simplebarchart.htm需要編輯條形圖的揮杆GUI代碼

我想增加此圖表中的小節之間的空間。我該怎麼做?

代碼 -

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.FontMetrics; 
import java.awt.Graphics; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.awt.event.WindowListener; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class ChartPanel extends JPanel { 
private double[] values; 

private String[] names; 

private String title; 

public ChartPanel(double[] v, String[] n, String t) { 
names = n; 
values = v; 
title = t; 
} 

public void paintComponent(Graphics g) { 
super.paintComponent(g); 
if (values == null || values.length == 0) 
    return; 
double minValue = 0; 
double maxValue = 0; 
for (int i = 0; i < values.length; i++) { 
    if (minValue > values[i]) 
    minValue = values[i]; 
    if (maxValue < values[i]) 
    maxValue = values[i]; 
} 

Dimension d = getSize(); 
int clientWidth = d.width; 
int clientHeight = d.height; 
int barWidth = clientWidth/values.length; 

Font titleFont = new Font("SansSerif", Font.BOLD, 20); 
FontMetrics titleFontMetrics = g.getFontMetrics(titleFont); 
Font labelFont = new Font("SansSerif", Font.PLAIN, 10); 
FontMetrics labelFontMetrics = g.getFontMetrics(labelFont); 

int titleWidth = titleFontMetrics.stringWidth(title); 
int y = titleFontMetrics.getAscent(); 
int x = (clientWidth - titleWidth)/2; 
g.setFont(titleFont); 
g.drawString(title, x, y); 

int top = titleFontMetrics.getHeight(); 
int bottom = labelFontMetrics.getHeight(); 
if (maxValue == minValue) 
    return; 
double scale = (clientHeight - top - bottom)/(maxValue - minValue); 
y = clientHeight - labelFontMetrics.getDescent(); 
g.setFont(labelFont); 

for (int i = 0; i < values.length; i++) { 
    int valueX = i * barWidth + 1; 
    int valueY = top; 
    int height = (int) (values[i] * scale); 
    if (values[i] >= 0) 
    valueY += (int) ((maxValue - values[i]) * scale); 
    else { 
    valueY += (int) (maxValue * scale); 
    height = -height; 
    } 

    g.setColor(Color.red); 
    g.fillRect(valueX, valueY, barWidth - 2, height); 
    g.setColor(Color.black); 
    g.drawRect(valueX, valueY, barWidth - 2, height); 
    int labelWidth = labelFontMetrics.stringWidth(names[i]); 
    x = i * barWidth + (barWidth - labelWidth)/2; 
    g.drawString(names[i], x, y); 
} 
} 

public static void main(String[] argv) { 
JFrame f = new JFrame(); 
f.setSize(400, 300); 
double[] values = new double[3]; 
String[] names = new String[3]; 
values[0] = 1; 
names[0] = "Item 1"; 

values[1] = 2; 
names[1] = "Item 2"; 

values[2] = 4; 
names[2] = "Item 3"; 

f.getContentPane().add(new ChartPanel(values, names, "title")); 

WindowListener wndCloser = new WindowAdapter() { 
    public void windowClosing(WindowEvent e) { 
    System.exit(0); 
    } 
}; 
f.addWindowListener(wndCloser); 
f.setVisible(true); 
} 
} 

回答

1

下面是一行代碼,您可以設置X座標酒吧:

int valueX = i * barWidth + 1; 

要改變各條進一步,你可以把它改成:

int valueX = i * (barWidth+20) + 1; 

您可以爲此聲明一個單獨的類級變量:

int barSpace = 20; 

...//later in paintComponent. 
int valueX = i * (barWidth+space) + 1; 

UPDATE:這裏是與barWidth計算一行代碼:

int barWidth = clientWidth/values.length; 

爲適合您的客戶端區域的圖表,你可以使用下面的代碼:

barWidth-= barSpace; //or barWidth-=20; 

這你會從每個酒吧拿走一些空間

+0

謝謝。順便說一下,你能告訴我valueQ和valueP的用途是什麼嗎? – SuperStar 2013-03-04 10:12:37

+0

@SuperStar在你的代碼中找不到這樣的名字。 – 2013-03-04 10:14:50

+0

抱歉 - 我的意思是valueX和valueY。我從另一個網站複製了相同的代碼,將其稱爲P和Q代替:P – SuperStar 2013-03-04 10:19:23