locked
How Show Odd values Axist Labels in MS Chart RRS feed

  • Question

  • User-1716253493 posted

    I Have use ms chart control in my web form

    xvalues are 1,2,3,4,5,6,7,8,9,10 (int)

    xaxist label display as 0,1,2,3,4,5,6,7,8,9,10,11

    I dont want to show 0 and 11 labels, but i have not get it yet

    To remove 0 and 11, i change the x value to string

    xvalues are '1','2','3','4','5','6','7','8','9','10' (varchar), now the chart don't show 0 and 11 anymore

    But now i have new issue. If x value more than 9 it show event labels only

    xaxist label display as 2,4,6,8,10 I want 1,2,3,4,5,6,7,8,9,10

    How to overcome this problem?

    Wednesday, November 21, 2018 12:45 AM

Answers

  • User283571144 posted

    Hi oned_gk,

    According to your description, I suggest you could try to set the AxisX.IntervalAxisX.Intervaloffset,AxisX.LabelStyle.IsStaggered property, then it will work well.

    More details, you could refer to below codes:

    ASPX:

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Chart ID="Chart1" runat="server">
                    <Series>
                        <asp:Series Name="Series1">
                            
                        </asp:Series>
                    </Series>
                    <ChartAreas>
                        <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
                    </ChartAreas>
                </asp:Chart>
            </div>
        </form>
    </body>
    

    Code-behind:

      public partial class mschart : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                DataTable dt = CreatData();
    
                #region
                             Chart1.DataSource = dt;
                             Chart1.Series["Series1"].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
                Chart1.Series[0].XValueMember = "Language";
                             Chart1.Series[0].YValueMembers = "Count";
                             Chart1.ChartAreas["ChartArea1"].AxisX.Title = "language";
                            
                             Chart1.ChartAreas["ChartArea1"].AxisX.TitleAlignment = StringAlignment.Far;
                             Chart1.ChartAreas["ChartArea1"].AxisY.Title = "count";
                             Chart1.ChartAreas["ChartArea1"].AxisY.TitleAlignment =StringAlignment.Far;//
                             Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
                Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;
                            
     Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;   //Set the interval of the AxisX as 1; 
                Chart1.ChartAreas["ChartArea1"].AxisX.IntervalOffset = 1;// Set the AxisX Intervaloffset as 1 
                Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.IsStaggered = true; //Set whether to interleave the display, such as when the data is too large, divide it into two lines to display.
                #endregion
    
            }
         
      DataTable CreatData()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Language", typeof(string ));
                dt.Columns.Add("Count", typeof(string));
    
                 string[] n = new string[] { "1", "2","3","4","5","6","7","8","9","10" };
                // int[] n = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10 };
                string[] c = new string[] { null, "2", null, "5",null,"7",null,null,null,null,};
    
                for (int i = 0; i < n.Length; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["Language"] = n[i];
                    dr["Count"] = c[i];
                    dt.Rows.Add(dr);
                }
                return dt;
            }
        }
    

    Result:


     

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 22, 2018 8:58 AM

All replies

  • User283571144 posted

    Hi oned_gk,

    According to your description, I suggest you could try to set the AxisX.IntervalAxisX.Intervaloffset,AxisX.LabelStyle.IsStaggered property, then it will work well.

    More details, you could refer to below codes:

    ASPX:

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Chart ID="Chart1" runat="server">
                    <Series>
                        <asp:Series Name="Series1">
                            
                        </asp:Series>
                    </Series>
                    <ChartAreas>
                        <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
                    </ChartAreas>
                </asp:Chart>
            </div>
        </form>
    </body>
    

    Code-behind:

      public partial class mschart : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                DataTable dt = CreatData();
    
                #region
                             Chart1.DataSource = dt;
                             Chart1.Series["Series1"].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
                Chart1.Series[0].XValueMember = "Language";
                             Chart1.Series[0].YValueMembers = "Count";
                             Chart1.ChartAreas["ChartArea1"].AxisX.Title = "language";
                            
                             Chart1.ChartAreas["ChartArea1"].AxisX.TitleAlignment = StringAlignment.Far;
                             Chart1.ChartAreas["ChartArea1"].AxisY.Title = "count";
                             Chart1.ChartAreas["ChartArea1"].AxisY.TitleAlignment =StringAlignment.Far;//
                             Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
                Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;
                            
     Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;   //Set the interval of the AxisX as 1; 
                Chart1.ChartAreas["ChartArea1"].AxisX.IntervalOffset = 1;// Set the AxisX Intervaloffset as 1 
                Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.IsStaggered = true; //Set whether to interleave the display, such as when the data is too large, divide it into two lines to display.
                #endregion
    
            }
         
      DataTable CreatData()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Language", typeof(string ));
                dt.Columns.Add("Count", typeof(string));
    
                 string[] n = new string[] { "1", "2","3","4","5","6","7","8","9","10" };
                // int[] n = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10 };
                string[] c = new string[] { null, "2", null, "5",null,"7",null,null,null,null,};
    
                for (int i = 0; i < n.Length; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["Language"] = n[i];
                    dr["Count"] = c[i];
                    dt.Rows.Add(dr);
                }
                return dt;
            }
        }
    

    Result:


     

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 22, 2018 8:58 AM
  • User-1716253493 posted

    Your solution have pointing me, thank you very much

    I have set this properties to fix

    <AxisX Title="No Tahapan" IntervalAutoMode="VariableCount">

    Friday, November 23, 2018 1:06 AM