Title, Axis, LegendBox - saving locations?

A forum dedicated to WinForms version of LightningChart Ultimate.

Moderator: Arction_LasseP

Greg9504
Posts: 38
Joined: Fri Dec 06, 2013 4:51 pm

Title, Axis, LegendBox - saving locations?

Post by Greg9504 » Tue May 06, 2014 5:49 pm

Hello,

If allowed (MoveByMouse set to true) the title, axis labels and legendbox call all be moved around the chart by the user. How do I get the locations so that I can restore the positions the next time the plot is shown? The Offset X and Y do not seem to get updated when any of these are moved by the user.

Thanks
Greg.

ArctionTero
Posts: 42
Joined: Thu Mar 28, 2013 9:20 am

Re: Title, Axis, LegendBox - saving locations?

Post by ArctionTero » Wed May 07, 2014 1:10 pm

Hi Greg,

You can restore LegendBoxXY and ChartTitle location by using kind of code that follows. Idea is to update Offset values according to rendered coordinates.
For WinForms, control is drawn first time on some unapplicable location, for which there is the flag to skip the first render.

Code: Select all

           //At chart initialize
            m_chart.BeforeRendering += new LightningChartUltimate.BeforeRenderingHandler(m_chart_BeforeRendering);
            m_chart.AfterRendering += new LightningChartUltimate.AfterRenderingHandler(m_chart_AfterRendering);

        //At component root
        bool m_bFirstRendering = true;
        String m_strLBFileLoc = @"C:\lbpos.txt";
        String m_strTitleFileLoc = @"C:\titlepos.txt";
        void m_chart_AfterRendering(LightningChartUltimate chart)
        {
            if (m_bFirstRendering)
            {
                m_bFirstRendering = false;
                return;
            }

            //Store lb coordinates
            Rectangle drawRect = chart.ViewXY.LegendBox.GetRenderedRect();
            if (File.Exists(m_strLBFileLoc))
                File.Delete(m_strLBFileLoc);
            using (StreamWriter file = File.CreateText(m_strLBFileLoc))
            {
                file.WriteLine("{0},{1}", drawRect.X, drawRect.Y);
            }

            //Store title coordinates
            drawRect = chart.Title.DrawRectangle;
            if (File.Exists(m_strTitleFileLoc))
                File.Delete(m_strTitleFileLoc);
            using (StreamWriter file = File.CreateText(m_strTitleFileLoc))
            {
                file.WriteLine("{0},{1}", drawRect.X, drawRect.Y);
            }
        }

        void m_chart_BeforeRendering(LightningChartUltimate chart, int width, int height)
        {
            if (m_bFirstRendering)
            {
                return;
            }
            Rectangle drawRect = new Rectangle();

            //Disable rendering
            chart.BeginUpdate();

            //LegendBox
            if (File.Exists(m_strLBFileLoc))
            {
                using (StreamReader file = File.OpenText(m_strLBFileLoc))
                {
                    char[] aSeparator = new char[] {','};
                    String[] aLines = file.ReadLine().Split(aSeparator);
                    drawRect.X = int.Parse(aLines[0]);
                    drawRect.Y = int.Parse(aLines[1]);
                }
                chart.ViewXY.LegendBox.Position = LegendBoxPosition.TopLeft;
                chart.ViewXY.LegendBox.Offset.X = drawRect.X - chart.Margin.Left + 2;
                chart.ViewXY.LegendBox.Offset.Y = drawRect.Y - chart.Margin.Top + 2;
            }

            //Chart title
            if (File.Exists(m_strTitleFileLoc))
            {
                using (StreamReader file = File.OpenText(m_strTitleFileLoc))
                {
                    char[] aSeparator = new char[] { ',' };
                    String[] aLines = file.ReadLine().Split(aSeparator);
                    drawRect.X = int.Parse(aLines[0]);
                    drawRect.Y = int.Parse(aLines[1]);
                }
                chart.Title.Align = ChartTitleAlignment.TopLeft;
                chart.Title.Offset.X = drawRect.X;
                chart.Title.Offset.Y = drawRect.Y;
            }
            //Allow rendering
            chart.EndUpdate();
        }
Unfortunately positioning axis titles is not possible using code like above.
LightningChart Support Team, TK