Wrapping axis title


https://www.nevron.com/Forum/Topic7922.aspx
Print Topic | Close Window

By Alex W - 11 Years Ago
Hi,

My Y-Axis title is quite long.
Is there any way to wrap axis title (make it multiline)?

Thanks.
By Nevron Support - 11 Years Ago
Hi Alex,

The title cannot be wrapped automatically, but it is possible to add new lines in the title string - they will be regarded. If you use XML formatted text, you can insert a <br /> tag to split the line.
By Alex W - 11 Years Ago
Thank you for your answer. I'll try to use XML formatted text.
However, automatic axis title wrapping when resizing a chart is a nice to have feature.
By Alex W - 11 Years Ago
1. How can I measure the width of XML-formatted string and the height of Y-Axis?
2. I want to wrap the title each time when new series is added or the chart is resized. Is there any event that fires after such actions have occurred?
By Nevron Support - 11 Years Ago
Hi Alex,

This can be achieved using custom range labels. The following code snippet shows how to create a custom range label that automatically wraps when then length of the text exceeds the length of the x axis:

         NChart chart = nChartControl1.Charts[0];

         NAxis axisX = chart.Axis(StandardAxis.PrimaryX);
         NStandardScaleConfigurator scaleX = (NStandardScaleConfigurator)axisX.ScaleConfigurator;

         // make modifications to the configurator here...

         axisX.UpdateScale();

         // create label
         NRangeScaleLabelStyle style = new NRangeScaleLabelStyle();
         style.WrapText = true;
         style.TextStyle.TextFormat = TextFormat.XML;
         style.TickMode = RangeLabelTickMode.None;

         NRulerRangeDecorationAnchor anchor = new NRulerRangeDecorationAnchor(Nevron.HorzAlign.Left, new NLength(0), Nevron.HorzAlign.Right, new NLength(0));
         NRangeScaleLabel label = new NRangeScaleLabel(anchor, "Some Title Text with <b>XML</b> formatting", style);

         // create decorator
         NCustomScaleDecorator decorator = new NCustomScaleDecorator();
         decorator.Decorations.Add(label);
         
         // create a new scale level
         NScaleLevel scaleLevel = new NScaleLevel();
         scaleLevel.Decorators.Add(decorator);
         axisX.Scale.Levels.Add(scaleLevel);

Hope this helps - let us know if you meet any problems.
By Ivan Gorenko - 10 Years Ago
Hi!

Your sample working perfect, thanks. But is it possible to do same for vertical axis? I mean label is displaying horizontally. So, is there a way to change this label orientation from horizontal to vertical?

Thanks!
By Nevron Support - 10 Years Ago

Hi,
Yes you can use the same code to apply an axis label to the vertical axis. If you want to change the orientation you can use the label style angle:

label.Style.Angle = new NScaleLabelAngle(ScaleLabelAngleMode.Scale, 90);

For more information you can take a look at the following topic:
http://helpdotnetvision.nevron.com/UsersGuide_Axes_Scale_Scale_Labels.html

Hope this helps - let us know if you meet any problems or have any questions.


By Timothy Wong - 2 Years Ago
Hello,

We have been using the suggested approach for years, but recently we needed to implement a new feature of toggling visibility of grid lines. We tried calling SetShowGridLines() on axis' scale configurator but noticed that when we hide grid lines, axis labels also get hidden. 

Is there another way to show/hide grid lines without affecting visibility of axis labels?

Thank you!

By Nevron Support - 2 Years Ago
Hi Timothy,

Can you please send the code you're testing with for review? - We just tested with:

   NChart chart = nChartControl1.Charts[0];

    NBarSeries bar = new NBarSeries();
    bar.Values.Add(10);
    bar.Values.Add(20);
    bar.Values.Add(30);

    chart.Series.Add(bar);

    chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator.MajorGridStyle.SetShowAtWall(ChartWallType.Back, false);

And this did not affect the visibility of the Y axis labels, so it must be some other setting that's affecting this....
By Timothy Wong - 2 Years Ago
Please find an example attached
By Nevron Support - 2 Years Ago
Hi Timothy,

Thank you for sending the code. The problem is that the code relies on custom decorations that are added to the scale initially, but not after you alter the scale (which will rebuild the scale decorations). To solve the problem you need to add custom decorations each time you modify the scale - for example:

  private void OnChartControlDoubleClick(object sender, EventArgs e)
   {
    var chart = nChartControl1.Charts[0];
    var axis = chart.Axis(StandardAxis.PrimaryY);
    var majorGridStyle = axis.ScaleConfigurator.MajorGridStyle;

    // toggle major grid line visibility
    majorGridStyle.SetShowAtWall(ChartWallType.Back, !majorGridStyle.GetShowAtWall(ChartWallType.Back));

    CreateVerticalAxisLabel(axis, "Title 1");

    nChartControl1.Refresh();
   }

Will fix the problem. You can also check out the following topic:
https://helpdotnetvision.nevron.com/UsersGuide_Axes_Scale_ProgrammingTheScale_Getting_Started.html

which talks about how to add custom-scale decorations.

We hope this helps - let us know if you have any questions or meet any problems.
By Timothy Wong - 2 Years Ago
Recreating the label with decorations solves the problem. Thank you!