2009-12-13 4 views
1

좋아, JFreeChart와 기타에 대해 알고 있지만 필자는 간단한 분산 형 그래프를 코딩하고 있습니다. 나는 이미 박스 차트를 가지고있다. (Y 축 레이블 없이도 보고서에서 설명 할 때 큰 문제는 안된다.)여러 개의 "산발 행"이있는 분산 형 그래프

저는 기본적인 분산 형 그래프 클래스를 가지고 있지만 다른 분산 값을 추가 할 수 있도록 변경하려고했습니다.

작동하지만 첫 번째 분산 배열 만 허용하고 나머지는 그립니다. 그것은 마지막 분산 배열의 색상에 첫 번째 분산 배열을 그리지 만 반 작업을합니다.

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 단지 두 개의 긴 배열을 저장하는 데 사용됩니다 :

여기 내 전체 ScatterPanel 클래스입니다.

내 주요 인터페이스 클래스의 내부에서,이 같은 분산 형 그래프를 그릴 :

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. 나는이 부분을 마친 후에도 곡선을 그리는 어떤 코드도 아직 가지고 있지 않다는 것을 알고있다.이 부분을 마친 후에 작업을하겠다.)

답변

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

관련 문제