WPF 工具包(2010 年 2 月版)当 ItemsSource 只有一项时,列系列图表为空

发布于 2024-10-13 10:55:47 字数 2035 浏览 1 评论 0原文

我遇到了一个烦人的问题:我有一个简单的图表,其中有一个 ColumnSeries 和两个轴(一个用于从属值的 LinearAxis 和一个用于独立值的 DateTimeAxis)。 我的 ColumnSeries 的 ItemsSource 绑定到 DataPoint 实例的集合(只有 3 个属性的简单类:Date、IndependentValue 和 DependentValue)。 如果集合中有 2 个或更多项目,则图表会正确显示列; 但如果只有一个,则不会显示任何

对发生的事情有什么想法吗?

下面是 XAML(为简洁起见,省略了标准命名空间和 WPF Toolkit 的命名空间):

<Window x:Class="Demo.MainWindow" (...) xmlns:local="clr-namespace:Demo">
  <Grid>
    <Grid.Resources>
      <local:DataPointCollection x:Key="DataPointCollection" />
    </Grid.Resources>
    <ct:Chart Title="Demo">
      <ct:ColumnSeries Title="A"
                       ItemsSource="{StaticResource DataPointCollection}"
                       IndependentValueBinding="{Binding Date}"
                       DependentValueBinding="{Binding DependentValue}" />
      <ct:Chart.Axes>
        <ct:LinearAxis Orientation="Y"
                       ShowGridLines="True"
                       Title="Dependent Title" />
        <ct:DateTimeAxis Orientation=X"
                         ShowGridLines="True"
                         Interval="1"
                         IntervalType="Days" />
      </ct:Chart.Axes>
    </ct:Chart>
  </Grid>
</Window>

DataPointCollection 类:

using System;

namespace Demo
{
  using System.Collections.ObjectModel;

  public class DataPointCollection: Collection<DataPoint>
  {
    public DataPointCollection()
    {
      Add(new DataPoint { Date = DateTime.Now.Date, DependentValue = 5 });
      // Comment next line to see an empty chart:
      Add(new DataPoint { Date = DateTime.Now.Date.AddDays(1), DependentValue = 6 });
    }
  }
}

和 DataPoint 类:

using System;

namespace Demo
{
  public class DataPoint
  {
    public DateTime Date { get; set; }
    public double DependentValue { get; set; }
  }
}

该项目是常规 WPF 应用程序 (WPF 4)。

提前致谢。

I'm having an annoying problem: I have a simple Chart with one ColumnSeries and two axis (a LinearAxis for the dependent value and a DateTimeAxis for the independent one).
My ColumnSeries' ItemsSource is bound to a Collection of instances of DataPoint (simple class with only 3 properties: Date, IndependentValue and DependentValue).
The Chart displays the columns correctly if the Collection has 2 or more items in it; but if it only has one, it won't show any column.

Any ideas on what's going on?

Here's the XAML (with both standard and WPF Toolkit's namespaces omitted for brevity):

<Window x:Class="Demo.MainWindow" (...) xmlns:local="clr-namespace:Demo">
  <Grid>
    <Grid.Resources>
      <local:DataPointCollection x:Key="DataPointCollection" />
    </Grid.Resources>
    <ct:Chart Title="Demo">
      <ct:ColumnSeries Title="A"
                       ItemsSource="{StaticResource DataPointCollection}"
                       IndependentValueBinding="{Binding Date}"
                       DependentValueBinding="{Binding DependentValue}" />
      <ct:Chart.Axes>
        <ct:LinearAxis Orientation="Y"
                       ShowGridLines="True"
                       Title="Dependent Title" />
        <ct:DateTimeAxis Orientation=X"
                         ShowGridLines="True"
                         Interval="1"
                         IntervalType="Days" />
      </ct:Chart.Axes>
    </ct:Chart>
  </Grid>
</Window>

The DataPointCollection class:

using System;

namespace Demo
{
  using System.Collections.ObjectModel;

  public class DataPointCollection: Collection<DataPoint>
  {
    public DataPointCollection()
    {
      Add(new DataPoint { Date = DateTime.Now.Date, DependentValue = 5 });
      // Comment next line to see an empty chart:
      Add(new DataPoint { Date = DateTime.Now.Date.AddDays(1), DependentValue = 6 });
    }
  }
}

And the DataPoint class:

using System;

namespace Demo
{
  public class DataPoint
  {
    public DateTime Date { get; set; }
    public double DependentValue { get; set; }
  }
}

The project is a regular WPF Application (WPF 4).

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方圜几里 2024-10-20 10:55:47

浏览 ColumnSeries 的源代码可以看出,列宽与数据范围相关,在您的情况下,列宽为零。除非您想修复工具包中的错误,否则我建议的唯一解决方法是用虚拟点包围您的单个点,使其看起来只显示一列:

        var now = DateTime.Now;
        Add(new DataPoint { Date = now, DependentValue = 5 });
        Add(new DataPoint { Date = now + TimeSpan.FromDays(1), DependentValue = 0 });
        Add(new DataPoint { Date = now - TimeSpan.FromDays(1), DependentValue = 0 });

Glancing at the source code for ColumnSeries shows that the column width is related to the range of the data, which in your case is zero. Unless you want to fix bugs in the toolkit, the only workaround I can suggest is to surround your single point with dummy points to make it look like only one column is being shown:

        var now = DateTime.Now;
        Add(new DataPoint { Date = now, DependentValue = 5 });
        Add(new DataPoint { Date = now + TimeSpan.FromDays(1), DependentValue = 0 });
        Add(new DataPoint { Date = now - TimeSpan.FromDays(1), DependentValue = 0 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文