c#中的交互式数据显示

时间:2018-01-02 22:29:48

标签: c# wpf visual-studio csv

我有一些我尝试阅读的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。我无法使其发挥作用

enter image description here

1 个答案:

答案 0 :(得分:1)

您的LineGraph XAML标记的保证金正在改变这一行。

保证金指定为:

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

因此-19,0,0的边距,-23指定向左移动19个单位并向下移动23个单位的xaml元素。这就是你所看到的。

变化:

<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" />