2013-04-26 183 views
0

你好家伙我有一个核心情节在Xamarin的问题。我设法创建一个类似下面的图形:xamarin核心图y轴值

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 
     }); 
    } 

我要显示在图表(在image红色正方形)y的值,因此可能看起来像一个适当的图。任何人都有如何使用Xamarin/monotouch创建图表的经验?核心情节没有太多的xamarin教程。谢谢:)

回答

0

原来我必须将labellingPolicy设置为自动。见下:

y.LabelingPolicy = CPTAxisLabelingPolicy.Automatic; 

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