We help IT Professionals succeed at work.

ASP.NET Chart Control

evanburen
evanburen asked
on
I am trying to create a simple bar chart but the results are not what I am expecting.

My SQL Code is

 SELECT CallCenter, COUNT(ReviewID) AS CallCount
                 
        FROM  
                CallCenters INNER JOIN
                      Calls ON CallCenters.CallCenterID = Calls.CallCenterID
WHERE  
        DateofReview >= '10/01/2011' AND DateofReview <= '10/31/2011'
GROUP BY
        CallCenter

the results in SQL Server are here which is what I want:

 searchresults.PNG

but my chart looks like this and I don't understand why I don't have a separate bar and label for each call center

 chart.PNG

My chart code

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Using myConnection As New SqlConnection
            myConnection.ConnectionString = ConfigurationManager.ConnectionStrings("CCMConnectionString").ConnectionString
 
            Dim myCommand As New SqlCommand
            myCommand.Connection = myConnection
            myCommand.CommandText = "SELECT CallCenter, COUNT(ReviewID) AS CallCount FROM CallCenters INNER JOIN Calls ON CallCenters.CallCenterID = Calls.CallCenterID"
            myCommand.CommandText += " WHERE DateofReview >= '10/01/2011' AND DateofReview <= '10/31/2011' Group BY CallCenter ORDER BY CallCenter DESC"
 
            myConnection.Open()
            Dim myReader As SqlDataReader = myCommand.ExecuteReader()
 
            'Bind the data using the DataBindTable method  
            chtCallCount.Series("CallCenters").Points.DataBindXY(myReader, "CallCenter", myReader, "CallCount")
                     
            myReader.Close()
            myConnection.Close()
             
        End Using
    End Sub
 
 <asp:Chart ID="chtCallCount" runat="server">  
   <Series>  
      <asp:Series Name="CallCenters" ChartType="Bar" Palette="Chocolate" ChartArea="MainChartArea"></asp:Series>  
   </Series>  
     
   <ChartAreas>  
      <asp:ChartArea Name="MainChartArea">  
      </asp:ChartArea>  
   </ChartAreas>  
</asp:Chart>  .
Comment
Watch Question

Commented:
just to check,  instead of DataReader try with DataView..bcoz i faced a same pbm once..and used DataView..it worked fine..

Commented:
also

1.  make sure your DataReader Returns the correct Data from the Query...though it seems to work in SQL Query Analyzer. To check this, u may have a GridView for testing..and assign its datasource to ur datareader.

2. ur  SQL Command Text doesnt have a space at the beginning of  ' WHERE ' statement

3. try with "BETWEEN' instead of using >= and <=.


Author

Commented:
it definitely has something to do with the X axis label style code in the chart control itself because if I use the same datasource to create a pie chart it looks fine. Also, if I change the CallCenter to values to CallCenterID (a small int value) then all of the labels fit in the chart.  I tried formatting the chart a dozen different ways and nothing seems to work.

Commented:
As I remember for bar chart, you need to specify which column to group with.

This can be done from the chart properties
Commented:
You can also try
chtCallCount.ChartAreas["MainChartArea"].AxisY.LabelStyle.Interval = 1;

check link