//网格线不会随着线点的变化而移动。我应该添加什么代码才能使它看起来像cpu性能图表?
Series test1 = new Series();
Series test2 = new Series();
private void Form1_Load(object sender, EventArgs e)
{
test1.Color = Color.Blue;
test1.ChartType = SeriesChartType.FastLine;
test1.BorderWidth = 2;
test2.Color = Color.Red;
test2.ChartType = SeriesChartType.FastLine;
test2.BorderWidth = 2;
chart1.Series.Add(test1);
chart1.Series.Add(test2);
chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
chart1.ChartAreas[0].AxisX.IsStartedFromZero = true;
chart1.ChartAreas[0].AxisX.IntervalOffsetType = DateTimeIntervalType.Number;
}
private void timer1_Tick(object sender, EventArgs e)
{
Axis xaxis = chart1.ChartAreas[0].AxisX;
xaxis.Minimum = xaxis.Maximum - 10;
Random rnd = new Random();
int dice = rnd.Next(1, 7);
float num1 = rnd.Next(1, 13);
test1.Points.Add(num1);
test2.Points.Add(dice);
chart1.ResetAutoValues();
}
答案 0 :(得分:0)
您有两种选择:
如果您要快速或长时间添加DataPoints,前一个选项是您最终需要执行的操作。
您可能还想使用缩放来回顾过去。如果您需要保留数据,您仍然可以将删除的点添加到内存中的列表中,并在需要时将其恢复。
答案 1 :(得分:0)
您可以使用Grid.IntervalOffset
属性:
int GridlinesOffset = 0;
// ...
// In chart update loop:
// Make gridlines move.
chart.ChartAreas[0].AxisX.MajorGrid.IntervalOffset = -GridlinesOffset;
// Calculate next offset.
GridlinesOffset++;
GridlinesOffset %= (int) chart.ChartAreas[0].AxisX.MajorGrid.Interval;
在以下示例中查看timer_Tick()
方法。
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CPUPerformanceChart
{
public partial class Form1 : Form
{
private int GridlinesOffset = 0;
public Form1()
{
InitializeComponent();
Color axisColor = Color.FromArgb(100, 100, 100);
Color gridColor = Color.FromArgb(200, 200, 200);
Color backColor = Color.FromArgb(246, 246, 246);
Color lineColor = Color.FromArgb(50, 50, 200);
chart.Series[0].Color = lineColor;
chart.ChartAreas[0].BackColor = backColor;
chart.ChartAreas[0].BorderWidth = 1;
chart.ChartAreas[0].BorderColor = axisColor;
chart.ChartAreas[0].BorderDashStyle =
System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
chart.ChartAreas[0].AxisX.LineColor = axisColor;
chart.ChartAreas[0].AxisY.LineColor = axisColor;
chart.ChartAreas[0].AxisX.MajorGrid.LineColor = gridColor;
chart.ChartAreas[0].AxisY.MajorGrid.LineColor = gridColor;
chart.ChartAreas[0].AxisX.MajorGrid.Interval = 10;
chart.ChartAreas[0].AxisY.MajorGrid.Interval = 10;
// 60 seconds interval.
chart.ChartAreas[0].AxisX.Minimum = 0;
chart.ChartAreas[0].AxisX.Maximum = 60;
chart.ChartAreas[0].AxisY.Minimum = 0;
chart.ChartAreas[0].AxisY.Maximum = 100;
chart.ChartAreas[0].AxisX.LabelStyle.Enabled = false;
chart.ChartAreas[0].AxisY.LabelStyle.Enabled = false;
chart.ChartAreas[0].AxisX.MajorTickMark.Enabled = false;
chart.ChartAreas[0].AxisY.MajorTickMark.Enabled = false;
for (int i = 0; i < 60; i++)
{
chart.Series["Series1"].Points.AddY(0);
}
}
// timer.Interval = 1000.
private void timer_Tick(object sender, EventArgs e)
{
float nextValue = cpuPerformanceCounter.NextValue();
labelCpuUsage.Text = String.Format("{0:0.00} %", nextValue);
chart.Series["Series1"].Points.AddY(nextValue);
chart.Series["Series1"].Points.RemoveAt(0);
// Make gridlines move.
chart.ChartAreas[0].AxisX.MajorGrid.IntervalOffset = -GridlinesOffset;
// Calculate next offset.
GridlinesOffset++;
GridlinesOffset %= (int) chart.ChartAreas[0].AxisX.MajorGrid.Interval;
}
}
}
结果是: