无法在asp:chart上更改标签间隔

时间:2011-05-13 12:21:41

标签: charts controls labels

特别的问题是之前被问过的问题。我有一个asp:图表控件,它没有在X轴上显示我的所有标签。同样的问题: Why do labels disappear in ASP.NET charting?

然而,答案(X轴上的间隔= 1)对我的控制无效。而且,我想知道我是否有一些错误的设置,因为当我尝试更改它们时很多事情都没有做,例如LabelAutoFitStyle设置对我的图表没有任何影响。

这是我的代码:

<asp:Chart ID="SalesChart" Width="500" Height="400" ImageStorageMode="UseHttpHandler" EnableViewState="False" runat="server" ImageType="Png">
        <Series>
            <asp:Series ChartType="Column" Name="StatsSeries" XAxisType="Secondary" ToolTip="#VALX: #VALY{C0}" label="#VALY{C0}" LabelBorderColor="Gray" LabelBackColor="#FFF6AA">
                <Points>
                </Points>
            </asp:Series>
        </Series>
        <ChartAreas>
            <asp:ChartArea Area3DStyle-Enable3D="true" Name="StatsChartArea" Area3DStyle-PointGapDepth="0" Area3DStyle-Rotation="20" Area3DStyle-PointDepth="60" Area3DStyle-Inclination="20">
                <AxisX LabelAutoFitMinFontSize="10" Interval="1">
                    <MajorGrid Interval="1" />
                    <MinorGrid />
                    <LabelStyle Interval="1" />
                </AxisX>
                <AxisY>
                    <LabelStyle Format='{C0}' />
                    <MinorGrid Enabled="True" LineColor="Gray" />
                </AxisY>
            </asp:ChartArea>
        </ChartAreas>
    </asp:Chart>

代码背后(我在这里隐藏了许多不相关的代码):

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Here I get chart data


            //Set everything up
            if (statsDataTable.Rows.Count > 0)
            {
                SetChartData(statsDataTable);
                SetChartGridResolution();
                SetChartBarColors();
            }
        }
    }


    private void SetChartData(DataTable statsDataTable)
    {
        ArrayList xValues = new ArrayList();
        ArrayList yValues = new ArrayList();

        // I populate the lists with values, and then bind it below


        SalesChart.Series[0].Points.DataBindXY(xValues, yValues);
    }

    /// <summary>
    /// Set the intervals on the grid, based on the highest value.
    /// </summary>
    private void SetChartGridResolution()
    {
        if (SalesChart.Series[0].Points.FindMaxByValue().YValues[0] > 2000000)
        {
            SalesChart.ChartAreas[0].AxisY.LabelStyle.Interval = 1000000;
            SalesChart.ChartAreas[0].AxisY.MajorGrid.Interval = 1000000;
            SalesChart.ChartAreas[0].AxisY.MajorTickMark.Interval = 1000000;
            SalesChart.ChartAreas[0].AxisY.MinorGrid.Interval = 500000;

        }
        else if (SalesChart.Series[0].Points.FindMaxByValue().YValues[0] > 1000000)
        {
            SalesChart.ChartAreas[0].AxisY.LabelStyle.Interval = 200000;
            SalesChart.ChartAreas[0].AxisY.MajorGrid.Interval = 200000;
            SalesChart.ChartAreas[0].AxisY.MajorTickMark.Interval = 200000;
            SalesChart.ChartAreas[0].AxisY.MinorGrid.Interval = 100000;
        }
        else
        {
            SalesChart.ChartAreas[0].AxisY.LabelStyle.Interval = 100000;
            SalesChart.ChartAreas[0].AxisY.MajorGrid.Interval = 100000;
            SalesChart.ChartAreas[0].AxisY.MajorTickMark.Interval = 100000;
            SalesChart.ChartAreas[0].AxisY.MinorGrid.Interval = 50000;
        }
    }

    private void SetChartBarColors()
    {
        System.Drawing.Color color1 = System.Drawing.ColorTranslator.FromHtml("#007dc3");
        System.Drawing.Color color2 = System.Drawing.ColorTranslator.FromHtml("#539dc8");

        for (int i = 0; i < SalesChart.Series[0].Points.Count; i++)
        {
            if (i % 2 == 0)
                SalesChart.Series[0].Points[i].Color = color1;
            else
                SalesChart.Series[0].Points[i].Color = color2;
        }
    }

如果你能给我一个线索,我会很棒!

关心雅各布

1 个答案:

答案 0 :(得分:0)

好的,我解决了!这个问题花了我几个小时,试图解决各种问题。我希望有人可以通过阅读本书来幸免。

全部是因为XAxisType =“Secondary”,当选择此项时(使用上面的x行而不是下面的x行),必须将x轴上的所有配置设置为on而不是。这导致很多事情对我“不起作用”......