0

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.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
SNP
  • 37
  • 11

2 Answers2

1

Let's say that you have collection of your data points which you want to display on chart:

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

Then, the first thing you can do is to set the Minimum and Maximum for your axes, depending on the values you have:

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;

If minY = maxY = 0, then you need to set axis range manually to some specific values as required, like:

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

Then assign its values to axis.

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

Repeat the same steps for X axis.

AND one more thing: why you do this in loop:

    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;

It it enough if you do it once, at the beginning, outside foreach loop.

UPDATE 13/06/2017:

Instead of max you must assign minX, maxX, minY and maxY which you have calculated above

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;

I would also suggest not to use a constant offset like you did (+/- 3), but as I said earlier, try to extend the axis range by percentages of minimum and maximum values:

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;

Thanks to this, you are independent of the values which you are displaying - it will be fine for both small values like 1, 0.5 etc as well as for big values like 1500.0, 99999.0.

pitersmx
  • 935
  • 8
  • 27
  • @pitersmx.why in here `0.9 and 1.1` – SNP Jun 09 '17 at 10:08
  • @pitersmx..It is working.thank you so much.please give me explanation about `0.9 and 1.1` – SNP Jun 09 '17 at 10:20
  • @SNP - those 0.9 and 1.1 are just an example - you could neglect those multipliers, I just wanted to add some axis space below min and above max values so that on graph it would be more clear :) – pitersmx Jun 09 '17 at 10:40
  • I want (means + / - 0.3) so shall I change your code `chart1.ChartAreas[0].AxisY.Maximum = maxY+3; chart1.ChartAreas[0].AxisY.Minimum = minY-3;`Is it ok? – SNP Jun 09 '17 at 11:12
  • Yes, it's ok :) – pitersmx Jun 09 '17 at 11:19
  • ,,I plotted graph by getting .csv file data that I mentioned in my question.so now graph is plot very nice.that's ok.but now my problem is that plotted graphs all points are in between 2.4-3.5.so others x axis gaps do not have y value.so values are now in small area in that chart area.so how i scale my x axis and get clear chart.as an example This is working fine.but my x axis is not scale well.why is that.as an example we assume there are x axis 1.2 to 9.0 but the out put show only 2.4-3.5.bcause y values are only that range.any help sir – SNP Jun 12 '17 at 12:14
  • Provide code which are you currently using for scaling X and Y axis. – pitersmx Jun 12 '17 at 12:19
  • @ pitersmx ,I added my code as my answer.Sir,please look at that and give me a solution.I edited your code there.in my .csv file most of values are in some part of graph.so scale is not clear.how i show that.I added my out put window in my answer.please look that. – SNP Jun 13 '17 at 04:04
0

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;
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • thank you so much your help.It is working.thank you for showing my mistake. – SNP Jun 09 '17 at 10:00
  • .This is working fine.but my x axis is not scale well.why is that.as an example we assume there are x axis 1.2 to 9.0 but the out put show only 2.4-3.5.bcause y axis data are only that range.then data point are not clear well.how I increase that.any help sir – SNP Jun 12 '17 at 09:00
  • @SNP actually it would be the same procedure as with the Y-axis: `chart1.ChartAreas[0].AxisX.Minimum = 1.2; chart1.ChartAreas[0].AxisX.Maximum = 9.0;` – Mong Zhu Jun 12 '17 at 09:27
  • @ Mong Zhu ,thak you sir,but my question is how to scale x axis,in here x axis results are in between 2.4-3.5.then others are not get any values.so this shows in little and can't see the clear curves in plotted graph.so I asked how i increase that x axis scaling well. – SNP Jun 12 '17 at 09:40
  • @SNP are talking about zooming into your curve? – Mong Zhu Jun 12 '17 at 09:41
  • ,No sir,I plotted graph by getting .csv file data that I mentioned in my question.so now graph is plot very nice.that's ok.but now my problem is that plotted graphs all points are in between `2.4-3.5`.so others x axis gaps are not y value.so values are now in small area in that chart area.so how i scale my x axis and get clear chart.sir,can you understand my question? – SNP Jun 12 '17 at 09:49
  • @pitersmx.It is working but not get clear scale.it is same like this out put I want to sacle this output window range `3.076 to 4.421` in clear sacle with those curves.In here that curves are not clear.Please give your solution for this sir. – SNP Jun 13 '17 at 08:24
  • @pitersmx.thank you sir.It is solved.i reduced the mean value (means + / - 0.3) – SNP Jun 13 '17 at 11:23