Wrap text in CategoryAxis Label using OxyPlot BarChart.

Dear All,
We want to Wrap text in CategoryAxis Label . In My case Label Text approximate 50 character which consumed complete Screen width. how to define width and Wrap Label text . Code is.

XML

<?xml version="1.0" encoding="utf-8" ?>

<ContentView.BindingContext>
    <ViewModels:BarChartViewModel/>
</ContentView.BindingContext>
<AbsoluteLayout>
    <oxy:PlotView Model="{Binding BarModel}" VerticalOptions="Center" HorizontalOptions="Center" />
</AbsoluteLayout>

View Model Code:-

using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace MTTPeople.ViewModels
{
public class BarChartViewModel
{
public PlotModel BarModel { get; set; }

    public BarChartViewModel()
    {
        BarModel = CreateBarChart(false);
    }

    private PlotModel CreateBarChart(bool stacked)
    {
        var model = new PlotModel
        {
            Title = "",
            LegendPlacement = LegendPlacement.Outside,
            LegendPosition = LegendPosition.BottomCenter,
            LegendOrientation = LegendOrientation.Vertical,
            PlotAreaBorderColor = OxyColor.FromRgb(128, 128, 128),
            IsLegendVisible = false,
            LegendBorderThickness = 0
        };

        var s1 = new BarSeries { Title = "Series 1", BarWidth = 100, IsStacked = stacked, FillColor = OxyColor.FromRgb(237, 25, 92), StrokeColor = OxyColors.White, StrokeThickness = 1 };
        var s2 = new BarSeries { Title = "Series 2", BarWidth = 100, IsStacked = stacked, FillColor = OxyColor.FromRgb(237, 25, 92), StrokeColor = OxyColors.White, StrokeThickness = 1 };
        var categoryAxis = new CategoryAxis { Position =  AxisPosition.Left, IsZoomEnabled = false, IsPanEnabled = false };


        var valueAxis = new LinearAxis { Position = AxisPosition.Bottom, Maximum = 100, IsZoomEnabled = false, IsPanEnabled = false };

        valueAxis.MajorGridlineStyle = LineStyle.Solid;
        valueAxis.MajorGridlineThickness = 1;
        valueAxis.MinorGridlineThickness = 1;
        valueAxis.MinorGridlineStyle = LineStyle.Solid;

        for (int i = 0; i <= 14; i++)
        {
            if (i % 2 != 0)
            {
                s1.Items.Add(new BarItem { Value = 34 });
                s2.Items.Add(new BarItem { Value = 0 });
                categoryAxis.Labels.Add("No. of Training Opened Vs No. of Training Completed");
            }
            else
            {
                s1.Items.Add(new BarItem { Value = 50 });
                s2.Items.Add(new BarItem { Value = 50 });
                categoryAxis.Labels.Add("No. of Training Completed On Time");
            }
        }

        model.Series.Add(s1);
        model.Series.Add(s2);
        model.Axes.Add(categoryAxis);
        model.Axes.Add(valueAxis);
        return model;
    }
}

}

Answers

Sign In or Register to comment.