Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF:如何删除我的图表在多次点后可见的点_Wpf_Charts - Fatal编程技术网

WPF:如何删除我的图表在多次点后可见的点

WPF:如何删除我的图表在多次点后可见的点,wpf,charts,Wpf,Charts,我每隔一秒钟通过计时器滴答声事件填充我的图表,这是开始: 几分钟后,我有很多点,我几乎看不到我的图表笔划线,所以在代表1分钟的60个点之后,我删除了第一个点,所以在这种情况下,我的图表将只有60个点,但我几分钟后看到的是: 在我的一个项目中,我有一些与此相关的代码: <Window x:Class="WPFToolkitProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentat

我每隔一秒钟通过
计时器滴答声事件
填充我的
图表
,这是开始:

几分钟后,我有很多点,我几乎看不到我的
图表笔划线
,所以在代表1分钟的60个
点之后,我删除了第一个
,所以在这种情况下,我的图表将只有60个
,但我几分钟后看到的是:


在我的一个项目中,我有一些与此相关的代码:

<Window x:Class="WPFToolkitProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    Title="MainWindow" Height="350" Width="525" Loaded="MainWindow_OnLoaded">
<Grid>
    <Grid.Resources>
        <Style x:Key="EmptyPointsStyle" TargetType="{x:Type chartingToolkit:LineDataPoint}">
            <Setter Property="Template" Value="{x:Null}"/>
        </Style>

        <Style x:Key="aa" TargetType="{x:Type chartingToolkit:LineSeries}">
            <Setter Property="DataPointStyle" Value="{StaticResource EmptyPointsStyle}"/>
            <Setter Property="PolylineStyle">
                <Setter.Value>
                    <Style TargetType="{x:Type Polyline}">
                        <Setter Property="StrokeThickness" Value="2"/>
                        <Setter Property="StrokeMiterLimit" Value="1"/>
                    </Style>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type chartingToolkit:LineSeries}">
                        <Canvas x:Name="PlotArea">
                            <Polyline Stroke="{TemplateBinding BorderBrush}" Style="{TemplateBinding PolylineStyle}" Points="{TemplateBinding Points}"/>
                        </Canvas>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <chartingToolkit:Chart  Name="lineChart" Title="Line Series Demo" VerticalAlignment="Top" Margin="50" Height="254">
        <chartingToolkit:LineSeries  DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding points}" IsSelectionEnabled="True"/>
    </chartingToolkit:Chart>
</Grid>

这就是代码隐藏:我正在启动一个新线程,以避免阻塞UI,并在Dispatcher的帮助下在有界集合上执行操作时返回UI

public partial class MainWindow : Window
{
    public ObservableCollection<KeyValuePair<int, int>> points { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        points = new ObservableCollection<KeyValuePair<int, int>>();
        points.Add(new KeyValuePair<int, int>(1, 60));
        points.Add(new KeyValuePair<int, int>(2, 20));
        points.Add(new KeyValuePair<int, int>(3, 50));
        points.Add(new KeyValuePair<int, int>(4, 30));
        points.Add(new KeyValuePair<int, int>(5, 40));
        points.Add(new KeyValuePair<int, int>(6, 200));
        points.Add(new KeyValuePair<int, int>(7, 1));
        points.Add(new KeyValuePair<int, int>(8, 450));
        points.Add(new KeyValuePair<int, int>(4, 30));
        points.Add(new KeyValuePair<int, int>(5, 40));

        this.DataContext = this;
    }


    private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
    {
        #region Adding points at runtime - test

        Task.Run(() =>
        {

            System.Threading.Thread.Sleep(5000);

            int pos = 300;
            var r = new Random();

            for (int i = 0; i < 1000; i++)
            {
                Dispatcher.Invoke(() =>
                {
                    points.Add(new KeyValuePair<int, int>(i, r.Next(1000)));
                    points.RemoveAt(0);
                });
                System.Threading.Thread.Sleep((100));
            }
        });

        #endregion Adding points at runtime - test
    }
}
公共部分类主窗口:窗口
{
公共可观测集合点{get;set;}
公共主窗口()
{
初始化组件();
点=新的可观察集合();
添加(新的KeyValuePair(1,60));
添加(新的KeyValuePair(2,20));
添加(新的KeyValuePair(3,50));
添加(新的KeyValuePair(4,30));
添加(新的KeyValuePair(5,40));
添加(新的KeyValuePair(6200));
添加(新的KeyValuePair(7,1));
添加(新的KeyValuePair(8450));
添加(新的KeyValuePair(4,30));
添加(新的KeyValuePair(5,40));
this.DataContext=this;
}
私有void主窗口\u已加载(对象发送方,路由目标)
{
#运行时添加点的区域-测试
Task.Run(()=>
{
系统线程线程睡眠(5000);
int pos=300;
var r=新的随机变量();
对于(int i=0;i<1000;i++)
{
Dispatcher.Invoke(()=>
{
添加(新的KeyValuePair(i,r.Next(1000));
点。移除(0);
});
系统线程线程睡眠((100));
}
});
#endregion在运行时添加点-测试
}
}

所以,每次我在这里添加一个点,我都会删除第一个点。它可以正常工作,但您可以选择另一种方法,即在每次添加时不删除第一种方法。取决于您的需要。

您还应该发布将图表绑定到积分集合的代码,这可能就是问题所在。
<Window x:Class="WPFToolkitProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    Title="MainWindow" Height="350" Width="525" Loaded="MainWindow_OnLoaded">
<Grid>
    <Grid.Resources>
        <Style x:Key="EmptyPointsStyle" TargetType="{x:Type chartingToolkit:LineDataPoint}">
            <Setter Property="Template" Value="{x:Null}"/>
        </Style>

        <Style x:Key="aa" TargetType="{x:Type chartingToolkit:LineSeries}">
            <Setter Property="DataPointStyle" Value="{StaticResource EmptyPointsStyle}"/>
            <Setter Property="PolylineStyle">
                <Setter.Value>
                    <Style TargetType="{x:Type Polyline}">
                        <Setter Property="StrokeThickness" Value="2"/>
                        <Setter Property="StrokeMiterLimit" Value="1"/>
                    </Style>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type chartingToolkit:LineSeries}">
                        <Canvas x:Name="PlotArea">
                            <Polyline Stroke="{TemplateBinding BorderBrush}" Style="{TemplateBinding PolylineStyle}" Points="{TemplateBinding Points}"/>
                        </Canvas>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <chartingToolkit:Chart  Name="lineChart" Title="Line Series Demo" VerticalAlignment="Top" Margin="50" Height="254">
        <chartingToolkit:LineSeries  DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding points}" IsSelectionEnabled="True"/>
    </chartingToolkit:Chart>
</Grid>
public partial class MainWindow : Window
{
    public ObservableCollection<KeyValuePair<int, int>> points { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        points = new ObservableCollection<KeyValuePair<int, int>>();
        points.Add(new KeyValuePair<int, int>(1, 60));
        points.Add(new KeyValuePair<int, int>(2, 20));
        points.Add(new KeyValuePair<int, int>(3, 50));
        points.Add(new KeyValuePair<int, int>(4, 30));
        points.Add(new KeyValuePair<int, int>(5, 40));
        points.Add(new KeyValuePair<int, int>(6, 200));
        points.Add(new KeyValuePair<int, int>(7, 1));
        points.Add(new KeyValuePair<int, int>(8, 450));
        points.Add(new KeyValuePair<int, int>(4, 30));
        points.Add(new KeyValuePair<int, int>(5, 40));

        this.DataContext = this;
    }


    private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
    {
        #region Adding points at runtime - test

        Task.Run(() =>
        {

            System.Threading.Thread.Sleep(5000);

            int pos = 300;
            var r = new Random();

            for (int i = 0; i < 1000; i++)
            {
                Dispatcher.Invoke(() =>
                {
                    points.Add(new KeyValuePair<int, int>(i, r.Next(1000)));
                    points.RemoveAt(0);
                });
                System.Threading.Thread.Sleep((100));
            }
        });

        #endregion Adding points at runtime - test
    }
}