OxyPlot Mono สำหรับ Android (aka Xamarin.Android) การวาดผิดที่ (ชุดคอลัมน์แบบเรียงซ้อน)

ฉันใช้ OxyPlot Library และฉันกำลังพยายามแสดงแผนภูมิคอลัมน์แบบเรียงซ้อน แต่แสดงผลผิด

นี่คือตัวอย่างว่าแผนภูมิควรเป็นอย่างไร:

แผนภูมิแท่งแบบซ้อนที่คาดหวัง

นี่คือวิธีที่ฉันสร้าง 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);
}

และนี่คือวิธีการเรนเดอร์:

ชุดคอลัมน์แบบเรียงซ้อนที่วางผิดที่

ถ้าฉันตั้งค่า IsStacked เป็น false ก็เพียงวาด Vertical BarChart แต่ทุกแท่งด้านล่างจะอยู่ที่ y = 0 ตามที่คาดไว้ แต่ถ้าตั้งค่า IsStacked เป็น true ทุกแท่งด้านล่างจะมีค่า y ที่แตกต่างกัน

มันเป็นจุดบกพร่องใน Oxyplot สำหรับ Mono สำหรับ Android Renderer หรือไม่? หรือฉันแค่กำลังทำอะไรผิด? (ถ้าใช่ ฉันทำอะไรผิด?)


person Alberto Estrella    schedule 30.10.2013    source แหล่งที่มา
comment
เฮ้ อัลเบอร์โต คุณช่วยโพสต์ข้อมูลบางอย่างได้ไหม (อาจเป็นเรื่องหลอกลวง) ที่ฉันสามารถทดสอบได้หรือไม่   -  person Noctis    schedule 04.11.2013


คำตอบ (1)


สิ่งต่อไปนี้ใช้ได้กับฉัน ฉันจะแนบภาพหน้าจอบางส่วนไว้หลังโค้ด แต่ฉันคิดว่าปัญหาของคุณคือวิธีที่คุณเพิ่มคะแนนลงในสแต็ก

คุณกำลังเพิ่ม 2 จุด (หรือคอลัมน์จริงๆ) ในแต่ละกรณี แต่คุณต้องกำหนดค่าให้กับ 3 (อันที่สามเป็นศูนย์) มิฉะนั้น ผลลัพธ์จะซ้อนกันจนกว่าคุณจะได้ 3 และจะไม่มีอะไรดูถูกต้อง

Xaml ของฉันตรงไปตรงมา:

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

จากนั้นฉันก็เรียกเมธอดนี้จาก Constructor ของฉันเพื่อสร้างและตั้งค่าโมเดล:

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);
    }
}

และนี่คือผลลัพธ์:

เป้าหมายคงที่

เป้าหมายสุ่ม

เส้นตลกๆ เป็นผลมาจากเส้นขอบของคอลัมน์ว่าง แต่คุณจะสามารถเข้าใจได้ :)

person Noctis    schedule 04.11.2013