我想创建一个图表,以便用户可以选择自动缩放y轴,也可以在运行时手动更改轴。我能够自动缩放,直到y轴没有使用updow更改,但是当我更改任何向上时,自动缩放没有响应。
private void numUD_Graph_Ymax_ValueChanged(object sender, EventArgs e)
{
try
{
int newmax = (int)numUD_Graph_Ymax.Value;
int allowedmin = (int)numUD_Graph_Ymax.Minimum;
int allowedmax = (int)numUD_Graph_Ymax.Maximum;
if (newmax >= allowedmin && newmax <= allowedmax && newmax > chart.ChartAreas[0].AxisY.Minimum)
{
chart.ChartAreas[0].AxisY.Maximum = newmax;
chart.Update();
}
}
catch (Exception exception)
{
MessageBox.Show(exception.ToString());
}
}
private void numUD_Graph_Ymin_ValueChanged(object sender, EventArgs e)
{
try
{
int newmin = (int)numUD_Graph_Ymin.Value;
int allowedmin = (int)numUD_Graph_Ymin.Minimum;
int allowedmax = (int)numUD_Graph_Ymin.Maximum;
if (newmin >= allowedmin && newmin <= allowedmax && newmin < chart.ChartAreas[0].AxisY.Maximum)
{
chart.ChartAreas[0].AxisY.Minimum = newmin;
chart.Update();
}
}
catch (Exception exception)
{
MessageBox.Show(exception.ToString());
}
}
这是我自动缩放可见数据的按钮处理程序:
private void btn_AutoSize_Click(object sender, EventArgs e)
{
chart.ChartAreas[0].RecalculateAxesScale();
chart.Update();
updateUI();
}
private void updateUI()
{
numUD_Graph_Xmin.Value = (decimal)chart.ChartAreas[0].AxisX.Minimum;
numUD_Graph_Xmax.Value = (decimal)chart.ChartAreas[0].AxisX.Maximum;
numUD_Graph_XInterval.Value = (decimal)chart.ChartAreas[0].AxisX.Interval;
numUD_Graph_Ymin.Value = (decimal)chart.ChartAreas[0].AxisY.Minimum;
numUD_Graph_Ymax.Value = (decimal)chart.ChartAreas[0].AxisY.Maximum;
}
我哪里错了?如何在数字上调中自动缩放和加载新的坐标,同时允许用户使用向上的方式进一步修改图形。
答案 0 :(得分:1)
设置Axis
Minimum
和/或Maximum
值之一后,恢复自动缩放的关键是重置(全部)这些值:
private void btn_AutoSize_Click(object sender, EventArgs e)
{
chart.ChartAreas[0].AxisX.Minimum = double.NaN; /// <-- this is the magic 'number'
chart.ChartAreas[0].AxisX.Maximum = double.NaN;
chart.ChartAreas[0].AxisY.Minimum = double.NaN;
chart.ChartAreas[0].AxisY.Maximum = double.NaN;
// not quite sure about these lines
/*
chart.ChartAreas[0].RecalculateAxesScale();
chart.Update();
updateUI();
*/
}
注意:
RecalculateAxesScale
不应该是必要的,因为您没有对Customize事件进行编码。
updateUI();
要将这些NumericUpDowns
设置为有用的值非常困难,因为他们只接受数字并且不知道某个状态未设置的状态&#39;除了被禁用。
我会删除CheckBox cbx_AutoRange {Text = "Automatic Range"}
的按钮,并在numUD
更改为Checked
时实际停用Series
。取消选中后,我会将值设置为private void cbx_AutoRange_CheckedChanged(object sender, EventArgs e)
{
if (cbx_AutoRange.Checked)
{
chart.ChartAreas[0].AxisX.Minimum = double.NaN;
chart.ChartAreas[0].AxisX.Maximum = double.NaN;
chart.ChartAreas[0].AxisX.Interval = double.NaN;
chart.ChartAreas[0].AxisY.Minimum = double.NaN;
chart.ChartAreas[0].AxisY.Maximum = double.NaN;
numUD_Graph_Xmin.Enabled = false;
numUD_Graph_Xmax.Enabled = false;
numUD_Graph_XInterval.Enabled = false;
numUD_Graph_Ymin.Enabled = false;
numUD_Graph_Ymax.Enabled = false;
}
else
{
numUD_Graph_Xmin.Enabled = true;
numUD_Graph_Xmin.Value = (decimal)chart1.Series[0].Points.FindMinByValue().XValue;
numUD_Graph_Xmax.Value = (decimal)chart1.Series[0].Points.FindMaxByValue().XValue;
//.. etc
//.. etc
}
}
中的当前最小值/最大值,如下所示:
1.1.0
如果您只想将自动调节仅转换为一个轴,不确定为什么您有两个轴的代码?