Java 摆动线图 [英] Java Swing Line Graph

查看:28
本文介绍了Java 摆动线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据数组中保存的数据绘制折线图.我已经设法绘制了 X 和 Y 轴.但是,当我在图形上绘制带有数据的线时,它与 X 和 Y 轴值不对齐.我不确定如何解决这个问题.我已经展示了下面的代码:

public void drawLineG(Graphics g, int yCoords[]) {String maxY = Integer.toString(getMax(yCoords));String minY = Integer.toString(getMin(yCoords));String maxX = Double.toString((xInterval * yCoords.length) - xInterval);String minX = Double.toString(xStart);g.setColor(Color.BLUE);int 高度 = (getHeight()/2);整数 x = 200;//起点 x 点//之前的坐标位置.int prevX = x;int prevY = yCoords[0];g.setColor(Color.BLACK);g.drawLine(prevX, getHeight()/2, 500, getHeight()/2);g.drawLine(prevX, 200, 200, getHeight()/2);g.setColor(Color.BLUE);g.drawString(minY, 190, (getHeight()/2));g.drawString(maxY, 180, (getHeight()/2) - 255);g.drawString(yLabel, 140, (getHeight()/2) - 100);g.drawString(minX, 192, (getHeight()/2) + 20);g.drawString(maxX, 500, (getHeight()/2) + 20);g.drawString(xLabel, 350, (getHeight()/2) + 50);for (int y : yCoords) {g.setColor(Color.RED);g.drawLine(prevX, height - prevY, x, height - y);上一个X = x;上一个 = y;//向 X 位置添加增量.x = x + 50;}}

我一直在测试的数据数组包含以下值:0, 3, 4, 7, 5, 10, 3

然而,当它绘制在图表上时,它与 x 和 y 轴上的值不一致.

当前:

I'm attempting to draw a line graph based on data held in array. I have managed to get the X and Y axis drawn. However when I draw the line with the data onto the graph it does not line up with the X and Y axis values. I am unsure of how this can be fixed. I have shown the code below:

public void drawLineG(Graphics g, int yCoords[]) {
    String maxY = Integer.toString(getMax(yCoords));
    String minY = Integer.toString(getMin(yCoords));
    String maxX = Double.toString((xInterval * yCoords.length) - xInterval);
    String minX = Double.toString(xStart);

    g.setColor(Color.BLUE);
    int height = (getHeight() / 2);
    int x = 200; // start x point

    // previous coord positions.
    int prevX = x;
    int prevY = yCoords[0];

    g.setColor(Color.BLACK);
    g.drawLine(prevX, getHeight() / 2, 500, getHeight() / 2);
    g.drawLine(prevX, 200, 200, getHeight() / 2);

    g.setColor(Color.BLUE);
    g.drawString(minY, 190, (getHeight() / 2));
    g.drawString(maxY, 180, (getHeight() / 2) - 255);
    g.drawString(yLabel, 140, (getHeight() / 2) - 100);
    g.drawString(minX, 192, (getHeight() / 2) + 20);
    g.drawString(maxX, 500, (getHeight() / 2) + 20);
    g.drawString(xLabel, 350, (getHeight() / 2) + 50);
    for (int y : yCoords) {
        g.setColor(Color.RED);
        g.drawLine(prevX, height - prevY, x, height - y);
        prevX = x;
        prevY = y;

        // add an increment to the X pos.
        x = x + 50;
    }
}

The array of data I have been testing contains the values: 0, 3, 4, 7, 5, 10, 3

However when this is plotted on the graph it doesn't line up with the values on the x and y axis.

Current: https://i.stack.imgur.com/Ju8BQ.jpg What I am trying to achieve: https://i.stack.imgur.com/n9Aio.jpg

Any help?
Thanks

解决方案

Your problem is your scale, you're trying to draw your yCoords as they're given, (3, 10 and so on) but they should be like: 30, 100 to fit the window's scale (Window = your program window).

For example in my code below, each "square" or each "unit" are of 30 x 30 pixels each, so I must convert yCoord = 3 to it's equivalent which would be 3 * 30 = 90 but as the axis is below starting at 400 I must substract it so I get the coord from the axis or from the bottom of the window to the real yCoord which would be 400 - 90 = 310, now this is my real point that I must paint.

However the painting should be done in the paintComponent method.

For my example I didn't draw the Strings for 0, 10, 0.0, 90.0 but you can do it later if you wish

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

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

public class GraphSample {
    private JFrame frame;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new GraphSample()::createAndShowGui);
    }

    private void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());

        GraphDrawer drawer = new GraphDrawer(new int[] {0, 3, 4, 7, 5, 10, 3});

        frame.add(drawer);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @SuppressWarnings("serial")
    class GraphDrawer extends JPanel {
        private int[] yCoords;
        private int startX = 100;
        private int startY = 100;
        private int endX = 400;
        private int endY = 400;
        private int unitX = (endX - startX) / 10;
        private int unitY = (endY - startY) / 10;
        private int prevX = startX;
        private int prevY = endY;

        public GraphDrawer(int[] yCoords) {
            this.yCoords = yCoords;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;

            //We draw in the following 2 loops the grid so it's visible what I explained before about each "unit"
            g2d.setColor(Color.BLUE);
            for (int i = startX; i <= endX; i += unitX) {
                g2d.drawLine(i, startY, i, endY);
            }

            for (int i = startY; i <= endY; i += unitY) {
                g2d.drawLine(startX, i, endX, i);
            }

            //We draw the axis here instead of before because otherwise they would become blue colored.
            g2d.setColor(Color.BLACK);
            g2d.drawLine(startX, startY, startX, endY);
            g2d.drawLine(startX, endY, endX, endY);

            //We draw each of our coords in red color
            g2d.setColor(Color.RED);
            for (int y : yCoords) {
                g2d.drawLine(prevX, prevY, prevX += unitX, prevY = endY - (y * unitY));
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(endX + 100, endY + 100);
        }
    }
}

I hope it's clear what the error was.

This is a sample of what the output looks like with a 10 x 10 grid

这篇关于Java 摆动线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆