1

As per the image, I would like for the entries that are the same to be on the same line instead of being seperated. I think I need to create different series for each entry but being new at this, I have no idea on how to do this. So far, I have one series created as shown. If you could help me out with some code, it would be greatly appreciated. Here is the code I have:

private void LoadChartData()
    {
        var s = new Series();
        s.ChartType = SeriesChartType.RangeBar;

        chart1.Series.Clear();
        chart1.Series.Add(s);

        s.SetCustomProperty("PixelPointWidth", "25");

        chart1.Series[0].YValueType = ChartValueType.DateTime;
        chart1.ChartAreas[0].AxisY.LabelStyle.Format = "yyyy-MM-dd";
        chart1.ChartAreas[0].AxisY.Interval = 1;
        chart1.ChartAreas[0].AxisY.IntervalType = DateTimeIntervalType.Days;
        chart1.ChartAreas[0].AxisY.IntervalOffset = 1;

        chart1.Series[0].XValueType = ChartValueType.String;
        chart1.ChartAreas[0].AxisY.Minimum = minDate.ToOADate();
        chart1.ChartAreas[0].AxisY.Maximum = maxDate.ToOADate();

        ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"];
        MySqlConnection con = new MySqlConnection(conSettings.ToString());
        MySqlCommand cmd = new MySqlCommand(@"select * from shopmanager.planning;", con);
        MySqlDataReader myReader;
        try
        {
            con.Open();
            myReader = cmd.ExecuteReader();

            while (myReader.Read())
            {
                //check if machine is "en attente"
                string notPlanned;
                notPlanned = myReader.GetString("machine_name");
                if(notPlanned == "En attente")
                {
                    return;
                }
                else
                {
                    s.LabelForeColor = Color.Black;
                    s.Font = new System.Drawing.Font("Arial", 10f);
                    var start_date = myReader.GetDateTime("predicted_start_date");
                    var predicted_finish_date = myReader.GetDateTime("predicted_delivery");
                    var machine = myReader.GetString("machine_name");
                    int pix = s.Points.AddXY(machine, start_date, predicted_finish_date);
                    s.Points[pix].Label = myReader.GetString("project_number") + " " + myReader.GetString("part_name");
                }
            }
            cmd.Parameters.Clear();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        con.Close();

    }

enter image description here

TaW
  • 53,122
  • 8
  • 69
  • 111
  • You are adding the machine name as a string. This is almost always a bad idea because this way __all x-values are `0`__ (test with the debugger!!) and therefore cannot be grouped correctly. Instead try to add them as numbers and add the text/machine-names as AxisLabels. Many chart types including RangeBar and StackedBars often have a similar problem. See [here](https://stackoverflow.com/questions/35744549/stacked-column-chart-in-c-sharp/35745456#35745456) for an example. It is up to you to decide if you want a lookup or some other scheme, just make sure to have numbers as x-values. – TaW Oct 03 '18 at 16:42
  • That makes total sense. The problem is I don't know how to code that but I'll try to reserach it. Thanks. – Christian Verner Oct 03 '18 at 16:53
  • One way could be, ((easiest if you can get all machine names beforehand, but it will also work while reading)) to fill a Dictionary with the names and the index of each name. Then you can use that index as the x-value and the key as the axislabel of each point.. – TaW Oct 03 '18 at 17:21
  • I feel bad for asking but would you be able to show me how you would code this would you? I've been coding for only about a month or so. – Christian Verner Oct 03 '18 at 17:31
  • Do ask if the answer code is not clear enough! – TaW Oct 03 '18 at 17:38

1 Answers1

1

Your problem comes from adding the x-values as strings. This is a common mistake, especially since at first all looks well. Also since one sometimes simply has no numeric values; your machines have names, cities are not numbers and IDs may or may not be numeric. People's names never are etc.. so there are many reasons why one would add non-numeric x-values.

But they are only added to the automatic labels, the actual x-values all are 0 and therefore cannot be grouped correctly or used for other things. This may or may not be a problem. You need grouping so here it is a problem!

So, if you have no numbers you can use, you must make some up.

Here is an example that stuffs all (new) names into a Dictionary as Keys and adds their index as a numeric Value:

// maybe best at class level
Dictionary<string, int> machines = new Dictionary<string, int>();


// your reading loop here..
   ..
   string machine = ..// your retrieval code here
   if (!machines.ContainsKey(machine) ) machines.Add(machine, machines.Count);
   // now add the point:
   int px = yourSeries.Points.AddXY(machines[machine], yourYvalue1, yourYvalue2);
   yourSeries.Points[px].AxisLabel = machine;
   ..
// loopend
TaW
  • 53,122
  • 8
  • 69
  • 111