Search code examples
vb.netmschart

showing all values in chart control Xaxis


I have a chart of a bunch of products, 35 in all. They scale up the X Axis. The chart plots fine but only 5 of the product names show and I need them all to show. I have enabled MinorTickMark to true so all the tick marks show but how do i get their respective label to be visible?

I couldn't get the image to post so here is the aspx markup and the code behind. .aspx markup;

<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>

Here is the code behind to put sample data in the 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

Solution

  • The answer lies in the Axis LabelStyles. The code below will format the axis (X or Y) so that all minor tick marks show, the interval is one and all the labels for each tickmark will show.

     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
    

    Note: If you want the labels staggered then uncomment out the next to last line