Search code examples
c#chartswinrt-xaml-toolkit

WinRT Xaml Toolkit Chart - Is there a maximum number of data points?


I am attempting to create a simple chart in a Windows 10 UWP app.

This screenshot shows my problem.

About half of my data points have been truncated :(

Does anyone have any idea why this might be happening?

Here's the code I used to generate the chart (I prefer using C# over XAML):

Chart ThisIsATestChart = new Chart
    {
        Title = "I made this chart in C#",
        HorizontalAlignment = HorizontalAlignment.Left,
        VerticalAlignment = VerticalAlignment.Top,
        Width = 800,
        Height = 600
    };

ThisIsATestChart.Margin = new Thickness { Left = 150, Top = 100 };

ThisIsATestChart.Series.Add(new LineSeries
    {
        Title = "Squiggly Line",
        IndependentValuePath = "xValue",
        DependentValuePath = "yValue",
        ItemsSource = ChartData,
        IndependentAxis = new LinearAxis
        {
            Minimum = 0,
            Maximum = yValueArray.Length,
            Orientation = AxisOrientation.X,
            Interval = 50
        }
    });

MyGrid.Children.Add(ThisIsATestChart);

And the following code provided the data for the chart:

byte[] yValueArray = MethodThatReturnsAnArrayOfBytes();

Collection<XYvalues> ChartData = new Collection<XYvalues>();

foreach (int index in yValueArray)
    ChartData.Add(new XYValues
    {
        xValue = index,
        yValue = yValueArray[index]
    });

In this case, ChartData had 528 objects in its collection. Yet only ~240 of them are displayed on the chart.

Thanks in advance to anyone who can help me understand!

Also possibly relevant:

public class XYValues
{
    public int xValue { get; set; }
    public byte yValue { get; set; }
}

System configuration:

Windows 10 Education, Version 1709, Build 16299.64

Visual Studio 2017, Version 15.4.4 (.NET Framework Version 4.7.02556)


Solution

  • You made a terrible mistake. This index is not an index of the array, it is the actual value of the elements in the array!

    foreach (int index in yValueArray) 
        ChartData.Add(new XYValues
        {
            xValue = index,
            yValue = yValueArray[index]
         });
    

    Use the old school for loop.

    for (int index = 0; index < yValueArray.Length; index++)