How do you use the Zooming UI in MSChart for .Net

2019-05-28 17:28发布

问题:

I have just started looking at potentially using the MSChart control for .Net 3.5 for a project that will be starting shortly. One of the requirements of the project is that the users be able to zoom into the charts to see small data points more clearly when necessary.

I have looked at a number of tutorials and the either do not mention zooming, or just give a brief bit of information about how to enable it and seem to assume that using it is so obvious that it requires no explanation.

I created a quick test project, added the control to the form, and then added a few Points to the default Series. I then went into the ChartAreas collection and made sure that in the default ChartArea, the Zoomable property was set to True in the ScaleView property of all of the Axis members.

When I run the application my chart shows all correctly, but I cannot fathom any way to zoom into it. I have tried clicking on it, double clicking, the scroll wheel, ctrl-scroll wheel, ctrl-+, and many other things.

I am obviously missing something. Could someone please tell me what I am doing wrong, how I enable the zooming UI, and how I actually use the zooming UI?

I am on Windows 7, using VS2012.

Thank you.

[edit: fixed idiotic spelling error in title]

回答1:

Doing something like the following should allow you to Zoom using the Mouse Left Click and Drag:

private void ZoomToggle(bool Enabled)
{
    // Enable range selection and zooming end user interface
    this.cwSubplot.ChartAreas(0).CursorX.IsUserEnabled = Enabled;
    this.cwSubplot.ChartAreas(0).CursorX.IsUserSelectionEnabled = Enabled;
    this.cwSubplot.ChartAreas(0).CursorX.Interval = 0;
    this.cwSubplot.ChartAreas(0).AxisX.ScaleView.Zoomable = Enabled;
    this.cwSubplot.ChartAreas(0).AxisX.ScrollBar.IsPositionedInside = true;
    this.cwSubplot.ChartAreas(0).AxisX.ScrollBar.ButtonStyle = System.Windows.Forms.DataVisualization.Charting.ScrollBarButtonStyles.SmallScroll;
    this.cwSubplot.ChartAreas(0).AxisX.ScaleView.SmallScrollMinSize = 0;

    this.cwSubplot.ChartAreas(0).CursorY.IsUserEnabled = Enabled;
    this.cwSubplot.ChartAreas(0).CursorY.IsUserSelectionEnabled = Enabled;
    this.cwSubplot.ChartAreas(0).CursorY.Interval = 0;
    this.cwSubplot.ChartAreas(0).AxisY.ScaleView.Zoomable = Enabled;
    this.cwSubplot.ChartAreas(0).AxisY.ScrollBar.IsPositionedInside = true;
    this.cwSubplot.ChartAreas(0).AxisY.ScrollBar.ButtonStyle = System.Windows.Forms.DataVisualization.Charting.ScrollBarButtonStyles.SmallScroll;
    this.cwSubplot.ChartAreas(0).AxisY.ScaleView.SmallScrollMinSize = 0;
    if (Enabled == false) {
        //Remove the cursor lines
        this.cwSubplot.ChartAreas(0).CursorX.SetCursorPosition(double.NaN);
        this.cwSubplot.ChartAreas(0).CursorY.SetCursorPosition(double.NaN);
    }
}

Where this.cwSubplot is the Chart object you want zooming to work on.