【问题标题】:Drawing a chart in which I manipulate the zoom in and out绘制一个图表,我在其中操纵放大和缩小
【发布时间】:2014-06-13 11:38:34
【问题描述】:

我正在绘制一个图表,其中填充了从不同程序获得的数据。我想做两个按钮来放大和缩小。我看到我可以使用 AxisX.ScaleView 中的不同功能,我正在使用这些功能。我快到了,但是在绘制图表的那一刻我有一个问题:如果您看到图像1,这是执行不同程序并第一次绘制后的图表。当我进行放大和缩小时,最后一个条形图(图 2 中的第 22 周)被切成两半并且不会恢复到原来的大小。

有谁知道如何操纵 X 轴的开始和结束位置以进行缩放?有谁知道如何获得图表区域开始和结束的初始值?我放置了我的函数代码来缩放图表:

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();
}

【问题讨论】:

    标签: c# charts mschart


    【解决方案1】:

    您的第一行错误地设置了轴的最大值:chartReport.ChartAreas[0].AxisX.Maximum = chartReport.Series[0].Points.Count; 将其设置为 22,而实际上应该是 23(基于第一张图像)。

    如果您的数据总是这样,只需添加 1:

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

    不幸的是,在实际绘制图表之前,使用自动最小/最大值不会为您提供实际值。如果您的图表很少有DataPoints,这不是问题,因为您可以调用chartReport.Refresh(); 或类似的东西,然后从轴中获取值。但是,如果您有很多积分,Refresh() 将花费很长时间,这是不可取的。在我广泛使用图表时,我最终自己设置了轴范围,因此我可以完全控制,而不是使用自动最小/最大值。

    【讨论】:

    • 就是这样。我认为这将是一件小事,会有所作为。已接受答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 2013-07-28
    • 2017-04-02
    • 1970-01-01
    • 2014-11-07
    相关资源
    最近更新 更多