简体   繁体   中英

chart X axis shifting

I'm having trouble trying to create ac# chart with an x axis that shifts.

What i want is to add a new Y value and have the x axis shift to the left removing the first data point.

I can add points to the right hand side fine,

 DataPoint dp2 = new DataPoint(x, rnd.Next(100));
 chart1.Series[0].Points.Add(dp2);
 x++;

i can remove the first point on the left hand side fine

chart1.Series[0].Points.RemoveAt(0);

but if i try to do both it does not work, it adds blank spaces at the beginning of the graph and goes off out the right hand side rather than staying inside the graph area ?

DataPoint dp2 = new DataPoint(x, rnd.Next(100));
chart1.Series[0].Points.RemoveAt(0);
chart1.Series[0].Points.Add(dp2);
x++;

full demo of my problem:- (make a chart, 3 buttons and 3 timers, paste my code over the top of form1.cs then connect the buttons (mouse click) & timers (ticks 1000ms) events to my codes event handlers.)

using System;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace test_chart2
{
    public partial class Form1 : Form
    {
        double x = 16;
        Random rnd = new Random();
        public Form1()
        {
            InitializeComponent();


            DrawToGraph1();

        }

        private void DrawToGraph1()
        {
            chart1.Series[0].Points.Clear();
            for (int g = 5; g <= 15; g++)
            {
                DataPoint dp = new DataPoint(g, rnd.Next(100));
                chart1.Series[0].Points.Add(dp);
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            DataPoint dp2 = new DataPoint(x, rnd.Next(100));
            chart1.Series[0].Points.Add(dp2);
            x++;
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            chart1.Series[0].Points.RemoveAt(0);
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            DataPoint dp2 = new DataPoint(x, rnd.Next(100));
            chart1.Series[0].Points.RemoveAt(0);
            chart1.Series[0].Points.Add(dp2);
            x++;
        }
        private void button2_MouseClick(object sender, MouseEventArgs e)
        {
            timer1.Enabled = false;
            timer2.Enabled = true;
            timer3.Enabled = false;
        }
        private void button3_MouseClick(object sender, MouseEventArgs e)
        {
            timer1.Enabled = false;
            timer2.Enabled = false;
            timer3.Enabled = true;
        }
        private void button1_MouseClick(object sender, MouseEventArgs e)
        {
            timer1.Enabled = true;
            timer2.Enabled = false;
            timer3.Enabled = false;
        }
    }
}

It does work if i use

series1.IsXValueIndexed = true;

but i cant use this as my project has a time X axis and when i use this option it does not produce a nice time line (the axis labels are not rounded to the nearest hour like they are normally) and it also causes alignment problems with my secondary X axis that shows the date at the top of the graph.

Please try this and tell me what happens:

private void timer3_Tick(object sender, EventArgs e)
{
  DataPoint dp2 = new DataPoint(x, rnd.Next(100));
  Series mySeries = chart1.Series[0];
  chart1.Series.Clear();
  mySeries.Points.RemoveAt(0);
  mySeries.Points.Add(dp2);
  chart1.Series.Add(mySeries);
  x++;
}

That is, you can try removing the series, modify it, then add it back to the chart. I could not run it myself because I cannot easily reconstruct your project in Visual Studio. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM