将值添加到 LiveChart c#
Adding values to a LiveChart c#
我正在创建一个 LiveChart 来显示某个函数的图形,让我们以 cos(x) 为例,我在循环 for
时将函数的值添加到列表中loop ,以 a
作为开始,b
作为结束, delta
作为增量,我不知道如何添加具有函数值的列表到 LiveChart ,我试图在互联网上搜索它,但我找不到任何东西。
有人可以告诉我怎么做吗?
代码:
using LiveCharts;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Windows;
namespace Lab1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void submitButton_Click(object sender, RoutedEventArgs e)
{
cartesianChart.Series.Clear();
SeriesCollection series = new SeriesCollection();
double a = Convert.ToDouble(valueOfA.Text);
double b = Convert.ToDouble(valueOfB.Text);
double delta = Convert.ToDouble(valueOfDelta.Text);
List<double> values = new List<double>();
for (double x=a;x<=b;x+=delta)
{
values.Add(Math.Cos(x));
series.Add();
}
//MessageBox.Show($"Values of A, B and Delta are : {a} , {b} , {delta}");
}
}
}
XAML :
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-
compatibility/2006"
xmlns:local="clr-namespace:Lab1"
xmlns:Wpf="clr-
namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
x:Class="Lab1.MainWindow"
mc:Ignorable="d" FontSize="18" FontFamily="Segoe UI Light"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="1" Grid.Row="1"
Text="GRAPH" FontSize="36" Margin="0,0,0,10"/>
<TextBlock Grid.Column="1" Grid.Row="2"
Text="A" FontSize="30"/>
<TextBlock Grid.Column="1" Grid.Row="3"
Text="B" FontSize="30"/>
<TextBlock Grid.Column="1" Grid.Row="4"
Text="Δ" FontSize="30"/>
<TextBox x:Name="valueOfA" Grid.Column="2" Grid.Row="2"
Width="80" Height="30"/>
<TextBox x:Name="valueOfB" Grid.Column="2" Grid.Row="3"
Width="80" Height="30"/>
<TextBox x:Name="valueOfDelta" Grid.Column="2" Grid.Row="4"
Width="80" Height="30"/>
<Button x:Name="submitButton" Grid.Column="1" Grid.Row="5"
Width="150" Height="30" Content="START" Margin="10,10"
Click="submitButton_Click"/>
<Wpf:CartesianChart x:Name="cartesianChart" Grid.Column="4"
HorizontalAlignment="Left" Height="100" Margin="345.6,5.4,0,0"
Grid.Row="4" Grid.RowSpan="3" VerticalAlignment="Top" Width="100">
<Wpf:CartesianChart HorizontalAlignment="Left" Height="364"
Margin="-301,-136,-48,-128" VerticalAlignment="Top" Width="449"/>
</Wpf:CartesianChart>
</Grid>
</Window>
我已经得到了您要循环显示的值,这些值显示在图表中,我认为您希望通过阅读您的问题得到这些值。这是一个非常简单的答案,不会设置工具提示或轴的格式,我会让你去做,如果你遇到困难,你可以 post 另一个问题并标记我。
XAML :
<Window x:Class="WpfApp4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp4"
mc:Ignorable="d"
xmlns:Wpf="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
FontSize="18" FontFamily="Segoe UI Light"
Title="MainWindow" Height="450" Width="800" d:DataContext="{d:DesignInstance local:MainWindow, IsDesignTimeCreatable=False}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="1" Grid.Row="1" Text="GRAPH" FontSize="36" Margin="0,0,0,10"/>
<TextBlock Grid.Column="1" Grid.Row="2" Text="A" FontSize="30"/>
<TextBlock Grid.Column="1" Grid.Row="3" Text="B" FontSize="30"/>
<TextBlock Grid.Column="1" Grid.Row="4" Text="Δ" FontSize="30"/>
<TextBox x:Name="valueOfA" Grid.Column="2" Grid.Row="2" Width="80" Height="30"/>
<TextBox x:Name="valueOfB" Grid.Column="2" Grid.Row="3" Width="80" Height="30"/>
<TextBox x:Name="valueOfDelta" Grid.Column="2" Grid.Row="4" Width="80" Height="30"/>
<Button x:Name="submitButton" Grid.Column="1" Grid.Row="5" Width="150" Height="30" Content="START" Margin="10,10" Click="submitButton_Click"/>
<Wpf:CartesianChart x:Name="cartesianChart" Grid.Column="4" Grid.Row="0" Grid.RowSpan="7">
<Wpf:CartesianChart Series="{Binding SeriesCollection}" Grid.Column="4" Grid.Row="0" Grid.RowSpan="7"/>
</Wpf:CartesianChart>
</Grid>
后面的代码 :
public partial class MainWindow : Window, INotifyPropertyChanged
{
public SeriesCollection SeriesCollection { get; set; }
public MainWindow()
{
InitializeComponent();
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Title = "Series 1",
Values = new ChartValues<double>()
}
};
}
private void submitButton_Click(object sender, RoutedEventArgs e)
{
double a = Convert.ToDouble(valueOfA.Text);
double b = Convert.ToDouble(valueOfB.Text);
double delta = Convert.ToDouble(valueOfDelta.Text);
List<double> values = new List<double>();
for (double x = a; x <= b; x += delta)
{
values.Add(Math.Cos(x));
}
foreach(var value in values)
{
SeriesCollection[0].Values.Add(value);
}
OnPropertyChanged(new PropertyChangedEventArgs("SeriesCollection"));
DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChanged?.Invoke(this, e);
}
}
结果 :
我正在创建一个 LiveChart 来显示某个函数的图形,让我们以 cos(x) 为例,我在循环 for
时将函数的值添加到列表中loop ,以 a
作为开始,b
作为结束, delta
作为增量,我不知道如何添加具有函数值的列表到 LiveChart ,我试图在互联网上搜索它,但我找不到任何东西。
有人可以告诉我怎么做吗?
代码:
using LiveCharts;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Windows;
namespace Lab1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void submitButton_Click(object sender, RoutedEventArgs e)
{
cartesianChart.Series.Clear();
SeriesCollection series = new SeriesCollection();
double a = Convert.ToDouble(valueOfA.Text);
double b = Convert.ToDouble(valueOfB.Text);
double delta = Convert.ToDouble(valueOfDelta.Text);
List<double> values = new List<double>();
for (double x=a;x<=b;x+=delta)
{
values.Add(Math.Cos(x));
series.Add();
}
//MessageBox.Show($"Values of A, B and Delta are : {a} , {b} , {delta}");
}
}
}
XAML :
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-
compatibility/2006"
xmlns:local="clr-namespace:Lab1"
xmlns:Wpf="clr-
namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
x:Class="Lab1.MainWindow"
mc:Ignorable="d" FontSize="18" FontFamily="Segoe UI Light"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="1" Grid.Row="1"
Text="GRAPH" FontSize="36" Margin="0,0,0,10"/>
<TextBlock Grid.Column="1" Grid.Row="2"
Text="A" FontSize="30"/>
<TextBlock Grid.Column="1" Grid.Row="3"
Text="B" FontSize="30"/>
<TextBlock Grid.Column="1" Grid.Row="4"
Text="Δ" FontSize="30"/>
<TextBox x:Name="valueOfA" Grid.Column="2" Grid.Row="2"
Width="80" Height="30"/>
<TextBox x:Name="valueOfB" Grid.Column="2" Grid.Row="3"
Width="80" Height="30"/>
<TextBox x:Name="valueOfDelta" Grid.Column="2" Grid.Row="4"
Width="80" Height="30"/>
<Button x:Name="submitButton" Grid.Column="1" Grid.Row="5"
Width="150" Height="30" Content="START" Margin="10,10"
Click="submitButton_Click"/>
<Wpf:CartesianChart x:Name="cartesianChart" Grid.Column="4"
HorizontalAlignment="Left" Height="100" Margin="345.6,5.4,0,0"
Grid.Row="4" Grid.RowSpan="3" VerticalAlignment="Top" Width="100">
<Wpf:CartesianChart HorizontalAlignment="Left" Height="364"
Margin="-301,-136,-48,-128" VerticalAlignment="Top" Width="449"/>
</Wpf:CartesianChart>
</Grid>
</Window>
我已经得到了您要循环显示的值,这些值显示在图表中,我认为您希望通过阅读您的问题得到这些值。这是一个非常简单的答案,不会设置工具提示或轴的格式,我会让你去做,如果你遇到困难,你可以 post 另一个问题并标记我。
XAML :
<Window x:Class="WpfApp4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp4"
mc:Ignorable="d"
xmlns:Wpf="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
FontSize="18" FontFamily="Segoe UI Light"
Title="MainWindow" Height="450" Width="800" d:DataContext="{d:DesignInstance local:MainWindow, IsDesignTimeCreatable=False}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="1" Grid.Row="1" Text="GRAPH" FontSize="36" Margin="0,0,0,10"/>
<TextBlock Grid.Column="1" Grid.Row="2" Text="A" FontSize="30"/>
<TextBlock Grid.Column="1" Grid.Row="3" Text="B" FontSize="30"/>
<TextBlock Grid.Column="1" Grid.Row="4" Text="Δ" FontSize="30"/>
<TextBox x:Name="valueOfA" Grid.Column="2" Grid.Row="2" Width="80" Height="30"/>
<TextBox x:Name="valueOfB" Grid.Column="2" Grid.Row="3" Width="80" Height="30"/>
<TextBox x:Name="valueOfDelta" Grid.Column="2" Grid.Row="4" Width="80" Height="30"/>
<Button x:Name="submitButton" Grid.Column="1" Grid.Row="5" Width="150" Height="30" Content="START" Margin="10,10" Click="submitButton_Click"/>
<Wpf:CartesianChart x:Name="cartesianChart" Grid.Column="4" Grid.Row="0" Grid.RowSpan="7">
<Wpf:CartesianChart Series="{Binding SeriesCollection}" Grid.Column="4" Grid.Row="0" Grid.RowSpan="7"/>
</Wpf:CartesianChart>
</Grid>
后面的代码 :
public partial class MainWindow : Window, INotifyPropertyChanged
{
public SeriesCollection SeriesCollection { get; set; }
public MainWindow()
{
InitializeComponent();
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Title = "Series 1",
Values = new ChartValues<double>()
}
};
}
private void submitButton_Click(object sender, RoutedEventArgs e)
{
double a = Convert.ToDouble(valueOfA.Text);
double b = Convert.ToDouble(valueOfB.Text);
double delta = Convert.ToDouble(valueOfDelta.Text);
List<double> values = new List<double>();
for (double x = a; x <= b; x += delta)
{
values.Add(Math.Cos(x));
}
foreach(var value in values)
{
SeriesCollection[0].Values.Add(value);
}
OnPropertyChanged(new PropertyChangedEventArgs("SeriesCollection"));
DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChanged?.Invoke(this, e);
}
}
结果 :