Click here to Skip to main content
15,888,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a c program attached to gdb debugger for which created gui interface in windows form application. when i run the application debugging watch variable, i will get the following output in the richtextbox of form1
old value = 0
new value = 118
now i have to plot graph indicating this so i extracted lines from the richtextbox of form 1 interfaced to form2 through class file. and in form2 i split these lines collecting string and values in two arrays binding it to chart. but i am not able to make it apt so i need help. below is the code that i have tried.

What I have tried:

namespace flowchart_new
{
    public partial class Form2 : Form
    {
       

        public Form2()
        {
            
            InitializeComponent();
            
        }



        private void Form2_Load(object sender, EventArgs e)
        {
            string[] array = Class1.temparray;

            var data = Getdata(array);
            var chart = chart1.ChartAreas[0];

            chart.AxisX.IntervalType = DateTimeIntervalType.Number;

            chart.AxisX.Interval = 1;
            chart.AxisY.Interval = 10;

            chart.AxisX.Minimum = 0;
            chart.AxisY.Minimum = 0;

            chart.AxisX.Maximum = 10;

            chart.AxisX.LabelStyle.Format = "";
            chart.AxisY.LabelStyle.Format = "";
            Series series = new Series("variable") { ChartType = SeriesChartType.Line, BorderWidth = 2, MarkerSize = 5, MarkerStyle = MarkerStyle.Square };
            chart1.Series.Clear();
            chart1.Series.Add("variable");
            chart1.Series["variable"].IsValueShownAsLabel = true;
            chart1.Series["variable"].ChartType = SeriesChartType.Line;
            chart1.Series["variable"].Color = Color.BlueViolet;

             foreach (var p in data)
             {
                
               chart1.Series["variable"].Points.AddXY( p.Item1,p.Item2);
                chart1.Series.Add(series);
               

            }

            chart1.Update();
<pre>  }

        private static readonly CultureInfo EnglishCulture = CultureInfo.GetCultureInfo("en-US");

        private static Tuple<string, int="">[] Getdata(string[] lines)
        {
            return Array.ConvertAll(lines, line =>
            {
                string[] elems = line.Split('=');
                return new Tuple<string, int="">(elems[0], int.Parse(elems[1], EnglishCulture));
            });
        }

        
    }  
    
}
Posted
Updated 26-May-21 0:56am
v2
Comments
BillWoodruff 25-May-21 10:45am    
Where does the Chart come from ? 3rd. party Control ?
Member 15021328 25-May-21 11:59am    
windows form chart control
[no name] 25-May-21 17:12pm    
I don't see in your code where you refresh the chart control. Try adding chart1.Refresh(); after your data loop.

1 solution

After looking at your code, I'm not sure about the DataBindXY used in a loop versus the AddXY method. It does appear that the Refresh method is missing.

Here is a method I use to display a stacked bar chart to display success versus error to show error rates.

C#
public static void ErrorRateChart(Chart chartControl, DateTime startDate, DateTime endDate, Int64 projectTypeID = 0)
 {
     try
     {
         //Clear the chart control
         chartControl.Titles.Clear();
         chartControl.Legends.Clear();
         chartControl.Series.Clear();

         //Get the data from the database
         SummaryEntityList errRate = StatusReport.GetErrorRate(projectTypeID, startDate, endDate);

         if (errRate != null)
         {
             //Add and format the legend
             chartControl.Legends.Add("Failed");
             chartControl.Legends.Add("Succeeded");

             chartControl.Legends[0].Position.Auto = false;
             chartControl.Legends[0].Position.X = 0;
             chartControl.Legends[0].Position.Y = 0;
             chartControl.Legends[0].Position.Width = 30;
             chartControl.Legends[0].Position.Height = 15;

             //Set the Y axis
             chartControl.ChartAreas[0].AxisY.Title = "Success Rate (%)";
             chartControl.ChartAreas[0].AxisY.TitleFont = new Font("Arial", 11, FontStyle.Regular);

             //Create the series and add data
             Series seriesSucceeded = chartControl.Series.Add("Succeeded");
             seriesSucceeded.ChartType = SeriesChartType.StackedBar100;
             seriesSucceeded.IsValueShownAsLabel = true;

             foreach (SummaryEntity item in errRate)
             {
                 seriesSucceeded.Points.AddXY(item.Project, item.Succeeded);
             }

             //Create the series and add data
             Series seriesFailed = chartControl.Series.Add("Failed");
             seriesFailed.ChartType = SeriesChartType.StackedBar100;
             seriesFailed.IsValueShownAsLabel = true;

             foreach (SummaryEntity item in errRate)
             {
                 seriesFailed.Points.AddXY(item.Project, item.Failed);
             }

             chartControl.Refresh();
         }
     }
     catch (Exception ex)
     {
         //Log the error and stack trace
         ErrorLogWriter.ErrorLog = ErrorLogWriter.ErrorLog.Write(ex);
         throw;
     }
 }
 
Share this answer
 

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