【发布时间】:2011-07-28 16:47:09
【问题描述】:
我有一个图表控件,除了一种情况外,它可以正常工作。
如果我选择 1 天的日期范围(2011 年 7 月 27 日至 2011 年 7 月 28 日)或任何更大的日期,我会看到趋势线,并可以相应地调整标签的布局和格式。
如果我选择一天(2011 年 7 月 27 日 - 2011 年 7 月 27 日),我会看到系列显示在图例中,并且 y 轴适当缩放,但没有显示趋势线。
每天有 144 个数据点,当我选择一天的数据时,如果我将图表中的数据导出到 XML 文件或在构建系列时放置一个消息框,我会看到 144 个数据点。如果我选择多天(包括适当的天数),我会看到相同的结果。
我每天每 10 分钟有一个数据点,如果我显示一个消息框,其中包含要添加到系列中的值,我会在查看单日范围时看到数据,所以我假设我应该看到趋势行。
我在这里错过了什么?
这是图表的代码:
<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>
这是我用来构建系列的代码:
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();
我没有在 .aspx 文件中设置间隔,但我查看了为图表提供的图表示例,并尝试使用以下函数设置间隔范围:
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;
}
天数范围为0天的情况如下调用:
SetAxisInterval(Chart1.ChartAreas["ChartArea1"].AxisX, 10, DateTimeIntervalType.Minutes);
【问题讨论】:
-
一条带一点的线实际上并不是一条线,这可能是您的问题。尝试为每个系列添加一个额外的虚拟点,作为测试。
-
请在您使用的 SQL 中编辑,而不是
"SQL QUERY HERE, IT WORKS CORRECTLY"。当您选择两天或更长时间时,它可能会正常工作,但能够查看您如何过滤数据可能有助于获得答案。 -
那么,当您选择多天时,您是每天获得 1 分还是每天获得 144 分(每 10 分钟)?
-
当我选择一天时,我看到了 144 个数据点.
-
这一行之后:
DateTime TimeStamp = (DateTime)reader["TimeStamp"];是显示正确解析日期和时间的TimeStamp变量吗?