none
Chart Area Axes - A logarithmic scale cannot be used for this axis RRS feed

  • Question

  • Hi,

    Here is my source code:

    Imports System.IO
    Public Class Form17
        Dim x As Integer
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim line As String
            Dim s(2) As String
            Dim Title1 As String
            Dim Title2 As String
            Dim p As Long
            OpenFileDialog1.Filter = "Text Files|*.txt"
            OpenFileDialog1.ShowDialog()
            'Chart1.Series.Clear()
            Title1 = TIL("Main Oscillator")
            Title2 = TIL("TF Oscillator")
            x = 0
            chap(Title1)
            x = 1
            chap(Title2)
            ' Try
            Using reader As StreamReader = New StreamReader(OpenFileDialog1.FileName)
                line = reader.ReadLine
                p = 1
                Do While (Not line Is Nothing)
                    p = p + 1
                    If p <= 5 Then Exit Do
                    s = line.Split(":")
                    '  MsgBox(s(0).ToString & ">" & s(1).ToString)
                    Chart1.Series(Title1).Points.AddXY(s(0), s(1))
                    Chart1.Series(Title2).Points.AddXY(s(0) + 10, s(1) + 10)
                    'Chart1.Series(Title2).Points.AddXY(h, XAlglib.besseljn(i, h))
                    line = reader.ReadLine
                Loop
            End Using
    
    
            With Chart1
                ' .ChartAreas(0).AxisX.LabelStyle.Interval = 10
                '.ChartAreas(0).AxisY.Maximum = 1.5
                '.ChartAreas(0).AxisY.Minimum = -0.5
                '.ChartAreas(0).AxisX.Maximum = 4
                '.ChartAreas(0).AxisX.Minimum = 0
                .ChartAreas(0).AxisX.LogarithmBase = 10
                .ChartAreas(0).AxisX.IsLogarithmic = True
                '.ChartAreas(0).AxisY.Interval = 10
                '  .ChartAreas(0).AxisX.Interval = 10
            End With
    
    
            With Chart1
                .Legends(0).Position.X = 63
                .Legends(0).Position.Y = 7
                .Legends(0).Position.Width = 30
                .Legends(0).Position.Height = 28
            End With
            'Catch ex As Exception
            '    MsgBox(ex.Message)
            '    Exit Sub
            'End Try
        End Sub
    
        Private Function TIL(ByVal Name As String)
            Return Name
            'Return "Name:" & "db"
        End Function
    
        Private Sub Chap(ByVal N As String)
            Dim Title As String
            Dim x1 As Integer
            Dim y As Integer
            Dim font As String
            x1 = 9
            y = 9
            font = "Cambria"
            Title = TIL(N)
            Try
                Chart1.Series.Add(Title)
    
            Catch ex As Exception
                MsgBox(ex.Message)
                Exit Sub
            End Try
    
    
            Chart1.ChartAreas(0).AxisX.Title = "X"
            Chart1.ChartAreas(0).AxisX.TitleFont = New Font(font, 10, FontStyle.Regular)
    
            Chart1.Series(Title).ChartType = DataVisualization.Charting.SeriesChartType.Line
            Chart1.Series(Title).Color = Color.Black
            Chart1.Series(Title).BorderWidth = 2
            Select Case x
                Case 0 : Chart1.Series(Title).BorderDashStyle = DataVisualization.Charting.ChartDashStyle.Solid
                Case 1 : Chart1.Series(Title).BorderDashStyle = DataVisualization.Charting.ChartDashStyle.Dash
                Case 2 : Chart1.Series(Title).BorderDashStyle = DataVisualization.Charting.ChartDashStyle.DashDot
                Case 3 : Chart1.Series(Title).BorderDashStyle = DataVisualization.Charting.ChartDashStyle.DashDotDot
                Case 4 : Chart1.Series(Title).BorderDashStyle = DataVisualization.Charting.ChartDashStyle.Dot : x = 0
            End Select
            Chart1.ChartAreas(0).AxisX.MajorGrid.LineDashStyle = DataVisualization.Charting.ChartDashStyle.Dash
            Chart1.ChartAreas(0).AxisY.MajorGrid.LineDashStyle = DataVisualization.Charting.ChartDashStyle.Dash
    
            x = x + 1
        End Sub
    
        Private Sub Form17_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            Form1.Show()
    
        End Sub
    
        Private Sub Form17_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Chart1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Chart1.Click
    
        End Sub
    End Class

    My input text file contains some data like these:

    1.2:-21
    1.25:-21.25
    1.5:-21.5
    1.75:-21.75
    2:-22
    2.25:-22.25
    2.5:-22.5

    as you can see the minimum value of s(0) is equal to 1.2(greater than zeor) , but I wasn't successful to  have a logarithm base x axis when PC executes

    .ChartAreas(0).AxisX.IsLogarithmic = True

    I will be grateful if you could help me.

    Monday, June 26, 2017 1:04 PM

Answers

  • That's probably due to you adding Strings to  the chart's Point collection.

    Change:

         Chart1.Series(Title1).Points.AddXY(s(0), s(1))

    to:

         Chart1.Series(Title1).Points.AddXY(CDbl(s(0)), CDbl(s(1)))

    • Marked as answer by Parisa_1 Tuesday, June 27, 2017 10:37 AM
    Tuesday, June 27, 2017 9:38 AM

All replies

  • Hi Parisa_1,

    I try your code, and I can reproduce your issue.

    Using reader As StreamReader = New StreamReader(OpenFileDialog1.FileName)
                    line = reader.ReadLine
                    p = 1
                    Do While (Not line Is Nothing)
                        p = p + 1
                        If p <= 5 Then
                            Exit Do
                        End If
                        s = line.Split(":")
                        '  MsgBox(s(0).ToString & ">" & s(1).ToString)
                        Chart1.Series(Title1).Points.AddXY(s(0), s(1))
                        Chart1.Series(Title2).Points.AddXY(s(0) + 10, s(1) + 10)
                        'Chart1.Series(Title2).Points.AddXY(h, XAlglib.besseljn(i, h))
                        line = reader.ReadLine
                    Loop
                End Using
                           

    Why you use "If p <= 5 Then", it leads to chart having no data.

    This exception is raised if you do not provide X xalues for the data points or Series.IsXValueIndexed is set tot True. In both scenariou logarithmic scale is not supported.

    Best Regards,

    Cherry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 27, 2017 3:25 AM
    Moderator
  • Dear Cherry Bu

    Thanks a zillion for your explanation and taking your time.

    I have tested different states and I have forgotten to remove these lines

                        p = p + 1
                        If p <= 5 Then
                            Exit Do
                        End If

    I didn't do anything special but I've got this new error:

    Axis object - Negative or zero values cannot be plotted correctly on logarithmic charts. Only positive values can be interpreted on a logarithmic scale.
    Parameter name: minimumValue

    you can see all my data in this text file( I don't have any negative number for x axis)

    Thanks

    Tuesday, June 27, 2017 7:34 AM
  • Change your X Axis Minimum value to a value greater than 0. eg:

        .ChartAreas(0).AxisX.Minimum = 1

    Tuesday, June 27, 2017 9:01 AM
  • Dear "S P C"

    Thanks for your tip and your contribution

    I added Chart1.ChartAreas(0).AxisX.Minimum = 1 to chap function but it didn't work again and when I disable these lines

      'chap(Title2)
      'Chart1.Series(Title2).Points.AddXY(s(0) + 10, s(1) + 10)

    I got  "Chart Area Axes - A logarithmic scale cannot be used for this axis" error.
    Tuesday, June 27, 2017 9:18 AM
  • That's probably due to you adding Strings to  the chart's Point collection.

    Change:

         Chart1.Series(Title1).Points.AddXY(s(0), s(1))

    to:

         Chart1.Series(Title1).Points.AddXY(CDbl(s(0)), CDbl(s(1)))

    • Marked as answer by Parisa_1 Tuesday, June 27, 2017 10:37 AM
    Tuesday, June 27, 2017 9:38 AM
  • Thank a zillion S P C , I have forgotten the data type of my variables.

    Thank you so much

    Tuesday, June 27, 2017 10:38 AM