【问题标题】:Draw graphs - show only single points绘制图形 - 仅显示单个点
【发布时间】:2017-02-21 19:38:04
【问题描述】:

(用于 WPF 的 C# 应用程序)

我有一个问题,我必须“绘制”一个坐标系,并且只输入图中所示的坐标(不包括线条)。

我想使用库,因为我还必须画一个框架,我认为使用库会很容易。 所以我发现了 GnuPlot、Oxyplot 并自己绘制。不幸的是,GnuPlot 很愚蠢,因为它没有用于 C# 应用程序的库。 (或者如果你有,请告诉我)。因此,我使用了 OxyPlot,但不幸的是 OxyPlot 只显示坐标系。 现在我的问题。 用坐标绘制坐标系有什么更好的方法吗? 应满足以下要求:

  • 它应该是一个预览应用程序,也就是说,如果我改变大小它应该直接发生
  • 我想制作一个框架,所以它应该对我有所帮助
  • 应该有图书馆
  • 它应该用于 C# 应用程序
  • 我想成为 X、Y 坐标的第一个点标记,但后来应该是圆直径的圆
  • 后来作为位图

如上所述,我已经将它与 OxyPlot 一起使用,但不幸的是它不会绘制图形(我使用了示例文档)

也许您对 OxyPlot 有更好的想法/解决方案。

提前致谢,我对每一个回复都很满意。

XAML:

 <Window x:Class="TestOxyPlot.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:oxy="http://oxyplot.org/wpf"
            xmlns:local="clr-namespace:TestOxyPlot"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <oxy:Plot x:Name="oxyPlot" Title="{Binding Title}" Margin="207,53,0,0">
                <oxy:Plot.Series>
                    <oxy:LineSeries ItemsSource="{Binding Points}"/>
                </oxy:Plot.Series>
            </oxy:Plot>
            <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="44,64,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" MouseLeave="textBox_MouseLeave" TextChanged="textBox_TextChanged"/>
            <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="44,101,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="textBox1_TextChanged"/>
            <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="68,174,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
        </Grid>
    </Window>

  using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using OxyPlot;

    namespace TestOxyPlot
    {
        /// <summary>
        /// Interaktionslogik für MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.Title = "Example 2";
                this.Points = new List<DataPoint>
                    {
                                      new DataPoint(0, 4),
                                      new DataPoint(10, 13),
                                      new DataPoint(20, 15),
                                      new DataPoint(30, 16),
                                      new DataPoint(40, 12),
                                      new DataPoint(50, 12)
                                  };


            }
            public string Title { get; private set; }

            public IList<DataPoint> Points { get; private set; }

            private void textBox_MouseLeave(object sender, MouseEventArgs e)
            {


           }

            private void textBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                try
                {
                    oxyPlot.Width = Int32.Parse(textBox.Text);
                }
                catch (Exception error)
                {
                    MessageBox.Show("Message: " + error);
                }

            }

            private void button_Click(object sender, RoutedEventArgs e)
            {

            }

            private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
            {
                try
                {
                    oxyPlot.Width = Int32.Parse(textBox.Text);
                }
                catch (Exception error)
                {
                    MessageBox.Show("Message: " + error);
                }
            }
        }
    }

【问题讨论】:

    标签: c# wpf gnuplot coordinate-systems oxyplot


    【解决方案1】:

    添加此代码

     DataContext = this;
    

    在这行之后

     InitializeComponent();
    

    它会显示图表。此外,为了删除线并仅绘制标记,请使用类似 LineSeries:

    <oxy:LineSeries ItemsSource="{Binding Points}" LineStyle="None"  MarkerType="Circle" MarkerSize="5" MarkerFill="Black"/>
    

    编辑

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            double randomNumX;
            double randomNumY;
            int h = DateTime.Now.Hour;
            int m = DateTime.Now.Minute;
            int s = DateTime.Now.Second;
            String u = h.ToString() + m.ToString() + s.ToString();
            int iu = Int32.Parse(u);
            Random zufall = new Random(iu);
    
            Points = new List<DataPoint>();
            for (int i = 0; i < 10; i++)
            {
                randomNumX = zufall.NextDouble() * (10 - -10) + -10;
                randomNumY = zufall.NextDouble() * (10 - -10) + -10;
                Points.Add(new DataPoint(randomNumX, randomNumY));
            }
            ls.ItemsSource = Points;
        }
    

    <DockPanel>
        <Button DockPanel.Dock="Top" Click="Button_Click" Content="Click Me"/>
        <oxy:Plot x:Name="oxyPlot" Title="{Binding Title}">
            <oxy:Plot.Axes>
                <oxy:LinearAxis Position="Bottom" />
                <oxy:LinearAxis Position="Right" MinimumPadding="0.1" MaximumPadding="0.1"/>
            </oxy:Plot.Axes>
            <oxy:Plot.Series>
                <oxy:LineSeries x:Name="ls" ItemsSource="{Binding Points}" LineStyle="None"  MarkerType="Circle" MarkerSize="5" MarkerFill="Black"/>
            </oxy:Plot.Series>
        </oxy:Plot> 
    </DockPanel>
    

    顺便说一句,由于某种原因,使用 ObservationCollection 或 InvalidatePlot(true) 不起作用

    【讨论】:

    • 首先非常感谢您!它成功了,但不幸的是我想将代码插入一个按钮,当我点击一个按钮时,首先出现坐标+生成。但有些不对劲。你看到错误了吗?这是代码的链接:gist.github.com/anonymous/42525209b6eec77d1aea5006f443eea3
    • 添加数据后重置 LineSeries ItemsSource。我提供了我的工作示例。
    • 感谢您的大力支持。我希望你再问一个问题。我想在坐标系中留出一个边距。我怎么得到这个?我用“Margin”尝试过,不幸的是没有成功。使坐标不是直接在边缘,而是有一定的距离。 &lt;oxy:LineSeries x:Name="ls" ItemsSource="{Binding Points}" LineStyle="None" MarkerType="Square" MarkerSize="5" MarkerFill="Black" Margin ="10 10 10 10"/&gt;
    • 不客气。见编辑。添加轴并设置MinimumPaddingMaximumPadding
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 2017-04-16
    • 1970-01-01
    • 2018-07-21
    相关资源
    最近更新 更多