【问题标题】:Oxyplot: AreaSeries ItemSource with custom DataPointsOxyplot:带有自定义数据点的 AreaSeries ItemSource
【发布时间】:2020-08-07 18:38:56
【问题描述】:

我正在使用 Oxyplot 中的 AreaSeries 来绘制两点之间的范围,并希望跟踪器在悬停时显示它们。通过为 DataPoints 创建自定义类并相应地更改 TrackerFormat,我实现了使 Tracker 与其他系列一起显示自定义属性。这是一个例子:

    public class CommentPoint : IScatterPointProvider
    {
        public CommentPoint(double x, double y, string text)
        {
            X = x; Y = y; Text = text;
        }
        public double X, Y;
        public string Text { get; set; }
        public ScatterPoint GetScatterPoint()
        {
            return new ScatterPoint(X, Y);
        }
    }

还有 TrackerFormat:

Series.TrackerFormatString = "{2}\n{Text}";

但是,这仅在添加新点数组作为系列的 ItemSource 时才有效。使用 Line/ScatterSeries 很容易,因为它们只需要一个点列表。但是 AreaSeries 需要绘制 2 个点。

我尝试将这些自定义点的二维数组添加为 ItemSource:

    public class AreaPoint : IDataPointProvider
    {
        public AreaPoint(double x, double y, double y2)
        {
            X = x; Y1 = y; Y2 = y2;
        }

        public double X { get; set; }
        public double Y1 { get; set; }
        public double Y2 { get; set; }
        public DataPoint GetDataPoint()
        {
            return new DataPoint(X, Y1);
        }
    }

所以基本上:

AreaPoint[,] points = new AreaPoint[2,amount.Count]
AreaPoint[0,i] = new AreaPoint(x,y,y2);
....
Series.ItemSource = points;

这不起作用,我收到一个错误“不是一维数组”。

接下来我尝试通过 GetDataPoint 方法直接添加点:

Series.Points.Add(new AreaPoint(x,y,y2).GetDataPoint());
Series.Points2.Add(new AreaPoint(x2,y2,y3).GetDataPoint());
Series.TrackerFormatString = "{2}\n{4}\n{Y2}";

这是因为 AreaSeries 被正确绘制,但 Tracker 不会显示我的自定义属性。

所以我想我的问题是,如何将自定义 ItemSource 添加到 AreaSeries?

【问题讨论】:

    标签: c# winforms oxyplot


    【解决方案1】:

    您可以将 X2、Y2 字段添加到您的自定义点类并在您的 AreaSeries 中设置 DataField 属性。

    public class AreaPoint : IDataPointProvider
    {
        public AreaPoint(double x, double y, double x2, double y2, string customValue)
        {
            X = x;
            Y = y;
            X2 = x2;
            Y2 = y2;
            CustomValue = customValue;
        }
    
        public double X { get; set; }
        public double Y { get; set; }
        public double X2 { get; set; }
        public double Y2 { get; set; }
        public string CustomValue { get; set; }
    }
    
    ...
    series = new AreaSeries();
    series.DataFieldX = "X";
    series.DataFieldY = "Y";
    series.DataFieldX2 = "X2";
    series.DataFieldY2 = "Y2";
    series.TrackerFormatString = "{0}\n{1}: {2}\n{3}: {4}" + 
        "\nMy custom value: {CustomValue}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2022-01-16
      • 2015-06-12
      • 2018-08-18
      • 1970-01-01
      相关资源
      最近更新 更多