Get value of a point on xy chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
stledger
Posts: 14
Joined: Sun Mar 09, 2008 7:39 am

Possible reason why last click is showing

Post by stledger » Sun Mar 09, 2008 7:46 am

I had a similar problem getting the coordinates to show on a plot for a mouse click. I solved the problem by including the "change" code in an invokeLater() block. Maybe the following code fragment will help:

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
plot.clearAnnotations();
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
double x, y;
x = plot.getDomainCrosshairValue();
y = plot.getRangeCrosshairValue();
XYTextAnnotation annotation = new XYTextAnnotation("(" + number2.format(x) + ", " + number2.format(y) + ")", x, y);
annotation.setTextAnchor(TextAnchor.BOTTOM_CENTER);
plot.addAnnotation(annotation);
}
});
}

This code should make the coordinates of the click show correctly on the next mouse click. Without the invokeLater(), the coordinates of the previous click show. Hopefully I didn't miss a bracket when I copied and trimmed this code.

Hope this helps,

John

anamika21
Posts: 13
Joined: Mon Feb 25, 2008 3:20 pm

Post by anamika21 » Tue Mar 11, 2008 1:44 pm

Hello Don,

I again have got problem...
when i am compiling the following code...

1) marker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
2) marker.setPaint(markerLineColor);
3) marker.setLabel(String.format("X: %-1.3f%sY: %-1.3f",scopeXYPlot.getDomainCrosshairValue(), NL, scopeXYPlot.getRangeCrosshairValue()));


it says that......cant find symbol - setLabelOffsetType( Jfree.org.ui.LengthAdjustmentType);
cant find symbol - markerLineColor
cant find symbol- NL

and its for every line of code---
whats is the problem in that - i have imported all the packages but still its giving cant find symbol---

Thx
Anamika

anamika21
Posts: 13
Joined: Mon Feb 25, 2008 3:20 pm

Post by anamika21 » Wed Mar 12, 2008 4:16 pm

Hello John,

Thx for the code...this would be helpful...
could you please tell me that which 'change' code you are talking abt?
and this code would be added in the last so that it would print current points
?
i am new to it so can you tell me in brief how to start with it?

Anamika

dlucas
Posts: 19
Joined: Sat Dec 29, 2007 5:07 am
Location: Boston (USA)

Post by dlucas » Wed Mar 12, 2008 5:18 pm

Hi Animika -

It looks like you have a classpath problem. Make sure that ALL of the JFreeChart jars are on your classpath. You must have all 5 of the following on your classpath:
1. JFreeChart.jar
2. JFreeChart-experimental.jar
3. JFreeChart-swt.jar
4. swtgraphics2d.jar
5. JCommon.jar

In addition, if you add

Code: Select all

String NL = "\n";
to your code, this will clear up part of your problems. NL stands for newline, and this code changes from OS to OS, but this should get you going for now.

The markerLineColor is the color that you want to assign to the marker line. Try adding

Code: Select all

    Color markerLineColor=Color.red;
If you are using Eclipse for your development system, try "Source / Organize Imports" once. This alters the order of the imports section of your source to a preferred order. Sometimes this helps, but it will not automatically add missing imports. Also, be aware that if you are mixing AWT and SWT, that many of the drawing classes are present in both UI's with identical names and that sometimes you have to use a fully qualified class name to get the right class. Make sure that you have all of the required import statements such as:

Code: Select all

    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Stroke;
    import java.awt.event.InputEvent;
    import java.awt.event.MouseEvent;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.text.DateFormat;
    import java.text.Format;
    import java.util.Date;
    import java.util.Properties;
    import java.util.Vector;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.PaintEvent;
    import org.eclipse.swt.events.PaintListener;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Control;
    import org.eclipse.swt.widgets.Event;
    import org.eclipse.swt.widgets.Listener;
    import org.eclipse.swt.widgets.Text;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartMouseEvent;
    import org.jfree.chart.ChartMouseListener;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.LogarithmicAxis;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.block.BlockBorder;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.plot.ValueMarker;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.XYItemRenderer;
    import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
    import org.jfree.chart.title.LegendTitle;
    import org.jfree.chart.title.TextTitle;
    import org.jfree.data.xy.XYSeries;
    import org.jfree.data.xy.XYSeriesCollection;
    import org.jfree.experimental.chart.annotations.XYTitleAnnotation;
    import org.jfree.experimental.chart.swt.ChartComposite;
    import org.jfree.ui.LengthAdjustmentType;
    import org.jfree.ui.RectangleAnchor;
    import org.jfree.ui.RectangleEdge;
    import org.jfree.ui.RectangleInsets;
    import org.jfree.ui.TextAnchor;
Note that these are the imports that I am using - they directly reflect the features that my very complex application requires. Some of these imports are most likely not necessary for your application. In addition, your chart type may require that you add other imports than the ones that I am using. If you aren't used to managing a lot of imports and if you are using Eclipse as your development system, try double clicking on any class name that is generating an error and then right click on the highlighted class name and select "Source / Add Import". If your class paths are correct, then Eclipse will automatically update your list of imports to include the missing import. If you are using some other development system, you will have to get someone who knows that development system to help you.

If you are using imports with wildcards such as

Code: Select all

import java.awt.*;
you will have to be very careful that you do not import a java.awt class when you are really wanting to import an org.eclipse.swt.xxx class of the same name. These problems are usually not very obvious other than that they produce compilation errors that seemingly don't make a lot of sense. In general, I don't use wildcards on imports to avoid this type of problem.
Best regards,

Don

stledger
Posts: 14
Joined: Sun Mar 09, 2008 7:39 am

Post by stledger » Thu Mar 13, 2008 4:40 am

anamika21 wrote:Hello John,

Thx for the code...this would be helpful...
could you please tell me that which 'change' code you are talking abt?
and this code would be added in the last so that it would print current points
?
i am new to it so can you tell me in brief how to start with it?

Anamika
Sorry I wasn't clear about the 'change'. I meant we are changing the display of the plot. Just execute the invokeLater block whenever you want the coordinates to show on a mouse click on the plot. After executing that code, every time you click on the plot the coordinates will show just above the click, and the cross hairs will cross at the point you clicked.

When you first create your chart, you might also want to execute these calls:

plot = (XYPlot) chart.getPlot();
plot.setDomainCrosshairLockedOnData(false);
plot.setRangeCrosshairLockedOnData(false);

Unless you want the crosshairs to lock on a data point. In my code the chart object is from a ChartFactory.createXYLineChart call. Also, the number2 object is a NumberFormat:

number2 = NumberFormat.getInstance();
number2.setGroupingUsed(true);
number2.setMaximumFractionDigits(2);

You might or might not want to use that format.

I think that explains all of the variables in the example code. Let me know how it works!

John

anamika21
Posts: 13
Joined: Mon Feb 25, 2008 3:20 pm

Post by anamika21 » Thu Mar 13, 2008 10:31 am

Hello Don,

I am using BlueJ editor...and
I have already included all 5 JFreeChart jars on classpath..

i have also added ....
String NL = "\n";
Color markerLineColor=Color.red;

but the problem is not only for this variables..but also for all the variable arguments ...

marker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
marker.setLabelFont(markerLabelFont)
marker.setStroke(markerStroke);
scopeXYPlot.removeDomainMarker(marker);
scopeChartComposite.update();

and the next is what is XLabel and YLable in following...
XLabel.setText(String.format("X: (Sec)%s%8.4f", NL, crossHairX));
YLabel.setText(String.format("Y: (P/U)%s%8.4f", NL, crossHairY));

As i am not not using eclipse ..its giving error for all the imports added for eclipse..like following..
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

but i must need to add this to run the code..
so what do u think....do i need to add some other imports for editor specific or can i define all the variables manually...or what can be done in other case?


Regards,
Anamika

Locked