Subscribe to andrewsw's Blog        RSS Feed

Exploring .NET Chart, a Pie Chart

Icon 1 Comments
I have only occasionally played with the WinForms Chart Class, mainly in response to a forum question. I decided to spend a little time exploring. Rather than the default column chart, I ventured with a pie chart (and VB.NET).

I just have a WinForm with an added Chart control, named chtLanguages. All the code is in the Form's Load event.

I created a DataTable, which is the most likely option. I would also (most often) use a BindingSource. The following link is excellent, and details the wide variety of data sources that can be used:

Data Binding Microsoft Chart Control

Imports System.Windows.Forms.DataVisualization.Charting

Public Class frmPie
    'Data Binding Microsoft Chart Control

    Private Sub frmPie_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim dtLanguages As New DataTable

        dtLanguages.Columns.Add("Language", System.Type.GetType("System.String"))
        dtLanguages.Columns.Add("Ratings", System.Type.GetType("System.Double"))

        'TIOBE Index for February 2016
        dtLanguages.Rows.Add(New Object() {"Java", 0.2115})
        dtLanguages.Rows.Add(New Object() {"C", 0.1559})
        dtLanguages.Rows.Add(New Object() {"C++", 0.0691})
        dtLanguages.Rows.Add(New Object() {"C#", 0.044})
        dtLanguages.Rows.Add(New Object() {"Python", 0.0418})
        dtLanguages.Rows.Add(New Object() {"PHP", 0.0277})
        dtLanguages.Rows.Add(New Object() {"VB.NET", 0.0245})
        dtLanguages.Rows.Add(New Object() {"Others", 0.4255})


        chtLanguages.Series("Langs").ChartType = DataVisualization.Charting.SeriesChartType.Pie

        chtLanguages.Palette = DataVisualization.Charting.ChartColorPalette.Pastel
        'Setting Microsoft Chart Series Colors

        chtLanguages.DataSource = dtLanguages
        chtLanguages.Series("Langs").XValueMember = "0"
        chtLanguages.Series("Langs").YValueMembers = "1"

Posted Image

        Dim theTitle As New Title("TIOBE Index for February 2016", _
                                  Docking.Top, New Font("Verdana", 12, FontStyle.Bold), _

Posted Image

        chtLanguages.Legends(0).Docking = Docking.Bottom
        'chtLanguages.Legends(0).LegendStyle = LegendStyle.Row

        chtLanguages.Series("Langs").IsValueShownAsLabel = True
        chtLanguages.Series("Langs").LabelFormat = "P2"     'percent, 2 decimals

Posted Image

Without creating custom labels I do not think there is an easy way to spread the labels further out. Their font could be reduced but that would be too small. I decided to, instead, push the labels outside the pie.

        chtLanguages.Series("Langs")("PieLabelStyle") = "Outside"   'custom property
        chtLanguages.Series("Langs")("PieLineColor") = "Black"

Posted Image

Notice that some features are tucked away as custom properties.

Alternatively, perhaps I could explode just the small slices:

        chtLanguages.DataBind()     'necessary before explode
        For pt As Integer = 2 To chtLanguages.Series("Langs").Points.Count - 2
            chtLanguages.Series("Langs").Points(pt)("Exploded") = True

Posted Image

(I should perform the DataBind() earlier in the code, now that I realise that it is necessary for some features to work.)

The explode-option isn't perfect, but I wasn't interested in pursuing custom labels at this point.

Anyway, this is as far as my experimenting took me. These charts are powerful, although not as easy as Excel charts, and not as well documented. If it helps you to explore further, I was using the search phrase ".net chart move legend"; i.e. ".net chart do something". This is my attempt to reduce the likelihood of discovering Excel and other charts instead of the Chart Class that I'm looking for.

1 Comments On This Entry

Page 1 of 1


07 February 2016 - 03:51 AM
This morning I was inspired with the perfect solution (or compromise..) which is to only take a label outside if its value is less than 10%.

Posted Image

        For pt As Integer = 0 To chtLanguages.Series("Langs").Points.Count - 1
            If chtLanguages.Series("Langs").Points(pt).YValues(0) < 0.1 Then
                chtLanguages.Series("Langs").Points(pt)("PieLabelStyle") = "Outside"
                chtLanguages.Series("Langs").Points(pt)("PieLineColor") = "Black"
            End If

I guessed that some custom properties could apply to individual points, rather than just the Series as a whole.
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

November 2020

2223 24 25262728


    Recent Entries

    Recent Comments

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)