Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
235
Binding datatable to NumericXAxis
posted

Hi.  I'm in the process of converting WPF XamCharts to XamDataCharts.  I'm tried to find an example of binding a datatable to ScatterLineSeries - I Found an example using a CatergoryXAxis but I'm unable to get more than the minimum Y Axis label to show up and none of the X Axis labels.  The graph itself is working great, I just don't have labels.  I have tried letting letting it autorange and have tried various setting for labels and MemberPath, but nothing seems to produce labels.   Here is the code I have:

                                     <ig:XamDataChart x:Name="TrainingStatGraph"

                                                     Grid.ColumnSpan="2"

                                                     Margin="2,2,-2,-2"

                                                     Grid.RowSpan="2" >

                                        <ig:XamDataChart.BindingGroup>

                                            <BindingGroup/>

                                        </ig:XamDataChart.BindingGroup>

                                        <ig:XamDataChart.Axes>

                                            <ig:NumericXAxis

                                                x:Name="xmXAxis"

                                                DataContext="{Binding}"

                                                Label="{}{Label}}">

                                                <ig:NumericXAxis.LabelSettings >

                                                    <ig:AxisLabelSettings Location="OutsideBottom" Extent="35" />

                                                </ig:NumericXAxis.LabelSettings>

                                            </ig:NumericXAxis>

                                            <ig:NumericYAxis x:Name="xmYAxis">

                                                <ig:NumericYAxis.LabelSettings>

                                                    <ig:AxisLabelSettings Extent="50" />   

                                                </ig:NumericYAxis.LabelSettings>                                                    

                                            </ig:NumericYAxis>

                                        </ig:XamDataChart.Axes>

                                        <ig:XamDataChart.Series>

                                            <ig:ScatterLineSeries 

                                                       ItemsSource="{Binding}"

                                                MarkerType="None"

                                                XMemberPath="xVal"

                                                YMemberPath="yVal"

                                                       XAxis="{Binding ElementName=xmXAxis}"

                                                       YAxis="{Binding ElementName=xmYAxis}"/>

                                        </ig:XamDataChart.Series>

                                    </ig:XamDataChart>

 

    Public Sub BuildGraph(ByRef DataChart As XamDataChart, ByRef xmXAxis As Infragistics.Controls.Charts.NumericXAxis, ByRef xmYAxis As Infragistics.Controls.Charts.NumericYAxis)

        Dim CurCulture As String

        CurCulture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString

 

        Try

            If (mConn.State = ConnectionState.Closed) Then

                mConn.Open()

            End If

 

            Dim customCulture As New CultureInfo("en-US")

            System.Threading.Thread.CurrentThread.CurrentCulture = customCulture

 

            Dim adapter As New OleDbDataAdapter("SELECT Pass,MAPE FROM MAPE where Prof_code=" & mCurrentProfileCode, mConn)

            Dim ds As New DataSet()

 

            adapter.Fill(ds, "MAPE")

            Dim data = From row In ds.Tables("MAPE").Rows.OfType(Of DataRow)() Select New DataItem() With {.xVal = CDbl(row("Pass")), .yVal = CDbl(row("MAPE"))}

             DataChart.DataContext = data

 

            ' there is only one table here so we can use the number of rows in the dataset as the number of passes

            mPasses = ds.Tables(0).Rows.Count

            If (mPasses = 0) Then

                Exit Sub

            End If

 

            mFinalMAPE = ds.Tables(0).Rows(ds.Tables(0).Rows.Count - 1)("MAPE")

 

            mBestMAPE = ds.Tables(0).Compute("MIN(MAPE)", "")

            mMaxMAPE = ds.Tables(0).Compute("MAX(MAPE)", "")

 

            With xmXAxis

                .DataContext = data 'ds.Tables(0)

                .Interval = FormatNumber((mMaxMAPE - mBestMAPE) / 4, 2)

                '.Label = "xVal"

                .MinimumValue = 0

                .Interval = 5

                .MaximumValue = mPasses

 

            End With

            With xmYAxis.LabelSettings

                .Location = AxisLabelsLocation.OutsideBottom

                .Extent = 25

                .VerticalAlignment = VerticalAlignment.Bottom 

            End With

 

            With xmYAxis

                .MinimumValue = FormatNumber(mBestMAPE - (mBestMAPE * 0.1), 2)

                .MaximumValue = FormatNumber(mMaxMAPE + (mMaxMAPE * 0.1), 2)

                .Interval = FormatNumber((mMaxMAPE - mBestMAPE) / 4, 2)

                If .Interval = 0 Then .Interval = 0.1

                '.Label = "yVal"

                '.LabelSettings.Location = AxisLabelsLocation.OutsideLeft

                .LabelSettings.Extent = 50

            End With

 

            mBestPass = ds.Tables(0).Select("MAPE=" & mBestMAPE.ToString)(0)("Pass")

 

            Dim series As New ScatterLineSeries()

            series.XAxis = xmXAxis

            series.YAxis = xmYAxis

            series.MarkerType = Charts.MarkerType.None

            series.ItemsSource = data 'ds.Tables(0)

            series.XMemberPath = "xVal"

            series.YMemberPath = "yVal"

 

            If DataChart.Series.Count >= 1 Then DataChart.Series.Remove(DataChart.Series(0))

            DataChart.Series.Add(series)

            mConn.Close()

        Catch ex As Exception

            LogHelper.LogError(Me.GetType(), "; " & ex.Message, ex)

        Finally

            Dim retCulture As New CultureInfo(CurCulture)

            System.Threading.Thread.CurrentThread.CurrentCulture = retCulture

        End Try

    End Sub

Public Class DataItem

    Public Property xVal() As Double

        Get

            Return m_xAxis

        End Get

        Set(value As Double)

            m_xAxis = value

        End Set

    End Property

    Private m_xAxis As Double

    Public Property yVal() As Double

        Get

            Return m_YAxis

        End Get

        Set(value As Double)

            m_YAxis = value

        End Set

    End Property

    Private m_YAxis As Double

End Class

 

 Thanks in advance for you help!

Parents
  • 30945
    Verified Answer
    Offline posted

    Hello John,

     

    Thank you for your email. I have been looking into it and the code snippets that you have provided and if I understand correctly, the issue that you are having is that the labels on the X axis are not shown. If this is correct, this behavior is caused by setting the Label property of the X axis.  When using CategoryXAxis, you can set the label as you have posted and the labels will be the property corresponding to the entered string. For example for the CatecoryXAxis, when you set the label to “{}{Label}”, the axis will look for property named Label on the objects in the ItemsSource of the axis and will display the value of the property as labels.

     

    When you are using ScatterLineSeries and NumericXAxis, you can leave the Label property unset and the labels will be shown. You can use the Label property to format the strings shown in the labels. For example, if you wish to show two digits after the decimal point and you wish the percent sign to be shown after the value, you can set the label as follow: Label=”{}{0:N2}%”, where the value after “0:” is the format you wish, which in this case is N2 (two digits after the decimal point).

     

    I have created a sample application for you, based on your code snippet, that shows how you can implement the approach I have described.

     

    Please let me know if you need any further assistance on the matter.

     

    Sincerely,

    Krasimir

    Developer Support Engineer

    Infragistics

    www.infragistics.com/support

    ScatterLineAndDatatable_VB.zip
Reply Children