我有一个图表控件,除了一种情况外,它对所有情况都能正常工作。
如果我选择的日期范围为1天(7/27/2011到7/28/2011)或更大的日期范围,我会看到趋势线,并可以相应地调整标签的布局和格式。
如果我选择一天(7/27/2011 - 7/27/2011),我会看到该系列显示在图例中,y轴适当缩放,但没有显示趋势线。
每天有144个数据点,当我选择一天的数据时,如果我将数据从图表导出到XML文件或在构建系列时放置消息框,我会看到144个点。如果我选择包含适当天数的多天,我会看到相同的结果。
这里我漏掉了什么?
以下是图表的代码:
<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);发布于 2011-08-10 02:31:45
问题出在设置图表上X轴的最小值和最大值时。
我一直在设置未显示数据的开始日期和结束日期的最小值和最大值。
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();使用上面的代码强制将最大值设置为当前所选日期的后天午夜,解决了该问题。
https://stackoverflow.com/questions/6862585
复制相似问题