Xamarin.Forms MicroCharts BarChart具有多个值

问题描述 投票:0回答:1

我正在使用MicroCharts barChart

  <forms:ChartView x:Name="chart1" HeightRequest="150">

我从Api读取数据列表。我需要显示按参数year(我从列表中的每个对象读取)分组的数据,并且每年要显示3个值。类似于此示例:

enter image description here

private PlotView _opv = new PlotView();
            MessagingCenter.Subscribe<Object, BindingList<Summary>>(this, "Sum", (args, list) =>
            {
                var plotModel = new PlotModel { Title = "OxyPlot Demo" };
                CategoryAxis xaxis = new CategoryAxis();
                xaxis.Position = OxyPlot.Axes.AxisPosition.Bottom;
                xaxis.MajorGridlineStyle = LineStyle.Solid;
                xaxis.MinorGridlineStyle = LineStyle.Dot;
                foreach (var el in list)
                {
                    xaxis.Labels.Add(el.Year);
                    ColumnSeries s = new ColumnSeries();
                    s.IsStacked = false;
                    s.Items.Add(new ColumnItem((double)el.Plus));
                    s.Items.Add(new ColumnItem((double)el.Minus));
                    s.Items.Add(new ColumnItem((double)el.Sum));

                    plotModel.Series.Add(s);
                    s.ClearSelection();

                }
                plotModel.Axes.Add(xaxis);
                plotModel.Axes.Add(new LinearAxis { Position = OxyPlot.Axes.AxisPosition.Left, Minimum = 0, Maximum = 10000000 });

                _opv.Model = plotModel;
            });
            Content = _opv;

这是我的代码,但未显示真实数据有什么建议吗?

xamarin xamarin.forms xamarin.android xamarin.ios
1个回答
0
投票

如果要达到上述效果,可以考虑使用它OxyPlot.Xamarin.Forms

MainActivity中,在Xamarin.Forms.Forms.Init之后初始化:

global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init();

在您的page.xam.cs中:

public class BarChar : ContentPage
{
    private PlotView _opv = new PlotView();
    public BarChar()
    {
        var plotModel = new PlotModel { Title = "OxyPlot Demo" };
        CategoryAxis xaxis = new CategoryAxis();
        xaxis.Position = AxisPosition.Bottom;
        xaxis.MajorGridlineStyle = LineStyle.Solid;
        xaxis.MinorGridlineStyle = LineStyle.Dot;
        xaxis.Labels.Add("2/2010");
        xaxis.Labels.Add("2/2011");
        xaxis.Labels.Add("2/2012");
        xaxis.Labels.Add("2/2013");

        ColumnSeries s1 = new ColumnSeries();
        s1.IsStacked = false;
        s1.Items.Add(new ColumnItem(1.2));
        s1.Items.Add(new ColumnItem(1.6));
        s1.Items.Add(new ColumnItem(1.4));
        s1.Items.Add(new ColumnItem(1.5));

        ColumnSeries s2 = new ColumnSeries();
        s2.IsStacked = false;
        s2.Items.Add(new ColumnItem(1.5));
        s2.Items.Add(new ColumnItem(1.6));
        s2.Items.Add(new ColumnItem(1.1));
        s2.Items.Add(new ColumnItem(1.2));

        ColumnSeries s3 = new ColumnSeries();
        s3.IsStacked = false;
        s3.Items.Add(new ColumnItem(1.2));
        s3.Items.Add(new ColumnItem(1.3));
        s3.Items.Add(new ColumnItem(1.4));
        s3.Items.Add(new ColumnItem(1.5));

        ColumnSeries s4 = new ColumnSeries();
        s4.IsStacked = false;
        s4.Items.Add(new ColumnItem(1.5));
        s4.Items.Add(new ColumnItem(1.4));
        s4.Items.Add(new ColumnItem(1.3));
        s4.Items.Add(new ColumnItem(1.2));



        plotModel.Series.Add(s1);
        plotModel.Series.Add(s2);
        plotModel.Series.Add(s3);
        plotModel.Series.Add(s4);

        plotModel.Axes.Add(xaxis);
        plotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Minimum = 0, Maximum = 1.6 });

        _opv.Model = plotModel;
        Content = _opv;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.