首页 > 解决方案 > LiveChart 不显示 x 轴 WPF 上的所有标签

问题描述

我正在使用LiveChart和加载一些数据。

<wpf:CartesianChart Name="LineChart" LegendLocation="top" >
<wpf:CartesianChart.AxisY>
    <wpf:Axis Title="Sales" ></wpf:Axis>
</wpf:CartesianChart.AxisY>
<wpf:CartesianChart.AxisX>
    <wpf:AxesCollection>
        <wpf:Axis Labels="{Binding Labels}">
            <wpf:Axis.Separator>
                <wpf:Separator Step="1" />
            </wpf:Axis.Separator>
        </wpf:Axis>
    </wpf:AxesCollection>
</wpf:CartesianChart.AxisX>


在后端我定义了标签。

public DailySalesProgressLineChart()
{
    InitializeComponent();
    Labels = new[]
                 {
                     1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
                     26, 27, 28, 29, 30
                 };
    DataContext = this;
    LoadLineChart();
}  
SeriesCollection seriesCollection = new SeriesCollection();
public int[] Labels { get; set; }  

但我没有看到全部 30Labelsx-axis。我已经提到了这些解决方案,但仍然是同样的问题。
实时图表未在 WPF 中的 x 轴上显示标签
https://github.com/Live-Charts/Live-Charts/issues/481

在此处输入图像描述

标签: c#wpflivecharts

解决方案


我认为 SeriesCollection 未绑定到您的图表。我希望您将它绑定在 LoadLineChart() 方法中的代码隐藏中。

由于没有为 LoadLineChart() 方法提供代码,我只是尝试随机为 seriesCollection 对象提供一些值,如下所示,

          private void LoadLineChart()
          {
              seriesCollection = new SeriesCollection
              {
                  new ColumnSeries
                  {
                      Title = "1988",
                      Values = new ChartValues<double> { 10, 50, 39, 50, 5, 10, 15, 20, 25, 30, 35, 40, 9, 18, 27, 36, 2, 4, 6, 8, 10, 12, 14,
  16, 3, 6, 9, 12, 14, 17, 21 }
                  }
              };

              //adding series will update and animate the chart automatically
              seriesCollection.Add(new ColumnSeries
              {
                  Title = "1989",
                  Values = new ChartValues<double> { 12, 71, 41, 21, 9, 6, 3, 61, 41, 21, 01, 8, 6, 4, 2, 63, 72, 81, 9, 04, 53, 03, 52, 02,
  51, 01, 5, 05, 93, 05, 01 }
              });

              **LineChart.Series = seriesCollection;**
          }

我猜您可能缺少将 seriesCollection 绑定到您的 LineChart。但是,如果您这样做并且仍然有其他问题,请更新您的评论。

注意:- 我尝试了一些条形图,您可以尝试使用您的图表代码。

希望这可以帮助你:)


推荐阅读