Skip to main content
 首页 » 编程设计

c#之如果 XValueType ="Time"和系列包含跨越多天的日期时间,则 ASP 图表格式不正确

2024年02月23日13Leo_wl

我必须遵循显示表示一段时间内数据的图表的代码。如果所有数据都来自同一天,则图表显示正确,X 轴刻度标记标签显示时间(以小时为单位)。如果数据跨越两天,图表会绕到开头并与其他点重叠。如果我设置 XValueType="DateTime"系列图正确但 X 轴刻度标记标签变成日期,甚至认为 IntervalType="Hours"。有什么方法可以让小时数显示在 X 轴上,而无需将数据换行到图表的开头?

<asp:Chart ID="Chart1" BackColor="Black" Width="400px" Height="125px" DataSource='<%# DataBinder.Eval(Container.DataItem, "Metrics") %>' 
                                runat="server"> 
                                <Series> 
                                    <asp:Series ChartType="Line" Name="Series1" YValueMembers="YValue" YValueType="Double" 
                                        XValueMember="Time" XValueType="Time"> 
                                    </asp:Series> 
                                </Series> 
                                <ChartAreas> 
                                    <asp:ChartArea Name="ChartArea1" BackColor="Black"> 
                                        <AxisY Interval="50" LineColor="White" IsStartedFromZero="true" Maximum="100"> 
                                            <MajorTickMark LineColor="White" Interval="10" /> 
                                            <LabelStyle ForeColor="White" /> 
                                        </AxisY> 
                                        <AxisX LineColor="White" IntervalType="Hours"> 
                                            <LabelStyle ForeColor="White" /> 
                                            <MajorTickMark LineColor="White" /> 
                                        </AxisX> 
                                    </asp:ChartArea> 
                                </ChartAreas> 
                            </asp:Chart> 

请您参考如下方法:

我通过设置 XValueType="DateTime"并将 x 轴上的标签格式化为显示时间而不是日期来解决我的问题。 x 轴的代码现在如下:

<AxisX LineColor="White" IntervalType="Hours"> 
      <LabelStyle ForeColor="White" Format="h:mm tt" /> 
      <MajorTickMark LineColor="White" /> 
</AxisX>