2017-05-25 41 views

回答

3

關鍵是要使用ColumnSeriesColumnItem對象。這應該呈現爲具有垂直方向條形的條形圖。如果你創建一個新的ContentPage並粘貼下面的代碼,你可以看到它的實際運行。我在代碼示例下面附加了一張圖片,詳細說明了它在iOS上的外觀。

public partial class DemoPage : ContentPage 
{ 
    public DemoPage() 
    { 
     InitializeComponent(); 

     var model = new PlotModel { Title = "Cake Type Popularity" }; 

     // generate a random percentage distribution between the 5 
     //cake-types (see axis below) 
     var rand = new Random(); 
     double[] cakePopularity = new double[5]; 
     for (int i = 0; i < 5; ++i) 
     { 
      cakePopularity[i] = rand.NextDouble(); 
     } 
     var sum = cakePopularity.Sum(); 

     var barSeries = new ColumnSeries 
     { 

      ItemsSource = new List<ColumnItem>(new[] 
      { 
       new ColumnItem{ Value = (cakePopularity[0]/sum * 100) }, 
       new ColumnItem{ Value = (cakePopularity[1]/sum * 100) }, 
       new ColumnItem{ Value = (cakePopularity[2]/sum * 100) }, 
       new ColumnItem{ Value = (cakePopularity[3]/sum * 100) }, 
       new ColumnItem{ Value = (cakePopularity[4]/sum * 100) } 
      }), 
      LabelPlacement = LabelPlacement.Inside, 
      LabelFormatString = "{0:.00}%" 
     }; 

     model.Series.Add(barSeries); 

     model.Axes.Add(new CategoryAxis 
     { 
      Position = AxisPosition.Bottom, 

      Key = "CakeAxis", 
      ItemsSource = new[] 
      { 
       "A", 
       "B", 
       "C", 
       "D", 
       "E" 
      } 
     }); 

     var grid = new Grid(); 

     grid.Children.Add(new PlotView 
     { 
      Model = model, 
      VerticalOptions = LayoutOptions.Center, 
      HeightRequest = 200, 
      HorizontalOptions = LayoutOptions.Fill, 
     }); 

     Content = grid; 
    } 
} 

這應該渲染的圖形是這樣的:

Graph with ColumnSeries

+0

可以這樣用棒的組合來完成。 A的3個酒吧,B的3個酒吧等等? – Ph0b0x

+0

不知道tbh,我認爲Oxyplot文檔應該能夠澄清這一點。 –

+0

我發現了Usinsg的「IsStacked」屬性和/或堆棧組。 – Ph0b0x