如何根据 C# 中的数据集启用放大 X 和 Y 轴?

How to enable scaling-up the X & Y Axis es based on the data set in C#?

我想通过考虑 C# 中的数据集来缩放 X 轴和 Y 轴。

这是 .csv 文件中数据集的一部分

Name,Speed,Distance
Amal,20.50,100.20
Kamal,52.60,254.90
Nimal,42.00,245.00
Perera,20.30,142.00
Kasun,56.40,368.00
Piyal,45.60,784.00
Roy,45.00,521.00
Tony,25.00,36.00
Nikky,36.00,56.00
Jems,47.00,48.00
Jully,56.00,120.00
Tizz,78.00,354.00
Taly,45.00,100.00
Row,18.00,350.00
Saga,15.60,250.00
Peter,45.00,120.00
Taw,89.00,56.00
Nanny,78.60,487.00
Jumo,108.00,150.00

这是我用来绘制图表的代码。

private void Output_Load(object sender, EventArgs e)
{

    List<Graph> ObservingData = new List<Graph>(); // List to store all available Graph objects from the CSV

    // Loops through each lines in the CSV
    foreach (string line in System.IO.File.ReadAllLines(pathToCsv).Skip(1)) // .Skip(1) is for skipping header
    {
        // here line stands for each line in the csv file

        string[] InCsvLine = line.Split(',');

        // creating an object of type Graph based on the each csv line

        Graph Inst1 = new Graph();

        Inst1.Speed = double.Parse(InCsvLine[1]);
        Inst1.Distance= double.Parse(InCsvLine[2]);

        chart1.Series["Distance"].YAxisType = AxisType.Primary;
        chart1.Series["Distance"].Points.AddXY(Inst1.Speed, Inst1.Distance);
        chart1.Series["Distance"].ChartType = SeriesChartType.FastLine;

        ChartArea CA = chart1.ChartAreas[0];
        CA.AxisX.ScaleView.Zoomable = false;
        CA.AxisY.ScaleView.Zoomable = false;
        CA.CursorX.AutoScroll = true;
        CA.CursorX.IsUserSelectionEnabled = true;

    }
}

这是存储数据的class。

class Graph   
{
    public string Name { get; set; } // property to  store Name
    public double Speed{ get; set; } // property to store Speed
    public double Distance { get; set; } // property to store Distance
}

现在我想通过考虑 .csv 文件中的数据集来缩放 X 轴和 Y 轴。规模应该在条件下。

我通过举个例子来解释。 假设在数据集中我们有:

the Distance max = 784.00 & min = 36.00

那么 Y 轴应该只显示从 33787

的值
(means + / - 0.3)

程序应该希望在文件读取期间获取 .csv 文件中的最小值和最大值。

明智地考虑 X 轴。

你能告诉我如何编码吗?非常感谢任何帮助。

假设您收集了要在图表上显示的数据点:

List<Graph> ObservingData = new List<Graph>();

然后,您可以做的第一件事是根据您的值设置轴的最小值和最大值:

double minY = ObservingData.Min(x => x.Distance) * 0.9;
double maxY = ObservingData.Max(x => x.Distance) * 1.1;

double minX = ObservingData.Min(x => x.Speed) * 0.9;
double maxX = ObservingData.Max(x => x.Speed) * 1.1;

如果minY = maxY = 0,则需要根据需要手动设置轴范围为一些特定值,如:

if (minY == maxY && minY == 0)
{
   minY = -0.1;
   maxY = 0.1;
}

然后将它的值赋给轴。

chart1.ChartAreas[0].AxisY.Maximum = maxY;
chart1.ChartAreas[0].AxisY.Minimum = minY;

对 X 轴重复相同的步骤。

还有一件事:为什么要循环执行此操作:

    chart1.Series["Distance"].YAxisType = AxisType.Primary;
    chart1.Series["Distance"].ChartType = SeriesChartType.FastLine;

    ChartArea CA = chart1.ChartAreas[0];
    CA.AxisX.ScaleView.Zoomable = false;
    CA.AxisY.ScaleView.Zoomable = false;
    CA.CursorX.AutoScroll = true;
    CA.CursorX.IsUserSelectionEnabled = true;

如果你在开始时在 foreach 循环之外执行一次就足够了。

2017 年 6 月 13 日更新:

而不是 max 您必须分配 minX, maxX, minYmaxY 您在上面计算的

chart1.ChartAreas[0].AxisX.Minimum = minX - 3;
chart1.ChartAreas[0].AxisX.Maximum = maxX + 3;

chart1.ChartAreas[0].AxisY.Minimum = minY - 3;
chart1.ChartAreas[0].AxisY.Maximum = maxY + 3;

我还建议不要像您那样使用常量偏移量 (+/- 3),但正如我之前所说,尝试按最小值和最大值的百分比扩展轴范围:

double minY = ObservingData.Min(x => x.Distance) * 0.9;  // Yaxis min is 90% of Min value
double maxY = ObservingData.Max(x => x.Distance) * 1.1; // Yaxis max is 110% of Max value

double minX = ObservingData.Min(x => x.Speed) * 0.9; // like above
double maxX = ObservingData.Max(x => x.Speed) * 1.1;

因此,您独立于所显示的值 - 对于 1、0.5 等小值以及 1500.0、99999.0 等大值都没有问题。

program should be want to get Min and Max value in .csv file during in file read.

为什么加载完所有数据后不获取最小值和最大值?在您的情况下,似乎最好将加载数据部分与图表详细信息的初始化分开。我在您的代码中看到一点,您没有使用 List<Graph> ObservingData 来存储项目。我在解决方案中添加了它,因为这将使您能够在加载整个数据集后提取最小和最大距离值。然后就可以设置轴的间隔了。

private void Output_Load(object sender, EventArgs e)
{

    List<Graph> ObservingData = new List<Graph>(); // List to store all available Graph objects from the CSV

    // Loops through each lines in the CSV
    foreach (string line in System.IO.File.ReadAllLines(pathToCsv).Skip(1)) // .Skip(1) is for skipping header
    {
        // here line stands for each line in the csv file

        string[] InCsvLine = line.Split(',');

        // creating an object of type Graph based on the each csv line
        Graph Inst1 = new Graph();
        Inst1.Speed = double.Parse(InCsvLine[1]);
        Inst1.Distance= double.Parse(InCsvLine[2]);

        chart1.Series["Distance"].Points.AddXY(Inst1.Speed, Inst1.Distance);

        // you forgot to store the items:
        ObservingData.Add(Inst1);
    }

        // after the loop you can pull out the min and max values to adjust your axes:
        double min = ObservingData.Min(x=>x.Distance);
        double max = ObservingData.Maxn(x=>x.Distance);

        chart1.ChartAreas[0].AxisY.Minimum = min - 3;
        chart1.ChartAreas[0].AxisY.Maximum = max + 3;

        chart1.Series["Distance"].YAxisType = AxisType.Primary;
        chart1.Series["Distance"].ChartType = SeriesChartType.FastLine;

        ChartArea CA = chart1.ChartAreas[0];
        CA.AxisX.ScaleView.Zoomable = false;
        CA.AxisY.ScaleView.Zoomable = false;
        CA.CursorX.AutoScroll = true;
        CA.CursorX.IsUserSelectionEnabled = true;
}

编辑:

至于你的X轴:

不能像对待 Y 轴那样对待 X 轴。 X 轴显示 Speed 的值 所以你也应该得到这些值的最小值和最大值:

double minX = ObservingData.Min(x=>x.Speed);
double maxX = ObservingData.Maxn(x=>x.Speed);

下一点也是使用这些值! + 和 - 值也应该符合 Speed 值的范围 0.3 在这种情况下似乎更合适:

chart1.ChartAreas[0].AxisX.Minimum = minX - 0.3;    
chart1.ChartAreas[0].AxisX.Maximum = maxX + 0.3;