Page Index Toggle Pages: 1 2 3 [4] 5  Send TopicPrint
Very Hot Topic (More than 25 Replies) Operations in the diagram (Read 31408 times)
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Operations in the diagram
Reply #45 - Mar 26th, 2020 at 2:59pm
Print Post  
Hi,

Already answered here:

Lyubo wrote on Mar 23rd, 2020 at 2:45pm:
For the image, I already explained how to maintain its size in my previous post. If you can modify the size of your image to be what you need to be seen inside the node - the effect from your screen shot can be achieved just by setting node.ImageAlign to ImageAlign.BottomRight. If you can't modify the size of the image, set node.ImageAlign to ImageAlign.Fit and use node.ImagePadding to restrict the image to your needed bounds. For example, if your node has a size of 50 by 50 and you need your image to have size 40 by 40 - set node.ImagePadding = new Thickness(10, 10, 0, 0);. That will have the same effect.


and here:

Lyubo wrote on Mar 25th, 2020 at 8:49am:
In addition to the above, if you want to convert the image size (which is in pixels) to diagram's MeasureUnit, use the following code:
Code
Select All
var imageWidthPx = myImage.PixelWidth * 96 / myImage.DpiX;
var imageHeightPx = myImage.PixelHeight * 96 / myImage.DpiY;

var imageWidth = GraphicsUnit.Pixel.Convert(imageWidthPx, diagram.MeasureUnit);
var imageHeight = GraphicsUnit.Pixel.Convert(imageHeightPx, diagram.MeasureUnit); 



Regarding zooming, I can't really understand your issue. Zooming to the center of the viewport works as expected in my test, following your requirements from here:

Lyubo wrote on Mar 23rd, 2020 at 7:16am:
Hi,

D wrote on Mar 23rd, 2020 at 3:08am:
I want to change the mouse wheel to the center point in the view during the zoom in and zoom out events of the diagram instead of the current coordinate position of the mouse as the base point.
How to achieve? Is there any sample code? Smiley


Change the mouse wheel handler to this:
Code
Select All
		private void OnDiagramMouseWheel(object sender, MouseWheelEventArgs e)
		{
			var zoomRect = diagram.Viewport;
			if (e.Delta < 0)
				zoomRect.Inflate(10, 10);
			else
				zoomRect.Inflate(-10, -10);
			diagram.ZoomToRect(zoomRect, true);
		} 



Regards,
Lyubo
MindFusion
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Operations in the diagram
Reply #46 - Mar 26th, 2020 at 3:59pm
Print Post  
Thank you, Lyubo, for teaching me how to convert the image size to MeasureUni for charts, but I don’t need it at this time. Let me tell you my current situation: if I use imageNode.ImageAlign = ImageAlign.BottomRight; the result is only showing the BottomRight of the node, obviously this is not what I want, I want the whole picture to be displayed. If I use imageNode.ImageAlign = ImageAlign.Fit; the result is that the node contains extra space in addition to the image, which is not the result I want. What I want is the size of imageNode is the size of imageNode.Image. And the bottom and right sides of the picture coincide with the bottom and right sides of another node overlayNode. Please give me sample code.I need it urgently.
  
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Operations in the diagram
Reply #47 - Mar 26th, 2020 at 4:33pm
Print Post  
Hi,

Did you try the code I posted? If you did, you would see that it does exactly this. It gives you the dimensions of the image and then converts them to the measure unit that's currently in use for your diagram instance, so you can use them as width and height of your imageNode. Just use the values of imageWidth and imageHeight from the code example and pass them to the CreateShapeNode method you're already using. When the node and image sizes coincide, it doesn't really matter what ImageAlign value you would use, provided that you don't resize the node.

If you want the image file to be of different size, you can use any number of techniques available in WPF or just resize the image file itself.

Regards,
Lyubo
MindFusion
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Operations in the diagram
Reply #48 - Mar 27th, 2020 at 1:53am
Print Post  
Thanks a lot Smiley
At present, I do n’t have enough knowledge about this. I need you to teach me a little bit more. The code block in the screenshot reminds me that I cannot use this code correctly. What type of myImage is this? How can I solve the problem that this code cannot be used?
  

3_27.png (Attachment deleted)
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Operations in the diagram
Reply #49 - Mar 27th, 2020 at 5:53am
Print Post  
Hi,

Try this:

Code
Select All
var myImage = new BitmapImage(new Uri(@"D:\Mindfusion_help\ruler.png"));

var imageWidthPx = myImage.PixelWidth * 96 / myImage.DpiX;
var imageHeightPx = myImage.PixelHeight * 96 / myImage.DpiY;
var imageWidth = GraphicsUnit.Pixel.Convert(imageWidthPx, diagram.MeasureUnit);
var imageHeight = GraphicsUnit.Pixel.Convert(imageHeightPx, diagram.MeasureUnit);

var imageNode = diagram.Factory.CreateShapeNode(0, 0, imageWidth, imageHeigh);
imageNode.Image = myImage;

// the rest of your code ... 



Regards,
Lyubo
MindFusion
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Operations in the diagram
Reply #50 - Mar 27th, 2020 at 7:26am
Print Post  
SmileyThank you very much, Lyubo.
At present, I have achieved the effect of screenshot 1, but I have not made the bottom and right sides of the overlayNode coincide with the bottom and right sides of the imageNode. I know the reason is definitely that I have not adjusted the size of the overlayNode or need some other operating. Please tell me what needs to be changed in the code.
  

3_27_1.png (Attachment deleted)
3_27_2.png (Attachment deleted)
3_27_3.png (Attachment deleted)
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Operations in the diagram
Reply #51 - Mar 27th, 2020 at 7:38am
Print Post  
diagram.NodeDoubleClicked + = (sender, args) =>
{
var node = args.Node;

var posX = args.MousePosition.X-node.GetCenter (). X;
var posY = args.MousePosition.Y-node.GetCenter (). Y;

var newPos = new Point (
diagram.Viewport.X + diagram.Viewport.Width / 2-node.Bounds.Width / 2-posX,
diagram.Viewport.Y + diagram.Viewport.Height / 2-node.Bounds.Height / 2-posY);

node.SetBounds (new Rect (newPos, node.Bounds.Size), true, true);
};

Now I want to extend this function: no matter the imageNode or overlayNode is double clicked, the imageNode and overlayNode will move together so that the point coincides with the center point of the centerline. What changes need to be made? Smiley
  
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Operations in the diagram
Reply #52 - Mar 27th, 2020 at 7:42am
Print Post  
Code
Select All
// width and height are just some arbitrary values, change as per your requirements
var overlayNodeWidth = 200;
var overlayNodeHeight = 200;

overlayNode = diagram.Factory.CreateShapeNode(imageNode.Bounds.Right - overlayNodeWidth,
  imageNode.Bounds.Bottom - overlayNodeHeight, overlayNodeWidth, overlayNodeHeight);

// rest of your code 

  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Operations in the diagram
Reply #53 - Mar 27th, 2020 at 9:07am
Print Post  
SmileyYes, you are right, I have done it, thank you so much, Lyubo. At present, I need to optimize the double-click event of the diagram. Because there was only one node before, and now there are two nodes, this function does not meet the current needs: diagram.NodeDoubleClicked + = (sender, args) =>
{
var node = args.Node;

var posX = args.MousePosition.X-node.GetCenter (). X;
var posY = args.MousePosition.Y-node.GetCenter (). Y;

var newPos = new Point (
diagram.Viewport.X + diagram.Viewport.Width / 2-node.Bounds.Width / 2-posX,
diagram.Viewport.Y + diagram.Viewport.Height / 2-node.Bounds.Height / 2-posY);

node.SetBounds (new Rect (newPos, node.Bounds.Size), true, true);
};

Now double-click on any point within the range of the overlayNode, but that point of the overlayNode moves to the center point of the centerline. This result is not what I want. What I want is that after double-clicking on any point of the overlayNode, the point of the overlayNode, along with the point of the imageNode, moves to the center point of the centerline. What changes do I need to make in this function code? Smiley
  

3_27_4.png (Attachment deleted)
3_27_6.png (Attachment deleted)
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Operations in the diagram
Reply #54 - Mar 27th, 2020 at 10:30am
Print Post  
Hi,

Add the following just after the node assignment in your DoubleClick handler:
Code
Select All
if (node.MasterGroup != null)
    node = node.MasterGroup.MainItem as DiagramNode; 



That way all double clicks on the attached node will move the master node instead.

Regards,
Lyubo
MindFusion
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Operations in the diagram
Reply #55 - Mar 27th, 2020 at 10:44am
Print Post  
Thank you very much, Lyubo Smiley
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Operations in the diagram
Reply #56 - Mar 27th, 2020 at 11:40am
Print Post  
This function:
private void diagram_MouseWheel (object sender, MouseWheelEventArgs e)
{
var zoomRect = diagram.Viewport;
if (e.Delta <0)
zoomRect.Inflate (10, 10);
else
zoomRect.Inflate (-10, -10);
diagram.ZoomToRect (zoomRect, true);
}

I would like to ask why the image cannot be reduced from the center point of the center line after being reduced to a certain extent. Instead, the picture swings up, down, left and right. However, at this time, if you enlarge the picture again, it will become a picture that swings up, down, left, and right, instead of the picture I want to zoom in and out at the midpoint of the crosshairs. What changes do I need to make in this code block? Smiley
  
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Operations in the diagram
Reply #57 - Mar 27th, 2020 at 1:21pm
Print Post  
Hello,

You need to make sure you don't zoom out to a point where the the viewport becomes bigger than the diagram document. You can add the following constraint before calling the ZoomToRect method:
Code
Select All
if (diagram.Bounds.Width >= zoomRect.Width && diagram.Bounds.Height >= zoomRect.Height)
    diagram.ZoomToRect(zoomRect, true); 



You may also want to consider the fact that the mouse wheel scrolls the diagram document by default. To prevent this, handle PreviewMouseScroll event instead (use same handler), and add this at the end:

Code
Select All
// stop the scrollviewer from scrolling
e.Handled = true; 



That way zooming will always happen relative to the center of the viewport.

Regards,
Lyubo
MindFusion
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Operations in the diagram
Reply #58 - Mar 27th, 2020 at 2:37pm
Print Post  
Thank you very much, Lyubo. Smiley
Does your implementation of PreviewMouseScroll event look like this:
private void diagram_PreviewMouseWheel (object sender, MouseWheelEventArgs e)
{
var zoomRect = diagram.Viewport;
if (diagram.Bounds.Width> = zoomRect.Width && diagram.Bounds.Height> = zoomRect.Height)
diagram.ZoomToRect (zoomRect, true);
if (e.Delta <0)
zoomRect.Inflate (10, 10);
else
zoomRect.Inflate (-10, -10);
diagram.ZoomToRect (zoomRect, true);
e.Handled = true;
}

I currently want to use the values ​​of the two TextBoxes as the length and width of the overlayNode. The code is as follows:
private void RibbonButton_Click (object sender, RoutedEventArgs e)
{
if (overlayNode == null)
return;

double x = overlayNode.Bounds.X,
y = overlayNode.Bounds.Y;
// Get the new values ​​from the 2 text boxes
double newValue;
if (double.TryParse (textBoxX.Text, out newValue))
x = newValue;
if (double.TryParse (textBoxY.Text, out newValue))
y = newValue;
var newBounds = new Rect (imageNode.Bounds.Right-x, imageNode.Bounds.Bottom-y, x, y);
// Change the node's position / dimensions
overlayNode.SetBounds (newBounds, true, true);
}

The result is that the overlayNode has only changed, but no transparency effect has occurred in its size range. The transparency effect is still the 200 size set previously, and I want to remove the border of the overlayNode. What changes do I need to make? Smiley
  

3_27_8.png (Attachment deleted)
3_27_9.png (Attachment deleted)
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Operations in the diagram
Reply #59 - Mar 27th, 2020 at 2:57pm
Print Post  
Hi,

My scroll handler looks like this:
Code
Select All
		private void OnDiagramMouseWheel(object sender, MouseWheelEventArgs e)
		{
			var zoomRect = diagram.Viewport;
			if (e.Delta < 0)
				zoomRect.Inflate(10, 10);
			else
				zoomRect.Inflate(-10, -10);
			if (diagram.Bounds.Width >= zoomRect.Width && diagram.Bounds.Height >= zoomRect.Height)
				diagram.ZoomToRect(zoomRect, true);

			// stop the scrollviewer from scrolling
			e.Handled = true;
		} 



To update the semi-transparent layer, after you call overlayNode.SetBounds, force repaint the imageNode by using imageNode.InvalidateVisual();. To remove the border of the overlayNode set its StrokeThickness property to 0.

Regards,
Lyubo
MindFusion
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1 2 3 [4] 5 
Send TopicPrint