0

I have some csv files that I try to read and then divide them in two columns or list and plot them in a mathematical chart. The csv files contains two columns, one for voltage and one for time. I read the csv files and use 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

    }
}

The XAML file to this is according to :

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

The result is that the line itself sticks out of the graph grid. as image below: Ive been sitting here and tried to modify the style of the graph with no luck. Anyone had this problem? The reason I use Interactive Data display is that it is very simple to use. Just needs two arrays and you are done. If you have any better suggestions that is Free of charge I would be happy to know about it. but not Oxyplot . I couldn't make it work

enter image description here

riQQ
  • 9,878
  • 7
  • 49
  • 66
Payam30
  • 689
  • 1
  • 5
  • 20
  • What happens if you set your margins to 0? – NetMage Jan 02 '18 at 22:35
  • It is unlikely that the problem is with the graphing. More likely it is graphing the data you provide, which means you're not providing the correct data. Set breakpoints, look at the contents of the arrays, see if the data look okay. I also note that the code you provide is not the one that produces the graph since your code has an error that would prevent it from compiling. – Ray Fischer Jan 02 '18 at 23:29
  • 1
    I suggest you to use [OxyPlot](http://www.oxyplot.org/), this is newer, feature rich and easy to work with. I haven't encountered the problem you have in IDD while I'm using OxyPlot. Why OxyPlot is a no go for you? – Gordon Jan 03 '18 at 01:35
  • @Gordon I would like to use OxyPlot but it seems too camplicated. But I will look at it more in detail – Payam30 Jan 03 '18 at 10:13
  • I used OxyPlot a bit, if you have some Q we can try to find answer together, just ping me in the [WPF chat room](https://chat.stackoverflow.com/rooms/18165/wpf) for more convenience chatting. – FoggyFinder Jan 03 '18 at 12:51
  • @Payam30 Trust me, OxyPlot is not that complicated. You can read a simple tutorial [here](http://docs.oxyplot.org/en/latest/getting-started/hello-wpf-xaml.html) – Gordon Jan 04 '18 at 06:21

1 Answers1

2

Your margin for your LineGraph XAML markup is shifting the line.

Margin is specified as:

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

So a margin of -19,0,0,-23 specifies a shift to a xaml element 19 units to the left and 23 units down. Which is what you are seeing.

Change:

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

To:

<d3:LineGraph x:Name="linegraph" Margin="0" Description="Simple linegraph" Stroke="Blue" StrokeThickness="2" />
Ehz
  • 2,027
  • 1
  • 12
  • 11