使用 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;
}
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 工具包。
您在 code-behind 中分配轴的方式有点 "risky"。试试这个:
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;
}
这个问题并不新鲜:具有 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;
}
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 工具包。
您在 code-behind 中分配轴的方式有点 "risky"。试试这个:
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;
}