【问题标题】:Getting a Gridline / Interval at point 0 of the X-Axis of a Chart (flexible axis-scaling)在图表 X 轴的点 0 处获取网格线/间隔(灵活的轴缩放)
【发布时间】:2015-10-21 13:22:26
【问题描述】:

我的图表是这样的:

我可以通过更改图表下方文本框中的值(X-min 和 X-max)来操纵 x 轴的比例。最小值和最大值总是四舍五入为可被 10 整除的数字,没有余数。 (858 得到 860,-107 得到 -110)。现在我正在寻找一种解决方案来获得 X 轴刻度,它始终在 X 点“0”处有一条线(在图片中标记为红色)。

我正在使用 C#。

下面是部分代码:

private void textBoxXmax_TextChanged(object sender, EventArgs e)
    {
        double max, min;

        Double.TryParse(this.textBoxXmin.Text, out min);
        //checks if the Entering is a double number and if it is greater than the min value
        if (Double.TryParse(this.textBoxXmax.Text, out max) && max > chartTest.ChartAreas[0].AxisX.Minimum)
        {
            max = Math.Round(max / 10) * 10; //round to tens (113->110)
            //MessageBox.Show(x.ToString());
            this.textBoxXmax.BackColor = Color.White;
            chartTest.ChartAreas[0].AxisX.Maximum = max;
            //chartCharacteristicCurvesResistanceThermometer.ChartAreas[0].AxisX.Interval = (max-min)/10;
            //Problem should be here

            //set the YScaleMax
            changeYScala(chartTest);
        }
        else
            //if not the textbox is highlighted 
            this.textBoxXmax.BackColor = Color.Orange;

        //double y;
        //checks if the Entering is a double number and if it is smaler than the max value
        if (Double.TryParse(this.textBoxXmin.Text, out min) && min < chartTest.ChartAreas[0].AxisX.Maximum)
        {
            min = Math.Round(min / 10) * 10; //round to tens (113->110)
            this.textBoxXmin.BackColor = Color.White;
            chartTest.ChartAreas[0].AxisX.Minimum = min;
            //same calculation for Interval here

            changeYScala(chartTest);
        }
        else
            //if not the textbox is highlighted 
            this.textBoxXmin.BackColor = Color.Orange;
    }

【问题讨论】:

  • 这是我如何缩放 X 轴的代码。但是我没有得到 Intervall,因为 X 点“0”处总是有一条线

标签: c# winforms charts axis scaling


【解决方案1】:

您可以使用chart.ChartAreas[0].AxisX.IntervalOffset 更改间隔的偏移量,另外我通过反复试验发现了这一点,我没有来源说明为什么会这样,但正确的偏移量似乎是:

chart.ChartAreas[0].AxisX.IntervalOffset = 
    (-chart.ChartAreas[0].AxisX.Minimum) % chart.ChartAreas[0].AxisX.Interval;

此解决方案要求您手动设置AxisX.Minimum(如果设置为自动,AxisX.Minimum 返回 NaN)。

编辑:还要求您将 chart.ChartAreas[0].AxisX.Interval 设置为 Auto 以外的其他值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-05
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 2014-05-06
    • 1970-01-01
    • 2015-10-21
    相关资源
    最近更新 更多