ChartView and PrintVisual

15 posts, 0 answers
  1. Andrew
    Andrew avatar
    37 posts
    Member since:
    Dec 2012

    Posted 23 Jul 2015 Link to this post

    I'm currently adding print functionality to my WPF application. To do this I'm using the function below which takes in a visual element, scales it to ensure that it will fit on a paper page, then prints using PrintDialog.PrintVisual(...) 

    private void ScalePrint(Visual visual, string printingTitle = null)
    {
        System.Windows.FrameworkElement e = visual as System.Windows.FrameworkElement;
        if (e == null)
            return;
     
        PrintDialog pd = new PrintDialog();
        if (pd.ShowDialog() == true)
        {
            //store original scale
            Transform originalScale = e.LayoutTransform;
            //get selected printer capabilities
            System.Printing.PrintCapabilities capabilities = pd.PrintQueue.GetPrintCapabilities(pd.PrintTicket);
     
            //get scale of the print wrt to screen of WPF visual
            double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / e.ActualWidth, capabilities.PageImageableArea.ExtentHeight /
                            e.ActualHeight);
     
            //Transform the Visual to scale
            e.LayoutTransform = new ScaleTransform(scale, scale);
     
            //get the size of the printer page
            System.Windows.Size sz = new System.Windows.Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
     
            //update the layout of the visual to the printer page size.
            e.Measure(sz);
            e.Arrange(new System.Windows.Rect(new System.Windows.Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
     
            //now print the visual to printer to fit on the one page.
            pd.PrintVisual(visual, printingTitle);
     
            //apply the original transform.
            e.LayoutTransform = originalScale;
        }
    }

    This works fine except when it comes to Telerik charts. For example when I try to scale and print a Scatter Spline chart the dots are drawn in the correct position, but the connecting spline is drawn offset to the right, the offset is worse when the chart is scaled to fit a portrait page. I have attached three images, the first showing the correct chart, the second showing how the chart prints in landscape view, and the third showing how the chart prints in ​portrait view. The printed images were captured using CutePDF but I get the same results printing to printers. 

    Do you know of a way to scale the charts to the size of the page correctly for printing, or do I need to use a separate print method. Please note that in many cases the chart will need to be printed along with other controls on the page; there will be very few, if any, instances of a chart being printed on its own. 

    Thanks for the help.

     Andrew. 

  2. Andrew
    Andrew avatar
    37 posts
    Member since:
    Dec 2012

    Posted 23 Jul 2015 in reply to Andrew Link to this post

    Sorry, I forgot to attach the images...
  3. UI for WPF is Visual Studio 2017 Ready
  4. Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 24 Jul 2015 Link to this post

    Hello Andrew,

    I created a small project based on the code you provided. I was unable to get the same results as yours. While we can make a suggestion on how to try to resolve this, we would like to understand first why you get a different behavior. I am attaching the project in mind. Do try it on your side and see if the spline is exported correctly. If it is, try to find what are the differences between the small demo project and your actual project. In need you can modify the small project so that it reproduces the faulty behavior and send it back to us for investigation.

    Regards,
    Petar Marchev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  5. Andrew
    Andrew avatar
    37 posts
    Member since:
    Dec 2012

    Posted 24 Jul 2015 in reply to Petar Marchev Link to this post

    Hi Petar,

    Thanks for the example program, sadly I get similar results (see attached pictures). I should have mentioned in my first post that we are using an older version of the Telerik dlls; 2013.3.1204.40. Is this something that has perhaps been fixed in a newer release? 

    I will have a further play with the example program and see if I can ​get it working as it is simpler to debug than my full application. I'll let you know if I find anything of interest. 

    Thanks,

     Andrew.

  6. Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 24 Jul 2015 Link to this post

    Hello Andrew,

    It is possible that this was some problem in an older version, but there is no easy way of finding what could be the reason for this. I suggest you either upgrade to the latest version, so that you get the desired effect, or try to include a Measure(Size.Empty) before the measure you have now and include a UpdateLayout right after the arrange. Let us know how it goes.

    Regards,
    Petar Marchev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  7. Andrew
    Andrew avatar
    37 posts
    Member since:
    Dec 2012

    Posted 24 Jul 2015 Link to this post

    Hi Petar,

    I've tried adding Measure() and UpdateLayout() to the code as below: 

    e.Measure(Size.Empty);
    e.Measure(sz);
    e.Arrange(new System.Windows.Rect(new System.Windows.Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
    e.UpdateLayout();

    This fixes the odd problem with the spline chart but also undoes the scaling. The chart in the example program is small but the chart in my app is larger than an A4 piece of paper, which is why I need to scale it to fit on one page. The printout produced by the code with Measure() and UpdateLayout() is the same as code without scaling with the chart not fitting on one page. 

    Do you have any other suggestions? Can you replicate the same problem with the older 2013.3.1204.40 dlls? If you can confirm that this is something that has been fixed in a later version then I can talk to my boss about purchasing the newest dlls, but I can't guarantee this will be an option.

    Thank you again for all of your help on this,

    Andrew. 

  8. Martin
    Admin
    Martin avatar
    1099 posts

    Posted 28 Jul 2015 Link to this post

    Hi Andrew,

    I tested Petar's project with version 2013.3 1204 of our controls and I can confirm that the chart is not printed properly. However, this issue is not reproducible in the latest release of our UI for WPF suite. Please consider upgrading the Telerik dlls so that you can take advantage of the latest features and bug fixes.

    I also tested the workaround proposed by Peter with the Q3 2013 version and the chart is printed as expected. You can try to manually delete the "bin" and "obj' folders of your project and then Clean, and Rebuild the solution on your side and let me know if the issue still occurs. 

    Regards,
    Martin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  9. Andrew
    Andrew avatar
    37 posts
    Member since:
    Dec 2012

    Posted 28 Jul 2015 in reply to Martin Link to this post

    Hi Martin,

    Thanks for the confirmation that this is an issue that has been fixed. I can now talk to my boss about the possibility of upgrading. 

    I tried your suggestion, deleting the bin and obj folders and doing a clean rebuild, but I still get the same result; the chart is no longer scaled to the page size. If the chart is smaller than A4 then the chart takes up a small part of the A4 page. If it is larger than A4 then the chart is cut off. 

    I replicated this in Petar's sample project by setting the chart width to 1000 and adding the e.Measure(Size.Empty) and e.UpdateLayout() lines. I have attached pictures showing a screenshot of the app when I printed (the X Axis going up to 6) and the resulting printout showing the chart cut off (chart missing the last point). 

    Can you replicate this in the Petar's sample project using the Q3 2013 version? If not could you post an updated project that scales  correctly without misdrawing the chart so I can see where I'm going wrong? 

    In the meantime I'll talk to my boss about the possibility of updating. 

    Thanks, 

    Andrew. 

  10. Martin
    Admin
    Martin avatar
    1099 posts

    Posted 31 Jul 2015 Link to this post

    Hello Andrew,

    Note that the PrintDialog will print only the part of control that is visible in the application's viewport. If for some reason the chart is clipped in the application, the print also will be clipped. In your case you are changing the chart's size in code which might lead to clipping if the print size is bigger than the chart's size. Please double check your code for such behavior and ensure that the chart is visible in the visual tree before the printing. 

    Let me know if this helps.

    Regards,
    Martin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  11. Andrew
    Andrew avatar
    37 posts
    Member since:
    Dec 2012

    Posted 31 Jul 2015 in reply to Martin Link to this post

    Hi Martin,

    I am aware with this limitation in PrintVisual and have already worked around it. 

    I've just encountered a new twist to this problem. I've now update to Windows 10 (from Windows 8.1) and found that both the sample app and my app work as described with the fix you suggested (e.Measure(Size.Empty) and e.UpdateLayout()). What OS were you testing on? Can you replicate the described problem in Windows 8.1? 

    Thanks,

    Andrew. 

  12. Andrew
    Andrew avatar
    37 posts
    Member since:
    Dec 2012

    Posted 03 Aug 2015 Link to this post

    I can confirm that the same problem is also present in Windows 7. 
  13. Martin
    Admin
    Martin avatar
    1099 posts

    Posted 05 Aug 2015 Link to this post

    Hello Andrew,

    I am testing the project on Windows 8.1. Can you please try the following:
    • Check if the issue reproduces on a different machine (with Windows 7 or 8)
    • Create new project with your code and check if the issue is still reproducible on your side
    • Can you send me runnable code snippets with your implementation, steps to reproduce the issue and drawings (and/or video) with the actual and expected results?

    Regards,
    Martin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  14. Andrew
    Andrew avatar
    37 posts
    Member since:
    Dec 2012

    Posted 05 Aug 2015 in reply to Martin Link to this post

    Hi Martin,

    I have tested the modified sample code you gave me on a different Windows 7 machine and encountered the exact same problem. I haven't been able to test it yet on a different Windows 8.1 machine. 

    I've been unable to upload the modified test program as you only allow uploads of type jpg, jpeg, .gif and png. I detailed the only changes I made to the code in posts 6 and 8 of this topic.

    To reproduce the error:

    • Stretch window until the whole chart is visible (see AppScreenshot.png from post 8 in this topic).
    • Press the "click" button.
    • Select landscape orientation.
    • Print (the problem happens both when printing to pdf or paper).

    When I did this on my Windows 8.1 before upgrading to Windows 10 this produced a print out that looked like PrintOut.png from post 8 in this topic. You can see that the X axis in AppScreenshot.png goes ranges from 0 to 6. However in PrintOut.png the X axis is cut off just before 6. I think this is because the chart is no longer being scaled. Removing e.Measure(Size.Empty) and e.UpdateLayout() causes the chart to scale correctly so that the whole X-axis is visible but causes the odd spline problem mentioned in the first post. It's as if e.Measure(Size.Empty) and e.UpdateLayout() are resetting the scaling that has previously been applied to FrameworkElement e. 

    If you require more detailed instructions or examples of the problems let me know. 

    Andrew. 

  15. Andrew
    Andrew avatar
    37 posts
    Member since:
    Dec 2012

    Posted 05 Aug 2015 Link to this post

    I've got around your upload limitations by renaming the files from .zip to .jpg. Please download them, change the extensions and you will have both the code and the exact version 2013.3.1204.40 dlls that I am using. 
  16. Martin
    Admin
    Martin avatar
    1099 posts

    Posted 10 Aug 2015 Link to this post

    Hi Andrew,

    Thank you for the additional information. I tested your project with the attached dlls and the printing orientation set to Landscape on couple machines but I was not able to reproduce the issue. You can take a look at the attached .oxps file to see the end result after following the steps to recreate the clipping behavior - the chart looks as expected. I am afraid that without reproducing this behavior I won't be able to further investigate it, so I recommend you to test your project with our latest release and use it if possible for you.

    Regards,
    Martin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WPF is Visual Studio 2017 Ready