1

我正在使用 OxyPlot 库,并且试图显示堆积柱形图,但它呈现错误。

这是图表应该如何的模型:

预期堆积条形图

以下是我创建 PlotModel 的方法:

private void InitWidget ()
{

    _goalsPlotModel = new PlotModel ("Metas") {
        LegendPlacement = LegendPlacement.Outside,
        LegendPosition = LegendPosition.BottomCenter,
        LegendOrientation = LegendOrientation.Horizontal,
        LegendBorderThickness = 0
    };

    SelectedChannel = new ListOfValue ();
    SelectedProduct = new Product ();

    SelectedChannel.Code = string.Empty;
    SelectedProduct.ProductCode = string.Empty;

    LoadFilters ();
    Refresh ();
}

这是我添加系列的方法:

private void FillGoalsPlotModel ()
{
    _goalsPlotModel.Series.Clear ();
    _goalsPlotModel.Axes.Clear ();

    var goals = new ColumnSeries {
        Title = "Goals",
        FillColor = OxyColors.Orange,
        IsStacked = true,
        StrokeColor = OxyColors.Black,
        StrokeThickness = 1
    };

    var sales = new ColumnSeries {
        Title = "Sales",
        FillColor = OxyColors.LightGreen,
        IsStacked = true,
        StrokeColor = OxyColors.White,
        StrokeThickness = 1
    };

    var surplus = new ColumnSeries {
        Title = "Surplus",
        FillColor = OxyColors.Cyan,
        IsStacked = true,
        StrokeColor = OxyColors.Black,
        StrokeThickness = 1
    };

    var categoryAxisForMonths = new CategoryAxis { 
        Position = AxisPosition.Bottom 
    };

    var valueAxis = new LinearAxis (AxisPosition.Left) { 
        MinimumPadding = 0, 
        MaximumPadding = 0.06, 
        AbsoluteMinimum = 0 
    };


    foreach (IGoal goal in _goals) {

        if (goal.GetSales () > goal.GetGoalValue ()) {
            sales.Items.Add (new ColumnItem { Value = goal.GetGoalValue () });
            surplus.Items.Add (new ColumnItem { Value = goal.GetSurplus () });
        } else {
            sales.Items.Add (new ColumnItem { Value = goal.GetSales () });
            goals.Items.Add (new ColumnItem { 
                Value = goal.GetGoalValue() - goal.GetSales ()  
            });
        }
    }

    foreach (var month in GetMonths()) {
        categoryAxisForMonths.Labels.Add (month);
    }


    _goalsPlotModel.Series.Add (sales);
    _goalsPlotModel.Series.Add (goals);
    _goalsPlotModel.Series.Add (surplus);

    _goalsPlotModel.Axes.Add (categoryAxisForMonths);
    _goalsPlotModel.Axes.Add (valueAxis);


    RaisePropertyChanged (() => GoalsPlotModel);
}

这是它的呈现方式:

错位叠柱系列

如果我设置IsStackedfalse它只绘制一个垂直条形图,但每个条形底部都y = 0按预期设置,但如果IsStacked设置为true每个条形底部的y值不同。

它是用于 Android 渲染器的 Mono 的 Oxyplot 中的错误吗?或者只是我做错了什么?(如果是,我做错了什么?)

4

1 回答 1

1

好吧,以下对我有用。我将在代码之后附上一些屏幕截图,但我认为您的问题是您将点添加到堆栈的方式。

您在每种情况下添加 2 个点(或真正的列),但您需要将值分配给 3(第三个为零)。否则结果会一直叠加,直到你得到 3,并且看起来没有任何正确。

我的 Xaml 很简单:

<oxy:Plot Model="{Binding MyPlotModel}" />

然后我从我的构造函数中调用这个方法来创建和设置模型:

private void SetPlot()
{
    var model = new PlotModel("Metas")
    {
        LegendPlacement = LegendPlacement.Outside,
        LegendPosition = LegendPosition.BottomCenter,
        LegendOrientation = LegendOrientation.Horizontal,
        LegendBorderThickness = 0
    };

    var goals = new ColumnSeries {
        Title = "Goals",
        FillColor = OxyColors.Orange,
        IsStacked = true,
        StrokeColor = OxyColors.Black,
        StrokeThickness = 1
    };

    var sales = new ColumnSeries {
        Title = "Sales",
        FillColor = OxyColors.LightGreen,
        IsStacked = true,
        StrokeColor = OxyColors.White,
        StrokeThickness = 1
    };

    var surplus = new ColumnSeries {
        Title = "Surplus",
        FillColor = OxyColors.Cyan,
        IsStacked = true,
        StrokeColor = OxyColors.Black,
        StrokeThickness = 1
    };

    var categoryAxisForMonths = new CategoryAxis { 
        Position = AxisPosition.Bottom 
    };

    var valueAxis = new LinearAxis (AxisPosition.Left) { 
        MinimumPadding = 0, 
        MaximumPadding = 0.06, 
        AbsoluteMinimum = 0 
    };

    // Creating random data for 12 months
    for (int i = 0; i < 12; i++)
    {
        //var goal = 10;   // if you want a set goal, use this
        var goal = RandomHelper.RandomInt(8, 11); // otherwise use this
        var actualsales = RandomHelper.RandomInt(5, 15);

        if (actualsales > goal)
        {
            sales.Items.Add  (new ColumnItem( goal               ));
            surplus.Items.Add(new ColumnItem( actualsales-goal   ));
            goals.Items.Add  (new ColumnItem( 0                  ));
        }
        else
        {
            sales.Items.Add  (new ColumnItem( actualsales        ));
            goals.Items.Add  (new ColumnItem( goal - actualsales ));
            surplus.Items.Add(new ColumnItem( 0                  ));
        }

        // Next will create jan - dec in the labels
        categoryAxisForMonths.Labels.Add(CultureInfo.CurrentUICulture.DateTimeFormat.MonthNames[i]);

    }

    model.Series.Add (sales);
    model.Series.Add (goals);
    model.Series.Add (surplus);

    model.Axes.Add (categoryAxisForMonths);
    model.Axes.Add (valueAxis);

    MyPlotModel = model;
}
public PlotModel MyPlotModel { get; set; }

RandomHelper 是一个帮助获取随机数的简单类:

public static class RandomHelper
{
    private static readonly Random RandomSeed = new Random();

    public static int RandomInt(int min, int max)
    {
        return RandomSeed.Next(min, max);
    }
}

结果如下:

固定目标

随机目标

有趣的线条是空列边框的结果,但您将能够弄清楚:)

于 2013-11-04T14:36:21.337 回答