Page 2 of 2
Re: FirstVisibleIndex and LastVisibleIndex while zooming
Posted: Tue Aug 13, 2019 5:31 am
by 15685014
Christopher wrote: ↑Mon Aug 12, 2019 11:41 am
Try setting the Labels separation to zero, e.g.
Up to 60 points it's working:
But for example for 70 points (when labels start to overlap each other) - chart itself performs labels thinning (my function Bottom_GetAxisDrawLabel is disabled here):
Re: FirstVisibleIndex and LastVisibleIndex while zooming
Posted: Tue Aug 13, 2019 7:58 am
by Christopher
Hello,
okay, well, another variation on an earlier technique seems to work in these circumstances, i.e.
Code: Select all
public partial class Form1 : Form
{
//max visible labels
private const Int32 maxDisplayedLabelsForBottomaAxis = 20;
List<string> _labels = new List<string>();
int _count;
public Form1()
{
var rnd = new Random();
InitializeComponent();
tChart1.Zoomed += TChart1_Zoomed;
tChart1.UndoneZoom += TChart1_UndoneZoom;
tChart1.AfterDraw += TChart1_AfterDraw;
_count = 4000;
for (int i = 0; i < _count; i++)
{
line1.Add(i, rnd.Next(5));
_labels.Add($"Point NoClue #{rnd.Next()}");
if (ShouldDrawIndex(i, 0, _count))
{
tChart1.Axes.Bottom.Labels.Items.Add(i, _labels[i]);
}
}
}
private void TChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
{
this.Text = $"Number of labels = {tChart1.Axes.Bottom.Labels.Items.Count}";
}
private void TChart1_UndoneZoom(object sender, EventArgs e)
{
tChart1.Axes.Bottom.Labels.Items.Clear();
for (int i = 0; i < _count; i++)
{
if (ShouldDrawIndex(i, 0, _count))
{
tChart1.Axes.Bottom.Labels.Items.Add(i, _labels[i]);
}
}
}
private bool ShouldDrawIndex(int index, int firstVisible, int lastVisible)
{
if ((lastVisible - firstVisible) > maxDisplayedLabelsForBottomaAxis)
{
var drawEvery = ((lastVisible - 1 - firstVisible) / maxDisplayedLabelsForBottomaAxis) + 1;
return index % drawEvery == 0;
}
return true;
}
private void TChart1_Zoomed(object sender, EventArgs e)
{
tChart1.Axes.Bottom.Labels.Items.Clear();
var xvalues = line1.XValues.Value.Where((x, i) => i < _count);
var min = line1.XValues.IndexOf(xvalues.Where(x => x >= tChart1.Axes.Bottom.Minimum).First());
var max = line1.XValues.IndexOf(xvalues.Where(x => x <= tChart1.Axes.Bottom.Maximum).Last());
for (int i = min; i <= max; i++)
{
if(ShouldDrawIndex(i, min, max))
{
tChart1.Axes.Bottom.Labels.Items.Add(i, _labels[i]);
}
}
}
}
Re: FirstVisibleIndex and LastVisibleIndex while zooming
Posted: Tue Aug 13, 2019 10:07 am
by 15685014
Christopher wrote: ↑Tue Aug 13, 2019 7:58 am
okay, well, another variation on an earlier technique seems to work in these circumstances, i.e.
So we've finally returned to using
Zoomed and
UndoneZoom events
Looks like it's working now, many thanks.
P. S. As I can see this code is in fact manually calculating
FirstVisibleIndex and
LastVisibleIndex. First time i was surprised by line:
Code: Select all
var xvalues = line1.XValues.Value.Where((x, i) => i < _count);
As I expected
line1.XValues.Value to have exactly
_count items.
Could you explain please, why that collection has more items despite of the fact that I've added
_count points?
Re: FirstVisibleIndex and LastVisibleIndex while zooming
Posted: Tue Aug 13, 2019 10:38 am
by Christopher
bairog wrote: ↑Tue Aug 13, 2019 10:07 am
Looks like it's working now, many thanks.
You're welcome.
bairog wrote: ↑Tue Aug 13, 2019 10:07 am
Could you explain please, why that collection has more items despite of the fact that I've added
_count points?
The Value field is public but is for internal use. If you are uncomfortable using it, you can use the more standard API calls:
Code: Select all
List<double> xvalues = new List<double>();
for (int i = 0; i < _count; i++)
{
xvalues.Add(line1.XValues[i]);
}
//var xvalues = line1.XValues.Value.Where((x, i) => i < _count);