Is it acceptable to make a graph this way for JPanel?
I need to graph some statistical data inside of someone's class which extends a JPanel
. Right now the plan is to use paintComponent
and the Graphics
class as follows:
public void paintComponent(Graphics g) {
super.paintComponent(g);
//create a rectangle to represent the outline of the graph
g.drawRect(300, 50, 400, 350);
//set up the da开发者_运维百科tapoints
for(int i = 0; i < data.size(); i++) {
//put the datapoints where ever they need to go within this rectangle
g.drawOval(x, y, width, height);
}
}
Is this terrible for some reason? More importantly, is there some awesome, easy-to-use graphing library I could use but don't know about?
Thanks.
Your code looks OK to me, other than perhaps placing your x and y points at
for(int i = 0; i < data.size(); i++) {
g.drawOval(data.get(i).getX() - width/2, data.get(i).getY() - height/2,
width, height);
}
so that the ovals are centered.
Also, you might want to consider transforming the data such that the points plotted remain within the boundaries of your graph's domain and range. Otherwise you could have ovals outside of your rectangle.
You might want to check out JFreeChart for graphing libraries.
Consider JFreeChart
Here is the Javadoc for the JFreeChart
精彩评论