2009-12-13 88 views
1

好吧,我知道JFreeChart和其他人,但我編寫了我自己的簡單散點圖。我已經下了一個箱形圖(沒有Y軸標籤,但是當我在我的報告中解釋它時,這應該不是一個大問題)。具有多個「散射線」的散點圖

我有一個基本的散點圖類,但我試圖改變它,以便我可以添加不同的散點值。

它的工作原理,但它只接受第一個分散數組,並沒有繪製其餘的。雖然它確實在最後一個分散數組的顏色中繪製了第一個分散數組......所以它是半工作的。

這裏是我的全部ScatterPanel類:

import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.FontMetrics; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.geom.Line2D; 
import javax.swing.JPanel; 

public class ScatterPanel extends JPanel { 
    private TwoArray[] values; 
    private String title; 
    private String[] color_list; 

    // Constructor for ScatterPanel 
    public ScatterPanel(TwoArray[] v, String t, String[] c) { 
     values = v; 
     title = t; 
     color_list = c; 
    } 

    /* This will paint the scatter chart 
    * using the values from the above variables: 
    * "values", "title" and "color_list" 
    */ 
    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D)g; 

     // Initialize the titleFont 
     Font titleFont = new Font("Verdana", Font.BOLD, 16); 
     FontMetrics titleFontMetrics = g.getFontMetrics(titleFont); 

     // Get the width of the JPanel 
     Dimension d = getSize(); 
     int clientWidth = d.width; 
     //int clientHeight = d.height; 

     // Setup the title position and size 
     int titleWidth = titleFontMetrics.stringWidth(title); 
     int title_y = titleFontMetrics.getAscent(); 
     int title_x = (clientWidth - titleWidth)/2; 

     // Set the font for the title 
     g.setFont(titleFont); 

     // Draw the title 
     g.drawString(title, title_x, title_y); 

     // Initialise min and max display scale 
     double min = -0.5; 
     double max = 5; 

     // Iterate through each different algorithm we are comparing 
     for(int point = 0; point < values.length; point++) { 
      // Iterate through each algorithm's size array and timing array 
      for (int i = 0; i < values[point].array_time.length; i++) { 
       // Find the overall max and min for x and y 
       double x = (double) values[point].array_size[i]; 
       double y = (double) values[point].array_time[i]; 
       // Adjust max and min to include x and y. 
       if (x < min) 
        min = x - 0.5; 
       if (x > max) 
        max = x + 0.5; 
       if (y < min) 
        min = y - 0.5; 
       if (y > max) 
        max = y + 0.5; 
      } 
     } 

     g2.translate(getWidth()/2,getHeight()/2); 
     g2.scale(getWidth()/(max-min), -getHeight()/(max-min)); 
     g2.translate(-(max+min)/2, -(max+min)/2); 

     // Horizontal size of a pixel in new coords. 
     double pixelWidth = (max-min)/getWidth(); 

     // Vertical size of a pixel in new coord. 
     double pixelHeight = (max-min)/getHeight(); 

     g2.setStroke(new BasicStroke(0)); 

     // Draw the x and y axis 
     g2.setColor(Color.BLUE); 
     g2.draw(new Line2D.Double(min,0,max,0)); 
     g2.draw(new Line2D.Double(0,min,0,max)); 

     for(int point = 0; point < values.length; point++) { 
      if(point % 3 == 0) 
       g2.setColor(Color.decode(color_list[0])); 
      else if(point % 3 == 1) 
       g2.setColor(Color.decode(color_list[4])); 
      else if(point % 3 == 2) 
       g2.setColor(Color.decode(color_list[8])); 

      for (int i = 0; i < values[point].array_time.length; i++) { 
       long x = values[point].array_size[i]; 
       long y = values[point].array_time[i]; 

       // Plot the x-y co-ords 
       g2.draw(new Line2D.Double(x-3*pixelWidth,y,x+3*pixelWidth,y)); 
       g2.draw(new Line2D.Double(x,y-3*pixelHeight,x,y+3*pixelHeight)); 
      } 
     } 
    } 
} 

TwoArray只是用來存儲兩個長數組。

在我的主界面類,我繪製的分佈圖是這樣的:

for(int i = 0; i < scat_size.length; i++) 
    scat_size[i] = i; 

for(int i = 0; i < scat_times.length; i++) 
    scat_times[i] = i; 

// This should be 1,1 2,2 3,3 etc. in Red 
scatter_values[0] = new TwoArray(scat_size, scat_times); 

// Trying to test a large co-ord so this should be green 
scat_size[2] = 70; 
scat_times[2] = 20; 
scatter_values[1] = new TwoArray(scat_size, scat_times); 

// Trying to test another different co-ord so this should be blue 
scat_size[2] = 3; 
scat_times[2] = 7; 
scatter_values[2] = new TwoArray(scat_size, scat_times); 

myScatter = new ScatterPanel(scatter_values, scat_title, color_list); 

一個JPanel設置爲myScatter。它的工作原理和繪製散點圖的方法很好,但它並沒有繪製不同顏色的點,並且它以藍色繪製「紅色散點圖」。

乾杯傢伙。

P.S.我知道我沒有任何代碼繪製通過分散曲線,但我會在完成此部分後處理它=)

回答

1

scatter_values []將包含指向數組scat_size和scat_times的指針。您正在更改這些數組中的值,因此更改將應用​​於scatter_values數組中的所有項目。因此它會在第三張圖上繪製三次。

您需要的尺寸添加到陣列:

for(int j = 0; j < 3; j++) { 
    for(int i = 0; i < scat_size.length; i++) 
     scat_size[j][i] = i; 

    for(int i = 0; i < scat_times.length; i++) 
     scat_times[j][i] = i; 
} 

// This should be 1,1 2,2 3,3 etc. in Red 
scatter_values[0] = new TwoArray(scat_size[0], scat_times[0]); 

// Trying to test a large co-ord so this should be green 
scat_size[1][2] = 70; 
scat_times[1][2] = 20; 
scatter_values[1] = new TwoArray(scat_size[1], scat_times[1]); 

// Trying to test another different co-ord so this should be blue 
scat_size[2][2] = 3; 
scat_times[2][2] = 7; 
scatter_values[2] = new TwoArray(scat_size[2], scat_times[2]); 

myScatter = new ScatterPanel(scatter_values, scat_title, color_list); 
+0

啊,我只是使用不同的陣列狀改變了我的界面:scat_size2和scat_size3,它顯示了所有3個陣列! 非常感謝! – dan2k3k4 2009-12-13 01:16:32