首页 > 解决方案 > Multiple Series in Line chart in Winform using C#

问题描述

I am new in Winform Application. I am trying to implement a line chart which have multiple series and have a checkedListbox to select the particular series.

Code:

if (tbROI.SelectedTab == tbROI.TabPages["tbPageROIPoint"])
            {
                //If all ROI TAB
                myIndex = GetMyChartIndex(mSeries, chartPointROI); // 4 for Point ROI tab

                m_PointDataCounter++;
                if (m_PointDataCounter > 15)
                {
                    if (myIndex > 5)
                    {
                        chartPointROI.Series[mSeries].Points.RemoveAt(0);
                        m_PointDataCounter--;
                    }
                }

                if (cbListPOI.GetItemChecked(ROIIndex))
                {
                    chartPointROI.Series[mSeries].Points.AddXY(timestring, mData);

                    chartPointROI.ResetAutoValues();
                }
            }

Using this code I am putting the data on the chart control. The X-axis representing Time and Y-axis representing Data.

Initailly when I select any item of listbox the series starts from left side, but after sometime if I start one more series it will also start from left side but I want to start that from the current time which is representing on X-axis.

And when I stop any series after some time if I again start the same series I want to some gap in the series so that it can be clearly seen that series is been stopped.

In my case the series always starts from left side. And if I stop any series and start that again It will continue where it stopped.

Thanks in advance

EDIT:

enter image description here

It is showing that

标签: c#winformschartsdata-visualization

解决方案


Here is an example of how to remove a few DataPoints and also of how to restore them.

Note the flat line in the gap. If you want to 'remove' that line best color the last point transparent; I have added commented code for this.

List<DataPoint> marked = new List<DataPoint>();
int markedStartIndex = -1;

private void button1_Click(object sender, EventArgs e)
{
    // I create a testperiod to remove
    DateTime dt0 = DateTime.Now.AddMonths(2);
    DateTime dt1 = dt0.AddHours(123);
    DateTime dt2 = dt0.AddHours(173);

    // convert to doubles:
    double startPeriod = dt1.ToOADate();
    double endPeriod = dt2.ToOADate();

    // short reference
    Series s = chart1.Series[0];
    // select the points in the period. pick your border conditions!
    marked = s.Points.Cast<DataPoint>()
                     .Where(x => x.XValue > startPeriod && x.XValue < endPeriod)
                     .ToList();

    if (marked.Count < 1) return;

    // remember where we started to remove
    markedStartIndex = s.Points.IndexOf(marked.First()); 
    foreach (DataPoint dp in marked) s.Points.Remove(dp);

    // Optionally 'hide' the gap line
    //if (markedStartIndex > 0) s.Points[markedStartIndex].Color = Color.Transparent;
}

The code to bring them back inserts them at the right spot and then clears the points.:

private void button2_Click(object sender, EventArgs e)
{
    Series s = chart1.Series[0];
    // optionally re-color the gap-line 
    //if (markedStartIndex > 0) s.Points[markedStartIndex].Color = s.Color;

    foreach (DataPoint dp in marked) s.Points.Insert(markedStartIndex++, dp);
    marked.Clear();
}

Result with a transparent gap:

enter image description here

You could also color the gap in red and also store more than one set of points; for this you would have to store the starting points as well as make sure to manage multiple periods when you re-insert them!

As an alternative to actually removing the points you could also choose to simply color them transparent..


推荐阅读