var mapper = Mappers.Xy<MeasureModel>()
.X(model => model.DateTime.Ticks) //use DateTime.Ticks as X
.Y(model => model.Value); //use the value property as Y
//The next code simulates data changes every 500 ms
Timer = new Timer
{
Interval = 1000
};
Timer.Tick += TimerOnTick;
R = new Random();
Timer.Start();
ChartValues.Add(new MeasureModel
{
DateTime = now,
Value = R.Next(0, 5)
});
从上面,我该怎么做,而不是让 R 随机 ,我可以将其设置为递减吗?就像它像 5.0 4.4 3.2 等一样下降为双精度,而不是在 0 到 5
设置第一个计时器的方法中,添加另一个,如下所示:
//The next code simulates data changes every 500 ms
Timer = new Timer
{
Interval = 1000
};
Timer.Tick += TimerOnTick;
R = new Random();
Timer.Start();
//another timer
SecondTimer = new Timer() { Interval = 2000 };
SecondTimer.Enabled = true;
SecondTimer.Tick += SecondTimer_Tick;
定义一些min
变量
double min = 4.9;
SecondTimer_Tick
方法中,调整这些变量并生成新值。 Random.NextDouble
将生成从 0 到 1(即 0.887)的数字,请将其添加到您的 min
var 中。此外,将最小值减少某个值(在本例中为 0.2)
private void SecondTimer_Tick(object sender, EventArgs e)
{
min -= 0.2;
if (min == 0)
//stop timer or something
SecondTimer.Stop();
Random R = new Random();
var value = R.NextDouble();
value += min;
}
希望这有帮助。
编辑:添加一些检查随机发生器生成的值是否太大。例如,如果您的min
是 4.8,生成的值是 0.5 - 将它们加在一起将得到 5.3,我想这对您的情况来说太大了。
var r = R.NextDouble()*5
会给你从0到5的十进制数。
从语义上讲,你可以像这样反转数字,var r= 5 - R.NextDouble()*5
当然,5 可以是任何数字的变量。
现在,在每个计时器时钟周期中从随机生成的数字中减值。
private double decrementingValue = 5;
...
r = R.NextDouble()* 5;
decrementingValue -= r;
if(decrementingValue <= 0)
decrementingValue= 0; // you are finished
构建一个生成器类,该类将为您执行新值的所有计算。
举个例子,这里有一个非常基本的实现,它可以成为你的类的入门
enum Direction
{
Increase,
Decrease,
}
class ValueGenerator
{
private readonly Random _generator;
public ValueGenerator( Random generator )
{
if ( generator == null )
throw new ArgumentNullException( nameof( generator ) );
_generator = generator;
}
public double MinInterval { get; set; } = 0;
public double MaxInterval { get; set; } = 1;
public double MinValue { get; set; } = 0;
public double MaxValue { get; set; } = 100;
public double CurrentValue { get; set; } = 0;
public Direction CurrentDirection { get; set; } = Direction.Increase;
public int PossibilityToChangeDirection { get; set; } = 10;
public double NextValue()
{
if ( _generator.Next( PossibilityToChangeDirection + 1 ) == PossibilityToChangeDirection / 2 )
{
switch ( CurrentDirection )
{
case Direction.Increase:
CurrentDirection = Direction.Decrease;
break;
case Direction.Decrease:
CurrentDirection = Direction.Increase;
break;
default:
throw new InvalidOperationException( );
}
}
var newInterval = ( MaxInterval - MinInterval ) * _generator.NextDouble( ) + MinInterval;
if ( CurrentDirection == Direction.Decrease )
newInterval = -newInterval;
if ( CurrentValue + newInterval < MinValue )
CurrentValue = MinValue;
else if ( CurrentValue + newInterval > MaxValue )
CurrentValue = MaxValue;
else
CurrentValue += newInterval;
return CurrentValue;
}
}
使用示例
//The next code simulates data changes every 500 ms
Timer = new Timer
{
Interval = 1000
};
Timer.Tick += TimerOnTick;
var vg = new ValueGenerator( new Random( ) )
{
MinValue = 0,
MaxValue = 5,
MinInterval = 0.1,
MaxInterval = 0.7,
CurrentValue = 3.0,
CurrentDirection = Direction.Decrease,
PossibilityToChangeDirection = 10,
};
Timer.Start();
ChartValues.Add(new MeasureModel
{
DateTime = now,
Value = vg.NextValue(),
});
您可以使用抽象测量源
public interface IMeasurementSource
{
MeasureModel GetNext();
}
为您的类(可能是 Form)提供一些IMeasurementSource
实现,并将其保存在类字段中。 例如
private IMeasurementSource measurementSource = new BouncingMesasurementSource(5, 0.1);
并在每次计时器滴答作响时调用它:
ChartValues.Add(measurementSource.GetNext());
实现抽象
您可以通过从真实来源获取测量值来实现此接口。或者您可以使用随机测量生成器:
public class RandomMeasurementSource : IMeasurementSource
{
private readonly Random random = new Random();
private readonly int min;
private readonly int max;
public RandomMeasurementSource(int min, int max)
{
this.min = min;
this.max = max;
}
public MeasureModel GetNext()
{
return new MeasureModel { DateTime = DateTime.Now, Value = random.Next(min, max) };
}
}
或者反弹,它会回去并在零和最大值之间强制:
public class BouncingMeasurementSource : IMeasurementSource
{
private readonly double max;
private double step;
private double current;
public BouncingMeasurementSource(int max, double step)
{
this.max = max;
this.step = step;
this.current = max;
}
public MeasureModel GetNext()
{
var model = new MeasureModel { DateTime = DateTime.Now, Value = current };
current -= step;
if (current < 0 || max < current)
{
step = -step;
current = current < 0 ? 0 : max;
}
return model;
}
}