Avatar billede tunc84 Nybegynder
21. marts 2005 - 12:11 Der er 11 kommentarer

Hjælp til graf

Hej folkens !!
Jeg har en GUI og ønsker en graf, hvor man kan vise nogle dataer.
Nogen der kan hjælpe mig dette.
Tænkte på at lave grafen en koordinatsystem, hvor dataerne vises som søjler.
Avatar billede arne_v Ekspert
21. marts 2005 - 12:12 #1
Det er et meget stort arbejde at lave selv fra bunden af.

Jeg vil foreslå dig at finde et eksternt bibliotek som f.eks.
JFreeChart.
Avatar billede arne_v Ekspert
21. marts 2005 - 12:13 #2
Avatar billede arne_v Ekspert
21. marts 2005 - 12:14 #3
Check f.eks. eksemplerne her:

http://www.jfree.org/jfreechart/samples.php
Avatar billede tunc84 Nybegynder
21. marts 2005 - 12:19 #4
Det ser fint ud, men ønsker først og fremmest bare at lave en graf på min GUI.
Er nemlig igang med eksamensprojekt, så ved ik helt om det vil blive accepteret at bruge JFreeChart.
Avatar billede arne_v Ekspert
21. marts 2005 - 12:22 #5
Se det kan jeg jo ikke svare på.

Har du mulighed for at spørge din lærer ?
Avatar billede tunc84 Nybegynder
21. marts 2005 - 12:26 #6
Ja det så rigtigt :)
Men er næsten 100% sikker på det ik vil godkendes.
Men kunne du hjælpe mig med at lave et simpelt kordinatsystem, med x og y akser??
Avatar billede _carsten Nybegynder
21. marts 2005 - 12:37 #7
Jeg lavede noget her, for noget tid siden, første kommentar :  http://eksperten.dk/spm/495415
Avatar billede arne_v Ekspert
21. marts 2005 - 12:38 #8
Det må du hellere få Carsten til at hjælpe med - det ligger uden for mit område
Avatar billede tunc84 Nybegynder
21. marts 2005 - 13:35 #9
jeg har følgende to classer

package bikeappl;

import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.JScrollBar;

/**
* <p>Title: OOP2A Bike Monitor</p>
*
* <p>Description: This is the abstract class for displaying all time dependent bike data graphs. The class
*                implements drawing the complete graph except the individual bike data objects, which is
*                of course delegated to these objects.</p>
*
*
* @author
* @version 1.0
*/
public abstract class BikeDataGraph extends Graph implements ModelObserver
{
    private JScrollBar scrollBar;    //Scrollbar for analyzing historical bike data
    private LinkedList graphObjList; //A list holding all bike data objects current displayed in the graph

    private String graphName;
    private String traineeName;
    private Color graphColor;
    private Color gridColor;
    private final int NUMBER_OF_X_INCREMENTS = 50;
    private final int NUMBER_OF_Y_INCREMENTS = 5;
    private final int FRAME_SIZE = 10;
    private int elapsedTime;
    private int deltaX;    //Defines a x increment in number of pixels
    private int deltaY;    //Defines a Y increment in number of pixels
    private int yOffset;    //Defines the offset at the y-axis in number of pixels
    private int yStart;
    private Model model;
    private Point startPoint; //Defines the new starting point for drawing a graph segment

    public BikeDataGraph(Model model, String graphName, String traineeName, Color graphColor,
                        Color backgroundColor)
    {

        this.model = model;
        graphObjList = new LinkedList();
        this.graphName = graphName;
        this.traineeName = traineeName;
        this.graphColor = graphColor;
        gridColor = Color.DARK_GRAY;
        elapsedTime = 0;
        yStart = 0;

        this.setLayout(new BorderLayout());
        setBackground(backgroundColor);
    }

    /**
    * Delegates graph drawing to the individual bike data object.
    *
    * @param bikeObj the bike object to be drawn
    * @param g the graphics for this window
    * @param startPoint the starting point for the drawning, this point is updated after drawning
    * @param deltaX the x increment
    * @param yOffset the offset at the y-axis
    */
    protected abstract void drawObject(BikeData bikeObj, Graphics g, Point startPoint, int deltaX, int yOffset);

    /**
    * Returns the specific y-value for a bike data object.
    *
    * @param bikeObj the bike data object
    */
    protected abstract int get_Y_Value(BikeData bikeObj);


    /**
    * Overrides the method in JComponent to draw the specific graph
    *
    * @param g Graphics
    */
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);      //first call the method in the super class to redraw its components

        //g.draw3DRect(0, 0, getWidth(), getHeight(), true);

        deltaX = getWidth() / NUMBER_OF_X_INCREMENTS;
        yOffset = getHeight() - FRAME_SIZE;
        deltaY = yOffset / NUMBER_OF_Y_INCREMENTS;

        //System.out.println("deltaX: " + deltaX);
        //System.out.println("yOffset: " + yOffset);

        startPoint = new Point(0, yStart);

        draw(g);  //draw grid and graph
    }


    /**
    * Implements the method defined in the super class
    *
    * @param view - true makes the graph visible else false makes the graph invisible
    */
    public void setView(boolean view)
    {
        setVisible(false);
        setVisible(view);

        if (view)
            model.addListener(this);            //register as listener at the model
        else
            model.removeListener(this);

    }

    /**
    * Draws the grid
    *
    * @param g
    */
    private void drawGrid(Graphics g)
    {
        g.setColor(gridColor);

        //draw the y-divisions
        for (int i = 0; i * deltaY < getHeight(); i++)
            g.drawLine(0, yOffset - i * deltaY, getWidth(), yOffset - i * deltaY);

        //draw the x-divisions
        for (int i = 0; i * deltaX < getWidth() - deltaX; i++)
            g.drawLine(i * deltaX, 0, i * deltaX, yOffset);

    }

    /**
    * Draws the informations
    *
    * @param g
    */
    private void drawInfo(Graphics g)
    {
        //Draw graph information
        g.setColor(graphColor);
        g.drawString(graphName + ": " + traineeName, FRAME_SIZE, 20);
        g.drawString("10 sec./div", getWidth() - 80, getHeight()- 3);
        g.drawString("Time elapsed: " + elapsedTime + " sec.", getWidth() - 200, 20);

    }


    /**
    * Implements the method defined in the super class and draws the complete graph
    *
    * @param g
    */
    protected void draw(Graphics g)
    {

        drawGrid(g);
        g.setColor(graphColor);

        int i = 0;
        for (Iterator iter = graphObjList.iterator(); iter.hasNext(); i++)
            drawObject((BikeData)iter.next(), g, startPoint, (i > 0) ? deltaX : 0, yOffset);

        drawInfo(g);
    }

    /**
    * Implements the method defined in the ModelObserver. When registered as listener in the model, new bike
    * objects will be received in this method
    *
    * @param BikeData bikeObj
    */
    public void bikeObjectAdded(BikeData bikeObj)
    {

        int modelSize = model.getModelSize(); //get number of bike objects in the model
        elapsedTime = 10*modelSize;          //set time elapsed - 10 sec. between each bike object
        graphObjList.addLast(bikeObj);        //add bike objects to the graph list

        if (graphObjList.size() > getWidth()/deltaX)
        {  //graph display is full
            graphObjList.removeFirst();      //removed the bike object displayed leftmost in the graph
            BikeData bikeData = (BikeData)graphObjList.getFirst();
            yStart = get_Y_Value(bikeData);  //set the new y start value
        }

        System.out.println(bikeObj);
        repaint();                          //invoke paintComponent() will redraw the graph
    }

    /**
    * objectRemoved
    *
    * @param BikeData bikeObj
    */
    public void bikeObjectRemoved(BikeData bikeObj)
    {
    }

    /**
    * Shows historical bike data for the whole training session
    */
    public void viewSession()
    {
        model.removeListener(this);    //Deregister as listener
        scrollBar = new JScrollBar(0); //Make scroll bar horizontal
        scrollBar.setMaximum(model.getModelSize()); //set scroll bar max. value equal number of bike objects in the model
        scrollBar.setValue(model.getModelSize());
        elapsedTime = 10*model.getModelSize();
        scrollBar.addAdjustmentListener(new ScrollBarAdjustmentListener());
        this.add(scrollBar, BorderLayout.SOUTH);
    }


    private class ScrollBarAdjustmentListener implements AdjustmentListener
    {
        public void adjustmentValueChanged(AdjustmentEvent e)
        {
            //System.out.println("adjustmentValueChanged() called with value: " + e.getValue() + " type: " + e.getAdjustmentType());
            int modelSize = model.getModelSize();

            graphObjList.clear(); //removed all bike objects from the graph list
            //get a new graph list matching the current value of the scroll bar
            //model.getModelSection(graphObjList, e.getValue(), NUMBER_OF_X_INCREMENTS);
            BikeData bikeData = (BikeData)graphObjList.getFirst();
            elapsedTime = 10*e.getValue();
            repaint();            //invoke paintComponent() will redraw the graph
        }
    }



}




















package bikeappl;

import java.awt.*;

/**
* <p>Title: OOP2A Bike Monitor</p>
*
* <p>Description: This is the concrete class for showning the pulse graph</p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* @version 1.0
*/
public class PulseGraph extends BikeDataGraph
{
    /**
    * Constructor
    *
    * @param model a refence to the objects model
    * @param traineeName the name of the person training this session
    */
    public PulseGraph(Model model, Color graphColor, String traineeName)
    {
        super(model, "Pulse graph", traineeName, graphColor, Color.BLACK);
    }

    /**
    * The concrete implementation drawing a bike object in the graph
    *
    * @param bikeObj the bike object to be drawn
    * @param g the graphics for this window
    * @param startPoint the starting point for the drawning, this point is updated after drawning
    * @param deltaX the x increment
    * @param yOffset the offset at the y-axis
    */
    protected void drawObject(BikeData bikeObj, Graphics g, Point startPoint, int dx, int yOffset)
    {
        bikeObj.drawPulse(startPoint, dx, yOffset, g);
    }

    /**
    * The concrete implementation returning the bike objects actual y-value
    *
    * @param bikeObj the bike data object
    */
    protected int get_Y_Value(BikeData bikeObj)
    {
        return bikeObj.getPulse();
    }


}
Avatar billede tunc84 Nybegynder
21. marts 2005 - 13:36 #10
Carsten jeg har kigget på linket, men ved bar ik hvordan jeg skal implemetere grafen udfra koden jeg har sendt.
Avatar billede _carsten Nybegynder
21. marts 2005 - 18:59 #11
Det var noget af en doktorafhandling du har postet, skal nok kigge på det, men tør ikke love det bliver i aften.

Har temmelig meget at rive i pt.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester