简体   繁体   中英

get y value from c# chart series?

I have chart series:

Series[] tempSeries = new Series[sensorNum];    // Series to hold current/past temperature data for plotting, for each sensor

I add new points to them:

tempSeries[i].Points.AddXY(current_time, temp_F[i]);        // Add new temperature data to series

Now I want to convert the series to string to send it via socket.

The question is how to get Y value from the series?

I tried this:

private string sendAll() {
        string myMsg = "";
        double[,] lastTemp = new double[4, 1200];
        double[,] lastWind = new double[4, 1200];
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 1200; j++){
                try
                {
                    lastTemp[i, j] = tempSeries[i].Points[j].YValues[0];
                    myMsg += lastTemp[i, j] + " ";
                }
                catch (Exception ex) {
                    lastTemp[i, j] = 0;
                    myMsg += 0 + " ";
                }
            }
        }
        myMsg += "; ";

        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 1200; j++)
            {
                try
                {
                    lastWind[i, j] = windSeries[i].Points[j].YValues[0];
                    myMsg += lastWind[i, j] + " ";
                }
                catch (Exception ex)
                {
                    lastWind[i, j] = 0;
                    myMsg += 0 + " ";
                }
            }
        }
        MessageBox.Show(myMsg);

        return myMsg;
    }

But my program is freezing...

Maybe it is not the only problem with your code but you should never use string concatenation in loops ( myMsg += 0 + " "; ...) cause strings are immutable. Use StringBuilder class instead. Like this:

    StringBuilder sb = new StringBuilder(); 
    for (int i = 0; i < 10000; i++)
    {
        sb.Append("x");
    } 
    string x = sb.ToString();

Here is detailed info on why strings are immutable and what are the consequences of it: http://en.morzel.net/post/2010/01/26/Why-strings-are-immutable-and-what-are-implications-of-it.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM