gpt4 book ai didi

c# - Xamarin 为条形图创建 OxyPlot

转载 作者:太空狗 更新时间:2023-10-30 01:00:46 25 4
gpt4 key购买 nike

我正在开发 Xamarin.Forms 应用程序,我想在其中实现截图中所附的条形图。 Xamarin.Forms 中没有这样的控件,所以我为此使用了 OxyPlot nuget 包,但问题是 oxyplot 中的条是水平的,并且在图形图中没有网格线选项。是否有任何用于条形图的开源库,以便我可以使用。 enter image description here

最佳答案

您可以使用 ColumnSeries 使用 OxyPlot 来完成。试试这个 MCVE :

XAML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App2"
xmlns:oxy="clr-namespace:OxyPlot.Xamarin.Forms;assembly=OxyPlot.Xamarin.Forms"
x:Class="App2.MainPage">

<ContentPage.BindingContext>
<local:MyViewModel></local:MyViewModel>
</ContentPage.BindingContext>

<oxy:PlotView Model="{Binding Model}"/>

</ContentPage>

查看模型:

using OxyPlot;
using OxyPlot.Series;
using OxyPlot.Axes;

public class MyViewModel
{
public PlotModel Model { get; set; }

public MyViewModel()
{
CategoryAxis xaxis = new CategoryAxis();
xaxis.Position = AxisPosition.Bottom;
xaxis.MajorGridlineStyle = LineStyle.Solid;
xaxis.MinorGridlineStyle = LineStyle.Dot;
xaxis.Labels.Add("Mon, 4/24");
xaxis.Labels.Add("Tue, 4/25");
xaxis.Labels.Add("Wed, 4/26");
xaxis.Labels.Add("Thu, 4/27");

LinearAxis yaxis = new LinearAxis();
yaxis.Position = AxisPosition.Left;
yaxis.MajorGridlineStyle = LineStyle.Dot;
xaxis.MinorGridlineStyle = LineStyle.Dot;

ColumnSeries s1 = new ColumnSeries();
s1.IsStacked = true;
s1.Items.Add(new ColumnItem(20));
s1.Items.Add(new ColumnItem(60));
s1.Items.Add(new ColumnItem(40));
s1.Items.Add(new ColumnItem(50));

ColumnSeries s2 = new ColumnSeries();
s2.IsStacked = true;
s2.Items.Add(new ColumnItem(50));
s2.Items.Add(new ColumnItem(30));
s2.Items.Add(new ColumnItem(10));
s2.Items.Add(new ColumnItem(20));

Model = new PlotModel();
Model.Title = "Xamarin Oxyplot Sample";
Model.Background = OxyColors.Gray;

Model.Axes.Add(xaxis);
Model.Axes.Add(yaxis);
Model.Series.Add(s1);
Model.Series.Add(s2);
}
}

enter image description here

关于c# - Xamarin 为条形图创建 OxyPlot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43636795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com