How to turn off default Mouse Event Handlers in ILNumerics PlotCube? - c#

I'm using the ILNumerics Math Library to create some 2D Plots. To display the data I use the ILPlotCube Class. I would like to turn off the default behavior of the EventHandler for MouseDoubleClick events, because I would like to implement my own. Is that possible?
Here's some more context:
The default event handler of an ILPlotCube for MouseDoubleClick events resets the view to default values. Usually this works quite well, but there seems to be a problem with very small x- and y-values. When I add a linePlot with very small y-values the limits of the plots are automatically set to YMax=0.525 and YMin=-0.525. Unfortunately, that's not what I want. So, I set the values myself after adding the linePlot and the plot looks exactly how I'd like to have it. Great... but: if I double click on the scene it uses the default (0.525) values again. Dough! That's why I would like to turn off or overwrite this behavior.
Any ideas?
private void ilPanel1_Load(object sender, EventArgs e)
{
var scene = new ILScene();
//data with very small "y-values"
ILArray<float> line1 = new float[,] {
{0.0f, 1.0f, 2.0f },
{2.042166e-08f, 2.070141e-08f , 2.042166e-08f} };
var linePlot1 = new ILLinePlot(line1.T,
lineColor: Color.Blue,
lineWidth: 3,
markerStyle: MarkerStyle.Dot);
//Create Plot Cube
var plotCube = new ILPlotCube();
plotCube.Add(linePlot1);
//plotCube.Plots.Limits.YMax is now 0.525
//plotCube.Plots.Limits.YMin is now -0.525
//manually set the value
float maxY = 0.0f;
using (ILScope.Enter())
{
var aPos = linePlot1.Line.Positions.Storage["1;:"];
maxY = ILMath.max(aPos).FirstOrDefault();
}
plotCube.Plots.Limits.YMax = maxY;
plotCube.Plots.Limits.YMin = 0.0f;
var plot = scene.Add(plotCube);
ilPanel1.Scene = scene;
}
Thanks,
Tim

All nodes in ILNumerics provide access to the common mouse handlers which you can use to provide your own logic - or to simply disable existing individual handlers. In your case, you can override the double click handler for the plot cube:
// ....
plotCube.MouseDoubleClick += (_e, _a) => {
_a.Cancel = true;
};
// you may want disable zoom and pan as well?
plot.AllowZoom = false;
plot.AllowPan = false;
// continue with your code here...
ilPanel1.Scene = scene;
The mouse handlers in ILNumerics are actually very flexible and powerful. See the documentation here: http://ilnumerics.net/mouse-events.html
#Edit: for your situation, the ILPlotCube.AutoScaleOnAdd property might also be of interest. It determines, if the plot cube limits are to be recomputed once a new plot has been added to the plot cube. You might find false more convenient.

Related

How can I hide a portion of annotation outside innerplotposition in mschart? [duplicate]

How do you create an Annotation on-the-run and how do you enable end-user placement with Annotation.BeginPlacement()? I've tried to do this in multiple ways, but cannot get it working. It should render itself in real-time after the BeginPlacement() has been called.
Documentations on this subject is little to none - and mostly none - so I'm not able to find any help for this problem.
What I've tried so far, is to create an annotation and place it with AnchorX/Y, set all Allow- flags to true and called BeginPlacement() while mouse is moving, but cannot see the annotation while placing it nor will it go in it's place accordingly. For example, LineAnnotation starts in right position, but doesn't end where I left it. When I move it so it starts from my ChartAreas {0,0}, it will hit the end-point.
What I want to know, is when and how to use these tools available? What I am trying to do, is to let the user draw annotations on a chart and use as tools when analyzing the charts.
You need to calculate the right positions. Remember that the MouseMove will not give you positions (percentages) or values(data) but pixels. You can transform them using the various axis functions. Officially they only work in a xxxPaint event, but during mouse events they also work fine.
Update: There two ways to do the anchoring:
Either by using the 'Positions', i.e. the percentages or the 'Values', i.e. the data values.
Here is an example of the 1st kind:
LineAnnotation laNew = null;
private void chart1_MouseDown(object sender, MouseEventArgs e)
{
if (cbx_drawAnnotation.Checked)
{
Axis ax = chart1.ChartAreas[0].AxisX;
Axis ay = chart1.ChartAreas[0].AxisY;
laNew = new LineAnnotation();
chart1.Annotations.Add(laNew);
double vx = ax.ValueToPosition(ax.PixelPositionToValue(e.X));
double vy = ay.ValueToPosition(ay.PixelPositionToValue(e.Y));
laNew.X = vx;
laNew.Y = vy;
}
}
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button.HasFlag(MouseButtons.Left) && cbx_drawAnnotation.Checked)
{
Axis ax = chart1.ChartAreas[0].AxisX;
Axis ay = chart1.ChartAreas[0].AxisY;
double vx = ax.ValueToPosition(ax.PixelPositionToValue(e.X))- laNew.X;
double vy = ay.ValueToPosition(ay.PixelPositionToValue(e.Y)) - laNew.Y;
laNew.Width = Math.Min(100, vx);
laNew.Height = Math.Min(100, vy);
laNew.LineColor = rb_green.Checked ? Color.Green : Color.Red;
laNew.AllowMoving = true; // optional
}
}
This works fine unles you need to rescale the axis in some way, like changing the axis minimum and/or maximum values.
In the case you need to anchor to data values.
First we need to relate the Annotation to the Axes and also set IsSizeAlwaysRelative to false. Then we can calculate the anchor and size values:
private void chart1_MouseDown(object sender, MouseEventArgs e)
{
if (cbx_drawAnnotation.Checked)
{
Axis ax = chart1.ChartAreas[0].AxisX;
Axis ay = chart1.ChartAreas[0].AxisY;
laNew = new LineAnnotation();
chart1.Annotations.Add(laNew);
laNew.IsSizeAlwaysRelative = false;
laNew.AxisX = ax;
laNew.AxisY = ay;
laNew.AnchorX = ax.PixelPositionToValue(e.X);
laNew.AnchorY = ay.PixelPositionToValue(e.Y);
laNew.LineColor = rb_green.Checked ? Color.Green : Color.Red;
laNew.AllowMoving = true;
}
}
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button.HasFlag(MouseButtons.Left) && cbx_drawAnnotation.Checked)
{
Axis ax = chart1.ChartAreas[0].AxisX;
Axis ay = chart1.ChartAreas[0].AxisY;
laNew.Width = ax.PixelPositionToValue(e.X) - laNew.AnchorX; // values
laNew.Height = ay.PixelPositionToValue(e.Y) - laNew.AnchorY;
}
}
Note how I now can scale the maximum and also still resize the the chart and the annotations stay with the data points..:
Update: To restrict the line to the ChartArea add this to the definition in the MouseDown event:
laNew.ClipToChartArea = chart1.ChartAreas[0].Name;
To prevent an exception from leaving the Chart, add this to the condition in the MouseMove..:
.. && chart1.ClientRectangle.Contains(e.Location)

How can I identify a specific data point in tee chart and apply style

I have a Points chart, I need to identify a specific data point and apply the style, below code does the styling for all the points but I need few point to be shown in Circle and few in Cross.
Code
Steema.TeeChart.Styles.Points points = new Steema.TeeChart.Styles.Points(frmApplication.DefInstance.VtChart1.Chart)
points.Pointer.Style = Steema.TeeChart.Styles.PointerStyles.Circle;
points.Add(xValue, yValue);
You can use the GetPointerStyle event to modify the Point
points.Add(0, 4);
points.Add(1, 3); //more point add etc
//connect to the GetPointerStyle event to modify specific Point Pointerstyle at runtime.
points.GetPointerStyle += new Steema.TeeChart.Styles.CustomPoint.GetPointerStyleEventHandler(point_GetPointerStyle);
}
private void point_GetPointerStyle(Steema.TeeChart.Styles.CustomPoint series, Steema.TeeChart.Styles.GetPointerStyleEventArgs e)
{
if (e.ValueIndex == 2)
e.Style = Steema.TeeChart.Styles.PointerStyles.Cross;
else
e.Style = Steema.TeeChart.Styles.PointerStyles.Circle;
}

Draw point where mouse clicked

I'm using HelixToolkit to see and interact with STL files. I need to draw or mark a point clicked by user on the window. I have the coordinates, I know where to draw that point, but I don't know how to draw it, can someone help me? I post some code to explain what I have right now:
private void vierport3d_MouseRightClick(object sender, MouseButtonEventArgs e)
{
Point mousePos = e.GetPosition(viewPort3d);
PointHitTestParameters hitParams = new PointHitTestParameters(mousePos);
VisualTreeHelper.HitTest(viewPort3d, null, ResultCallback, hitParams);
}
public HitTestResultBehavior ResultCallback(HitTestResult result)
{
RayHitTestResult rayResult = result as RayHitTestResult;
if (rayResult != null)
{
RayMeshGeometry3DHitTestResult rayMeshResult = rayResult as RayMeshGeometry3DHitTestResult;
//HERE I HAVE THE LOCATION TO DRAW
MessageBox.Show(rayMeshResult.PointHit.X + " " + rayMeshResult.PointHit.Y + " " + rayMeshResult.PointHit.Z);
if (rayMeshResult != null)
{
// I THINK I HAVE TO DRAW THE POINT HERE
}
}
return HitTestResultBehavior.Continue;
}
PD: I show the stl on a viewport3d.
We had same scenario in our project and used a sphere to visually indicate the point.
<ht:SphereVisual3D Radius="0.75" Fill="Red" Center="{Binding ContactPoint}" />
ContactPoint is a Point3D type.
This might help, but its probably not the most effecient.
Try the following:
This will create a 3D sphere that can be rendered at the given coordinates.
var sphereSize = 0.025;
/* keep these values low, the higher the values the more detailed the sphere which may impact your rendering perfomance.*/
var phi = 12;
var theta = 12;
MeshBuilder meshBuilder = new MeshBuilder();
Pass in your x,y,z to the first parameter. i.e. the click 3D location.
meshBuilder.AddSphere( new Point3D(x,y,z), sphereSize , theta, phi);
GeometryModel3D sphereModel = new GeometryModel3D(meshBuilder.ToMesh(),MaterialHelper.CreateMaterial(Brushes.Green,null,null,1,0));
Rendering the Point in your viewport
You will need a ModelVisual3D component as a child of the HelixViewport. ( This can be implemented in C# or in XAML) its up to you, ill show both ways.
C# version
NB: You need a reference to the helixviewport if its defined in xaml. Set the x:Name:"" to something appropriate. e.g x:Name="helixViewPort"
ModelVisual3D visualizer = new ModelVisual3D();
visualizer.Content = sphereModel;
helixViewPort.Children.Add(visualizer);
XAML version
I'll assume your xaml code has at least a helix view port so you'll have to add a ModelVisual3D child to the helix viewport if there's none.
<h:HelixViewport3D x:Name="HelixPlotViewPort" >
<h:DefaultLights/>
<ModelVisual3D x:Name="Visualizer">
</ModelVisual3D>
</h:HelixViewport3D>
//Then in C# add the following
Visualizer.Content = sphereModel;
That should do it, hope it helps, do inform us if you find a better solution. :)

Windows Phone - check if MapCore.SetView doesn't zoom too much

I am using method SetView to zoom for two points in map but when they are too close I am too zoomed. So I hope I can check something like this:
myMap.SetView(bounds);
if (myMap.ZoomLevel > Constants.DefaultZoomLevel)
myMap.ZoomLevel = Constants.DefaultZoomLevel;
But method SetView isn't immediately set ZoomLevel property. What can I do to fix it? How can I set some zoom level border? Thanks
Edit:
I found that in 8.0 sdk there is ZoomLevelChanged event? This could be useful for me. So is there possible how to get it worked in 7.1?
I tried several solutions and these are my results (I needed working for WP7.1, with just targeting for WP8 there could be easier solution):
Use of TargetZoomLevel
Like this:
myMap.ZoomLevel = Math.Min(myMap.TargetZoomLevel, maxZoomLevel);
Doesn't solve my problem because SetView set ZoomLevel after this code execute. Map was just blinking between ZoomLevel from SetView and maxZoomLevel (I have timer for checking positions so that is why it was blinking and isn't good for me).
Use of MapZoom event
Like this:
private void map1_MapZoom(object sender, Microsoft.Phone.Controls.Maps.MapZoomEventArgs e)
{
if (((Map)sender).ZoomLevel > maxZoomLevel)
{
e.Handled = true;
}
}
Problem: MapZoom event handler is not fired when SetView it's used.
Check distance and set ZoomLevel if distance is little:
public static double GetDistanceBetweenPoints(LatLon firstPoint, LatLon secondPoint)
{
var sCoord = new GeoCoordinate(firstPoint.Lat, firstPoint.Lon);
var eCoord = new GeoCoordinate(secondPoint.Lat, secondPoint.Lon);
return sCoord.GetDistanceTo(eCoord);
}
usage:
var distance = LocationHelper.GetDistanceBetweenPoints(carPosition, userPosition);
if (distance < MinMetersDistance)
myMap.ZoomLevel = maxZoomLevel;
Problem: Again blinking. Sometimes I set ZoomLevel by this code. Other time it was set by SetView.
My real solution:
I just check distance between two points like in previous dot. But this time if the distance was little I just create new points from the old ones and set them in new positions (latitude and longitude) which was in min distance (one on the left side I moved more to left and second on the right side I moved more to right). Then I used SetView on new points.

How to retrieve the selected range in the .Net WinForms Chart Control?

I'm using the C# built-in Winforms Chart control (System.Windows.Forms.DataVisualization.Charting.Chart) with its built-in ability to let the user select a range. What I'd like to do is to read back what range the user has selected. Surely there must be some easy way to do this, but I haven't been able to find it.
The cursor is enabled like so:
var ca = chart1.ChartAreas["ChartArea1"].CursorX;
ca.CursorX.IsUserEnabled = true;
ca.CursorX.IsUserSelectionEnabled = true;
I am aware that I can make the chart zoom when the user selects a range by enabling ca.AxisX.ScaleView.Zoomable, but I don't want the picture to change: instead I am using the chart as a way to display information and let the user select a range of X values for which I then do some extra processing.
I tried hooking to chart1.SelectionRangeChanged and that indeed fires every time the range is changed - I just can't seem to get the selection range from the CursorEventArg I get back. It has "NewSelectionStart" and "NewSelectionEnd" fields, but those are NaN, disappointingly. I tried looking at the various properties of the chart and the axes, but didn't find anything that sounded promising.
Further investigation reveals the ChartArea.CursorX.SelectionStart property which sounds like exactly what I need... except that it's NaN too. I don't know whether this is normal or I'm hitting some sort of bug?
So, how can I figure out what range the user selected?
OK, well, I figured it out. Here's the scoop:
There's a SelectionRangeChang*ing* event, and when that one runs the ChartArea.CursorX.SelectionStart and ChartArea.CursorX.SelectionEnd fields have correct values in them. But the user hasn't released the mouse button yet, so you should just store them.
When the user releases the mouse button, the SelectionRangeChang*ed* event fires. Somehow it's designed in such a way that SelectionStart and SelectionEnd are reset to NaN (just like the NewSelectionStart and NewSelectionEnd fields in the event parameters). What you have to do is to use the values that you squirreled away from the other event handler now that you know the time is right to use them.
So there you have it! Hopefully this answer will save someone else from wasting time.
In addition to redtuna to set cursors in a c# chart:
It worked for me to use "SelectionRangeChanging" instead of "SelectionRangeChanged" to not get the NaN issue:
When initializing the Form
this.chart1.SelectionRangeChanging += chart1_SelectionRangeChanging;
and
chart1.ChartAreas[0].CursorX.IsUserEnabled = false; // red cursor at SelectionEnd
chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = false; // zoom into SelectedRange
chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
chart1.ChartAreas[0].CursorX.Interval = 0.01; // set "resolution" of CursorX
What is executed if the range is chosen / the cursors are set
private void chart1_SelectionRangeChanging(object sender, CursorEventArgs e)
{
double x1 = x1 = e.NewSelectionStart; // or: chart1.ChartAreas[0].CursorX.SelectionStart;
double x2 = e.NewSelectionEnd; // or: x2 = chart1.ChartAreas[0].CursorX.SelectionEnd;
double diffx1x2 = x2 - x1;
}
To zoom in & out (x-axis) I just added a button that takes the cursor values. That way zooming by mouseClick (ScaleView.Zoomable = false;) does not interfere my cursor positioning :)
private void button_ZoomIn(object sender, EventArgs e)
{
double x1 = chart1.ChartAreas[0].CursorX.SelectionStart; // x1 = X1
double x2 = chart1.ChartAreas[0].CursorX.SelectionEnd; // x2 = X2
if (x2 > x1)
{
// hard setting: chart1.ChartAreas[0].AxisX.Minimum = x1;
// hard setting: chart1.ChartAreas[0].AxisX.Maximum = x2;
chart1.ChartAreas[0].AxisX.ScaleView.Zoom(x1,x2); // dynamic approach with scrollbar
}
else
{
chart1.ChartAreas[0].AxisX.ScaleView.Zoom(x2,x1);
}
}
Zoom out
private void button_ZoomOut(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset(0);
}
Zooming can also be implemented by mouseWheel: how to enable zooming in Microsoft chart control by using Mouse wheel
And if you also want right-click action in the chart: How to get a right click mouse event? Changing EventArgs to MouseEventArgs causes an error in Form1Designer?

Categories