显示图表控件 Xaxis 中的所有值



我有一堆产品的图表,总共35种。它们放大了 X 轴。图表绘制得很好,但只显示了 5 个产品名称,我需要它们全部显示。我已将 MinorTickMark 启用为 true,以便显示所有刻度线,但我如何让它们各自的标签可见?

我无法发布图像,所以这里是 aspx 标记和背后的代码。.aspx标记;

<asp:Chart ID="MonthinYearchart" Width="350px" Height="420px" runat="server">
            <Series> 
            <asp:Series  ChartType="Bar"  ChartArea="MainChartArea" Name="PnL"> 
            </asp:Series> 
            </Series> 
            <ChartAreas> 
                 <asp:ChartArea Name="MainChartArea"> 
                 </asp:ChartArea> 
            </ChartAreas> 
        </asp:Chart>

下面是将示例数据放入图表中的代码。

Private Sub AllCommodforMonthChart()
    Dim cht As Chart = MonthinYearchart
    'create the arraylist of data
    'this is hardcoded to get chart to work, you will have to
    'set up the code to retrieve it from database
    Dim list As List(Of String) = GetList("Futures Data")
    Const val As Integer = 65
    'create all the data points
    For i As Integer = 0 To list.Count - 1
        cht.Series("PnL").Points.AddXY(list(i), val * i)
    Next
    cht.Series("PnL").ChartType = SeriesChartType.Bar
    cht.ChartAreas("MainChartArea").AxisX.MinorTickMark.Enabled = True
End Sub

图表控件非常有限,如果要自定义它,最好通过生成图像来创建自己的图表:

见链接

答案就在 Axis LabelStyles 中。下面的代码将格式化轴(X 或 Y),以便显示所有次要刻度线,间隔为 1,并且将显示每个刻度线的所有标签。

 cht.ChartAreas("MainChartArea").AxisX.MinorTickMark.Enabled = True
cht.ChartAreas("MainChartArea").AxisX.Interval = 1
cht.ChartAreas("MainChartArea").AxisX.IsLabelAutoFit = True
'cht.ChartAreas("MainChartArea").AxisX.LabelStyle.IsStaggered = True
cht.ChartAreas("MainChartArea").AxisX.LabelAutoFitStyle =  LabelAutoFitStyles.DecreaseFont

注意:如果您希望标签交错,请取消注释掉最后一行旁边的

注释

最新更新