OxyPlot Zero Line

时间:2014-11-26 17:52:55

标签: charts xamarin xamarin.forms oxyplot

我们有一个带正值和负值的简单ColumnChart。

虽然在0线,但图表上没有线。我们如何实现零线?

见附图

enter image description here

2 个答案:

答案 0 :(得分:5)

假设您正在使用LinearAxis作为Y轴。

您需要做的只是添加到您的LinearAxis。

plotModel.Axes.Add(new LinearAxis()
        {
            Title = "Percentage",
            Position = AxisPosition.Left,
            // Magic Happens here we add the extra grid line on our Y Axis at zero
            ExtraGridlines = new Double[] { 0 }
        });

答案 1 :(得分:-1)

看看这款适用于Android的产品尚未在IOS上进行过测试:

public static PlotModel Withnegativevalues()
{
    var plotModel1 = new PlotModel();
    plotModel1.LegendBorderThickness = 0;
    plotModel1.LegendOrientation = LegendOrientation.Horizontal;
    plotModel1.LegendPlacement = LegendPlacement.Outside;
    plotModel1.LegendPosition = LegendPosition.BottomCenter;
    plotModel1.Title = "With negative values";
    var categoryAxis1 = new CategoryAxis();
    categoryAxis1.MinorStep = 1;
    categoryAxis1.Labels.Add("Category A");
    categoryAxis1.Labels.Add("Category B");
    categoryAxis1.Labels.Add("Category C");
    categoryAxis1.Labels.Add("Category D");
    categoryAxis1.ActualLabels.Add("Category A");
    categoryAxis1.ActualLabels.Add("Category B");
    categoryAxis1.ActualLabels.Add("Category C");
    categoryAxis1.ActualLabels.Add("Category D");
    plotModel1.Axes.Add(categoryAxis1);
    var linearAxis1 = new LinearAxis();
    linearAxis1.MaximumPadding = 0.06;
    linearAxis1.MinimumPadding = 0.06;
    linearAxis1.ExtraGridlines = new Double[1];
    linearAxis1.ExtraGridlines[0] = 0;
    plotModel1.Axes.Add(linearAxis1);
    var columnSeries1 = new ColumnSeries();
    columnSeries1.StrokeThickness = 1;
    columnSeries1.Title = "Series 1";
    columnSeries1.Items.Add(new ColumnItem(25,-1,"OxyColors.Automatic"));
    columnSeries1.Items.Add(new ColumnItem(137,-1,"OxyColors.Automatic"));
    columnSeries1.Items.Add(new ColumnItem(18,-1,"OxyColors.Automatic"));
    columnSeries1.Items.Add(new ColumnItem(40,-1,"OxyColors.Automatic"));
    plotModel1.Series.Add(columnSeries1);
    var columnSeries2 = new ColumnSeries();
    columnSeries2.StrokeThickness = 1;
    columnSeries2.Title = "Series 2";
    columnSeries2.Items.Add(new ColumnItem(-12,-1,"OxyColors.Automatic"));
    columnSeries2.Items.Add(new ColumnItem(-14,-1,"OxyColors.Automatic"));
    columnSeries2.Items.Add(new ColumnItem(-120,-1,"OxyColors.Automatic"));
    columnSeries2.Items.Add(new ColumnItem(-26,-1,"OxyColors.Automatic"));
    plotModel1.Series.Add(columnSeries2);
    var columnSeries3 = new ColumnSeries();
    columnSeries3.StrokeThickness = 1;
    columnSeries3.Title = "Series 3";
    columnSeries3.Items.Add(new ColumnItem(21,-1,"OxyColors.Automatic"));
    columnSeries3.Items.Add(new ColumnItem(8,-1,"OxyColors.Automatic"));
    columnSeries3.Items.Add(new ColumnItem(48,-1,"OxyColors.Automatic"));
    columnSeries3.Items.Add(new ColumnItem(3,-1,"OxyColors.Automatic"));
    plotModel1.Series.Add(columnSeries3);
    var columnSeries4 = new ColumnSeries();
    columnSeries4.StrokeThickness = 1;
    columnSeries4.Title = "Series 4";
    columnSeries4.Items.Add(new ColumnItem(-8,-1,"OxyColors.Automatic"));
    columnSeries4.Items.Add(new ColumnItem(-21,-1,"OxyColors.Automatic"));
    columnSeries4.Items.Add(new ColumnItem(-3,-1,"OxyColors.Automatic"));
    columnSeries4.Items.Add(new ColumnItem(-48,-1,"OxyColors.Automatic"));
    plotModel1.Series.Add(columnSeries4);
    return plotModel1;
}