【问题标题】:Interactive Data display in c#c#中的交互式数据显示
【发布时间】:2018-06-12 14:49:01
【问题描述】:

我尝试读取一些 csv 文件,然后将它们分成两列或列出并将它们绘制在数学图表中。 csv 文件包含两列,一列用于电压,另一列用于时间。我阅读了 csv 文件并使用了Interactive Data display

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Controls;


namespace TWord.Pages
{
    /// <summary>
    /// Interaction logic for Pico.xaml
    /// </summary>
    public partial class Pico : Page
    {
        /// <summary>
        ///  Members
        /// </summary>
        int numEvent;
        StreamReader myCSVStream;
        int currentSlide;
        ListOfList<double> AllEventsList;


        /// <summary>
        /// Constructor
        /// </summary>
        public Pico()
        {
            InitializeComponent();
        }

        #region Button Event
        /// <summary>
        /// Implement the button function to read the csv files.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCSV_Browser_Click(object sender, RoutedEventArgs e)
        {
            // Creates a null stream

            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Multiselect = openFileDialog.RestoreDirectory = true;

            // Initial directory to open to
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            // only open the csv files
            openFileDialog.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";

            // Applying filter index
            openFileDialog.FilterIndex = 2;

            // Create a new instance of list of lists and
            // run the ReadCSV for each of files.
            if (openFileDialog.ShowDialog() == true)
            {
                // Names of the files imported
                string[] filePath = openFileDialog.FileNames;

                // Path of the file
                numEvent = openFileDialog.FileNames.Length;

                AllEventsList = new ListOfList<double>();

                foreach (var address in filePath)
                {
                    ReadCSV(address);
                }
            }

            // only if thr browser had done the work
            //if (AllEventsList != null)
            //   DrawPlot(0);

        }


        #endregion

        #region Help Functions
        /// <summary>
        /// Help function to make graphs and reading the csv files.
        /// </summary>

        private void ReadCSV(string s)
        {

            string line;

            // new stream to read each line
            myCSVStream = new StreamReader(s);

            // one list for voltage
            List<double> voltage = new List<double>();

            // one list for time
            List<double> time = new List<double>();


            // reading whole csv file and split it into two columns
            while ((line = myCSVStream.ReadLine()) != null)
            {

                try
                {
                    string[] parm = line.Trim().Split(',');
                    voltage.Add(double.Parse(parm[0], CultureInfo.InvariantCulture));
                    time.Add(double.Parse(parm[1], CultureInfo.InvariantCulture));

                }
                catch { }
            }

            // add it to the list of lists.
            AllEventsList.Add(voltage);
            AllEventsList.Add(time);

            // Draw the first plot
            DrawPlot(0);
        }
        #endregion


        #region
        /// <summary>
        /// Drawing the plot for the CSVs
        /// </summary>
        private void DrawPlot(int i)
        {
            // Array for Voltage
            double[] voltage = AllEventsList[2 * i].ToArray();
            // Array for time
            double[] time = AllEventsList[2 * i + 1].ToArray();

            //plot to the linegraph
            linegraph.Plot(time,voltage,);
        }

        #endregion

    }
}

对此的 XAML 文件是根据:

<GroupBox Grid.Row="1" Grid.Column="0" Header="Sampled points" Style="{StaticResource GroupboxStyle}" FontFamily="{StaticResource LatoThin}" Foreground="{StaticResource TercoTextBrush}">
    <d3:Chart BottomTitle="Time" LeftTitle="voltage" Margin="-10" Style="{DynamicResource ChartStyle}">
        <d3:LineGraph x:Name="linegraph" Description="Simple linegraph" Stroke="Blue" StrokeThickness="2" Margin="-19,0,0,-23"/>
    </d3:Chart>
</GroupBox>

结果是线本身伸出图形网格。如下图:我一直坐在这里,尝试修改图表的样式,但没有成功。有人遇到过这个问题吗?我使用交互式数据显示的原因是它使用起来非常简单。只需要两个数组就完成了。如果您有任何更好的免费建议,我很乐意知道。但不是 Oxyplot 。我无法让它工作

【问题讨论】:

  • 如果您将边距设置为 0 会发生什么?
  • 问题不太可能出在图形上。更有可能是绘制您提供的数据,这意味着您没有提供正确的数据。设置断点,查看数组的内容,看看数据是否正常。我还注意到,您提供的代码不是生成图表的代码,因为您的代码有一个错误会阻止它编译。
  • 我建议您使用OxyPlot,这是较新的,功能丰富且易于使用。我在使用 OxyPlot 时没有遇到您在 IDD 中遇到的问题。为什么 OxyPlot 不适合您?
  • @Gordon 我想使用 OxyPlot,但它似乎过于拘谨。但我会更详细地看它
  • 我用了一点OxyPlot,如果你有什么问题我们可以尝试一起寻找答案,只需在WPF chat room 联系我,更方便聊天。

标签: c# wpf visual-studio csv dynamic-data-display


【解决方案1】:

LineGraph XAML 标记的边距正在移动。

边距指定为:

<object Margin="left,top,right,bottom"/>

因此,-19,0,0,-23 的边距指定向 xaml 元素移动 19 个单位,向下移动 23 个单位。你看到的是什么。

变化:

<d3:LineGraph x:Name="linegraph" Margin="-19,0,0,-23" Description="Simple linegraph" Stroke="Blue" StrokeThickness="2" />

收件人:

<d3:LineGraph x:Name="linegraph" Margin="0" Description="Simple linegraph" Stroke="Blue" StrokeThickness="2" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多