Xamarin.Forms: Putting A Label Inside A BarChart Layout

636 Views Asked by At

I am using oxyplot to plot a bar chart. I wish to add an extra label in the chart. I've tried to use Textannotation but it's not showing on the chart. What is the mistake I'm making to cause the text not to appear, is it the DataPoint? Please help.

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

There are 0 best solutions below