How to trim the values of x on X-axis in Chart control in Winforms?

大兔子大兔子 提交于 2019-12-25 04:27:34

问题


I have a winforms application which contains a chart control called

comparisonChart

I have implemented Zoom capability in the chart control by subscribing to the mousewheel event and doing the following.

private void comparisonChartMouseWheel(object sender, MouseEventArgs e)
    {
        if (e.Delta < 0)
        {
            this.comparisonChart.ChartAreas[0].AxisX.ScaleView.ZoomReset(0);
            this.comparisonChart.ChartAreas[0].AxisY.ScaleView.ZoomReset(0);
        }
        else if (e.Delta > 0)
        {
            double xMin = this.comparisonChart.ChartAreas[0].AxisX.ScaleView.ViewMinimum;
            double xMax = this.comparisonChart.ChartAreas[0].AxisX.ScaleView.ViewMaximum;
            double yMin = this.comparisonChart.ChartAreas[0].AxisY.ScaleView.ViewMinimum;
            double yMax = this.comparisonChart.ChartAreas[0].AxisY.ScaleView.ViewMaximum;
            double posXStart = this.comparisonChart.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) - (xMax - xMin) / 4;
            double posXFinish = this.comparisonChart.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) + (xMax - xMin) / 4;
            double posYStart = this.comparisonChart.ChartAreas[0].AxisY.PixelPositionToValue(e.Location.Y) - (yMax - yMin) / 4;
            double posYFinish = this.comparisonChart.ChartAreas[0].AxisY.PixelPositionToValue(e.Location.Y) + (yMax - yMin) / 4;
            this.comparisonChart.ChartAreas[0].AxisX.ScaleView.Zoom(posXStart, posXFinish);
            this.comparisonChart.ChartAreas[0].AxisY.ScaleView.Zoom(posXStart, posXFinish);
        }

    }

When I zoom in on the chart, the X axis values show decimal values.

To remove the decimal values I have also done the following.

comparisonChart.Series[0].XValueType = ChartValueType.Int32;

But again it is showing decimal values when I zoom in.


回答1:


The chart control as such doesn't have this logic.

Check the solution of this question.

Hope this helps.

Edit: In the meanwhile, I tried the below code snippet in a sample program with your zoom code. Now the axis values are displayed without decimals.

chart1.ChartAreas[0].AxisX.LabelStyle.Format = "0";
chart1.ChartAreas[0].AxisY.LabelStyle.Format = "0";


来源:https://stackoverflow.com/questions/21422186/how-to-trim-the-values-of-x-on-x-axis-in-chart-control-in-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!