Tuesday, 27 January 2009

Microsoft Chart Controls in a WPF application!

Microsoft Chart Controls are great! If you have never used this addin for the .NET framework 2.0 + then I adivse you to download it from Microsoft and give it a whirl. There is a plugin for Visual Studio which lets you create the charts in minutes but you can always use the code.

Recently I have been pushing the use of using WPF applications for business use, once a developer (or designer) becomes accustomed to the products availble to them to create WPF applications (aka. Expression Blend) then UI and Business Logic code can be done seperatly and at the same time resulting in faster development time, and more importantly more time to spend on giving the user a greater experience with the application they are using.

In this small project, I needed to show a datasource in a chart, I already have used Microsoft Chart Controls and know of its power but how would I show this in a WPF application? Well Microsoft knew that there would be times that Windows.Forms controls may need to be hosted within a WPF application and so have given us the power to do so.

The steps below allow you to load in almost any control that is designed to be run on the .NET 2.0 + framework, in this case it is tailored to the Microsoft Chart Controls but you should be able to see how it comes together.

Many thanks go to the author of the blog host-windows-forms-controls-in-wpf, Keyvan Nayyeri ('http://nayyeri.net/blog/host-windows-forms-controls-in-wpf/') without the information on this blog I would have been lost, I advise you all to give the blog a look as it has lots of information.

1) Importing DLL's

Ok the first step is the reference the DLL's you need to accomplish this task, firstly you will need to reference the System.Windows.Forms.Integration (WindowsFormsIntegration.dll). This dll is included in the Windows SDK so download and install that first (warning its about a 1GB download). This dll can be found at
C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0, please remember I am running Windows Vista 64Bit and so the directory could be different for you.

Secondly reference the chart controls dll, usually found in C:\Program Files\Microsoft Chart Controls\Assemblies. The dll you want to reference is 'System.Windows.Forms.DataVisualization.dll'

Then also reference the System.Windows.Forms dll.

2) Once this is done we need to reference the namespaces within the WPF window. To do this add the following just below the line:

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:CHR="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization"



This will allow us to use the namespace CHR within the WPF xaml code to create objects from the namespace System.Windows.Forms.DataVisualization.Charting. In this namespace we can create a chart.

3) We need to create a container within the WPF application that will host the Windows.Forms controls.

WPF nativly does not run Windows.Forms controls, the Forms integration dll that we imported allows this to happen. So in the XAML code of the WPF window enter the following

<wfi:WindowsFormsHost x:Name="mainFGrid" >

</wfi:WindowsFormsHost>

This will insert a container in which Windows.Forms controls can be placed, you will need to add any controls by hand as the Blend program does not know of these controls.

4) So to finally add the Chart control, add the following line within the Windows Forms Host container

<CHR:Chart x:Name="mainChart" />





This adds the chart control to the container, once built the chart can be accessed normally in code through Visual Stuido.

Using this method you can create a Chart control and then code bind data to it, you can manipulate all the features of the control and all properites and methods are accessable via VS.



I recommend downloading the Microsoft Chart Controls sample from Microsoft to see what is capable.


Links.

Microsoft Chart Controls Samples

Microsoft Chart Controls Download (It states .NET framework 3.5 but it works in 2.0+)

Microsoft Chart Controls Addin for VS2008

Download the above and go forth and CHART!!!!

46 comments:

  1. can you please put source somewhere... thanks :)

    ReplyDelete
  2. Thanks, this got me started. I was running around in circles before I got here.

    ReplyDelete
  3. Thanks ever so much, very useful article. If you do not mind, please visit my article related to travel to Pandeglang district in Banten, Indonesia at Kenali dan Kunjungi Objek Wisata di Pandeglang

    Best regards

    ReplyDelete
  4. I don’t know If I said it already, but this so good stuff keep up the good work. I read a lot of blogs on a daily basis and for the most part Belajar blogging just wanted to make a quick comment to say I’m glad I found your blog. Thanks.

    Best regards
    Seo Motivation Oes tsetnoc faq

    ReplyDelete
  5. Lantern FFXIV GIL hanging up high, singing Wow Power Leveling indistinct indistinct, ambiguous buy wow gold voice aion kinah of an endless supply, Yan Yi extremely coquettish woman, the endless, is indeed maple story mesos the ancient aion power leveling red-light district, this in no way inferior to the modern dofus kamas scene.

    Fei Zhuge eyes looked ffxi gil slightly greedy, the immediate metin2 yang exposure of the Courtesan clothes woman, see their own wow gold winks straight throw, breast Luanhuang, d.m.z. some color eve isk greedy, so that one knight online gold side of the purple spirit companions men stare at him, in last chaos gold the He had a rom gold waist-twisting, painful grimace atlantica gold in pain, he looked a purple Ling said: "do pinch me?"

    ReplyDelete
  6. 我從來不認為不同意我的看法就是冒犯........................................

    ReplyDelete
  7. 若對自己誠實,日積月累,就無法對別人不忠了。........................................

    ReplyDelete
  8. 若對自己誠實,日積月累,就無法對別人不忠了。........................................

    ReplyDelete
  9. 樂觀進取,勇往直前,持之以恒,是克服困難的妙方。 ............................................................

    ReplyDelete
  10. 在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」....................................................................

    ReplyDelete
  11. 向著星球長驅直進的人,反比踟躕在峽路上的人,更容易達到目的。............................................................

    ReplyDelete
  12. 向著星球長驅直進的人,反比踟躕在峽路上的人,更容易達到目的。............................................................

    ReplyDelete
  13. 這麼用心的經營你的文章, 當然值得我們留連拜訪的!............................................................

    ReplyDelete
  14. 當你真心渴望某一樣東西,整個宇宙都會聯合起來幫助你。..................................................

    ReplyDelete
  15. 永遠不要躊躇伸出你的手。也永遠不要躊躇接受別人伸出的手。.................................................................

    ReplyDelete
  16. [做人難,人難做,難做人] 人.事的艱困與磨難,是一種考驗!要以樂觀歡喜之心,很珍惜地過每一天!^^..................................................

    ReplyDelete
  17. 我們必須先有哭泣,才有歡笑;也必須先感到人生的悲哀,然後才感到人生的快樂。............................................................

    ReplyDelete
  18. 生存乃是不斷地在內心與靈魂交戰;寫作是坐著審判自己。..................................................

    ReplyDelete
  19. 噴泉的高度,不會超過它的源頭。一個人的事業也是如此,它的成就絕不會超過自己的信念。............................................................

    ReplyDelete
  20. 拒絕冒險和成長的人,終將被生命的潮流陶汰。..................................................

    ReplyDelete
  21. Such kind of articl is our need , it can give us help from different aspect for different people . after reading this article , I learn a mount of knowkedge increase my power , my families and I all like you article ,reading you article is our best love. May be you are Sports fans? Do you like NFL jerseys or Puma Shoes , Ecco shoes , Nike Sneakers ,these have the high quality,low price,professional service,Just For You if you like.Thank you again for writing this article!

    ReplyDelete
  22. Pretty good post. I just found your site and wanted to say that I have really enjoyed browsing your posts.In any case I'll be subscribing to your blog and I hope you post again soon.
    There are many brand from France, also including herve leger, and most of womens stars love wearing herve leger dress when they join in some important party. Now polo ralph lauren is very popular with youthful people, everyone want to get ralph lauren polo shirts, there are lots of online shop which are ralph lauren polo outlet, true religion jeans outlet, it will be convenient for us.

    ReplyDelete
  23. In a report dragonica gold obtained by the newspaper, Dr. Francisco Meza dfo gold says adult film companies refuse to ddo plat cooperate with the investigation eve isk, and stage names for everquest plat performers make it difficult eq2 plat to track down partners. The Los Angeles faxion gold Times reports that fiesta gold health officials are struggling to flyff penya make headway on a probe, a process that gaia online gold is usually much more efficient when grand fantasia gold there is a disease iris gold outbreak.

    In an gamegoldbuy blog interview with The guild wars gold Associated Press on knight noah Friday, Obama predicted loco gold that Congress would raise last chaos gold the debt ceiling, but he acknowledged legend of edda gold that he would have to offer lineage 2 adena more spending cuts in the budget mabinogi gold to get a deal. Later, Obama's spokesman perfect world gold said a debt ceiling vote ro zeny could not be contingent rf online gold on upcoming negotiations rohan crone over the budget.

    "I think that what rift platinum we have to runes of magic gold become aware of rift platinum is that if we allow taxes shaiya gold to fill in the holes silkroad gold here, we are going to find that we are swg credits getting ever closer to the type of vindictus gold economies that exist in warhammer gold Europe, which are very heavily wonderking zed laden and not rapidly growing the ways zentia gold ours can," Greenspan said on NBC.

    ReplyDelete
  24. "I don't comment on the polls wow gold, and I don't spend much time wondering buy wow gold about them. Polls come, polls go. If I spent a lot of cheap wow gold time studying polls I wouldn?t get anything World of Warcraft Gold else done," she said.

    Despite Gillard Aion Gold floundering Aion Kinah, influential Australian ffxi gil Workers Union national secretary Paul ffxiv gil Howes said he continued to back her lotro gold despite threatening last week to withdraw maplestory mesos support if she could not guarantee metin2 yang jobs under the carbon tax."Julia Gillard rift gold is the best leader the Labor Party's had in rift platinum a very, very long period of time," Howes, a central figure in the runescape gold Labor right faction rift platinum that anointed Gillard last year, told reporters.

    "I'm confident she will lead 4story gold Labor to an election aika gold victory at the next apb cash election. But that archlord gold doesn't mean I have to asda story gold support the government on atlantica gold everything they do." Gillard leaves boi gold Wednesday for a trip to Japan, South cabal alz Korea and China before heading to dcuo cash London for the wedding of Prince dekaro dil William and Kate dofus kamas Middleton.

    ReplyDelete