Marialvy Martínez
Marialvy Martínez

Reputation: 286

Drawing a chart in which I manipulate the zoom in and out

I am drawing a chart which I populate with the data I obtain from different procedures. I want to make two buttons to zoom in and out. I saw that I can use different functions from AxisX.ScaleView and I am playing a bit with those. I am almost there but I have a problem at the moment of drawing the chart:If you see the image 1, this is the chart after executing the different procedures and drawing it for the first time. When I do a zoom in and a zoom out, the last bars (Week 22 from image 2) are cut in half and doesn't go to its original size.

Does anyone have any idea how can I manipulate the start and end position for the Axis X in order to make the zoom? Does anyone know how to get the initian values of start and end of the Chart Area? I place the code of my function to make the zoom of the chart:

private void setSize(int zoom)
{
 int blockSize = (Convert.ToInt32(tbZoom.Text) + zoom) / 100;

 // set view range to [0,max]
 chartReport.ChartAreas[0].AxisX.Minimum = 0;
 chartReport.ChartAreas[0].AxisX.Maximum = chartReport.Series[0].Points.Count;

 // enable autoscroll
 chartReport.ChartAreas[0].CursorX.AutoScroll = true;
 chartReport.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;

 // let's zoom to [0,blockSize] (e.g. [0,100])
 chartReport.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
 chartReport.ChartAreas[0].AxisX.ScaleView.SizeType = DateTimeIntervalType.Number;
 int actualHeight = chartReport.Height;
 int actualWidth = chartReport.Width;
 int position = 0;
 int size = blockSize;
 chartReport.ChartAreas[0].AxisX.ScaleView.Zoom(position, size);

 // disable zoom-reset button (only scrollbar's arrows are available)
 chartReport.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;

 // set scrollbar small change to blockSize (e.g. 100)
 chartReport.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = blockSize;
 tbZoom.Text = (blockSize * 100).ToString();
}

This is image 1:

And this is image 2:

Upvotes: 0

Views: 760

Answers (1)

mmathis
mmathis

Reputation: 1610

Your first line is setting the maximum of the axis wrong: chartReport.ChartAreas[0].AxisX.Maximum = chartReport.Series[0].Points.Count; sets it to 22, when it really should be 23 (based on the first image).

If your data will always look like this, simply add 1:

chartReport.ChartAreas[0].AxisX.Maximum = chartReport.Series[0].Points.Count + 1;

Unfortunately, using the automatic min/max values won't give you the actual values until the chart is actually drawn. If your chart has few DataPoints this isn't a problem, as you can just call chartReport.Refresh(); or something similar and then get the values from the axes. But, if you have a lot of points, the Refresh() will take a long time which is undesirable. In my extensive use of the charts, I wound up setting the axis ranges myself so I have full control, rather than using the automatic min/max values.

Upvotes: 2

Related Questions