Search code examples
c#csvchartsscaleaxis

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


I want to scale X and Y axis by considering data set in C#.

This is part of data set in .csv file

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

This is the code that I used to plot the graph.

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;

    }
}

and this is the class that store data.

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
}

Now I want to scale this X and Y axis by considering data set in .csv file. Scale should be under condition.

I explain that by getting an example for that. lets say in data set we have:

the Distance max = 784.00 & min = 36.00

then that Y axis should show values only from 33 to 787

(means + / - 0.3)

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

like wise think about X axis.

Can you tell me how I to code that? Any help very appreciated.


Solution

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

    Why not get the min and max values after all data is loaded? In your case it seems to be preferable to split the loading the data part from the initialization of the chart details. One point I see in your code that you don't use List<Graph> ObservingData to store the items. I added it in the solution because this will enable you to pull out the minimum and maximum distance values after the entire data set is loaded. Then you can set the axis interval.

    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;
    }
    

    EDIT:

    As for you X-Axis:

    You cannot treat the X-axis as you treat the Y-axis. The X-axis shows values of Speed so you should also get these values for the minimum and maximum of it:

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

    The next point would be also to use these values! Also the + and - values should fit the range of the Speed values 0.3 seems to be more appropriate in this case:

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