Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How to force axis updates

Hello there!

I'm currently using a ScatterGraph with two Y Axes.
Both of them have different (and custom) FormatString objects, derived from FormatString, to provide formatting of the MajorDivisions label content.
These FormatString objects have a Range property, and FormatDouble formats the value according to this range. For example, if the axis goes from 0 mm to 10 mm, the MajorDivisions are displayed "{value} mm", but if the axis goes from 0 mm to 10000 mm, MajorDivisions are displayed as "{value/1000} m".

Now everytime there is a RangeChanged event on the axis, the format should get updated and displayed, so I set the Range property of my custom FormatString object to the actual Range of the axis.

Of course, the axis doesn't know that my custom FormatString changed, so it won't update it's MajorDivisions labels. And because the RangeChanged event apparently fires after the MajorDivisions are drawn, the axis will format the MajorDivisions labels according to the old range (which is always wrong).

Now the nice and clean solution would be, in my opinion, to have a BeforeRangeChanged and AfterRangeChanged event, something like in the NumericEdit control, but since these events don't exist, I need to be able to force the axis to update.

Invalidate() won't update these labels, so right now my only fix is to go through this procedure:

MyAxis.Visible = false;
MyAxis.Visible = true;

I'm not really happy with this solution, since setting the Visible property might have a lot of impact on other things, and may also be very expensive.

Is there a recommended solution to this?
0 Kudos
Message 1 of 3
(3,101 Views)

It seems that you are trying to change the FormatString to your derived class in the RangeChanged event of the axis. Instead, you should assign an instance of your FormatString derived class to the LabelFormat property of the axis MajorDivisions once and perform your formatting in the FormatDouble override.

MyAxis.MajorDivisions.LabelFormat = new MyCustomFormatString(...);

You do not need to reassign this property every time the axis range changes, nor do you need to hold the Range values in your custom FormatString. The argument provided to the FormatDouble method is a one of the range minimum or maximum values that you should format.

I may have misunderstood how exactly you are accomplishing your objective now, so if you could post a code sample, it would help us analyze the issue more effectively. Thanks for your question.

Abhishek Ghuwalewala | Measurement Studio | National Instruments
0 Kudos
Message 2 of 3
(3,098 Views)
Hello,

it seems like there is some misunderstanding in what I try to achieve.

I'll try to clarify the issue with a short sample.

First, I've created a custom FormatString like that:

  public class TimeFormatString : FormatString
  {
    private Range m_Range = new Range(0, 1);

    public TimeFormatString() : base(FormatStringMode.Numeric, "0 ms")
    {
    }

    public Range Range
    {
      get { return m_Range; }
      set { m_Range = value; }
    }

    public override string FormatDouble(double value)
    {
      if ((Range.Maximum - Range.Minimum) >= 60)
        return (value / 60).ToString("0.00 min");
      else
        return value.ToString("0.00 s");
    }
  }

After creating the graph and everything, I set the MajorDivisions.Label Format of the Axis to an instance of my TimeFormatString, like that:

...
TimeFormatString MyCustomFormatString = new TimeFormatString();
MyAxis.MajorDivisions.LabelFormat = MyCustomFormatString;
...

Now I need to update the Range property of MyCustomFormatString. I currently do this in the RangeChanged event of the axis

    private void MyAxis_RangeChanged(object sender, EventArgs e)
    {
      MyCustomFormatString.Range = MyAxis.Range;

      // Update the labels
      MyAxis.MajorDivisions.LabelVisible = false;
      MyAxis.MajorDivisions.LabelVisible = true;
    }

It looks like I've found the solution already, though. Instead of formatting according to a Range object which I update myself, I now format the values according to an Axis object which I set once.

I wonder what's the usual way to achive this kind of custom formatting, is there a recommended practice?

0 Kudos
Message 3 of 3
(3,087 Views)