0

大家好,我对 xamarin 的核心情节有疑问。我设法创建了一个如下图:

点击这里查看图表

我正在使用这段代码:

/// <summary>
    /// Configure the Axes of the graph
    /// </summary>
    void SetupAxes()
    {
        var plotspace = _graph.DefaultPlotSpace;
        plotspace.AllowsUserInteraction = true;

        var axisSet = (CPTXYAxisSet)_graph.AxisSet;

        // Label x with a fixed interval policy
        var x = axisSet.XAxis;
        x.LabelingPolicy = CPTAxisLabelingPolicy.None;
        x.Title = "X Axis";
        x.TitleOffset = 0;
        x.MinorTickLength = 5f;
        x.MajorTickLength = 7f;
        x.LabelOffset = 3;
        x.MajorIntervalLength = 5;
        x.MinorTicksPerInterval = 4;



        // Label y with an automatic label policy. 
        var y = axisSet.YAxis;
        y.LabelingPolicy = CPTAxisLabelingPolicy.None;
        y.LabelOffset = 0;
        y.Title = "Y Axis";
        y.TitleOffset = 0;
        y.MinorTickLength = 5f;
        y.MajorTickLength = 7f;
        y.LabelOffset = 3;
        y.MajorIntervalLength = 5;
        y.MinorTicksPerInterval = 4;
    }

    /// <summary>
    /// Set up data source and configure plots
    /// </summary>
    void SetupBarPlots(List<float> inputSocket)
    {

        var barPlot = new CPTBarPlot
        {
            DataSource = new BarSourceData(inputSocket),
            BaseValue = 0,
            BarOffset = (NSDecimal)(-0.25),
            Identifier = (NSString)"Bar Plot 1"
        };

        _graph.AddPlot(barPlot);

        barPlot.Fill = new CPTFill(CPTColor.BrownColor);
        _graph.AddPlot(barPlot, _graph.DefaultPlotSpace);

        var space = _graph.DefaultPlotSpace as CPTXYPlotSpace;
        space.ScaleToFitPlots(new CPTPlot[] { barPlot });

        //get the highest value in the input data
        var yMax = (decimal)inputSocket.Max();
        decimal newYMax = yMax*2;            
        space.YRange = new CPTPlotRange(-20, new NSDecimalNumber(newYMax.ToString()).NSDecimalValue);

        decimal newXMax = (decimal)space.XRange.MaxLimit + 2;
        decimal newXMin = (decimal)space.XRange.MinLimit - 1;
        space.XRange = new CPTPlotRange(new NSDecimalNumber(newXMin.ToString()).NSDecimalValue,
                                         new NSDecimalNumber(newXMax.ToString()).NSDecimalValue);

        //RectangleF(position x, position y, width, height
        //AddSubview = adding a view on top of a View
        _view.AddSubview(new CPTGraphHostingView(new RectangleF(20, 320, 662, 320))
        {
            HostedGraph = _graph
        });
    }

我想在图表中显示 y 值(图像中的红色方块),以便它看起来像一个正确的图表。任何人都有如何使用 Xamarin/monotouch 创建图形的经验?核心情节的xamarin教程并不多。谢谢之前:)

4

1 回答 1

0

原来我必须将 labellingPolicy 设置为 Automatic。见下文:

y.LabelingPolicy = CPTAxisLabelingPolicy.Automatic;

然后它将根据数据设置y轴值。

于 2013-05-01T11:48:07.530 回答