Search code examples
c#webformsmschart

MS Pie Chart with both X,Y Value on graph using asp.net web forms


I am working on asp.net web application in which i need to show the data in a pie chart. Graph should show both the X Value and Y Value. In below image you can see i am able to display only total on comments in a year but i also want to show the Year next to each color. I tried different thing but so far i have no success.

I am also post code related to Chart

<asp:Chart ID="crtYearWise" runat="server" Height="260px" Width="460px">
    <Titles>
        <asp:Title Name="Title1" Text="Year Wise"
            Alignment="TopCenter" Font="Verdana, 12pt">
        </asp:Title>
    </Titles>
    <Series>
        <asp:Series Name="Series1" CustomProperties="DrawingStyle=Pie,
            PieDrawingStyle=Concave, MaxPixelPointWidth=20" ShadowOffset="1"
            ChartType="Pie" IsValueShownAsLabel="True" >
        </asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1" Area3DStyle-Enable3D="True" AlignmentOrientation="All">
            <Area3DStyle Enable3D="true" />
        </asp:ChartArea>
    </ChartAreas>
</asp:Chart>  

Code Behind

String strSql2 = "SELECT Count(comPostDate) AS 'comTOTAL',  DATEPART(yyyy, comPostDate) AS 'whichYear' ";
strSql2 += " FROM Comments  Group by  DATEPART(yyyy, comPostDate), DATEPART(yyyy, comPostDate) ";
DataSet ds2 = DataProvider.Connect_Select(strSql2);

DataTable dt2 = ds2.Tables[0];
crtYearWise.DataSource = dt2;
crtYearWise.Series["Series1"].XValueMember = "whichYear";
crtYearWise.Series["Series1"].YValueMembers = "comTOTAL";
//crtYearWise.Series["Series1"]["PieLabelStyle"] = "Outside";
crtYearWise.DataBind();

Data and Chart as Image

I would appreciate if some can guide me in this.

Answer: Finally Managed got it with

crtYearWise.Series["Series1"].Label = "Year : #VALX , #VALY";


Solution

  • This blog post has some information that will help.

    For example, you can do this by setting:

    <asp:Series Label="#VALX: #VALY" ...
    

    You can also format the X and Y values, e.g.:

    <asp:Series Label="#VALX: #VALY{N0}"
    

    I must say the MSDN documentation on the Chart control is poor; for example the Chart overview doesn't have any links to "how-to" topics for common tasks like this, and the best source of info seems to be MSDN blog posts.