开发者

ASP.NET Chart not showing line with one day of data, data is present in the series

开发者 https://www.devze.com 2023-03-23 09:39 出处:网络
I have a chart control that works normally for all but one case. If I select a date range of 1 day, (7/27/2011 to 7/28/2011) or anything greater I see the trend line and can adjust the layout and for

I have a chart control that works normally for all but one case.

If I select a date range of 1 day, (7/27/2011 to 7/28/2011) or anything greater I see the trend line and can adjust the layout and formatting of the labels accordingly.

If I select one day (7/27/2011 - 7/27/2011) I see the series show up in the legend and the y-axis scales appropriately but no trend line is shown.

Each day has 144 datapoints and when I select one day of data I see 144 points come across if I export data from the chart into an XML file or put up a message box when I am building my series. I see the same results if I select multiple days, with the appropriate amount of days included.

I have datapoints every 10 minutes during each day and if I display a messagebox with the values that are being added to the series I see data when I am looking at the single day range so I assume that I should be seeing a trend line.

What am I missing here?

Here is the code for the chart:

<asp:chart id="Chart1" runat="server" Height="365px" 
      Width="700px" ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)" ImageType="Png" 
      BackColor="WhiteSmoke" BackSecondaryColor="White" BackGradientStyle="TopBottom" 
      palette="BrightPastel" BorderWidth="2" 
      BorderColor="26, 59, 105">
          <titles>
              <asp:Title ShadowColor="32, 0, 0, 0" Font="Trebuchet MS, 14.25pt, style=Bold" ShadowOffset="3" Text="Chart Title" Alignment="MiddleLeft" ForeColor="26, 59, 105"></asp:Title>
          </titles>
      <legends>
          <asp:Legend Enabled="True" IsTextAutoFit="False" Name="Default" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold"></asp:Legend>
          </legends>
      <borderskin skinstyle="Emboss"></borderskin>
          <chartareas>
              <asp:ChartArea Name="ChartArea1" BorderColor="64,                                64, 64, 64" BorderDashStyle="Solid" BackSecondaryColor="White" BackColor="Gainsboro" ShadowColor="Transparent" BackGradientStyle="TopBottom">
               <axisy2 enabled="False"></axisy2>
               <axisx2 enabled="False"></axisx2>
                   <area3dstyle Rotation="10" perspective="10"   Inclination="15" IsRightAngleAxes="False" wallwidth="0" IsClustered="False"></area3dstyle>
                   <axisy linecolor="64, 64, 64, 64" IsLabelAutoFit="False" ArrowStyle="Triangle">
                   <labelstyle font="Trebuchet MS, 8.25pt, style=Bold" />
           <majorgrid linecolor="64, 64, 64, 64" />
           </axisy>
           <axisx linecolor="64, 64, 64, 64" IsLabelAutoFit="False" ArrowStyle="Triangle">
                   <labelstyle font="Trebuchet MS, 8.25pt, style=Bold" IsStaggered="False"/>
               <majorgrid linecolor="64, 64, 64, 64" />
        </axisx>
            </asp:ChartArea>
        </chartareas>
    </asp:chart>

And here is the code I am using to build the series:

foreach (TreeNode node in ChartTreeView.CheckedNodes)
{
    if(node.Text.Contains("XXX") )
    {
        Series series = Chart1.Series.Add(node.Text);
        series.ChartArea = "ChartArea1";
        series.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), charts[1], true);
        SqlConnection sqlConnection = new SqlConnection(@"DATA GOES HERE FOR SQL");
        sqlConnection.Open();

            SqlCommand nodeQuery = new SqlCommand();
            nodeQuery.CommandText = "";
            if (node.Text.Contains("XXX"))
            {
                nodeQuery.CommandText = "SELECT (Date + CONVERT(datetime,Time)) As TimeStamp, (MW + kW) As Energy, [EquipmentID] FROM EquipmentData WHERE [EquipmentID] = '" + node.Text + "' AND (Date + CONVERT(datetime,Time)) BETWEEN '" + startDateFilter + "' AND '" + endDateFilter + "' and [SiteID] = "+ProjectNavigation.SelectedValue.ToString()+" ORDER BY TimeStamp";";
                nodeQuery.Connection = sqlConnection;

            }
            if (nodeQuery.CommandText != "")
            {
                SqlDataReader reader = nodeQuery.ExecuteReader();
                while (reader.Read())
                {
                    double value = (double)reader["Energy"];
                    DateTime TimeStamp = (DateTime)reader["TimeStamp"];
                    string equipID = (string)reader["EquipmentID"];

                    series.Points.AddXY(TimeStamp, value);

                }
                sqlConnection.Close();
            }
        }
    }
    Chart1.DataBind();

I don't have an interval set in the .aspx file, but I looked through the chart samples provided for the charts and have tried using the following function to set the interval ranges:

public void SetAxisInterval(System.Web.UI.DataVisualization.Charting.Axis axis, double interval, DateTimeIntervalType intervalType)
    {
        SetAxisInterval(axis, interval, intervalType, 0, DateTimeIntervalType.Auto);
    }

    public void SetAxisInterval(System.Web.UI.DataVisualization.Charting.Axis axis, double interval, DateTimeIntervalType intervalType, double intervalOffset, DateTimeIntervalType intervalOffsetType)
    {
        // Set interval-related properties
        axis.Interval = interval;
        axis.IntervalType = intervalType;
        axis.IntervalOffset = intervalOffset;
        axis.IntervalOffsetType = intervalOffsetType;

    }

Calling it with the following for the case where the range of days is 0 days:

SetAxisInterval(Chart1.ChartAreas["Char开发者_Go百科tArea1"].AxisX, 10, DateTimeIntervalType.Minutes);


The problem was in setting the minimum and maximum values for the X-axis on the chart.

I had been setting the minimum and maximum to the start and end date which was not displaying data.

DateTime startDateVal;
DateTime endDateVal;

TimeSpan span = DateTime.Parse(endDate.Text) - DateTime.Parse(startDate.Text);
if (span.TotalDays > 0)
{
    startDateVal = DateTime.Parse(startDate.Text);
    endDateVal = DateTime.Parse(endDate.Text;
}
else
{
    startDateVal = DateTime.Parse(startDate.Text);
    endDateVal = DateTime.Parse(endDate.Text).AddDays(1);
}

Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = startDateVal.ToOADate();
Char1t.ChartAreas["ChartArea1"].AxisX.Maximum = endDateVal.ToOADate();

Using the code above to force the maximum to be midnight the day after the currently selected day fixed the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号