Xamarin.Forms:在BarChart布局中放置一个标签

时间:2016-12-26 14:56:43

标签: xamarin.forms oxyplot

我正在使用oxyplot绘制条形图。我希望在图表中添加额外的标签。我尝试过使用Textannotation,但它没有在图表上显示。我做错导致文本不出现的错误是什么,是DataPoint吗?请帮忙。

plotModel = new PlotModel
            {
                Title = "Daily",
                LegendPlacement = LegendPlacement.Outside,
                LegendPosition = LegendPosition.BottomCenter,
                LegendOrientation = LegendOrientation.Horizontal,
                LegendBorderThickness = 0
            };

            TextAnnotation txtlabel = new TextAnnotation();
            txtlabel.Text = "Test";
            txtlabel.TextColor = OxyColors.Red;
            txtlabel.Stroke = OxyColors.Red;
            txtlabel.StrokeThickness = 5;
            txtlabel.FontSize = 36;
            txtlabel.TextPosition = new DataPoint(21, 3.5);

            plotModel.Annotations.Add(txtlabel);

            string sPrevType = "";
            string sCurrentType = "";
            DateTime dtdate;

            var sr = new ColumnSeries();
            var col = new ColumnItem();
            int iCount = 0;
            List<decimal> lstI = new List<decimal>();
            List<decimal> lstD = new List<decimal>();
            List<decimal> lstS = new List<decimal>();

            foreach (var itm in _dateodr)
            {
                dtdate = itm.date;
                sCurrentType = itm.Type;
                lstI.Add(itm.I_Unit);
                lstD.Add(itm.D_Unit);
                lstS.Add(itm.S_Unit);
                if (sCurrentType != sPrevType && sPrevType != "")
                {
                    sr = new ColumnSeries();
                    sr.Title = sPrevType;
                    sr.LabelPlacement = LabelPlacement.Outside;
                    sr.StrokeColor = OxyColors.Black;
                    sr.StrokeThickness = 1;
                    //sr.LabelFormatString = "{0:#,##0.00}";
                    plotModel.Series.Add(sr);

                }
                sPrevType = sCurrentType;
                iCount += 1;
            }

            if (iCount == _dateodr.Count)
            {
                sr = new ColumnSeries();
                sr.Title = sPrevType;
                sr.LabelPlacement = LabelPlacement.Outside;
                sr.StrokeColor = OxyColors.Black;
                sr.StrokeThickness = 1;
                //sr.LabelFormatString = "{0:#,##0.00}";
                sr.FontSize = 10;
                plotModel.Series.Add(sr);
            }

            for (int i = 0; i < iCount; i++)
            {
                ColumnSeries ssr = (ColumnSeries)plotModel.Series[i];
                var colIm = new ColumnItem();
                colIitm.Value = double.Parse(lstI[i].ToString()) / 1000;
                ssr.Items.Add(colIitm);

                var colDOitm = new ColumnItem();
                colDitm.Value = double.Parse(lstD[i].ToString()) / 1000;
                ssr.Items.Add(colDitm);

                var colSitm = new ColumnItem();
                colSitm.Value = double.Parse(lstS[i].ToString()) / 1000;
                ssr.Items.Add(colSitm);

            }


            categoryaxis.Labels.Add("I");
            categoryaxis.Labels.Add("Dr");
            categoryaxis.Labels.Add("Sr");


            switch (sUnitType)
            {
                case "2":
                    valueAxis = new LinearAxis { Position = AxisPosition.Left, MinimumPadding = 0, MaximumPadding = 0.06, AbsoluteMinimum = 0, Title = "m", Angle = 90, FontWeight = FontWeights.Bold, FontSize = 15 };
                    break;

                case "qy":
                    valueAxis = new LinearAxis { Position = AxisPosition.Left, MinimumPadding = 0, MaximumPadding = 0.06, AbsoluteMinimum = 0, Title = "Qy", Angle = 90, FontWeight = FontWeights.Bold, FontSize = 15 };
                    break;

                case "Am":
                    valueAxis = new LinearAxis { Position = AxisPosition.Left, MinimumPadding = 0, MaximumPadding = 0.06, AbsoluteMinimum = 0, Title = "Am", Angle = 90, FontWeight = FontWeights.Bold, FontSize = 15 };
                    break;
            }

            plotModel.Axes.Add(categoryaxis);
            plotModel.Axes.Add(valueAxis);

        }


        PlotView plot = new PlotView
        {
            VerticalOptions = LayoutOptions.Fill,
            HorizontalOptions = LayoutOptions.Fill,
            BackgroundColor = Color.White,
            Model = plotModel
        };


        Content = plot;

0 个答案:

没有答案