1

I have a few DateTime values in a MySQL database and would like to use them as either the X-Axis or the Y-Axis in my graph, which will be drawn in c#. I can get DateTime but it does not show me the values from MySQL Database, it shows random values. This is how far I am now:

void load_Chart()
    {
        string connectSource = "datasource=;port=;username=;password=";
        MySqlConnection myConnection = new MySqlConnection(connectSource);
        MySqlCommand command = new MySqlCommand(" select * from armr.rigpart ;", myConnection);
        MySqlDataReader myReader;
        try
        {
            myConnection.Open();
            myReader = command.ExecuteReader();

            while(myReader.Read())
            {
                chart1.Series["Start"].Points.AddXY(myReader.GetString("ID"), myReader.GetDateTime("Start"));
                chart1.Series["End"].Points.AddXY(myReader.GetString("ID"), myReader.GetDateTime("End"));
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

Hope that my question is clear enough, thank you.

  • 1) what is your charttype? Column? 2) you really should know what each axis is. DateTimes often are on the x-axis; but what then is the y-axis? You need to have numeric values; if they convert to 0 the columns will not show, obviously.. 3) After answering these questions I suggest you switch the two paramters in your AddXY calls: The first is for x-values and the 2nd for y-values. GetString doesn't sound like it is fetching numbers. For y-values this may still work, as the chart will try to convert to double. Does it have a chance or will the columns have a height of 0?? – TaW Apr 28 '18 at 17:41
  • It is a StackedBar100 and the x-Axis is for DateTime and the y-Axis is dor ID's. The values are are set to DateTime(for the x-Axis) and the ID's are Strings, but in mySql are called Varchar(45). Ok i will try and switch the two, but i do believe that i hvae done it in the past. Thank you very much – AmirRoohi2000 Apr 29 '18 at 18:51
  • Ok, let's get the terms right: In __Bar__ charts of all types the x- and y-axis are displayed __reversed__. So the x-axis is vertical and for IDs and the y-axis is horizontal and for DateTime? But: Do those Dates actually stack? Or are they TimeSpans?(IDs surely wouldn't) To make it work you need a numeric scheme for the x-values (strings are not stored in the x-values, which as all values in a chart are doubles). Also, possibly set StackedGroupNames.. – TaW Apr 29 '18 at 19:19
  • See [here](https://stackoverflow.com/questions/49937962/combine-barchart-and-pointchart/49939521#49939521) for more on these things.. (Warnig, long post, lots of info..!!) – TaW Apr 29 '18 at 19:21
  • The DateTime, yes they do stack up and are time spans. The data are actually from the same c# application, when the start button is pressed, an ID is created plus the date and time, then it is saved to a mySql Database, and when the stop button is pressed(the ID can be changed, but if I want to maintain the time span, the ID will stay the same) the stop date and time are added to the same row. Thank you for the link, I will definitely read through it. – AmirRoohi2000 May 01 '18 at 13:25
  • Did you tackle the x-values to be numbers? In the link was another [link](https://stackoverflow.com/questions/49853610/how-to-add-background-color-to-columns-in-chart/49855033#49855033) which at the end has a simple example how you can have numeric x-values but still show strings in the axislabels.. – TaW May 01 '18 at 13:43
  • I don't want to have the x-values of the data transferred to a string, I want to have them as DateTime. I still need to read through the articles you have commented(school and stuff). Thank you – AmirRoohi2000 May 02 '18 at 11:10
  • Sure, take your time. As for the date/time: You can use a format for them BUT: They are NOT the X- but the y-values! That is because x- and y-axis are switch in bar charts!!! Your x-values are your IDs and to allow stacking they need to have non-zero values, but when adding strings they are all `0` internally.. – TaW May 02 '18 at 11:15
  • Thank you, i will see what i can do – AmirRoohi2000 May 04 '18 at 14:01

0 Answers0