2016-04-22 71 views
0

对于我显示与高度数据图表的UWP-应用程序,它目前看起来是这样的: enter image description hereUWP WinRTXamlToolkit:图表的标签与单位

我想拥有的y值单位像100 m,但我只能设法获得价值本身。 我能够在“AxisLabelStyle”把一个静态的单元值落后“的StringFormat”像这样

<Setter Property="StringFormat" Value="{}{0:0 m}" /> 

但不幸的是,我需要一个动态的单位(如米或英尺)。

我错过了什么?想法?

+0

当你从米到脚的变化,你需要做什么触发(例如按钮点击事件或大小改变事件)?或者你想全部随机? –

+0

这是一个设置页面内的用户设置,除了图表之外,所以我在创建图表之前得到了这些信息... –

回答

1

正如我们所讨论的,这种风格是由用户设置的。所以我只用ComboBox来选择测试的样式。

这里是我的代码:

<Charting:Chart x:Name="AreaChart" Title="Area Chart" Margin="0,0"> 
    <Charting:AreaSeries x:Name="areaseries" IndependentValuePath="Value" DependentValuePath="Number" IsSelectionEnabled="True" /> 
</Charting:Chart> 

<ComboBox x:Name="comboBox" VerticalAlignment="Bottom" SelectionChanged="comboBox_SelectionChanged"> 
    <ComboBoxItem>Meters</ComboBoxItem> 
    <ComboBoxItem>Feet</ComboBoxItem> 
</ComboBox> 

后面的代码只是为了测试,我没有试图重建图表中的图片:

public MainPage() 
{ 
    this.InitializeComponent(); 
    this.Loaded += MainPage_Loaded; 
} 

private void MainPage_Loaded(object sender, RoutedEventArgs e) 
{ 
    LoadChartContents(); 
} 

private void LoadChartContents() 
{ 
    Random rand = new Random(); 
    List<ChartTest> testitem = new List<ChartTest>(); 

    for (int i = 0; i < 30; i++) 
    { 
     testitem.Add(new ChartTest() { Value = i, Number = rand.Next(0, 100) }); 
    } 

    (AreaChart.Series[0] as AreaSeries).ItemsSource = testitem; 
} 

private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    areaseries.IndependentAxis = new LinearAxis { Orientation = AxisOrientation.X }; 
    var axis = (LinearAxis)areaseries.IndependentAxis; 
    var item = comboBox.SelectedItem as ComboBoxItem; 
    if ((string)item.Content == "Meters") 
    { 
     var labelstyle = new Style(typeof(AxisLabel)); 
     labelstyle.Setters.Add(new Setter(AxisLabel.StringFormatProperty, "{0:0 m}")); 
     axis.AxisLabelStyle = labelstyle; 
    } 
    else 
    { 
     var labelstyle = new Style(typeof(AxisLabel)); 
     labelstyle.Setters.Add(new Setter(AxisLabel.StringFormatProperty, "{0:0 feet}")); 
     axis.AxisLabelStyle = labelstyle; 
    } 
} 

而且我是ChartTest类这样的:

public class ChartTest 
{ 
    public int Value { get; set; } 
    public int Number { get; set; } 
} 

这里的关键是动态添加AxisLabelStyle到的SelectionChanged事件中的4。

+0

非常感谢您的帮助! –

+0

@JeromeDreyer,不客气;)。 –