带有UWP的WinRT Xaml图表中的间隔

时间:2017-02-04 11:28:23

标签: c# xaml charts uwp winrt-xaml-toolkit

这个问题并不新鲜:使用LinearAxis的图表只使用整数。很多答案建议使用Interval,但如果我有min值= 1且最大值= 100,Interval = 1,则轴将有100个数字,数字太多。我想要的是LinearAxis的自动间隔计算,稍作修改。所以这是Andrew Barrett找到的解决方案:

public class LineSeriesAxis : LinearAxis
{
    protected override double CalculateActualInterval(Size availableSize)
    {
        var result = base.CalculateActualInterval(availableSize);
        return (result < 1.0) ? 1.0 : result;
    }
}

在我使用他的代码应用我的示例应用程序后:

class Report
{
    public string months { get; set; }
    public int countlent { get; set; }
}

public MainPage()
{
    this.InitializeComponent();
    LoadChartContents();
}

private void LoadChartContents()
{
    List<Report> lstSource = new List<Report>();
    lstSource.Add(new Report() { months = "1", countlent = 10 });
    lstSource.Add(new Report() { months = "2", countlent = 15 });
    lstSource.Add(new Report() { months = "3", countlent = 20 });
    lstSource.Add(new Report() { months = "4", countlent = 10 });
    lstSource.Add(new Report() { months = "5", countlent = 13 });
    lstSource.Add(new Report() { months = "6", countlent = 18 });
    lstSource.Add(new Report() { months = "7", countlent = 33 });
    lstSource.Add(new Report() { months = "8", countlent = 41 });
    lstSource.Add(new Report() { months = "9", countlent = 31 });
    lstSource.Add(new Report() { months = "10", countlent = 21 });
    lstSource.Add(new Report() { months = "11", countlent = 12 });
    lstSource.Add(new Report() { months = "12", countlent = 37 });
    (LineChart.Series[0] as LineSeries).DependentRangeAxis = new LineSeriesAxis();
    (LineChart.Series[0] as LineSeries).ItemsSource = lstSource;
}

Xaml页面:

<Chart:Chart x:Name="LineChart" HorizontalAlignment="Center" Margin="5" Width="500">
     <Chart:LineSeries Title="Chart Name" IndependentValuePath="months" DependentValuePath="countlent" />
</Chart:Chart>

每次我运行或调试应用程序时,它都会停止并显示

的App.g.i.cs页面
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
        UnhandledException += (sender, e) =>
        {
            if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
        };
#endif

我使用他的代码吗?我正在使用UWP和WinRTXamlToolkit.Controls.DataVisualization.Charting Toolkit。

1 个答案:

答案 0 :(得分:1)

您在代码隐藏中分配轴的方式有点“冒险”。试试这个:

<强> XAML:

    <Charting:Chart x:Name="LineChart" HorizontalAlignment="Center" Margin="5" Width="500">
        <Charting:Chart.Axes>
            <local:LineSeriesAxis Orientation="Y"></local:LineSeriesAxis>
        </Charting:Chart.Axes>
        <Charting:LineSeries Title="Chart Name" 
                             IndependentValuePath="months" 
                             DependentValuePath="countlent" 
                             ItemsSource="{Binding}" />
    </Charting:Chart>

<强> CS:

    private void LoadChartContents()
    {
        List<Report> lstSource = new List<Report>();
        lstSource.Add(new Report() { months = "1", countlent = 10 });
        lstSource.Add(new Report() { months = "2", countlent = 15 });
        lstSource.Add(new Report() { months = "3", countlent = 20 });
        lstSource.Add(new Report() { months = "4", countlent = 10 });
        lstSource.Add(new Report() { months = "5", countlent = 13 });
        lstSource.Add(new Report() { months = "6", countlent = 18 });
        lstSource.Add(new Report() { months = "7", countlent = 33 });
        lstSource.Add(new Report() { months = "8", countlent = 41 });
        lstSource.Add(new Report() { months = "9", countlent = 31 });
        lstSource.Add(new Report() { months = "10", countlent = 21 });
        lstSource.Add(new Report() { months = "11", countlent = 12 });
        lstSource.Add(new Report() { months = "12", countlent = 37 });

        DataContext = lstSource;
    }

enter image description here