Getting (x,y) value over click mouse from JFreeChart - onclicklistener

I would like to catch click event over JFreeChart and show by console the x and y value of the nearest data, not just clicks within the boundary of a ChartEntity.
If it is not possible (y value is not exist for that x value), I would like to get the x value.
EDIT:
Marker shows so far to click. Click handler code:
Point2D po = chartPanel.translateScreenToJava2D(event.getTrigger().getPoint());
Rectangle2D plotArea = chartPanel.getScreenDataArea();
XYPlot plot = (XYPlot) chart.getPlot();
chartX = plot.getDomainAxis().java2DToValue(po.getX(), plotArea, plot.getDomainAxisEdge());
chartY = plot.getRangeAxis().java2DToValue(po.getY(), plotArea, plot.getRangeAxisEdge());
plot.removeDomainMarker(marker);
marker = new ValueMarker(chartX);
marker.setPaint(Color.red);
plot.addDomainMarker(marker);
System.out.println("X:" + chartX + ", Y:" + chartY);

Related

How do you zoom to bounding box using Xamarin Maps?

I have a Xamarin Android map (VS2019) and a number of lat/long points. What I want to do is have the map zoom in (or out), given a set of coordinates that set a bounding box.
I have this code (below) which doesn't seem to do anything. I've read all the documentation which suggests it should work but doesn't. I have checked and the bounding box coordinates are set correctly but the map doesn't do anything.
According to the docs calling LayOut should make it update but it doesn't do anything, no errors, just nothing.
How do I achieve this, any help would be much appreciated.
MyMap.Layout(GetBounds(points));
private Rectangle GetBounds(List<Point> points)
{
var minx = (from x in points select x.X).Min();
var maxx = (from x in points select x.X).Max();
var miny = (from x in points select x.Y).Min();
var maxy = (from x in points select x.Y).Max();
Rectangle rectangle = new Rectangle(minx, miny, maxx - minx, maxy - miny);
return rectangle;
}
Here's what I did
I created a rectangle with the bounds where I need them
Rectangle rectangle = GetBounds(points);
Then I got the rectangle location
Position p1 = new Position(rectangle.X, rectangle.Y);
Position p2 = new Position((rectangle.X + rectangle.Width), (rectangle.Y + rectangle.Height));
... and then got the center point of the rectangle
double x = rectangle.X + (rectangle.Width / 2);
double y = rectangle.Y + (rectangle.Height / 2);
Then called .MoveToRegion using that data, and it works
MyMap.MoveToRegion(
MapSpan.FromCenterAndRadius(
new Xamarin.Forms.Maps.Position(x, y), Distance.BetweenPositions(p1, p2))
);

How We Can Change Objects Position With Animation On Game Maker?

I want to code an object that, when the left mouse button it pressed on it, starts moving towards (x,y) point and when it reaches the (x,y) point it stops moving.
I order to object when it's clicked by the left mouse button. The object moves towards (x,y) with 75 px/s speed but it doesn't stop at (x,y) point, it keeps moving on.
You can use something like this:
Create Event:
moving = false;
moving_speed = 4;
target_x = 0;
target_y = 0;
Global Mouse Left Pressed Event:
target_x = mouse_x;
target_y = mouse_y;
moving = true;
sprite_index = spr_walk; // Start animation
image_speed = 0.5; // Animation speed
Step Event:
if moving and point_distance(x, y, target_x, target_y) > moving_speed
{
dir = point_direction(x, y, target_x, target_y);
x += lengthdir_x(moving_speed, dir);
y += lengthdir_y(moving_speed, dir);
}
else
{
moving = false;
x = target_x;
y = target_y;
image_speed = 0; // Stop animation
sprite_index = spr_stay;
}

Rotation is different after resizing movieclip

I am trying to implement transform methods to rotate and resize a movieclip at runtime and I have a problem when recalculating the rotation to follow the mouse.
I have a squared movieclip and a rotator object (a circle) at the right bottom corner of the square where I am listening for mouse events and on mouse move I do this:
_rotation = -(Math.atan2((event.stageX - this.x ), (event.stageY - this.y ))) * 180/Math.PI;
this.rotation = rotation + 45;//this is because my rotator object is on right,bottom corner
this works perfect as long as I donĀ“t modify the width or height of the object but if I do modify then there is a small "jump" on the rotation of the object that I am not able of avoid.
I know this is due event.stageX and even.stageY are different as the rotator object, the one with the mouse listener, has moved after the resize event but no clue how to avoid the "jump".
Please excuse my bad English
You need to rotate the object around its center.
/**
* Rotates the object based on its center
* Parameters: #obj => the object to rotate
* # rotation => angle to rotate
* */
public function RotateAroundCenter(obj:Object, rotation:Number):void
{
var bound:Rectangle = new Rectangle();
// get the bounded rectangle of objects
bound = obj.getRect(this);
// calculate mid poits
var midx1:Number = bound.x + bound.width/2;
var midy1:Number = bound.y + bound.height/2;
// assign the rotation
obj.rotation = rotation;
// assign the previous mid point as (x,y)
obj.x = midx1;
obj.y = midy1;
// get the new bounded rectangle of objects
bound = obj.getRect(this);
// calculate new mid points
var midx2:Number = bound.x + bound.width/2;
var midy2:Number = bound.y + bound.height/2;
// calculate differnece between the current mid and (x,y) and subtract
//it to position the object in the previous bound.
var diff:Number = midx2 - obj.x;
obj.x -= diff;
diff = midy2 - obj.y;
obj.y -= diff;
}
Usage:
_rotation = -(Math.atan2((event.stageX - this.x ), (event.stageY - this.y ))) * 180/Math.PI;
RotateAroundCenter(this, rotation + 45);
Check this link

Is there any other way to calculate X value in coords of other coord system?

Sorry, for weird topic name, but I don't know how else to name it.
The problem is.. I have a zedgraph control. There is some lines drawn inside.
I have the coords of left border of chart area and right border of chart area.
I draw vertical lines as PictureBoxes over zedgraph control, so they moves in different coords. This vertical lines can be moved to left and right.
That way I trying to get X value in coords of XAxis:
public double Get_X_InContextOfChart(int left_coord_of_border) //Left of vertical line
{
//zed_graph.GraphPane.XAxis.Scale.Min is minimal X value shown on XAxis
//zed_graph.GraphPane.Chart.Rect.Left is Left of YAxis
//Same for else if, aside of using Right and maximum X at right
if (zed_graph.GraphPane.XAxis.Scale.Min != 0) //to avoid division by zero
return (left_coord_of_border * zed_graph.GraphPane.XAxis.Scale.Min) / zed_graph.GraphPane.Chart.Rect.Left;
else if (zed_graph.GraphPane.XAxis.Scale.Max != 0)
return (left_coord_of_border * zed_graph.GraphPane.XAxis.Scale.Max) / zed_graph.GraphPane.Chart.Rect.Right;
return double.NaN;
}
This code calculate X fine as long as else if used, but in my example it calculate something wrong.
I hope someone understand this.
Updated with new code:
public double Get_X_InContextOfChart(int left_coord_of_border)
{
double scale_left = zed_graph.GraphPane.XAxis.Scale.Min;
double scale_right = zed_graph.GraphPane.XAxis.Scale.Max;
double graph_width = zed_graph.GraphPane.Chart.Rect.Width;
double x = left_coord_of_border;
return scale_left + (scale_right - scale_left) / graph_width * x;
}
Result:
scale_left = -0.1
scale_right = 0.9
graph_width = 540 // pixels
x = 190 // pixels
scale_x = scale_left + (scale_right - scale_left) / graph_width * x // 0.25

Simulating the accelerometer event feature in Firefox 3.6?

I just downloaded Firefox 3.6 today and I noticed in the list of new features they have an Orientation API that can detect the direction that your laptop/computer is tilted. This is clearly a hardware feature of some sort; So if you don't have the hardware to do so is there any way of simulating it so that you can test it out on your projects?
You should be able to simulate the MozTransform with the following code (code borrowed from Mozilla Orientation Demo 2):
var x = 0;
var y = 0;
// This will rotate / zoom your document based on the x and y values:
document.body.style.MozTransform = 'rotate(' + (-y * 30) + 'deg) ' +
'scale(' + (x + 1) + "," + (x + 1) + ')';
If you are building an application based on the Orientation API, you could implement your event listener as follows:
function onChangeOrientation(e)
{
var x = e.x;
var y = e.y;
var z = e.z;
// Here you may need to standardize the values of x, y and z
// For example: (-1 * y) on MacBookPro > 5.1.
processOrientationLogic(x, y, z);
}
// Add the Event Listener for the Accelerometer
window.addEventListener("MozOrientation", onChangeOrientation, true);
Then to simulate the orientation event, simply launch processOrientationLogic() with your x, y and z values. You could do this from Firebug, or you can also create some sort of three-slider-control on your page that calls this function on every change.

Resources