1

几天前我开始使用 C# 和 Live Charts。

根据网上找到的例子,我做了一个非常简单的图表,以便理解它背后的概念。

这就是我所拥有的(它工作得很好):

public partial class Wdw_graph : Window
{
    public SeriesCollection SeriesCollection { get; set; }
    public string[] Labels { get; set; }

    public Wdw_graph(List<dated_value> serie)
    {
        InitializeComponent();

        SeriesCollection = new SeriesCollection();

        ColumnSeries col_serie = new ColumnSeries {
                Values = new ChartValues<double>(),
                DataLabels = true };

        Labels = new string[serie.Count];

        for(int i = 0; i < serie.Count; i++)
        {
            col_serie.Values.Add(serie[i].value);
            Labels[i] = serie[i].date;
        }

        SeriesCollection.Add(col_serie);          

        DataContext = this;
    }
}
<Grid>
    <lvc:CartesianChart Series="{Binding SeriesCollection}" >
          <lvc:CartesianChart.AxisX>
            <lvc:Axis Labels="{Binding Labels}" LabelsRotation="80">
              <lvc:Axis.Separator>
                <lvc:Separator IsEnabled="False" Step="1"></lvc:Separator>
              </lvc:Axis.Separator>
            </lvc:Axis>
          </lvc:CartesianChart.AxisX>
        <lvc:CartesianChart.AxisY>
            <lvc:Axis Title="Sold Apps"></lvc:Axis>
        </lvc:CartesianChart.AxisY>
    </lvc:CartesianChart>
</Grid>

如您所见,我在类代码中处理了“SeriesCollection”和“Labels”,它们绑定到 XAML。

我想知道是否可以使用相同的方法来处理其他图形元素,例如“AxisX”。如果是这样,我该怎么做?

在这篇文章“更改 LiveCharts 中轴刻度标签的格式”中,有一段代码显示了我想做的事情:

cartesianChart2.AxisX.Add(new Axis
{
    Name = "xAxis",
    Title = "DateTime",
    FontSize = 22,
    Foreground = System.Windows.Media.Brushes.Black,
    MinValue = 0,
    MaxValue = _amountValues,
});

但我无法重现。我不知道'cartesianChart2'来自哪里。

4

1 回答 1

0

好吧,我学会了如何在类代码中引用 XAML 元素。

我在 CartesianChart 中添加了 'x:Name="something"' 并删除了其他所有内容(不再使用 'Binding'):

<Grid>
    <lvc:CartesianChart x:Name="cartesian_chart">
    </lvc:CartesianChart>
</Grid>

然后,在类代码中,我插入了轴和系列:

public partial class Wdw_graph : Window
{
    public Wdw_graph(List<dated_value> serie)
    {
        InitializeComponent();         

        cartesian_chart.AxisX.Add(new Axis
        {
            Name = "xAxis",
            Title = "Date and Time",
            FontSize = 20,
            Foreground = System.Windows.Media.Brushes.Black,
            MinValue = 0,
            MaxValue = serie.Count,
            Labels = new String[serie.Count],
        });
        cartesian_chart.AxisY.Add(new Axis
        {
            Name = "yAxis",
            Title = "Currency",
            FontSize = 20,
            Foreground = System.Windows.Media.Brushes.Black,
            MinValue = 0,
            MaxValue = 10,
        });

        cartesian_chart.AxisX[0].Separator.Step = 1;
        cartesian_chart.AxisX[0].LabelsRotation = 80;

        ColumnSeries col_serie = new ColumnSeries {
                Values = new ChartValues<double>(),
                DataLabels = true };

        for(int i = 0; i < serie.Count; i++)
        {
            col_serie.Values.Add(serie[i].value);
            cartesian_chart.AxisX[0].Labels[i] = serie[i].date;
        }

        cartesian_chart.Series.Add(col_serie);

        DataContext = this;
    }
}

我真的更喜欢直接从代码中处理图表,而不是使用 XAML。

你能告诉我这种方法的缺点是什么吗?

于 2017-11-14T11:37:49.300 回答