并非所有数据点都显示在Microsoft图表中

时间:2014-08-02 14:50:59

标签: asp.net charts mschart

我的asp页面中有MS图表,但问题是并非所有数据点都显示在图表中,就像我期望看到5列字符,但我只看到4,所以我不确定为什么第5一个没有出现。总有一个数据点丢失。我试图增加图表的宽度,但似乎没有帮助,似乎图表区域很小。我不知道我在这里做错了什么,或者我如何显示该系列的所有数据点。谢谢。这是

   <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>

以下是代码:

 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();

        }

1 个答案:

答案 0 :(得分:1)

使用时

while (myReader.Read())

你读了第一行并移到下一行,所以DataBindXY()让读者获得第二行的当前位置

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

这就是为什么图表总是会跳过系列中的第一个数据项。

摆脱while (myReader.Read())以使您的代码看起来像

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