xamarin核心图y轴值

时间:2013-04-26 07:08:18

标签: graph xamarin.ios core-plot

大家好,我在xamarin的核心情节有问题。我设法创建了如下图表:

click here for the graph

我正在使用此代码:

/// <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值(image中的红色方块),因此它看起来像一个正确的图形。任何人都有使用Xamarin / monotouch创建图形的经验吗?对于核心情节,没有太多的xamarin教程。在此之前谢谢:)

1 个答案:

答案 0 :(得分:0)

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

y.LabelingPolicy = CPTAxisLabelingPolicy.Automatic;

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