图轴问题

时间:2012-03-02 00:57:45

标签: java math graph charts

我正在使用来自线程的输出创建折线图,线程是在52秒的过程中运行的传入和传出账单的模拟,这将在线图上显示,如下所示,以显示银行余额52秒!

问题是我似乎无法正确计算Y轴。 下面的代码显示输出轴右上角的红色标记,但不是

enter image description here

public void paintComponent(Graphics g) {

    int y = 10000; // balance
    int x = 52 ; // weeks
    int prevX, prevY;
    int maxX = 52;
    int maxY = 10000;

    int Xleft = 200;
    int Xright = 900;
    int Ytop = 50;  
    int Ybottom = 330;// defining axis

    Graphics2D g2 = (Graphics2D) g;
    super.paintComponent(g2);
    g2.setColor(Color.BLUE);

    BasicStroke pen = new BasicStroke(4F);
    g2.setStroke(pen);

    g2.drawLine(Xleft,Ytop,Xleft,Ybottom);
    g2.drawLine(Xleft,280,Xright,280);

    Font f = new Font("Serif", Font.BOLD, 14);
    g2.setFont(f); 
    g2.drawString("Account Balance (£)", 35, 200);
    g2.drawString("Elapsed Time (Weeks)", 475, 340);



//retrieve values from your model for the declared variables     

//calculate the coords  line on the canvas

double balance = (((double)y / maxY) * Y_AXIS_LENGTH) - Y_AXIS_OFFSET; //floating point arithmetic
double weeks = (((double)x / maxX) * X_AXIS_LENGTH) + X_AXIS_OFFSET;

int xPos = (int) Math.round (weeks);
int yPos = (int)Math.round(balance); // changing back to int to be used in drawing oval

g2.setColor(Color.RED);
g.drawOval( xPos, yPos, 2, 2);
System.out.println(xPos + "  " + yPos);

}

1 个答案:

答案 0 :(得分:2)

不应该这样:

double balance = (((double)y / maxY) * Y_AXIS_LENGTH) - Y_AXIS_OFFSET;
是吗?

double balance = Y_AXIS_OFFSET - (((double)y / maxY) * Y_AXIS_LENGTH);