Why does auto Zoom/Scroll not work for my Chart?
Solution 1
Have a look here: http://archive.msdn.microsoft.com/mschart There is an example there which does zooming/scrolling and much, much more! :)
Solution 2
I think you were actually really looking for this:
chart1.ChartAreas["Default"].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas["Default"].CursorY.IsUserSelectionEnabled = true;
used in conjunction with what you already have should work well, which should look like this:
// Set automatic zooming
chart1.ChartAreas["Default"].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas["Default"].AxisY.ScaleView.Zoomable = true;
// Set automatic scrolling
chart1.ChartAreas["Default"].CursorX.AutoScroll = true;
chart1.ChartAreas["Default"].CursorY.AutoScroll = true;
// Allow user selection for Zoom
chart1.ChartAreas["Default"].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas["Default"].CursorY.IsUserSelectionEnabled = true;
Solution 3
To enable easy zooming, add a trackbar and use it to zoom:
private void trackBar1_Scroll(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.ScaleView.Size = trackBar1.Maximum - trackBar1.Value;
chart1.ChartAreas[1].AxisX.ScaleView.Size = trackBar1.Maximum - trackBar1.Value;
(etc for however many chart areas you have)
}
the "maximium - value" is to so that the higher the trackbar value, the fewer points are shown (closer zoom)
and make sure that in designer the 'chart1->ChartAreas->Axes->(whichever axes)->scaleview->zoomable' is set to true
A scroll bar will normally appear when a datapoint exceeds the scaleview size of an axis, if it has been set (scrolling doesn't really work reliably if left at 'auto'), if it hasn't, set it, if a scrollbar doesn't appear, a trackbar can yet again be used:
private void trackBar2_ValueChanged(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.ScaleView.Position = trackBar2.Value;
chart1.ChartAreas[1].AxisX.ScaleView.Position = trackBar2.Value;
(etc for however many chart areas you have)
}
Make sure you set the "Maximum" in the trackbars to a nice high number (eg 5000) and "Value" to what you desire it to load at.
Have yet to notice too much of a difference between "trackBar_Scroll" and "trackBar_ValueChanged", except "ValueChanged" works if the trackbar is moved by the program or user mouse click, whereas "Scoll" only works if moved by users mouse click.
Anything I missed?
Solution 4
My users dislikes the standard behavior of the mschart zooming and scrolling. That's why I implement a mouse based zoom/scroll that use dragging and mousewheel on axises.
The source code is here: https://bitbucket.org/controlbreak/mschartfriend
It is very simple and short, you can change it very quickly if you need.
Comments
-
チーズパン almost 2 years
to make it short I checked on the "WinFormsChartSamples" provided by Microsoft. What I wanted to know is how to enable zooming and scrolling for Chartcontrols. The example which is shown there is pretty short.
using System.Windows.Forms.DataVisualization.Charting; ... // Set automatic zooming chart1.ChartAreas["Default"].AxisX.ScaleView.Zoomable = true; chart1.ChartAreas["Default"].AxisY.ScaleView.Zoomable = true; // Set automatic scrolling chart1.ChartAreas["Default"].CursorX.AutoScroll = true; chart1.ChartAreas["Default"].CursorY.AutoScroll = true; ...
I tried this and nothing happened, no zooming and no scrolling. I tried two things:
In Form1.Designer.cs I added that information to the chart.
chartArea1.Name = "ChartArea1"; chartArea1.CursorX.AutoScroll = true; chartArea1.CursorY.AutoScroll = true; chartArea1.AxisX.ScaleView.Zoomable = true; chartArea1.AxisY.ScaleView.Zoomable = true; this.chart1.ChartAreas.Add(chartArea1); this.chart1.Cursor = System.Windows.Forms.Cursors.Cross; legend1.Name = "Legend1"; this.chart1.Legends.Add(legend1); this.chart1.Location = new System.Drawing.Point(297, 62); this.chart1.Name = "chart1"; series1.ChartArea = "ChartArea1"; series1.Legend = "Legend1"; series1.Name = "Series1"; this.chart1.Series.Add(series1); this.chart1.Size = new System.Drawing.Size(963, 668); this.chart1.TabIndex = 6; this.chart1.Text = "chart1";
I tried to add it directly into the constructor in Form1.cs.
Perhaps it is important to mention that I am using OpenFileDialog in order to add data to the series:
private void openToolStripMenuItem_Click(object sender, EventArgs e) { Stream fileStream = null; OpenFileDialog fDialog = new OpenFileDialog(); fDialog.Title = "Open File.."; //First the description of the file separated by "|" fDialog.Filter = "((ASC files)| *.asc"; fDialog.InitialDirectory = @"C:\"; //Show Messagebox if the file was loaded (Source: MSDN - FileDialog.FilterProperty) if (fDialog.ShowDialog() == DialogResult.OK) { MessageBox.Show("The File was loaded successfully."); try { if ((fileStream = fDialog.OpenFile()) != null) { using (fileStream) { //Insert code for reading the stream here. Spectrum newSpectrum = new Spectrum(chart1.Series.Count, fDialog.FileName, fDialog.SafeFileName, DataHandler.readSpectrumFromFile(fileStream)); addSpectrumToView(newSpectrum); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
Any advice is welcome, thanks in advance,
BC++