Search code examples
c#.netvb.netchartsasp.net-charts

Hide data point on x axis at location "0" in win chart controls


I am plotting graph as shown in the image below. On x axis i dont have location "0", can i hide that point. The values on x axis vary from -35 to 35 where 0 is not present. The x axis values are just numbers.

enter image description here


Solution

  • You can't freely change the text of the axis labels nor skip any of them nor make them transparent selectively.

    But there are two options:

    • You can replace the axis labels by CustomLabels. This means you need to create and add them all, each with two numbers which give a FromPosition and a ToPosition, i.e. a range inside of which the CustomLabel shall be centered.

    Often you can cheat and use an actual x-value and simply subtract / add a small value; not too small so that the label still fits in.

    If you have explicitly set the axis Interval (as you seem to have done) 1/2 or 1/3 of that would be good offsets; or if you also have set Minumum and Maximum you can calculate the range properly in loop, see below!

    • But in your case the solution is much simpler: you can format the axis label like this:

    Axis ax = yourChartArea.AxisX;
    ax.AxisX.LabelStyle.Format = "#";
    

    Of course this only work because the value you want to hide is 0, which the number format can suppress.


    The first option, of course is much more powerful. So let's look at an example for this option as well:

    double delta = ax.Interval / 3d;
    for (double x = ax.Minimum; x <= ax.Maximum; x+=ax.Interval)
    {
        CustomLabel cl = new CustomLabel();
        cl.ToPosition = x + delta;
        cl.FromPosition = x - delta;
        cl.Text =  x.ToString();  // pick your text/format!
        if (x != 0) ax.CustomLabels.Add(cl);
    }
    

    The result looks the same for both methods:

    enter image description here