1

I'm creating in Visual Basic an application in winforms with a Chart control that displays the values taken from different CSV files. After having selected the files, the graph appears as in the figure:

My Chart

I've noticed that blue vertical lines are created when a value in the series is missing. How can I fix?

This is my code:

Dim openFileDialog1 As New OpenFileDialog With {
            .Filter = "CSV files (*.csv)|*.csv",
            .FilterIndex = 1,
            .Multiselect = True,
            .Title = "Select only CSV files"
        }

        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            Chart1.Visible = True
            If Chart1.Series.Count > 0 Then
                Chart1.Series.Clear()
            End If

            Dim fileCounter As Integer = 0

            For Each file As String In openFileDialog1.FileNames
                fileCounter += 1
                Dim lineCounter As Integer = 0
                Dim sr As New StreamReader(file)
                Dim currentLine As String

                While sr.Peek() >= 0
                    currentLine = sr.ReadLine()
                    lineCounter += 1

                    If lineCounter = 1 Then
                        Dim series As New Series With {
                            .Name = Path.GetFileNameWithoutExtension(file),
                            .ChartType = SeriesChartType.Spline
                        }
                        Chart1.Series.Add(series)
                    Else
                        Dim values() As String = currentLine.Split(",")
                        Dim timers As String = values(0)
                        Dim speed As Double = Double.Parse(values(1))
                        Chart1.Series(fileCounter - 1).Points.AddXY(timers, speed)
                    End If
                End While
                sr.Close()
            Next

            Chart1.Series(0).ChartType = SeriesChartType.Spline
        End If
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
smal
  • 15
  • 5
  • If a value is missing, then don't add that point to the series. Without a (small) amount of sample data, I can't suggest a method for that ;) – Andrew Morton Jan 30 '23 at 18:26
  • These are the first 10 lines of a CSV file: 00:00:00,3.72 00:00:01,1.11 00:00:01,1.13 00:00:02,0.937 00:00:03,0.892 00:00:03,0.864 00:00:04,0.88 00:00:04,0.869 00:00:05,0.82 00:00:05,0.86 – smal Jan 30 '23 at 18:42
  • It looks like there is more than one point with the same x-value, so you will get vertical(ish) lines for those. Maybe the LineTension parameter mentioned in [Spline chart smooth corners](https://stackoverflow.com/a/38080546/1115360) would be helpful. Or do you have enough points that a spline is not really needed, and a line chart would be better? – Andrew Morton Jan 30 '23 at 18:51
  • even with a Line type graph it gives me the same problem – smal Jan 30 '23 at 19:27
  • Can you give an example of where a value is missing, please? – Andrew Morton Jan 30 '23 at 19:30

1 Answers1

0

The numbers in the series are decimal, but in the American style, i.e. floating point with . While in Italy we are in the habit of using the ,

I fixed it by changing the culture with the code:

Threading.Thread.CurrentThread.CurrentCulture = New CultureInfo("us-US")
Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator = "."
smal
  • 15
  • 5