Sunday 8 May 2011

Creating Bar Chart using C#

Creating Bar Chart in windows using C#
Introduction-
For making bar chart we will make use of ‘DrawRectangle’ in built function. because we have to first create a big rectangle defining the boundary of bar graphs. Parameters of DrawRectangle is :

1. Pen – This defines the color and style of border
2. Rectangle – Rectangle object to be created

Rectangle(int x, int y, int width,int height)

X and y are the co-ordinates of top left corner of rectangle. Width and height are width and height of rectangle.

Height = (weight of current array element *height of outer rectangle )/ maximum weight.
X coordinate is incremented by width of bars everytime a new bar is created.

Y co-ordinate is calculated by the following formula:
y = y coordinate of outer rectangle + height of outer rectangle – height of bar 
Code for DrawBarChart

private void DrawBarChart(PaintEventArgs e, int[] alWeight)
        {
            int numberOfSections = alWeight.Length;
            int lengthArea = 330;
            int heightArea = 280;
            int topX = 20;
            int topY = 20;
            int maxWeight = MaxValue(alWeight);
            int[] height = new int[numberOfSections];
            int total = SumOfArray(alWeight);
            Random rnd = new Random();
            SolidBrush brush = new SolidBrush(Color.Aquamarine);
            Pen pen = new Pen(Color.Gray);
            Rectangle rec = new Rectangle(topX, topY, lengthArea, heightArea);
            e.Graphics.DrawRectangle(pen, rec);
            pen.Color = Color.Black;
            int smallX = topX;
            int smallY = 0;
            int smallLength = (lengthArea / alWeight.Length);
            int smallHeight = 0;
            for (int i = 0; i < numberOfSections; i++)
            {
                brush.Color = Color.FromArgb(rnd.Next(200, 255),
           rnd.Next(255), rnd.Next(255), rnd.Next(255));
                smallHeight = ((alWeight[i] * heightArea) / maxWeight);
                smallY = topY + heightArea - smallHeight;
                Rectangle rectangle = new Rectangle
           (smallX, smallY, smallLength, smallHeight);
                e.Graphics.DrawRectangle(pen, rectangle);
                e.Graphics.FillRectangle(brush, rectangle);
                brush.Color = Color.FromKnownColor(KnownColor.Black);
                e.Graphics.DrawRectangle(pen, rectangle);
                smallX = smallX + smallLength;
            }
        }

37 comments:

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in C SHARP, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on C SHARP. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Nitesh Kumar
    MaxMunus
    E-mail: nitesh@maxmunus.com
    Skype id: nitesh_maxmunus
    Ph:(+91) 8553912023
    http://www.maxmunus.com/




    ReplyDelete
  2. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.

    Data Science Training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune
    Data Science training in kalyan nagar
    Data Science training in OMR
    selenium training in chennai

    ReplyDelete
  3. Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.
    java training in marathahalli | java training in btm layout

    java training in jayanagar | java training in electronic city

    ReplyDelete
  4. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
    python training in rajajinagar
    Python training in btm
    Python training in usa
    Python training in marathahalli
    Python training in pune

    ReplyDelete
  5. The site was so nice, I found out about a lot of great things. I like the way you make your blog posts. Keep up the good work and may you gain success in the long run.
    python online training
    python training in OMR

    ReplyDelete
  6. Really nice experience you have. Thank you for sharing. It will surely be an experience to someone.
    Blueprism online training

    Blue Prism Training in Pune

    ReplyDelete
  7. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    AWS Interview Questions And Answers

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    AWS Training in Pune | Best Amazon Web Services Training in Pune

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    AWS Online Training | Online AWS Certification Course - Gangboard

    ReplyDelete
  8. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    AWS Interview Questions And Answers

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    AWS Training in Pune | Best Amazon Web Services Training in Pune

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    AWS Online Training | Online AWS Certification Course - Gangboard

    ReplyDelete
  9. can you offer guest writers to write content for you? I wouldn’t mind producing a post or elaborating on some the subjects you write concerning here. Again, awesome weblog!
    nebosh igc courses in chennai

    ReplyDelete
  10. indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
    SOFTWARE TRAINING IN CHENNAI
    POWERBI TRAINING IN CHENNAI
    CCNA TRAINING IN CHENNAI
    ANDROID TRAINING IN CHENNAI

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.
    MCSE Training in chennai | mcse training class chennai


    ReplyDelete
  13. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!

    Digital marketing course mumbai

    ReplyDelete
  14. An intriguing discussion is worth comment. There's no doubt that that you should write more on this topic, it may not be a taboo matter but typically folks don't speak about such subjects. To the next! dora Best wishes!!

    ReplyDelete
  15. It was a very wonderful post!!! Thank you for your great work sharing with us. I am very glad to read your blog and I want more updates about this topic...
    AWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

    ReplyDelete
  16. Really great post, I simply unearthed your site and needed to say that I have truly appreciated perusing your blog entries. I want to say thanks for great sharing.thanska
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  17. It is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog. Please, continue to give me such valuable postsData Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete
  18. Thank you for the informative post. It was thoroughly helpful to me. Keep posting more such articles and enlighten us.

    AI Training in Hyderabad

    ReplyDelete
  19. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.

    DevOps Training in Hyderabad

    ReplyDelete
  20. This comment has been removed by the author.

    ReplyDelete
  21. It's difficult to find experienced individuals about this subject, yet you seem like you hear what you're saying! Much obliged
    tech news

    ReplyDelete
  22. Generally excellent review. I absolutely love this site. Much appreciated!
    best interiors

    ReplyDelete
  23. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one.
    data analytics course in hyderabad

    ReplyDelete
  24. I truly like your style of blogging. I added it to my preferred's blog webpage list and will return soon…
    data analytics course in hyderabad

    ReplyDelete
  25. Microsoft Office 2019 Crack is the sophisticated software to execute various files. It is created to carry out different workplace and company tasks . Cracker Office 2019

    ReplyDelete
  26. Microsoft Office 2010 Free Download Full Version For Windows 7/8/10.It Is Full Offline/Standalone Installer With The Official Product Key.MS Office 2010 Free Download With Crack

    ReplyDelete
  27. Kindly Visit my website for cracked software, Windows & Mac. Download Now Latest Versions.
    Windows 7 Ultimate Crack

    ReplyDelete