WinForms范围条形图坐标轴未按预期呈现

gr8qqesn  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(118)

我尝试使用System.Windows.Forms.DataVisualization.Charting.SeriesChartType.RangeBar生成范围条形图。当我将x轴作为整数添加时,它会正确呈现,如下所示:

int[] x = { 0, 0, 0, 1, 1, 1 };
        int[] y = { 10, 2, 35, 1, 10, 20 };
        int[] y2 = { 35, 4, 36, 2, 15, 40 };
        var xValue = x.ToList();
        var yValue1 = y.ToList();
        var yValue2 = y2.ToList();

        chart2.Series[0].Points.DataBindXY(x, y, y2);

但是,当我将x轴整数转换为字符串时,图表并不像上面那样呈现:

string[] x = { "Test", "Test", "Test", "Test1", "Test1", "Test1" };
        int[] y = { 10, 2, 35, 1, 10, 20 };
        int[] y2 = { 35, 4, 36, 2, 15, 40 };
        var xValue = x.ToList();
        var yValue1 = y.ToList();
        var yValue2 = y2.ToList();
        chart2.Series[0].XValueType = 
        System.Windows.Forms.DataVisualization.Charting.ChartValueType.String;

        chart2.Series[0].Points.DataBindXY(x, y, y2);

有没有一种很好的解决方法可以在x轴上使用字符串值,同时在y轴上保持int或DateTime值?在此先感谢您的帮助。

6tr1vspr

6tr1vspr1#

你可以试试!

int[] tmpx = { 0, 0, 0, 1, 1, 1 };
            string[] x = { "Test", "Test", "Test", "Test1", "Test1", "Test1" };
            int[] y = { 10, 2, 35, 1, 10, 20 };
            int[] y2 = { 35, 4, 36, 2, 15, 40 };
            //var xValue = x.ToList();
            //var yValue1 = y.ToList();
            //var yValue2 = y2.ToList();
            for (int i = 0; i<x.Length; i++)
            {
                int index = chart1.Series[0].Points.AddXY(tmpx[i], y[i], y2[i]);
                chart1.Series[0].Points[index].AxisLabel = x[i];
            }

相关问题