Search code examples
asp.netchartsmschart

Not all Data Points Showing up in Microsoft Chart


I have MS chart in my asp page but the problem is that not all data points are showing up in the chart like I am expecting to see 5 column chars, but I am only seeing 4 so I am not sure why the 5th one is not showing up. There is always one data point is missing. I have tried to increase the width of the chart but that does not seem to help and it seems the chart area is small. I am not sure what am i doing wrong here or how can i show all data points for the series. Thanks. Here is the

   <table class="sampleTable">
                <tr>
                    <td class="tdchart">
                    <asp:chart id="Chart1" runat="server" BackColor="#D3DFF0" Palette="Chocolate" 
                            ImageType="Png" ImageUrl="~/TempImages/ChartPic_#SEQ(300,3)" Width="861px" 
                            Height="296px" borderlinestyle="Solid" backgradientendcolor="White" 
                            backgradienttype="TopBottom" borderlinewidth="2" 
                            borderlinecolor="26, 59, 105" BackGradientStyle="TopBottom">
                            <titles>
                                <asp:Title ShadowColor="32, 0, 0, 0" Font="Trebuchet MS, 14.25pt, style=Bold" ShadowOffset="3" Text="Work Effort Hours by Weekly" Alignment="TopCenter" ForeColor="Yellow"></asp:Title>
                            </titles>
                            <legends>
                                <asp:Legend Enabled="False" Name="Default" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold">
                                    <position y="21" height="22" width="18" x="73"></position>
                                </asp:Legend>
                            </legends>
                            <borderskin skinstyle="FrameTitle8"></borderskin>
                            <series>
                                <asp:Series Name="HOURS" BorderColor="180, 26, 59, 105" 
                                    IsValueShownAsLabel="True" BackGradientStyle="VerticalCenter"></asp:Series>
                            </series>
                                <chartareas>
                                <asp:ChartArea Name="Default" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid" BackSecondaryColor="White" BackColor="64, 165, 191, 228" ShadowColor="Transparent" BackGradientStyle="TopBottom">

                                    <axisy linecolor="64, 64, 64, 64" IsLabelAutoFit="False">
                                        <labelstyle font="Trebuchet MS, 8.25pt, style=Bold"></labelstyle>
                                        <majorgrid linecolor="64, 64, 64, 64"></majorgrid>
                                    </axisy>
                                    <axisx linecolor="64, 64, 64, 64" isLabelAutofit="False">
                                        <labelstyle font="Trebuchet MS, 8.25pt, style=Bold" Interval="Auto"></labelstyle>
                                        <majorgrid linecolor="64, 64, 64, 64"></majorgrid>
                                    </axisx>
                                </asp:ChartArea>
                            </chartareas>
                        </asp:chart></td>
                    <td valign="top">
                        <table class="controls" cellpadding="4">
                            <tr>
                                <td class="label48"></td>
                                <td>&nbsp;-&nbsp;<a href="javascript:window.history.back(1);">Back</a> -</td>
                            </tr>
                        </table>

Here is code behind:

 protected void Page_Load(object sender, System.EventArgs e)
        {
            string WeekBeginDate = "";

            if (this.Page.Request["WeekBeginDate"] != null)
            {
                WeekBeginDate = (string)this.Page.Request["WeekBeginDate"];
                Chart1.Titles[0].Text = WeekBeginDate;


            }
            // load the chart with values

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ConnectionString);

            string sqlStatement = "SELECT DT, HOURS FROM  myTable where UID = 'mike07' and WeekBeginDate = '06/30/2014' order by DT asc   ";

            SqlCommand cmd = new SqlCommand(sqlStatement, con);
            con.Open();
            SqlDataReader myReader = cmd.ExecuteReader();

            while (myReader.Read())

                Chart1.Series["HOURS"].Points.DataBindXY(myReader, "DT", myReader, "HOURS");

            // close the reader and the connection
            myReader.Close();
            con.Close();

        }

Solution

  • When you use

    while (myReader.Read())
    

    you read a first row and move to a next one, so the DataBindXY() gets the reader with the current position at the second row

    Chart1.Series["HOURS"].Points.DataBindXY(myReader, "DT", myReader, "HOURS");
    

    and that's why the chart will always skip the first data item from your series.

    Get rid of while (myReader.Read()) so that your code looks like

    SqlDataReader myReader = cmd.ExecuteReader();
    Chart1.Series["HOURS"].Points.DataBindXY(myReader, "DT", myReader, "HOURS");
    myReader.Close();