Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using the Chart Control to plot data. When the x value reaches 360 degrees I want to wrap the data around and start plotting again at/near the start of the graph x axis. I do this by subtracting 360 from the x value if the x value is > 360. Now this causes a line to be drawn from the 360 value back to the start of the graph before plotting starts again. I want this line to be transparent/not drawn. I am simulating the plotting with a button - each button press plots the next point.

// Plot Data
float[,] buf = new float[19, 2]{
                    {0.0f, 0.0f},
                    {30, 0.3f},
                    {30, 0.9f},
                    {30, 1.9f},
                    {120, 2.0f},
                    {150, 2.1f},
                    {180, 2.2f},
                    {210, 2.3f},
                    {240, 2.3f},
                    {270, 2.3f},
                    {300, 2.3f},
                    {330, 2.3f},
                    {360, 2.3f},
                    {390, 2.0f},
                    {420, 1.8f},
                    {450, 1.5f},
                    {480, 1.1f},
                    {510, 0.9f},
                    {540, 0.7f},
                   };


public Graph()
{
    InitializeComponent();

    chart1.BackColor = Color.Aquamarine;

    chart1.Series["Series1"].LegendText = "Current";
    chart1.Series["Series1"].IsVisibleInLegend = false;

    chart1.ChartAreas[0].AxisY.Maximum = 2.500;
    chart1.ChartAreas[0].AxisY.Minimum = 0;
    chart1.ChartAreas[0].AxisY.Title = "Current mA";
    chart1.ChartAreas[0].AxisY.Interval = 0.500;
    chart1.ChartAreas[0].AxisX.Maximum = 360;
    chart1.ChartAreas[0].AxisX.Minimum = 0;
    chart1.ChartAreas[0].AxisX.Interval = 60;
    chart1.ChartAreas[0].AxisX.Title = "degrees";

    chart1.Show();
    chart1.Series["Series1"].Points.AddXY(0, 0);

}

bool revolutionFlag = false;
private void button2_Click(object sender, EventArgs e)
{
    if (cnt >= ( buf.Length / 2))
    {
        // End of data array so clear graph and reset data pointer
        cnt = 0;
        chart1.Series["Series1"].Points.Clear();
        revolutionFlag = false;
    }
    else
    {
        if (buf[cnt, 0] > 360)
        {
            float x = buf[cnt, 0] - 360;
            if (revolutionFlag == false)
            {
                // At 360 degrees - change colour before rewinding
                revolutionFlag = true;
                chart1.Series["Series1"].Color = Color.Transparent;
            }
            else
            {
                // Back to plotting
                chart1.Series["Series1"].Color = Color.Blue;
            }
            chart1.Series["Series1"].Points.AddXY(x, buf[cnt, 1]);
        }
        else
        {
            chart1.Series["Series1"].Color = Color.Blue;
            chart1.Series["Series1"].Points.AddXY(buf[cnt, 0], buf[cnt,1]);
        }
        cnt++;
    }


What I have tried:

I have tried to change the plot colour to transparent and then back to blue hoping this might achieve the line not being seen/drawn - as seen in my demo code. But this property seems to apply to all the points, ie I am just toggling the colour. I have looked for a Chart method which will move to XY position but have not come across anything.
Posted
Updated 7-Mar-18 14:56pm

1 solution

Have you considered to treat each crossing of 360 as a new serie to avoid the draw of the wrap?
C#
// Plot Data
float[,] buf1 = new float[13, 2]{
                    {0.0f, 0.0f},
                    {30, 0.3f},
                    {30, 0.9f},
                    {30, 1.9f},
                    {120, 2.0f},
                    {150, 2.1f},
                    {180, 2.2f},
                    {210, 2.3f},
                    {240, 2.3f},
                    {270, 2.3f},
                    {300, 2.3f},
                    {330, 2.3f},
                    {360, 2.3f},
                   };
float[,] buf2 = new float[7, 2]{
                    {0, 2.3f},
                    {30, 2.0f},
                    {60, 1.8f},
                    {90, 1.5f},
                    {120, 1.1f},
                    {150, 0.9f},
                    {180, 0.7f},
                   };

And make sure drawing parameters are same for both.
 
Share this answer
 
Comments
R Read 8-Mar-18 10:39am    
Thanks for that solution - I was thinking of doing that as a last option but was hoping for a simpler solution as I will also have other plots on the graph with their associated series.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900