marcuthh
marcuthh

Reputation: 596

C# - Chart.Series[index].Points.Label not producing any label of datapoint

I have a chart which has 9 different series to it, only two of which can ever be enabled at the same time.

My user has asked that when the first datapoint is added to the chart, it is labelled 'Start', and this enables the button that will then allow the user to mark 'End'.

During the development of the solution, this wasn't a problem as I had control over which of the series were enabled at any one time. Now the facility has been added for the user to change this in the 'Options' menu (series name and enabled state written to 'Settings' file, to then be read in next time the program begins.

In order to try and do this, I created a List<> 'enabledSeries' in my updateChart function that finds the enabled of the chart series and adds them to the list. This has been done fine, and adding datapoints to the chart works using this method. For some reason, however, Labels now do not appear at the start of each series.

The code for updateChart() and subfunctions is shown below:

    public void updateChart(int minutesElapsed)
    {
        //int latestReading = 0; //local variable to hold va1ue from txtBP.Text
        chartVitals.ChartAreas[0].AxisY2.Enabled = AxisEnabled.True;

        chartVitals.Text = "Vitals Visual";
        //Create correct coordinate using reading and time taken
        //add newest (last) element in each list to respective chart series, along with value from timer
        chartVitals.Series["Heart Rate"].Points.AddXY(minutesElapsed, HRlist.Last());
        chartVitals.Series["Ventilation Rate"].Points.AddXY(minutesElapsed, VRlist.Last());
        chartVitals.Series["Blood Pressure"].Points.AddXY(minutesElapsed, BPlist.Last());
        chartVitals.Series["o2 Saturation"].Points.AddXY(minutesElapsed, BOlist.Last());
        chartVitals.Series["ET Co2"].Points.AddXY(minutesElapsed, ETCo2list.Last());
        chartVitals.Series["Vaporiser Setting"].Points.AddXY(minutesElapsed, VSlist.Last());
        chartVitals.Series["FI Agent"].Points.AddXY(minutesElapsed, FIAlist.Last());
        chartVitals.Series["ET Agent"].Points.AddXY(minutesElapsed, ETAlist.Last());
        chartVitals.Series["Fresh Gas Flow"].Points.AddXY(minutesElapsed, FGFlist.Last());

        //stores all enabled series (MAX 2)
        List<string> enabledSeries = new List<string>();

        //identify and isolate the enabled series in the chart
        identifyEnabled(enabledSeries);
   }

    public void identifyEnabled(List<string> enabledSeries)
    {
        //takes name of chart series at current index
        string seriesName = "";

        //access all items in list
        for (int index = 0; index < chartVitals.Series.Count; ++index)
        {
            //assign name to variable
            seriesName = chartVitals.Series[index].Name;

            //series with this name is enabled
            if (chartVitals.Series[seriesName].Enabled)
            {
                //add name to list
                enabledSeries.Add(seriesName);
            }
        }

        formatEnabled(seriesName, enabledSeries);
    }

    public void formatEnabled(string seriesName, List<string> enabledSeries)
    {

        //color series in by index (0 - blue, 1 - red)
        string blueSeries = enabledSeries.First();
        string redSeries = enabledSeries[enabledSeries.IndexOf(blueSeries) + 1];

        //access all elements in enabledSeries
        for (int enabledIndex = 0; enabledIndex < enabledSeries.Count; ++enabledIndex)
        {
            //access all series in chartVitals
            for (int seriesIndex = 0; seriesIndex < chartVitals.Series.Count; ++seriesIndex)
            {
                //when there is item in series
                if (chartVitals.Series[seriesName].Points.Count > 0)
                {
                    string start = "[Start]";

                    //set series type as line
                    chartVitals.Series[seriesName].ChartType = SeriesChartType.Line;
                    //apply label to first point of series
                    chartVitals.Series[seriesName].Points.First().Label = start;

                    //enable button to mark end
                    btnOpEnd.Enabled = true;
                }
            }
        }

        //apply colours to series
        chartVitals.Series[blueSeries].Color = Color.Blue;
        chartVitals.Series[redSeries].Color = Color.Red;
    }

The code is a bit intricate, but it's commented and should all be there. If anyone can point out what might be causing the problem or a simpler way of doing things, I would really appreciate it!

Thanks, Mark

Upvotes: 0

Views: 3087

Answers (1)

jsanalytics
jsanalytics

Reputation: 13188

Is this what you expected to see? enter image description here

EDIT: I changed formatEnabled to use seriesIndex instead of seriesName, like below:

    public void formatEnabled(string seriesName, List<string> enabledSeries)
    {

        //color series in by index (0 - blue, 1 - red)
        string blueSeries = enabledSeries.First();
        string redSeries = enabledSeries[enabledSeries.IndexOf(blueSeries) + 1];

        //access all elements in enabledSeries
        for (int enabledIndex = 0; enabledIndex < enabledSeries.Count; ++enabledIndex)
        {
            //access all series in chartVitals
            for (int seriesIndex = 0; seriesIndex < chartVitals.Series.Count; ++seriesIndex)
            {
                //when there is item in series
                if (chartVitals.Series[seriesIndex].Points.Count > 0)
                {
                    string start = "[Start]";

                    //set series type as line
                    chartVitals.Series[seriesIndex].ChartType = SeriesChartType.Line;
                    //apply label to first point of series
                    chartVitals.Series[seriesIndex].Points.First().Label = start;

                    //enable button to mark end
                    //btnOpEnd.Enabled = true;
                }
            }
        }

        //apply colours to series
        chartVitals.Series[blueSeries].Color = Color.Blue;
        chartVitals.Series[redSeries].Color = Color.Red;
    }

Upvotes: 1

Related Questions