如何使用按钮更新图表?
How to update the chart using button?
我将使用 按钮更新图表上的数据。
我试图寻找并解决各种案例,但没有成功。
这是XAML代码
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="12*"/>
</Grid.RowDefinitions>
<Button Click="StartChart">
Start
</Button>
<lvc:CartesianChart Series="{Binding Series}"
Grid.Row="1">
<lvc:CartesianChart.AxisX>
<lvc:Axis LabelFormatter="{Binding Formatter}"></lvc:Axis>
</lvc:CartesianChart.AxisX>
</lvc:CartesianChart>
</Grid>
这是代码的背后
public void StartChart(object sender, RoutedEventArgs e)
{
var dayConfig = Mappers.Xy<DateModel>()
.X(dayModel => (double)dayModel.DateTime.Ticks / TimeSpan.FromHours(1).Ticks)
.Y(dayModel => dayModel.Value);
Series = new SeriesCollection(dayConfig)
{
new LineSeries
{
Values = new ChartValues<DateModel>
{
new DateModel
{
DateTime = DateTime.Now,
Value = value1
},
new DateModel
{
DateTime = DateTime.Now.AddMinutes(30),
Value = value2
},
new DateModel
{
DateTime = DateTime.Now.AddHours(1),
Value = value3
},
new DateModel
{
DateTime = DateTime.Now.AddHours(2),
Value = value4
}
},
Fill = Brushes.Transparent
}
};
Formatter = value => new DateTime((long)(value * TimeSpan.FromHours(4).Ticks)).ToString("t");
value1++;
value2--;
value3++;
value4--;
DataContext = this;
}
public Func<double, string> Formatter { get; set; }
public SeriesCollection Series { get; set; }
每次单击按钮时,您都希望图表的值发生变化。
请给我一个解决方案。
您想使用
public static readonly DependencyProperty FormatterProperty = DependencyProperty.Register(
nameof(Formatter), typeof(Func<double, string>), typeof(MainWindow), new PropertyMetadata(default(Func<double, string>)));
并将public Func<double, string> Formatter { get; set; }
更改为
public Func<double, string> Formatter
{
get => (Func<double, string>)GetValue(FormatterProperty);
set => SetValue(FormatterProperty, value);
}
所有其他绑定预期变量也是如此。
这称为依赖属性的数据绑定。当 DependecyProperty 值更改时,绑定也会更改。通过这种方式,您将不需要不断地重新分配 DataContext = this;
。只设置一次 DataContext。 DependencyPropertys 完成剩下的工作。
您可能还想研究 MVVM 模式,您将在其中了解 class 您将命名 ViewModel 并将其设置为 DataContext;并且,不使用 DependencyProperties,而是使用 INotifyPropertyChange 来达到同样的效果。
我将使用 按钮更新图表上的数据。 我试图寻找并解决各种案例,但没有成功。
这是XAML代码
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="12*"/>
</Grid.RowDefinitions>
<Button Click="StartChart">
Start
</Button>
<lvc:CartesianChart Series="{Binding Series}"
Grid.Row="1">
<lvc:CartesianChart.AxisX>
<lvc:Axis LabelFormatter="{Binding Formatter}"></lvc:Axis>
</lvc:CartesianChart.AxisX>
</lvc:CartesianChart>
</Grid>
这是代码的背后
public void StartChart(object sender, RoutedEventArgs e)
{
var dayConfig = Mappers.Xy<DateModel>()
.X(dayModel => (double)dayModel.DateTime.Ticks / TimeSpan.FromHours(1).Ticks)
.Y(dayModel => dayModel.Value);
Series = new SeriesCollection(dayConfig)
{
new LineSeries
{
Values = new ChartValues<DateModel>
{
new DateModel
{
DateTime = DateTime.Now,
Value = value1
},
new DateModel
{
DateTime = DateTime.Now.AddMinutes(30),
Value = value2
},
new DateModel
{
DateTime = DateTime.Now.AddHours(1),
Value = value3
},
new DateModel
{
DateTime = DateTime.Now.AddHours(2),
Value = value4
}
},
Fill = Brushes.Transparent
}
};
Formatter = value => new DateTime((long)(value * TimeSpan.FromHours(4).Ticks)).ToString("t");
value1++;
value2--;
value3++;
value4--;
DataContext = this;
}
public Func<double, string> Formatter { get; set; }
public SeriesCollection Series { get; set; }
每次单击按钮时,您都希望图表的值发生变化。 请给我一个解决方案。
您想使用
public static readonly DependencyProperty FormatterProperty = DependencyProperty.Register(
nameof(Formatter), typeof(Func<double, string>), typeof(MainWindow), new PropertyMetadata(default(Func<double, string>)));
并将public Func<double, string> Formatter { get; set; }
更改为
public Func<double, string> Formatter
{
get => (Func<double, string>)GetValue(FormatterProperty);
set => SetValue(FormatterProperty, value);
}
所有其他绑定预期变量也是如此。
这称为依赖属性的数据绑定。当 DependecyProperty 值更改时,绑定也会更改。通过这种方式,您将不需要不断地重新分配 DataContext = this;
。只设置一次 DataContext。 DependencyPropertys 完成剩下的工作。
您可能还想研究 MVVM 模式,您将在其中了解 class 您将命名 ViewModel 并将其设置为 DataContext;并且,不使用 DependencyProperties,而是使用 INotifyPropertyChange 来达到同样的效果。