UWP WinRTXamlToolkit:带有单位的图表标签

时间:2016-04-22 15:33:38

标签: charts label win-universal-app axis toolkit

对于UWP-app我正在显示一个包含高度数据的图表,它目前看起来像这样: enter image description here

我希望像 import csv File1 = 'F:\somedata\somefolder\file1.csv' File2 = 'F:\somedata\somefolder\file2.csv' File3 = 'F:\\somedata\somefolder\final.csv' with open('r', 'File1' and 'File2', 'rt') as f, open('r', 'File3', 'wt', newline='') as f_out: headings = next(iter(csv.reader(f))) csv.writer(f_out).writerow(headings) csvout = csv.DictWriter(f_out, fieldnames=headings) for d in csv.DictReader(f, fieldnames=headings): csvout.writerow(d) 这样的y值有单位,但我只能设法获取值本身。 我可以在“AxisLabelStyle”中使用“StringFormat”将静态单位放在该值后面,就像这样

100 m

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

我错过了什么吗?想法?

1 个答案:

答案 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的{​​{1}}事件中动态添加AreaSeriesSelectionChanged