繁体   English   中英

使用 UWP 的 WinRT Xaml 图表中的间隔

[英]Interval in WinRT Xaml Chart with UWP

这个问题并不新鲜:带有 LinearAxis 的图表仅使用整数。 很多答案都建议使用间隔,但如果我有最小值 = 1 和最大值 = 100,且间隔 = 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;
}

Xml 页面:

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

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

#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。

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

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;
    }

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM