cursor line controlled by a slider

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
hassan.elkhatib
Posts: 20
Joined: Fri Jul 24, 2015 2:20 pm
antibot: No, of course not.

cursor line controlled by a slider

Post by hassan.elkhatib » Wed Jul 29, 2015 8:39 am

Hello,
How can i make an horizontal (parallel with X axis) line cursor that is controlled by a slider?
Thank you very much!

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: cursor line controlled by a slider

Post by paradoxoff » Wed Jul 29, 2015 12:41 pm

Write an object that hold a reference to an e. g. ValueMarker and is also a changeListener for your JSlider.
if this object is catching a changeEvent from the JSlider, get the value from the JSlider, convert that by whatever rule you think is appropriate to a double data value, and call setValue on the ValueMarker with the data value. Use the ValueMarker as range marker in your plot.

hassan.elkhatib
Posts: 20
Joined: Fri Jul 24, 2015 2:20 pm
antibot: No, of course not.

Re: cursor line controlled by a slider

Post by hassan.elkhatib » Fri Jul 31, 2015 10:11 am

ok,Thank's a lot!
I've done this untill now :
ValueMarker marker = new ValueMarker(0);
marker.setPaint(Color.blue);


XYPlot plot=(XYPlot) chart.getPlot();
plot.addRangeMarker(marker);



slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {


marker.getValue();

marker.setValue(4);
}


});

I don't know how to convert to double value..so if you can help me again please?
Thank you very much!

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: cursor line controlled by a slider

Post by paradoxoff » Fri Jul 31, 2015 12:08 pm

hassan.elkhatib wrote: I don't know how to convert to double value..so if you can help me again please?
The JSlider or better its BoundedRangeModel only works with integers. For simplicity, lets assume that the value range of the BoundedRangeModel goes from 0 to 100.
You could then interpret the current value of the JSlider as a percentage. The question is "a percentage of what range". I can´t answer that, because I have no idea what you want to indicate with your marker.
A few tempting assumptions what the "100%" could be:
- The current range of the y axis
- The auto range of the y axis, e.g. the range that is required to show all the data points
- A user defined range

Please give more hints on what you want to achieve.

hassan.elkhatib
Posts: 20
Joined: Fri Jul 24, 2015 2:20 pm
antibot: No, of course not.

Re: cursor line controlled by a slider

Post by hassan.elkhatib » Fri Jul 31, 2015 12:34 pm

ok, i have a preasure sensor and a arduino and i display the signal with jfreechart .i want now to have a cursor (controlled by a slider) that when i click on it (the slider range is between 0 and 5 :0,2,3,4,5) will calculate the frequency ,or the period of signal. I'm interested in The current range of the y axis
Last edited by hassan.elkhatib on Mon Aug 24, 2015 1:09 pm, edited 1 time in total.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: cursor line controlled by a slider

Post by paradoxoff » Fri Jul 31, 2015 1:46 pm

Please put your code between the code tags in your next post and not after the closing code tag.
hassan.elkhatib wrote:i want now to have a cursor (controlled by a slider) that when i click on it (the slider range is between 0 and 5 :0,2,3,4,5) will calculate the frequency ,or the period of signal. I'm interested in The current range of the y axis
What is the "cursor"? The JSlider or the marker? And what is "it" that you want to click on? The JSlider or the marker?
When I was suggesting to "write an object that hold a reference to an e. g. ValueMarker and is also a changeListener for your JSlider", I thought about a new class and not of an anonymous ChangeListener instance.
if you write such a class, you can define the constructor and its parameters as needed. You could have a constrcutor with a ValueMarker and a ValueAxis parameter that is storing the instances as instance variables and accesses them in the stateChanged method. You do not need to store a reference to the JSlider because that is supplied as source of the ChangeEvent. Something like that might do the job:

Code: Select all

public class MarkerShifter implements ChangeListener{
	private ValueAxis axis;
	private ValueMarker marker;
	public MarkerShifter(ValueAxis axis, ValueMarker marker){
		this.axis = axis;
		this.marker = marker;
	}
	public void public void stateChanged(ChangeEvent e) {
		if(!(e.getSource() instanceof JSlider)) return;
		int sliderValue = ((JSlider)e.getSource).getValue();
		Range axisRange = axis.getRange();
		double markerValue = axisRange*sliderValue/5.0;//or anything else
		marker.setValue(markerValue);
	}
}

hassan.elkhatib
Posts: 20
Joined: Fri Jul 24, 2015 2:20 pm
antibot: No, of course not.

Re: cursor line controlled by a slider

Post by hassan.elkhatib » Fri Jul 31, 2015 2:06 pm

the "cursor" is the maker and "it" is the slider.Sorry for my English.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: cursor line controlled by a slider

Post by paradoxoff » Fri Jul 31, 2015 2:48 pm

hassan.elkhatib wrote:the "cursor" is the maker and "it" is the slider.
Than, the approach suggested above should work.

hassan.elkhatib
Posts: 20
Joined: Fri Jul 24, 2015 2:20 pm
antibot: No, of course not.

Re: cursor line controlled by a slider

Post by hassan.elkhatib » Mon Aug 03, 2015 9:13 am

Thank's a lot! it apears an error :
"Syntax error on token "void", volatile expected."
at this line:
public void public void stateChanged(ChangeEvent e) {

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: cursor line controlled by a slider

Post by paradoxoff » Mon Aug 03, 2015 9:17 pm

I wrote the code without IDE and without a test whether it could be compiled.
Delete one of the two "public void public void".
The statement "axisRange*sliderValue/5.0" won´t compile as well since axisRange is an instance of Range, and you can´t multiply a range with a double.
The statement

Code: Select all

double markerValue = axisRange.getLength()*sliderValue/5.0 + axis.getLowerBound();
should compile.

hassan.elkhatib
Posts: 20
Joined: Fri Jul 24, 2015 2:20 pm
antibot: No, of course not.

Re: cursor line controlled by a slider

Post by hassan.elkhatib » Tue Aug 04, 2015 10:49 am

When i write what you said i have an error now just at the line :"public class MarkerShifter implements ChangeListener"
and the error is :Multiple markers at this line
- The type MarkerShifter is never used locally
- Illegal modifier for the local class MarkerShifter; only abstract or final is
permitted
When i put "final" in place of "public" the program runs but it don't display any change..i mean i don't have any valuemarker on the screen.
code:
public class MarkerShifter implements ChangeListener{
private ValueAxis axis;

private ValueMarker marker;

public MarkerShifter (final ValueAxis axis, final ValueMarker marker){
this.axis = axis;
this.marker = marker;
}
public void stateChanged(ChangeEvent e) {
if(!(e.getSource() instanceof JSlider)) return;
int sliderValue = ((JSlider)e.getSource()).getValue();
Range axisRange = axis.getRange();
double markerValue = axisRange.getLength()*sliderValue/5.0 + axis.getLowerBound();
marker.setValue(markerValue);

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: cursor line controlled by a slider

Post by paradoxoff » Tue Aug 04, 2015 12:48 pm

When I add the two missing closing brackets, the code snippet looks ok in my IDE.
I assume that you have copy-pasted the fragment somewhere in your existing code, and that your use of the class is not correct. But these are issues outside the code snippet, and I obviously can not have a clue about how you use the class.
And once more: use code tags.

hassan.elkhatib
Posts: 20
Joined: Fri Jul 24, 2015 2:20 pm
antibot: No, of course not.

Re: cursor line controlled by a slider

Post by hassan.elkhatib » Wed Aug 05, 2015 11:16 am

Thank's !!! now it works! thank very much!!!

Locked