Hi,
i have a Xamarin iOS chart that uses custom labels. My issue is the width of the Vertical axis labels: for a certain data set, the default vertical labels look like "0.22, 0.4599999999, 1.2999999999...."; when i set the custom labels to "0.1, 0.4, 5 ....", the width of the labels remains very big, just as if the large labels were still showing. The same thing happens if i set the range to something like "0.4599999999...." - the custom labels will have a lot of padding.
How can i force the labels to resize?
Thanks!
3 Answers, 1 is accepted
Thank you for writing.
Could you, please, send me a code snippet of your setup so I could investigate the issue further? Thank you in advance for your cooperation.
Regards,
Adrian
Telerik by Progress
Hi Adrian,
here is my code:
Initially:
private void SetupChart()
{
Chart = new TKChart(CGRect.Inflate(ChartContainerView.Bounds, 0, 0);
Chart.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
Chart.Delegate = chartDelegate;
ChartContainerView.AddSubview(Chart);
}
Then, when the ChartModel becomes available, i start customising it:
Chart.AllowAnimations = true;
Chart.BeginUpdates();
Chart.RemoveAllData();
var dataPoints = GetDataPoints();
var series = new TKChartLineSeries(dataPoints.ToArray());
StyleSeries(series);
Chart.AddSeries(series);
var axis = new TKChartDateTimeAxis(ChartModel.MinDate, ChartModel.MaxDate);
AdjustXAxis(axis);
Chart.XAxis = axis;
AdjustYAxisRange();
Chart.EndUpdates();
AddChartBandAnnotation();
StyleChart();
Here are the details of the methods i am using:
void StyleSeries(TKChartLineSeries series)
{
........
palette settings, stroke colors, etc
........
}
void StyleChart()
{
Chart.GridStyle.HorizontalLinesHidden = true;
Chart.GridStyle.HorizontalFill = null;
Chart.GridStyle.HorizontalAlternateFill = null;
Chart.GridStyle.VerticalLinesHidden = false;
Chart.GridStyle.VerticalFill = null;
Chart.GridStyle.VerticalAlternateFill = null;
}
void AdjustYAxisRange()
{
Range axisRange = ChartModel.RoundedVerticalAxisRange();
Chart.YAxis.Range = new TKRange(new NSNumber(axisRange.Min), new NSNumber(axisRange.Max));
//force the axis not to display any default labels
((TKChartNumericAxis)Chart.YAxis).MajorTickInterval = axisRange.Span;
}
//Call this after EndUpdates();
void AddChartBandAnnotation()
{
Chart.RemoveAllAnnotations();
var range = GetChartBandRange();
if (range != null)
{
var fillColor = new UIColor(0.83f, 0.92f, 0.88f, 0.6f);
var strokeColor = new UIColor(0.6f, 0.85f, 0.76f, 0.8f);
var fill = new TKSolidFill(fillColor);
var stroke = new TKStroke(strokeColor, 1.5f);
var annotation = new TKChartBandAnnotation(range, Chart.YAxis, fill, stroke);
Chart.AddAnnotation(annotation);
}
var labels = GetVerticalAxisLabels();
if (labels.Count > 0)
{
Chart.YAxis.CustomLabels = labels;
Chart.YAxis.Style.LabelStyle.TextAlignment = TKChartAxisLabelAlignment.Left;
Chart.YAxis.Style.LabelStyle.FirstLabelTextAlignment = TKChartAxisLabelAlignment.Left;
Chart.YAxis.Style.LabelStyle.TextOffset = new UIOffset(2, 0);
Chart.YAxis.Style.LabelStyle.FirstLabelTextOffset = new UIOffset(2, 0);
Chart.YAxis.Style.LabelStyle.TextHidden = false;
}
else
{
Chart.YAxis.Style.LabelStyle.TextHidden = true;
}
Chart.YAxis.AllowPan = false;
Chart.YAxis.AllowZoom = false;
AdjustYAxisRange();
Chart.Update();
}
Thanks!
Thank you for the code snippet. I can confirm that this issue exists in TKChart. The width of the axis is measured considering the width of the default labels not the custom ones. I logged this issue and it will be addressed in a future release. I also updated your Telerik points for reporting this issue.
A possible workaround is to use the axis' LabelFormat property to format the labels with 0 digits after the decimal point. I hope this helps.
Regards,
Adrian
Telerik by Progress