Adding extra lines?

Hey,
The code in the 'related question' was suitable to me and I've managed to make a decent enough take on it but there's a few things I'm struggling with.

I've attached the code supplied. I've made a few changes like the increments of the data on the y axis.

I would like to be able to add another set of the data to the graph, I've added a new data array but when I go to edit the code that plots the second line it seems to mess with the previous one. I'm assuming this is something simple?

I'd also like to be able to change the X axis labels to days of the week rather than numbers.  Could I just do this with some sort of loop?

Thanks in advance
PS I'm trying to avoid JFreeChart. I know it's awesome but I want the self satisfaction of having something I've done myself
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
 
public class LineGraph
{
    public static void main(String[] args)
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new GraphPanel());
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}
 
class GraphPanel extends JPanel
{
    final int
        HPAD = 60,
        VPAD = 40;
    int[] data;
    Font font;
 
    public GraphPanel()
    {
        data = new int[] {
            120, 190, 211, 75, 30, 290, 182, 65, 85, 120, 100, 101
        };
        font = new Font("lucida sans regular", Font.PLAIN, 16);
        setBackground(Color.white);
    }
 
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setFont(font);
        FontRenderContext frc = g2.getFontRenderContext();
        int w = getWidth();
        int h = getHeight();
        // scales
        float xInc = (w - HPAD - VPAD) / 11f;
        float yInc = (h - 2*VPAD) / 10f;
        int[] dataVals = getDataVals();
        float yScale = dataVals[2] / 10f;
 
        // ordinate
        g2.draw(new Line2D.Double(HPAD, VPAD, HPAD, h - VPAD));
        // tic marks
        float x1 = HPAD, y1 = VPAD, x2 = HPAD - 3, y2;
        for(int j = 0; j < 10; j++)
        {
            g2.draw(new Line2D.Double(x1, y1, x2, y1));
            y1 += yInc;
        }
        // labels
        String text; LineMetrics lm;
        float xs, ys, textWidth, height;
        for(int j = 0; j <= 10; j++)
        {
            text = String.valueOf(dataVals[1] - (int)(j * yScale));
            textWidth = (float)font.getStringBounds(text, frc).getWidth();
            lm = font.getLineMetrics(text, frc);
            height = lm.getAscent();
            xs = HPAD - textWidth - 7;
            ys = VPAD + (j * yInc) + height/2;
            g2.drawString(text, xs, ys);
        }
 
        // abcissa
        g2.draw(new Line2D.Double(HPAD, h - VPAD, w - VPAD, h - VPAD));
        // tic marks
        x1 = HPAD; y1 = h - VPAD; y2 = y1 + 3;
        for(int j = 0; j < 12; j++)
        {
            g2.draw(new Line2D.Double(x1, y1, x1, y2));
            x1 += xInc;
        }
        // labels
        ys = h - VPAD;
        for(int j = 0; j < 12; j++)
        {
            text = String.valueOf(j + 1);
            textWidth = (float)font.getStringBounds(text, frc).getWidth();
            lm = font.getLineMetrics(text, frc);
            height = lm.getHeight();
            xs = HPAD + j * xInc - textWidth/2;
            g2.drawString(text, xs, ys + height);
        }
 
        // plot data
        x1 = HPAD;
        yScale = (float)(h - 2*VPAD) / dataVals[2];
        for(int j = 0; j < data.length; j++)
        {
            y1 = VPAD + (h - 2*VPAD) - (data[j] - dataVals[0]) * yScale;
            if(j > 0)
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
            x2 = x1;
            y2 = y1;
            x1 += xInc;
        }
    }
 
    private int[] getDataVals()
    {
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for(int j = 0; j < data.length; j++)
        {
            if(data[j] < min)
                min = data[j];
            if(data[j] > max)
                max = data[j];
        }
        int span = max - min;
        return new int[] { min, max, span };
    }
}

Open in new window

nicbhAsked:
Who is Participating?
 
manavilaiCommented:
hi, please see if this is useful
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
 
public class LineGraph
{
    public static void main(String[] args)
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new GraphPanel());
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}
 
class GraphPanel extends JPanel
{
    final int
        HPAD = 60,
        VPAD = 40;
    int[] data;
    int[] data2;
    String[] daw;
    Font font;
    Font font2;
 
    public GraphPanel()
    {
        data = new int[] {
            120, 190, 211, 75, 30, 290, 182
        };
        data2 = new int[] {
                110, 180, 200, 175, 130, 190, 142
            };
        daw = new String[] {
        		"Mon","Tue","Wed","Thu","Fri","Sat","Sun"
        };
        font = new Font("lucida sans regular", Font.PLAIN, 16);
        font2 = new Font("lucida sans regular", Font.PLAIN, 16);
        setBackground(Color.white);
    }
 
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setFont(font);
        FontRenderContext frc = g2.getFontRenderContext();
        int w = getWidth();
        int h = getHeight();
        // scales
        float xInc = (w - HPAD - VPAD) / 6f;
        float yInc = (h - 2*VPAD) / 10f;
        int[] dataVals = getDataVals();
        float yScale = dataVals[2] / 10f;
 
        // ordinate
        g2.draw(new Line2D.Double(HPAD, VPAD, HPAD, h - VPAD));
        // tic marks
        float x1 = HPAD, y1 = VPAD, x2 = HPAD - 3, y2;
        for(int j = 0; j < 10; j++)
        {
            g2.draw(new Line2D.Double(x1, y1, x2, y1));
            y1 += yInc;
        }
        // labels
        String text; LineMetrics lm;
        float xs, ys, textWidth, height;
        for(int j = 0; j <= 10; j++)
        {
            text = String.valueOf(dataVals[1] - (int)(j * yScale));
            textWidth = (float)font.getStringBounds(text, frc).getWidth();
            lm = font.getLineMetrics(text, frc);
            height = lm.getAscent();
            xs = HPAD - textWidth - 7;
            ys = VPAD + (j * yInc) + height/2;
            g2.drawString(text, xs, ys);
        }
 
        // abcissa
        g2.draw(new Line2D.Double(HPAD, h - VPAD, w - VPAD, h - VPAD));
        // tic marks
        x1 = HPAD; y1 = h - VPAD; y2 = y1 + 3;
        for(int j = 0; j < 12; j++)
        {
            g2.draw(new Line2D.Double(x1, y1, x1, y2));
            x1 += xInc;
        }
        // labels
        ys = h - VPAD;
        for(int j = 0; j < 7; j++)
        {
            //text = String.valueOf(j + 1);
        	text = daw[j];
            textWidth = (float)font.getStringBounds(text, frc).getWidth();
            System.out.println("textWidth "+textWidth);
            lm = font2.getLineMetrics(text, frc);
            System.out.println("lm "+lm);
            height = lm.getHeight();
            System.out.println("lm "+lm);
            xs = HPAD + j * xInc - textWidth/2;
            System.out.println("xs "+xs);
            g2.drawString(text, xs, ys + height);
        }
 
        // plot data
        x1 = HPAD;
        yScale = (float)(h - 2*VPAD) / dataVals[2];
        for(int j = 0; j < data.length; j++)
        {
            y1 = VPAD + (h - 2*VPAD) - (data[j] - dataVals[0]) * yScale;
            if(j > 0)
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
            x2 = x1;
            y2 = y1;
            x1 += xInc;
        }

        x1 = HPAD;
        for(int j = 0; j < data2.length; j++)
        {
            y1 = VPAD + (h - 2*VPAD) - (data2[j] - dataVals[0]) * yScale;
            if(j > 0)
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
            x2 = x1;
            y2 = y1;
            x1 += xInc;
        }
    }
 
    private int[] getDataVals()
    {
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for(int j = 0; j < data.length; j++)
        {
            if(data[j] < min)
                min = data[j];
            if(data[j] > max)
                max = data[j];
        }
        int span = max - min;
        return new int[] { min, max, span };
    }
}

Open in new window

0
 
robthewolfCommented:
Why reinvent the wheel. The point of having a rich langugar like this, with so many external packages like jfreechart is so that once someone has done it you don't have to. You can then spend you time on other more interesting things.
0
 
Pramod KumarCommented:
I have tried to fix bit but not fully, but now can easy do the rest
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
 
public class LineGraph
{
    public static void main(String[] args)
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new GraphPanel());
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}
 
class GraphPanel extends JPanel
{
    final int
        HPAD = 60,
        VPAD = 40;
    int[] data;
	int [] data2;
    Font font;
 
    public GraphPanel()
    {
        data = new int[] {
            120, 190, 211, 75, 30, 290, 182, 65, 85, 120, 100, 101
        };
        data2 = new int[] {
            12, 150, 21, 175, 130, 90, 12, 165, 185, 20, 80, 151
        };
        font = new Font("lucida sans regular", Font.PLAIN, 16);
        setBackground(Color.white);
    }
 
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setFont(font);
        FontRenderContext frc = g2.getFontRenderContext();
        int w = getWidth();
        int h = getHeight();
        // scales
        float xInc = (w - HPAD - VPAD) / 11f;
        float yInc = (h - 2*VPAD) / 10f;
        int[] dataVals = getDataVals();
        float yScale = dataVals[2] / 10f;
 
        // ordinate
        g2.draw(new Line2D.Double(HPAD, VPAD, HPAD, h - VPAD));
        // tic marks
        float x1 = HPAD, y1 = VPAD, x2 = HPAD - 3, y2;
        for(int j = 0; j < 10; j++)
        {
            g2.draw(new Line2D.Double(x1, y1, x2, y1));
            y1 += yInc;
        }
        // labels
        String text; LineMetrics lm;
        float xs, ys, textWidth, height;
        for(int j = 0; j <= 10; j++)
        {
            text = String.valueOf(dataVals[1] - (int)(j * yScale));
            textWidth = (float)font.getStringBounds(text, frc).getWidth();
            lm = font.getLineMetrics(text, frc);
            height = lm.getAscent();
            xs = HPAD - textWidth - 7;
            ys = VPAD + (j * yInc) + height/2;
            g2.drawString(text, xs, ys);
        }
 
        // abcissa
        g2.draw(new Line2D.Double(HPAD, h - VPAD, w - VPAD, h - VPAD));
        // tic marks
        x1 = HPAD; y1 = h - VPAD; y2 = y1 + 3;
        for(int j = 0; j < 12; j++)
        {
            g2.draw(new Line2D.Double(x1, y1, x1, y2));
            x1 += xInc;
        }
        // labels
        ys = h - VPAD;
		String week[] = {"SUN","MON","TUE","WED","THU","FRI","SAT"};
        for(int j = 0; j < 12; j++)
        {
			int r=j;
			if(j>6){
				r = j-6;
				r = r-1;
			}
            text = week[r];
            textWidth = (float)font.getStringBounds(text, frc).getWidth();
            lm = font.getLineMetrics(text, frc);
            height = lm.getHeight();
            xs = HPAD + j * xInc - textWidth/2;
            g2.drawString(text, xs, ys + height);

        }
 
        // plot data

        x1 = HPAD;
		float x22 = x1;		
		yScale = (float)(h - 2*VPAD) / dataVals[2];
        for(int j = 0; j < data.length; j++)
        {
            y1 = VPAD + (h - 2*VPAD) - (data[j] - dataVals[0]) * yScale;
            if(j > 0)
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
            x2 = x1;
            y2 = y1;
            x1 += xInc;
        }
        // plot data2
       
        for(int j = 0; j < data2.length; j++)
        {
            y1 = VPAD + (h - 2*VPAD) - (data2[j] - dataVals[0]) * yScale;
            if(j > 0)
                g2.draw(new Line2D.Double(x22, y1, x2, y2));
            x2 = x22;
            y2 = y1;
            x22 += xInc;
        }
    }
 
    private int[] getDataVals()
    {
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for(int j = 0; j < data.length; j++)
        {
            if(data[j] < min)
                min = data[j];
            if(data[j] > max)
                max = data[j];
        }
        int span = max - min;
        return new int[] { min, max, span };
    }
}

Open in new window

0
 
manavilaiCommented:
hi, please see if this is useful
0
 
nicbhAuthor Commented:
Perfect thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.