首页 文章

ASP.Net Chart X Axis显示误导性日期标签

提问于
浏览
1

我有一个直线柱形图,上面有4个系列,Y轴上的%和X轴上的日期时间 . 一切正常,但X轴标记和间隔标记不正确(好,误导) . 第一列的列是Januarys数据,但是图表将集群直接放在Feb标签的顶部 . 间隔线(正确的名称?)没有帮助,因为它通过Januarys数据集群运行,使得它看起来像col 1和2在Jan和col 3和4在2月 . 正在使用的数据从1月到7月,Aprils失踪(故意),但是图表看起来像2月到8月,5月失踪 .

Dodgy Chart

数据
Correct Data

所以我的问题是:如何将列的簇集中在它们自己的间隔部分中,并在其正下方的X轴上使用正确的月份标签?只要正确的月份显示在正确的数据下方,我甚至可以没有间隔线 .

我用Splines,Lines尝试了这个图表,没有任何格式,它们都有同样的问题 . 救命 . 这是星期五,我希望得到这个,所以我可以去酒吧 .

更新:根据要求 - 代码背后:

// set Y axix range
        Chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
        Chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 100;

        // show y line every x%
        Chart1.ChartAreas[0].AxisY.Interval = 10;

        // Set axis title
        Chart1.ChartAreas["ChartArea1"].AxisX.Title = "Period";
        Chart1.ChartAreas["ChartArea1"].AxisY.Title = "Percentage (%)";

        // set the x axis date format to short month and year
        Chart1.ChartAreas[0].AxisX.IsLabelAutoFit = false;
        Chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MMM \n yyyy";

        // The legend
        Chart1.Legends.Add("Default");
        Chart1.Legends["Default"].Docking = Docking.Bottom;
        Chart1.Legends["Default"].Alignment = StringAlignment.Center;

        // load the template for the basic styles
        Chart1.Serializer.IsResetWhenLoading = false;
        Chart1.LoadTemplate("SkyBlue.xml");

并标记:

<asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1"
        Width="600px">
        <Series>
            <asp:Series Name="thing1" XValueMember="Period"
                YValueMembers="thing1">
            </asp:Series>
            <asp:Series ChartArea="ChartArea1" Name="Team" XValueMember="Period"
                YValueMembers="thing2">
            </asp:Series>
            <asp:Series ChartArea="ChartArea1" Name="Systems" XValueMember="Period"
                YValueMembers="thing3">
            </asp:Series>
            <asp:Series ChartArea="ChartArea1" Name="Env" XValueMember="Period"
                YValueMembers="thing4">
            </asp:Series>
        </Series>
        <ChartAreas>
            <asp:ChartArea Name="ChartArea1">
            </asp:ChartArea>
        </ChartAreas>
    </asp:Chart>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:myConnString %>"
        SelectCommand="mySP"
        SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:Parameter DefaultValue="1" Name="ID" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

2 回答

  • 0

    图表将群集直接置于Feb标签的顶部 .

    不完全的!查看数据,然后仔细查看图表 . 第一行的日期为 31 Jan ,条形集群的正确位置恰好位于左侧(即之前) Feb . 类似地,第二个簇 - 来自 28 Feb 的数据仅位于 Mar 轴刻度线的左侧 .

    如何将列的簇集中在它们自己的间隔部分中,并在其正下方的X轴上使用正确的月份标签

    确保传递给图表的数据是您想要绘制的数据 . 如果您想在整个月内工作,请确保您的数据只有几个月 - 如果您有数据

    Date        Thing1  Thing2
    01-01-2012   100     200
    01-02-2012   200     200
    01-03-2012   300     300
    

    你的图表将完全按照你的意愿 .

    关键是要明白,只是说

    Chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MMM \n yyyy";
    

    不会将基础数据从“完整”日期更改,它只是更改轴的输出格式 .

  • 3

    将LabelStyle标记中的Interval属性设置为1,将解决您跳过标签的问题;它只会放置您指定的标签 .

    Chart1-> ChartAreas-> ChartArea1-> AxisX-> LabelStyle->间隔

    Source/More details

相关问题