1

I am using chart in real time, feeding it by seperated thread. But everytime I add another series and legend that legend pushes chart away making it smaller. I can not find any solution to this.

This is my function for setting up chart before any series are added:

        chart1.BackColor = Color.DarkGray;
        chart1.ForeColor = Color.White;
        chart1.ChartAreas[0].BackColor = Color.WhiteSmoke;

        chart1.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.White;
        chart1.ChartAreas[0].AxisX.LineColor = Color.White;
        chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.DarkGray;
        chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";

        chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.White;
        chart1.ChartAreas[0].AxisY.LineColor = Color.White;
        chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.DarkGray;

        chart1.ChartAreas[0].AxisX.Interval = 5;
        chart1.ChartAreas[0].AxisX.IntervalOffset = 1;

        chart1.ChartAreas[0].AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Seconds;

        //SetX();
        DateTime act = DateTime.Now;
        DateTime max = act.AddSeconds(60);
        //DateTime min = act.AddSeconds(-30);

        chart1.ChartAreas[0].AxisX.Minimum = act.ToOADate();
        chart1.ChartAreas[0].AxisX.Maximum = max.ToOADate();

When I add series/points:

        if (chart1.Legends.IndexOf(name) == -1)
        {
            //Legend
            chart1.Legends.Add(name);
            chart1.Legends[name].Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Left;
            chart1.Legends[name].LegendStyle = System.Windows.Forms.DataVisualization.Charting.LegendStyle.Column;



        }

        // Must exists
        if (chart1.Series.IndexOf(name) == -1)
        {
            // Series
            chart1.Series.Add(name);
            chart1.Series[name].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StepLine;
            chart1.Series[name].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime;
            chart1.Series[name].BorderWidth = 4;
            chart1.Series[name].Legend = name;

        }


        DateTime act = DateTime.Now;

        //Get value from string
        switch (format)
        {
            case "BOOL":
                int boolval = value.ToUpper() == "TRUE" ? 1 : 0;
                chart1.Series[name].Points.AddXY(act, boolval);
                break;
            case "DEC":
                int intval = Int32.Parse(value);
                chart1.Series[name].Points.AddXY(act, intval);
                break;
            case "FLOAT":
                float fvalue = float.Parse(value);
                chart1.Series[name].Points.AddXY(act, fvalue);
                break;
        }

        if (act.ToOADate() >= chart1.ChartAreas[0].AxisX.Maximum)
        {
            SetX();
        }

Without this line:

chart1.Series[name].Legend = name;

It seems to connect all legend into one "DIV" but still, it always stacks legend in a way that shrinks the chart:

enter image description here

Picture is WITH this line.

How to do I setup legend so it would not change the size of the chart?

Somachr
  • 464
  • 1
  • 8
  • 21
  • You can control the ElementPosition of the ChartArea to make it sit below the row of legends. Why do you add any legends, though?? One Legend with several LegendItems are all that is needed and even that is provided with the default Legend.. – TaW Mar 18 '19 at 13:35
  • Could you post any examples? – Somachr Mar 18 '19 at 14:04

1 Answers1

2

You will want to control the position and size of the ChartArea.

This is done by setting the Position from Automatic to fixed values.

Note that ChartArea.Position is an ElementPosition, which is a property that

  • holds all four elements of a rectangle, so it is not so much a position but an area and
  • all values are in percent of the surrounding element, i.e. the chart's ClientRectangle.

Example:

ChartArea ca = chart1.ChartAreas[0];
ca.Position = new ElementPosition(3, 13, 96, 84);

This leaves 3% padding on all sides and moves the ChartArea down by 10%.

You will want to play with the numbers and maybe adjust them when you need a second row of Legends or LegendItems..

enter image description here

By default the system adds one LegendItem to the default Legend. If you want more control you could add custom LegendItems. See here for more on this..

And by default the system will adapt the position of the ChartArea to fit nicely under the default Legend. Looks like you have done a bit more than is shown in the code..

TaW
  • 53,122
  • 8
  • 69
  • 111
  • "Looks like you have messed things up a lillte?" No I did not. – Somachr Mar 18 '19 at 16:56
  • Well, no offense meant but you must have done more than the code you show. Did you try my suggestion? – TaW Mar 18 '19 at 17:35
  • 1
    I have defined area for chart and legends. Legends are now filling their area very nicely. I am still testing it. – Somachr Mar 19 '19 at 11:02