简体   繁体   English

Xamarin中使用CorePlot的饼图显示白色空白屏幕

[英]Pie Chart using CorePlot in Xamarin shows a white blank screen

I got the component off the Xamarin Component Store and have successfully managed to make a line and bar chart but having a little difficulty with a pie chart. 我从Xamarin Component Store中获得了该组件,并成功地制作了折线图和条形图,但是在饼图上却遇到了一些困难。

Code for Pie Chart: 饼图代码:

protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
        var view = NativeView;
        var graph = new CPTXYGraph
        {
            Title = "Pie Chart",
            TitleTextStyle = new CPTMutableTextStyle
            {
                FontSize = 50,
                Color = CPTColor.GreenColor,
            },
        };

        var plotSpace = (CPTXYPlotSpace)graph.DefaultPlotSpace;
        plotSpace.AllowsUserInteraction = true;     

        graph.ApplyTheme(CPTTheme.ThemeNamed((NSString)"Plain Black"));
        graph.PlotAreaFrame.BorderLineStyle.LineColor = CPTColor.BlackColor;

        var plot = new CPTPieChart
        {
            DataSource = new PiePlotDataSource()
        };                   

        graph.AddPlot(plot);

        view.AddSubview(new CPTGraphHostingView
        {
            HostedGraph = graph
        });       
    }

Code for Data Source: 数据源代码:

class PiePlotDataSource : CPTPieChartDataSource
{
    public override int NumberOfRecordsForPlot(CPTPlot plot)
    {
        return DummyData.Data.Length;
    }

    public override NSNumber NumberForPlot(CPTPlot plot, CPTPlotField forFieldEnum, uint index)
    {
        return forFieldEnum == CPTPlotField.PieChartWidth ? DummyData.Data[index] : index;
    }
}

All I get is just a white blank screen with a misplaced title that cannot be zoomed or scrolled (I apologize for the big image, I'm not sure how to resize it): 我得到的只是一个白色的空白屏幕,其标题放错了位置,无法缩放或滚动(我为大图表示歉意,不确定如何调整大小):

屏幕截图

The line and bar charts were implmeneted in a similar manner and they work fine. 折线图和条形图都以类似的方式插入,并且效果很好。 I have put break points in and checked if it was stopping in anywhere of the code but execution also seemed to be working fine. 我已经设置了断点,并检查它是否在任何代码中都停止了,但是执行似乎也可以正常工作。

The thing that it is not doing however is that the PiePlotDataSource methods are not getting called. 但是,它没有做的是没有调用PiePlotDataSource方法。 I have verified they are getting called in the line and bar charts but for some reason they are not getting called here. 我已经证实它们在折线图和条形图中被调用,但是由于某些原因,它们在此处未被调用。

It has taken me several days to just get a few generic graphs working for Android and iOS due to so little documentation. 由于文档很少,我花了几天的时间才得到一些适用于Android和iOS的通用图形。 This is the last chart I need to do so I would greatly appreciate any help. 这是我需要做的最后一个图表,因此我将不胜感激。

I have managed to get it working and I am 90% sure what may have been the underlying problem. 我已经设法使其正常运行,并且我90%的确定可能是潜在的问题。 I originally thought that the order of setting up the host view, graph and plot may have been the problem but it turns out it was not the case. 我本来以为设置主机视图,图形和绘图的顺序可能是问题所在,但事实并非如此。

The revised code is: 修改后的代码是:

protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
    base.OnElementChanged(e);
    var view = NativeView;

    var graph = new CPTXYGraph
    {
        Title = "Pie Chart",

    };

    var hostView = new CPTGraphHostingView(view.Bounds);
    view.AddSubview(hostView);

    var plot = new CPTPieChart
    {
        DataSource = new PiePlotDataSource(),
        PieRadius = hostView.Bounds.Size.Height / 4,
    };

    graph.AddPlot(plot);   

    hostView.HostedGraph = graph;            
}

What I have changed is that when creating a CPTGraphHostingView() , I pass in the parent's View.Bounds as a parameter and then assign the result to the variable hostView , which will be used later. 我已更改的是,在创建CPTGraphHostingView() ,我传入了父级的View.Bounds作为参数,然后将结果分配给变量hostView ,稍后将使用它。 I believe this was what was causing the layout problems. 我相信这就是造成布局问题的原因。

The hostView is then used when setting the PieRadius property of the pie chart. 所述hostView设置时,然后使用PieRadius饼图的属性。 If this property isn't set, the pie chart does not show (only a set of X and Y axes are shown). 如果未设置此属性,则不会显示饼图(仅显示一组X和Y轴)。

In terms of the PiePlotDataSource methods not being called, a simple clean and rebuild fixed that problem. 就不调用PiePlotDataSource方法而言,简单的清理和重新PiePlotDataSource解决了该问题。 The updated code for its NumberForPlot() method is here: NumberForPlot()方法的更新代码在这里:

public override NSNumber NumberForPlot(CPTPlot plot, CPTPlotField forFieldEnum, uint index)
{
    return forFieldEnum == CPTPlotField.BarLocation ? DummyData.Data[index] : index;
}

Notice that I am comparing forFieldEnum to CPTPlotField.BarLocation instead of CPTPlotField.PieChartWidth . 请注意,我正在将forFieldEnumCPTPlotField.BarLocation而不是CPTPlotField.PieChartWidth进行比较。 This was because I put some break points in and stepped through the method, only to find that forFieldEnum was always CPTPlotField.BarLocation . 这是因为我放入了一些断点并逐步执行了该方法,才发现forFieldEnum始终是CPTPlotField.BarLocation I believe this is a binding error of some sort to the Xamarin CorePlot Compoenent. 我相信这是Xamarin CorePlot组件的某种绑定错误。

Not sure with your coreplot version in xamarin component. 不确定您的xamarin组件中的coreplot版本。 I'm using version 1.5.1.2 我正在使用1.5.1.2版

But I found out that i could not use your method to run public override NSNumber NumberForPlot(CPTPlot plot, CPTPlotField forFieldEnum, uint index) 但我发现我无法使用您的方法来运行public override NSNumber NumberForPlot(CPTPlot plot, CPTPlotField forFieldEnum, uint index)

Solve by : 解决方法:

public override NSNumber NumberForPlot(CPTPlot plot, CPTPlotField forFieldEnum, nuint index)
            {
                int i = Convert.ToInt32(index);
                return forFieldEnum == CPTPlotField.BarLocation ? data[i].X : data[i].Y;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM