X axis offset

I am having problems creating a C # chart with an x ​​axis that is shifting.

I want to add a new Y value and shift the x axis to the left, deleting the first data point.

I can add dots to the right side,

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

i can delete the first point on the left side of the thin

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

but if I try to do both, it doesn’t work, does it add spaces at the beginning of the chart and go out from the right side, and not stay inside the chart area?

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

full demonstration of my problem: - (make a diagram, 3 buttons and 3 timers, paste my code on top of form1.cs, then connect the buttons (mice) and timers (1000 ms marks) to the code 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 works if I use

 series1.IsXValueIndexed = true; 

but I cannot use this because my project has an X axis of time, and when I use this parameter, it does not create a good time line (axis labels are not rounded to the nearest hour, as is usually the case), and this also causes alignment problems with my secondary X axis, which shows the date at the top of the graph.

+4
source share
1 answer

Please try this and tell me what will happen:

 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 to delete a series, change it, and then add it back to the diagram. I could not start it myself, because I cannot easily restore the project in Visual Studio. Hope this helps.

0
source

Source: https://habr.com/ru/post/1444830/


All Articles