how can I use drawString() on a ChartPanel

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
shen

how can I use drawString() on a ChartPanel

Post by shen » Tue Sep 14, 2004 6:53 am

i tried to use chartPanel.getGraphics().drawString() on ChartPanel and it seemed to be working at first.
But when the chartPanel redraws,the things are gone!!
i traced some code and found that Annotations are drawn by using getGraphics().drawString(),too.And Annotations wouldn't disappeared after chartPanel's redrawing
So... I'm wondering how i can use getGraphics().drawString() to draw on the chartPanel and let it not to disappeare after ChartPanel's redrawing?
thanks~~

Miky70

Post by Miky70 » Thu Oct 28, 2004 3:35 pm

I've got the same problem with drawing lines over candlestick chart.
With mouse listener I take points to draw lines and put them into a vector (points).

here is mi code:

public void drawLines(){
Point p1, p2;
Graphics g = chartpanel.getGraphics();
int validPoints = -1;
if((points.size() % 2) == 0)
validPoints = points.size();
else
validPoints = points.size()-1;
for(int i=0; i < validPoints-1; i+=2) {
p1 = (Point)points.elementAt(i);
p2 = (Point)points.elementAt(i+1);
g.setColor(Color.GREEN);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}

I can see the lines, but they disappears after they're drawn.

Could someone help me?
Thanks

enrico
Posts: 49
Joined: Thu Jul 22, 2004 7:13 pm
Location: Shaanxi, P.R.China

Post by enrico » Thu Oct 28, 2004 6:01 pm

Here is a solution, it might not be the best...

1. Write a class extends the corresponding Plot you want to use, overwrite the draw() method in order to draw lines. But at the very beginning, there is no mouse action, so the lines should not be drawn...
2. Add a variable(or some variables) or a method(or some methods) to indicate whether those lines should be drawn, in draw() method, invoke it first...
3. Add a method to set where the lines should be drawn, then triger notifyListeners(new PlotChangeEvent(this));

Here is a demo:

Code: Select all

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.event.*;
/*
 * 创建日期 2004-10-29
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */

/**
 * @author Administrator
 *
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class NewPlot extends FastScatterPlot {
	private Point p;
	private boolean visible = false;
	public NewPlot(float[][] data, ValueAxis domainAxis, ValueAxis rangeAxis) {
		super(data, domainAxis, rangeAxis);
	}
	
	public void draw(Graphics2D g2, Rectangle2D plotArea, PlotState parentState, 
            PlotRenderingInfo info) {
		super.draw(g2, plotArea, parentState, info);
		if (visible) {
			g2.drawLine((int) p.getX(), (int) plotArea.getMinY(), (int) p.getX(), (int) plotArea.getMaxY());
		}
	}
	
	public void setPoint(Point p) {
		this.p = p;
		notifyListeners(new PlotChangeEvent(this));
	}
	
	public void setVisible(boolean b) {
		visible = b;
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("test");
		Container c = frame.getContentPane();
		float[][] data = new float[2][50];
		for (int i = 0; i < data[0].length; i++) {
			data[0][i] = i;
			data[1][i] = (float) Math.random() * 100;
		}
		
		NewPlot plot = new NewPlot(data, new NumberAxis(), new NumberAxis());
		JFreeChart chart = new JFreeChart(plot);
		ChartPanel panel = new ChartPanel(chart);
		panel.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				((NewPlot) ((ChartPanel) e.getSource()).getChart().getPlot()).setVisible(true);
				((NewPlot)((ChartPanel) e.getSource()).getChart().getPlot()).setPoint(e.getPoint());
				
			}
		});
		
		c.add(panel);
		frame.pack();
		frame.setVisible(true);
	}

}

Miky70

Drawing lines in chart panel

Post by Miky70 » Fri Oct 29, 2004 8:28 am

Many thanks, Enrico

It works great!!!

anntaylor
Posts: 1
Joined: Thu Sep 23, 2010 10:20 am
antibot: No, of course not.

Re: how can I use drawString() on a ChartPanel

Post by anntaylor » Thu Sep 23, 2010 10:31 am

I have try the both of them but some error appear in my pc in macintosh.

Locked