0
votes

When I plot my data with linear Y-axis (not logarithmic), chart is good, as below:

enter image description here

But when I use logarithmic scale for Y-axis, my chart looks upside-down:

chart1.ChartAreas[0].CursorY.IsUserEnabled = true;
chart1.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisY.ScrollBar.Enabled = true;
chart1.ChartAreas[0].AxisY.ScrollBar.IsPositionedInside = true;
chart1.ChartAreas[0].CursorY.Interval = 1e-10;//zoom resolution threshold

/////////////////when I added following line, chart becomes upside-down:
chart1.ChartAreas[0].AxisY.IsLogarithmic = true;

using (StreamReader reader = new StreamReader(
                "Frequencies(Hz)_and_corresponding_SingularValues.txt"
                ))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] tokens = line.Split(new char[] { '\t' });
                    x = Convert.ToDouble(tokens[0]);
                    y = Convert.ToDouble(tokens[1]);
                    chart1.Series["Series1"].Points.AddXY(x, y);
                }
            }

Upside-down chart is shown below, does anybody know why it gets flipped:

enter image description here


Another question: when plotting with logarithmic scale, what is the best way to avoid exceptions due to zero/negative data without modifying original data?


EDIT: I thought maybe color of background and color of chart series are switched, therefore I added following line to test it, but chart is still upside-down:

chart1.Series["Series1"].Color = Color.Blue;
1
Is this really a question about MSChart? Log(x) is negative if x < 0.5, so I'm not sure what you expect the charting control to do. Note the log scale is usually used for numbers that grow very large, rather than very small.zeFrenchy
It'a math: ln(x) < 0 if x < 1. You can set chart1.ChartAreas[0].AxisY.IsReversed = true to invert the asix.Dmitry
@Dmitry That's right, if x is smaller than some threshold value then log(x) and ln(x) would be negative, I forgot about it. However when I changed chart type, chart looks good now.user3405291

1 Answers

2
votes

As mentioned in comments, the reason was math: if y<1 then Ln(y)<0. Eventually, I solved the problem with changing ChartType as below:

chart1.Series["Series1"].ChartType = SeriesChartType.Line;

Also, to avoid any future possible exception when having zero/negative data points in logarithmic scale, I modified code like this:

using (StreamReader reader = new StreamReader(
                "Frequencies(Hz)_and_corresponding_SingularValues.txt"
                ))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] tokens = line.Split(new char[] { '\t' });
                    x = Convert.ToDouble(tokens[0]);
                    y = Convert.ToDouble(tokens[1]);
                    /////////////////to skip zero/negative data points, 
                    /////////////////to avoid exceptions in logarithmic scale:
                    /////////////////singular values look like to be positive, but we add this just in case:
                    //chart1.Series["Series1"].Points.AddXY(x, y);
                    if(y>0){
                        chart1.Series["Series1"].Points.AddXY(x,y);
                    }
                }
            }

Now chart looks good:

enter image description here