Beruflich Dokumente
Kultur Dokumente
My Blog
Home / My Blog / Charting with VB.Net 2010
MS Access: 2010
1) To start off, lets create a small database in Access. Your Access Database looks like this once it is ready.
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/
1/16
08/01/14
Name the Table as Table1 and the Database as Sample.accdb. Save it to a location of your choice. Remember the location as we will have to use this database later from vb.net. 2) Start Visual Studio and create a new Windows Application Project on VB.NET. Lets Call it Charting. See snapshot below.
Click OK and then place the controls as shown in the image below.
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/
2/16
08/01/14
In earlier versions of vb.net, you had to actually activate the chart control by right clicking on the ContextMenuStrip and then selecting MS Chart Control from the COM Tab. But now you can find that control right under the Data Tab.
Once your controls are ready, open the code editor and simply paste this code. This code will create a very basic chart. I m p o r t sS y s t e m . D a t a . O l e D b I m p o r t sS y s t e m . W i n d o w s . F o r m s . D a t a V i s u a l i z a t i o n . C h a r t i n g P u b l i cC l a s sF o r m 1 ' ~ ~ >C o d er e q u i r e dt ob r o w s ef o rt h eA c c e s sD a t a b a s eF i l e P r i v a t eS u bB u t t o n 1 _ C l i c k ( B y V a ls e n d e rA sS y s t e m . O b j e c t ,B y V a le_ A sS y s t e m . E v e n t A r g s )H a n d l e sB u t t o n 1 . C l i c k W i t hO p e n F i l e D i a l o g 1 . D e f a u l t E x t=" a c c d b " . D e r e f e r e n c e L i n k s=T r u e . F i l t e r=_ " A c c e s sf i l e s( * . a c c d b ) | * . a c c d b " . M u l t i s e l e c t=F a l s e . T i t l e=" S e l e c ta nA c c e s sD a t a b a s ef i l et oo p e n " . V a l i d a t e N a m e s=T r u e I f. S h o w D i a l o g=W i n d o w s . F o r m s . D i a l o g R e s u l t . O KT h e n T r y T e x t B o x 1 . T e x t=. F i l e N a m e C a t c hf i l e E x c e p t i o nA sE x c e p t i o n T h r o wf i l e E x c e p t i o n E n dT r y E n dI f E n dW i t h E n dS u b ' ~ ~ >C o d et og e n e r a t et h ec h a r t P r i v a t eS u bB u t t o n 2 _ C l i c k ( B y V a ls e n d e rA sS y s t e m . O b j e c t ,B y V a l_ eA sS y s t e m . E v e n t A r g s )H a n d l e sB u t t o n 2 . C l i c k D i ms t r C o n nA sS t r i n g=_ " P r o v i d e r = M i c r o s o f t . A C E . O L E D B . 1 2 . 0 ; D a t aS o u r c e = "&T e x t B o x 1 . T e x t&_
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/ 3/16
08/01/14
" ; P e r s i s tS e c u r i t yI n f o = F a l s e ; " D i mt b l F i e l d sA sS t r i n g=" S E L E C T*f r o mT a b l e 1 " D i mc o n nA sN e wO l e D b C o n n e c t i o n ( s t r C o n n ) D i mo C m dA sN e wO l e D b C o m m a n d ( t b l F i e l d s ,c o n n ) D i mo D a t aA sN e wO l e D b D a t a A d a p t e r ( t b l F i e l d s ,c o n n ) D i md sA sN e wD a t a S e t c o n n . O p e n ( ) o D a t a . F i l l ( d s ," T a b l e 1 " ) c o n n . C l o s e ( ) C h a r t 1 . D a t a S o u r c e=d s . T a b l e s ( " T a b l e 1 " ) D i mS e r i e s 1A sS e r i e s=C h a r t 1 . S e r i e s ( " S e r i e s 1 " ) S e r i e s 1 . N a m e=" S a l e s " C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . X V a l u e M e m b e r=" n F r u i t s " C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . Y V a l u e M e m b e r s=" n S a l e s " C h a r t 1 . S i z e=N e wS y s t e m . D r a w i n g . S i z e ( 7 8 0 ,3 5 0 ) E n dS u b E n dC l a s s
When you run the code your form will pop up and looks as shown in the image below. Select your file by clicking on the Load File button. Once you have selected the file, click on Create Chart
When you click on Create Chart you see a chart generated as shown below.
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/
4/16
08/01/14
Now this was making a very basic chart. What if we want it to give a more professional look? What if we wanted to format the title or make the chart look more appealing?
Advanced Charting
Now replace the old code with this new code. I have commented the code so you know what the code is doing. I m p o r t sS y s t e m . D a t a . O l e D b I m p o r t sS y s t e m . W i n d o w s . F o r m s . D a t a V i s u a l i z a t i o n . C h a r t i n g P u b l i cC l a s sF o r m 1 ' ~ ~ >C o d er e q u i r e dt ob r o w s ef o rt h eA c c e s sD a t a b a s eF i l e P r i v a t eS u bB u t t o n 1 _ C l i c k ( B y V a ls e n d e rA sS y s t e m . O b j e c t ,_ B y V a leA sS y s t e m . E v e n t A r g s )H a n d l e sB u t t o n 1 . C l i c k W i t hO p e n F i l e D i a l o g 1 . D e f a u l t E x t=" a c c d b " . D e r e f e r e n c e L i n k s=T r u e . F i l t e r=_ " A c c e s sf i l e s( * . a c c d b ) | * . a c c d b " . M u l t i s e l e c t=F a l s e . T i t l e=" S e l e c ta nA c c e s sD a t a b a s ef i l et oo p e n " . V a l i d a t e N a m e s=T r u e I f. S h o w D i a l o g=W i n d o w s . F o r m s . D i a l o g R e s u l t . O KT h e n T r y T e x t B o x 1 . T e x t=. F i l e N a m e C a t c hf i l e E x c e p t i o nA sE x c e p t i o n T h r o wf i l e E x c e p t i o n E n dT r y E n dI f E n dW i t h E n dS u b ' ~ ~ >C o d et og e n e r a t et h ec h a r t P r i v a t eS u bB u t t o n 2 _ C l i c k ( B y V a ls e n d e rA sS y s t e m . O b j e c t ,_ B y V a leA sS y s t e m . E v e n t A r g s )H a n d l e sB u t t o n 2 . C l i c k ' ~ ~ >C o n n e c t i o nS t r i n gt oc o n n e c tt ot h ea c c e s sD a t a b a s e ' ~ ~ >I fy o ua r ep l a n n i n gt ou s eS Q LD a t a b a s et h e n ' ~ ~ >p l e a s ev i s i tw w w . c o n n e c t i o n s t r i n g s . c o mf o ra n ' ~ ~ >a p p r o p r i a t ec o n n e c t i o ns t r i n g D i ms t r C o n nA sS t r i n g=_ " P r o v i d e r = M i c r o s o f t . A C E . O L E D B . 1 2 . 0 ; D a t aS o u r c e = "&T e x t B o x 1 . T e x t&_ " ; P e r s i s tS e c u r i t yI n f o = F a l s e ; " ' ~ ~ >Q u e r ys t r i n g D i mt b l F i e l d sA sS t r i n g=" S E L E C T*f r o mT a b l e 1 " ' ~ ~ >C o n n e c t i n gt oD a t ab a s ea n ds t o r i n gt h ed a t ai nad a t a s e t D i mc o n nA sN e wO l e D b C o n n e c t i o n ( s t r C o n n ) D i mo C m dA sN e wO l e D b C o m m a n d ( t b l F i e l d s ,c o n n ) D i mo D a t aA sN e wO l e D b D a t a A d a p t e r ( t b l F i e l d s ,c o n n )
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/ 5/16
08/01/14
D i md sA sN e wD a t a S e t c o n n . O p e n ( ) o D a t a . F i l l ( d s ," T a b l e 1 " ) c o n n . C l o s e ( ) ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ~ ~ >S E TD A T AS O U R C E< ~ ~ ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' C h a r t 1 . D a t a S o u r c e=d s . T a b l e s ( " T a b l e 1 " )
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ~ ~ >W O R K I N GW I T HC H A R T A R E A< ~ ~ ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' D i mC A r e aA sC h a r t A r e a=C h a r t 1 . C h a r t A r e a s ( 0 ) C A r e a . B a c k C o l o r=C o l o r . A z u r e ' ~ ~ >C h a n g i n gt h eB a c kC o l o ro f C A r e a . S h a d o w C o l o r=C o l o r . R e d ' ~ ~ >C h a n g i n gt h eS h a d o wC o l o ro C A r e a . A r e a 3 D S t y l e . E n a b l e 3 D=T r u e ' ~ ~ >C h a n g i n gt h eC h a r tS t y l et o
' ~ ~ >F o r m a t t i n gXA x i s C A r e a . A x i s X . M a j o r G r i d . E n a b l e d=F a l s e ' ~ ~ >R e m o v e dt h eXa x i sm a j o rg r C A r e a . A x i s X . L a b e l S t y l e . F o n t=N e wS y s t e m . D r a w i n g . F o n t ( " T i m e sN e wR o m a n " 1 1 . 0 F ,S y s t e m . D r a w i n g . F o n t S t y l e . I t a l i c )' ~ ~ >S e t t i n gF o n t ,F o n tS i z ea n d
' ~ ~ >F o r m a t t i n gYA x i s C A r e a . A x i s Y . M a j o r G r i d . E n a b l e d=F a l s e ' ~ ~ >R e m o v e dt h eYa x i sm a j o rg r C A r e a . A x i s Y . L a b e l S t y l e . F o r m a t=" 0 . 0 0 % "' ~ ~ >F o r m a t t i n gYa x i st od i s p l a C A r e a . A x i s Y . I n t e r v a l=0 . 2 ' ~ ~ >F o r m a t t i n gYa x i sI n t e r v a l ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ~ ~ >W O R K I N GW I T HT I T L E< ~ ~ ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ~ ~ >A d d i n gaT i t l e D i mTA sT i t l e=C h a r t 1 . T i t l e s . A d d ( " S a l e sR e p o r tF r u i t s-Y e a r2 0 1 0 " ' ~ ~ >F o r m a t t i n gt h eT i t l e W i t hT . F o r e C o l o r=C o l o r . B l a c k ' ~ ~ >C h a n g i n gt h eF o r eC o l o ro f . B a c k C o l o r=C o l o r . C o r a l ' ~ ~ >C h a n g i n gt h eB a c kC o l o ro f
' ~ ~ >S e t t i n gF o n t ,F o n tS i z ea n dB o l d / I t a l i c i z i n g . F o n t=N e wS y s t e m . D r a w i n g . F o n t ( " T i m e sN e wR o m a n " ,1 1 . 0 F ,S y s t e m . D r a . F o n t=N e wS y s t e m . D r a w i n g . F o n t ( " T i m e sN e wR o m a n " ,1 1 . 0 F ,S y s t e m . D r a . B o r d e r C o l o r=C o l o r . B l a c k ' ~ ~ >C h a n g i n gt h eB o r d e rC o l o ro . B o r d e r D a s h S t y l e=C h a r t D a s h S t y l e . D a s h D o t D o t' ~ ~ >C h a n g i n gt h eB o r d e E n dW i t h ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ~ ~ >W O R K I N GW I T HS E R I E S< ~ ~ ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' D i mS e r i e s 1A sS e r i e s=C h a r t 1 . S e r i e s ( " S e r i e s 1 " ) ' ~ ~ >S e t t i n gt h es e r i e sN a m e S e r i e s 1 . N a m e=" S a l e s " ' ~ ~ >A s s i g n i n gv a l u e st oXa n dYA x i s
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/ 6/16
08/01/14
C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . X V a l u e M e m b e r=" n F r u i t s " C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . Y V a l u e M e m b e r s=" n S a l e s " ' ~ ~ >S e t t i n gF o n t ,F o n tS i z ea n dB o l d C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . F o n t=N e wS y s t e m . D r a w i n g . F o n t ( " T i m e sN e wR o ' ~ ~ >S e t t i n gV a l u eT y p e C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . Y V a l u e T y p e=C h a r t V a l u e T y p e . D o u b l e ' ~ ~ >S e t t i n gt h eC h a r tT y p ef o rD i s p l a y C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . C h a r t T y p e=S e r i e s C h a r t T y p e . R a d a r ' ~ ~ >D i s p l a yD a t aL a b e l s C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . I s V a l u e S h o w n A s L a b e l=T r u e ' ~ ~ >S e t t i n gl a b e l ' sF o r eC o l o r C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . L a b e l F o r e C o l o r=C o l o r . D a r k G r e e n ' ~ ~ >S e t t i n gl a b e l ' sF o r m a tt o% a g e C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . L a b e l F o r m a t=" 0 . 0 0 % "
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ~ ~ >W O R K I N GW I T HL E G E N D< ~ ~ ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' D i mL GA sL e g e n d=C h a r t 1 . L e g e n d s ( 0 ) ' ~ ~ >C h a n g i n gt h eB a c kC o l o ro ft h eL e g e n d L G . B a c k C o l o r=C o l o r . W h e a t ' ~ ~ >C h a n g i n gt h eF o r eC o l o ro ft h eL e g e n d L G . F o r e C o l o r=C o l o r . D a r k S l a t e B l u e ' ~ ~ >S e t t i n gF o n t ,F o n tS i z ea n dB o l d L G . F o n t=N e wS y s t e m . D r a w i n g . F o n t ( " T i m e sN e wR o m a n " ,1 1 . 0 F ,S y s t e m . D r a w i ' ~ ~ >A s s i g n i n gat i t l ef o rt h el e g e n d L G . T i t l e=" L e g e n d " ' ~ ~ >S e t t i n gt h el o c a t i o nf o rt h ec h a r t C h a r t 1 . S i z e=N e wS y s t e m . D r a w i n g . S i z e ( 7 8 0 ,3 5 0 ) E n dS u b E n dC l a s s
When you run the above code you will see the old chart now transforms into a much more meaningful chart.
Similarly we can create other charts like Bar Charts, Pie Charts, Line Charts, Area Charts etc. All you need to do is change the line in the code above. C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . C h a r t T y p e=S e r i e s C h a r t T y p e . C o l u m n
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/ 7/16
08/01/14
Few Examples
Line Chart
C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . C h a r t T y p e=S e r i e s C h a r t T y p e . L i n e
Pie Chart
C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . C h a r t T y p e=S e r i e s C h a r t T y p e . P i e
Funnel Chart
C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . C h a r t T y p e=S e r i e s C h a r t T y p e . F u n n e l
Radar Chart
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/ 8/16
08/01/14
C h a r t 1 . S e r i e s ( S e r i e s 1 . N a m e ) . C h a r t T y p e=S e r i e s C h a r t T y p e . R a d a r
Share this:
Facebook 13 Pinterest
Twitter Tumblr
Google Print
Digg
StumbleUpon
Fill/Retrieve data from PDF Form Fields using VB.Net From an Excel File
08/01/14
1 Trackback
1. Rafael Almeida04-06-2012 Thanks for this tutorial. It worked like a charm!!! Reply
2. seor fa04-15-2012 excelente busque muchas veces en internet pero un tuto como el tuyo es lo mejor para los que apenas comoenzamos con vb 2010 saludos u muchas gracias dios te bendiga Reply
4. Peter Williams06-15-2012 This is an excellent piece of code! This give you a template for developing further. Reply
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/
10/16
08/01/14
5. Arnaldo pinheiro07-02-2012 Thanks for this magnificent tutorial! For a newbie like me this kind of details helps a lot. Reply
6. Mukesh Chalodiya07-18-2012 this code is not display the chart . I used sql server database. there is no error but it wil not display chart.Please upload code with sql database. Thanks in advance. Reply
Siddharth Rout 07-18-2012 Mukesh, I would suggest asking such questions in a forums like MSDN/VBForums.com/Stackoverflow. You can then post the link here. If I can help you then I definitely will. If you want specific code to be developed for you then consider sending me an email with the exact requirements + Budget that we are looking at :) Reply
7. zafi09-23-2012 Hi Siddharth, I am trying to run the codes that you have implemented. It works without error! (Great!) However, I cant see the percentage appears correctly. It can be noted it appears as 4000% instead of 40 %. And I unable to to values and labels on Y But works fine on X. Can you advise me pertaining this matter please? I really appreciate your help. Thank you Reply
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/ 11/16
08/01/14
Siddharth Rout 09-23-2012 Where and how are the values stored? Reply
8. jeff 09-23-2012 Hi siddharth, A very informative information. I would like to get your advise. Is it possible to hide the legend from appears in the chart? Just would like to show the graph and title only. I hope to hearing from you soon. Reply
Siddharth Rout 09-23-2012 Hello Jeff You can remove the legends by using
C h a r t 1 . L e g e n d s . C l e a r ( )
Reply
9. jeff 09-23-2012 Hello Siddharth, Tq so much for prompt answers! :) I really appreciate it. Reply
10. akuya09-28-2012
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/ 12/16
08/01/14
Hi Your guide on codes are really helpful. Would like some opinion from you. I am using chart-pie. Like in your code you are using wheat back color. In visual studio 2010, there is an option to choose color like control color . Is that possible to that for this? The same color like a standard windows back color? I would really appreciate if you can guide me on this please? Thank you sir Reply
Siddharth Rout 09-28-2012 Did you try using the ColorDialog1? Reply
11. jeff 10-01-2012 Hi siddharth, I am using the codes that you provided. I would like to make a pie chart 1. show percentage outside the circle/pie chart with percentage 2. Location of pie chart and legend in parallel 3.color scheme of pie chart one color for example blue. but to make it different I can differentiate it using like dark blue, navy and blue (example) I have tried to edit the codes but I still unable to figure out ways to achieve my aims. I really appreciate if you as the author can help me. I really appreciate your help. Reply
Siddharth Rout 10-02-2012 Hello Jeff Sure I can help you out. But first I need to see your exact code. You may ask a question in any of the below forums and send me the link and I will help you in that thread if I can. http://stackoverflow.com http://www.vbforums.com
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/ 13/16
08/01/14
http://social.msdn.microsoft.com/Forums The reason why I request this is because there will be lot of exchange of views and this place is not apt for it as it will clutter the article :) To post in any of those forums you have to first register as a new user (if you already are not a user) and then ask your query. I will wait for your link. Reply
12. Venny09-21-2013 Thank you, this helped me very much. But chart titles typed in serif face look horrible. It annoys my eyes! Reply
13. mz11-25-2013 how can i create a graph which is by week, by month and by year? Reply
14. cristian12-13-2013 man, how do you make two axis y ? and also charge data in axis y Reply
Leave a Reply
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/
14/16
08/01/14
Visual Basic 2011/12, 2012/13, 2013/14 Recent Posts What is Ace OLEDB data provider Microsoft MVP : Re-Awarded Find Excels VBE Command Bar names and built-in controls IDs. VBA and VB.Net Unprotecting VBA Project Password (Using a password that you know) Excel Inputbox Automation from VB.NET Subscribe to Blog via Email Enter your email address to subscribe to this blog and receive notifications of new posts by email.
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/ 15/16
08/01/14
Archives
Select Month
Categories
Select Category
Jan
Copy Rows From Multiple T abs Data Validation Embed Excel in VB.Net Excel Excel Add-In Excel formula arguments Excel Formula Complete list
VB.Net And
Excel
VB.Net Create Table in Excel File VB.Net Creating Charts in Excel VB.Net Excel
Automation VB.Net Excel Charting VB.Net Format T able in Excel File VB.Net Formatting Charts in Excel VB.Net Formatting T ext in Excel
File VB.Net Insert Formulas in Excel File VB.Net Open Excel File VB.Net Write to Excel File Visual Studio 2 "." Rule Visual Studio 2 Dot Rule Visual Studio T wo "." Rule Visual Studio T wo Dot Rule
2012 Siddharth Rout. All rights reserved. | Microsoft Visual Studio logo & Excel logo are trademarks or registered trademarks of Microsoft Corporation in the US and/or other countries.
www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/
16/16