简体   繁体   中英

Top X-axis label not showing Windows Form chart

I am trying to draw a chart for an application I am developing for Windows Form. I am using the Chart Control, and customizing it programmatically.

I have learned a lot so far, since it is a .NET control and therefore there is plenty of documentation over the Internet on this issue.

However, something is putting me out since yesterday: I've enabled the two secondary labels for my 2D line chart (AxisX2 and AxisY2). The problem is that the label for these axes only show up for the second one (AxisY2), altough they are created and configured exactly the same way.

I've got a screenshot of my application so that you can see what I am talking about:

没有顶级标签的WinChart

The code for the creation of these two secondary axes is given bellow:

        var xAxisTop = new Axis
        {
            Minimum = 0,
            Maximum = 100,
            Interval = 5.0,
            LabelStyle = new LabelStyle { ForeColor = Color.Black, Enabled = true, Font = new Font("Verdana", 18.0f) },
            Title = "Pos (%)",
            LineWidth = 2,
            MajorGrid = new Grid { LineColor = Color.DimGray },
            MinorGrid = new Grid { LineColor = Color.DimGray },
            IsMarksNextToAxis = true,
            Enabled = AxisEnabled.True
        };
        chartArea1.AxisX2 = xAxisTop;

        var yAxisTop = new Axis
        {
            Minimum = 0,
            Maximum = 100,
            Interval = 5.0,
            LabelStyle = new LabelStyle { ForeColor = Color.Black, Enabled = true, Font = new Font("Verdana", 18.0f) },
            Title = "Load (%)",
            LineWidth = 2,
            MajorGrid = new Grid { LineColor = Color.DimGray },
            MinorGrid = new Grid { LineColor = Color.DimGray },
            IsMarksNextToAxis = true,
            Enabled = AxisEnabled.True
        };
        chartArea1.AxisY2 = yAxisTop;

I literally have no idea what is going one. Can someone help me?

You don't show the code for adding the DataPoints but my crystal ball says: Most likely your Axes both are perfectly ok and your problem stems from your X-Values .

Your Y-Values are, as usual, numeric but if your X-Values are not, they still will seem to be OK but they are not! : The Labels do show just fine as they are created from the strings you feed in, but the X-Values actually are all 0 s. Check it in the debugger!!

This is a common mistake and it only shows when you are trying to actually use those X-Values , for example by using a numeric formatting string or do a calculation or, as you are doing, try to use them implictly, by setting a Minimum and Maximum for your secondary X-Axis .

As all values are 0 there is no room between Minimum and Maximum at all and nothing is showing..

Check on the code that adds the DataPoints and make sure it adds numbers, not strings for the X-Values as well!

See here for a similar case and its discussion and solution! Scroll to the Update! (( SO still doesn't allow anchors in posts :-( ))

Update: I just found one more reason why the labels on a secondary axis don't show: They only show if you also show the labels on the primary axis. If you don't want that you must not disable them! Instead you can set their LabelStyle.ForeColor = Color.Transparent !

Obviously this is not your case, as the primary axis does show its labels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM