Search code examples
c#.netwinformsmschart

How can I add multiple x axes in mschart?


I'm new to C# but am already surprised by it's powerful develope environment(Visual Studio 2017). I wonder if there are ways to put two x axes on one mschart that is supported by default.

My program is quite simple. It gets "Y-Value" and "Time" data, which are strings like "45.1", "45.2", 45.3" and "115958", "115959", "120000" (hh:mm:ss) and the program plots a series of Y-Value' that comes in whenever OnReceiveData event occurs.

The below shows the code and it's chart.

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.OnReceiveData += this.OnReceiveRealData;
        }

        private void OnReceiveRealData(object sender, OpenAPILib._OpenAPIEvents_OnReceiveRealDataEvent e)
        {
            this.chart1.Series[0].Points.AddY(OpenAPI1.GetCommRealData(e.sRealType, "Y-Value").Trim());
        }
    }
}

enter image description here

X axis shows the number of "Y-Value"s the program has received. But it's not like it gets "Y-Value" every second(or OnReceiveData event occurs every second). Sometimes the event occurs several times for one second, and sometimes it doesn't occur for several seconds. So, I want to add another X axis and see how much time it has passed or how many "Y-Value"s have received during the time, maybe like the below.

enter image description here

How can I add another X-axis in mschart? Could I get some advice or links related to this?


Solution

  • You can enable the secondary x-axis:

    yourChartArea.AxisX2.Enabled = AxisEnabled.True;
    

    You can associate a series to it by setting

    someSeries.XAxisType = AxisType.Secondary;
    

    It will, by default, sit on the top and you could play with the Crossing to move it around.

    For more powerful and much much more complicated extra axes see here.. Actually I would not recommend this, as it is not for the faint of heart or newbies ;-)

    The Secondary axis can be styled just like the Primary one..