c#图表中的动态启用= true
本文关键字:启用 true 动态 | 更新日期: 2023-09-27 18:16:02
我有一个包含不同音轨值的数据库。
例如:跟踪1:
Sensor 1
Sensor 2
Sensor 3
Sensor 4
Sensor 5
Sensor 6
Sensor 7
Sensor 8
跟踪2:
Sensor 1
Sensor 2
Sensor 3
Sensor 4
Sensor 5
Sensor 6
Sensor 7
Sensor 8
Sensor 9
Sensor 10
Sensor 11
Sensor 12
你可以看到Track 2有12个传感器(Witch是最大的!)。
现在我想在图形中显示传感器的值。
然而,现在有12项硬编码。所以当有8个传感器时,图例仍然显示12个。
现在我所做的是添加Enabled = False,所以你不会看到它。(见下面的例子)
<chart:DataSeries x:Name="dsSensor1" Enabled="False" RenderAs="Line" LineThickness="3" LegendText="1" XValueType="DateTime" XValueFormatString="dd-MM HH:mm" YValueFormatString="#0.##'V'" MarkerEnabled="False">
<chart:DataSeries.DataPoints>
<chart:DataPoint XValue="2001-01-01" YValue="3.2" Enabled="False"/>
</chart:DataSeries.DataPoints>
</chart:DataSeries>
<chart:DataSeries x:Name="dsSensor2" Enabled="False" RenderAs="Line" LineThickness="3" LegendText="2" XValueType="DateTime" XValueFormatString="dd-MM HH:mm" YValueFormatString="#0.##'V'" MarkerEnabled="False">
<chart:DataSeries.DataPoints>
<chart:DataPoint XValue="2001-01-01" YValue="3.2" Enabled="False"/>
</chart:DataSeries.DataPoints>
</chart:DataSeries>
现在你不会看到图例。
现在在代码中我创建了一个新的循环,像这样:
foreach (DCHistory item in loadOperation.Entities.OrderByDescending(t => t.SensorNumber).Take(1))
这将取特定音轨的最大值。例如,这个查询将产生8。
我现在在做什么=
foreach (DCHistory item in loadOperation.Entities.OrderByDescending(t => t.SensorNumber).Take(1))
{
DataSeries series1 = chart.Series.First(s => s.Name == string.Format("dsSensor1"));
DataSeries series2 = chart.Series.First(s => s.Name == string.Format("dsSensor2"));
DataSeries series3 = chart.Series.First(s => s.Name == string.Format("dsSensor3"));
DataSeries series4 = chart.Series.First(s => s.Name == string.Format("dsSensor4"));
DataSeries series5 = chart.Series.First(s => s.Name == string.Format("dsSensor5"));
DataSeries series6 = chart.Series.First(s => s.Name == string.Format("dsSensor6"));
DataSeries series7 = chart.Series.First(s => s.Name == string.Format("dsSensor7"));
DataSeries series8 = chart.Series.First(s => s.Name == string.Format("dsSensor8"));
DataSeries series9 = chart.Series.First(s => s.Name == string.Format("dsSensor9"));
DataSeries series10 = chart.Series.First(s => s.Name == string.Format("dsSensor10"));
DataSeries series11 = chart.Series.First(s => s.Name == string.Format("dsSensor11"));
DataSeries series12 = chart.Series.First(s => s.Name == string.Format("dsSensor12"));
int sensor = item.SensorNumber;
if (sensor == 8)
{
series1.Enabled = true;
series2.Enabled = true;
series3.Enabled = true;
series4.Enabled = true;
series5.Enabled = true;
series6.Enabled = true;
series7.Enabled = true;
series8.Enabled = true;
}
}
如果结果是8,这段代码将计数。如果为8,则启用图例中的前8项。
现在可以工作了。然而,我也需要做一个像12个传感器,或6个。这将导致大量的代码,但它仍然是硬编码的!
我的问题:
有可能在for循环中实现这个吗?
我已经试过了,但是没有成功。
What i tried:
foreach (DCHistory item in loadOperation.Entities.OrderByDescending(t => t.SensorNumber).Take(1))
{
int sensor = item.SensorNumber;
int sensor2 = sensor + 1; //set +1 because if number = 0, it gives error.
if (sensor >= 1)
{
for (int number = 1; number < sensor2; number++)
{
DataSeries series = chart.Series.First(s => s.Name == string.Format("dsSensor{0}", number));
series.Enabled = true;
}
}
}
这段代码的结果是,图例显示我12个传感器,即使有8…
当然,我会将您的代码转换为使用一些Linq
。
var series = Enumerable.Range(1, item.SensorNumber)
.Select(i => chart.Series.First(s => s.Name == ("dsSensor" + i)))
.ToArray();
for(var i = 0; i < series.Length; ++i){
series[i].Enabled = true;
}
好了,我找到了答案!
我想有一次我访问了一个有12个传感器的轨道,它使传感器保持在Enabled = true。
所以当我访问一个有8个传感器的轨道时,旧的那个保持启用。
我现在做了什么:
foreach (DCHistory item in loadOperation.Entities.OrderByDescending(t => t.SensorNumber).Take(1))//Gets highest sensor number of the track
{
var sensor = item.SensorNumber + 1;//Gets the number we just asked from the loop. For example 8 or 12.
var start = 1;
var max = 12;
while (start < sensor)
{
var test = chart.Series.First(s => s.Name == string.Format("dsSensor{0}", start.ToString()));
test.Enabled = true;
start++;
}
if (sensor < max)
{
while (sensor <= max)
{
var test2 = chart.Series.First(s => s.Name == string.Format("dsSensor{0}", sensor.ToString()));
test2.Enabled = false;
sensor++;
}
}
}
基本上我做了一个检查,如果传感器计数是8个例子。它会检查它是否小于最大值。