簡體   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