This is a migrated thread and some comments may be shown as answers.

MVC Razor - Having problems displaying date and time on category axis

4 Answers 572 Views
Charts
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 24 Aug 2017, 07:35 PM

Below is my code.  Kendo chart is taking my date time data and displaying the axis by date not date and time.  I've tried many ways to get this to work.  When I tried Labels(labels => labels.DateFormats(formats => formats.Days("dd/MM/yyyy hh tt"))) and it displays all dates as 12 AM.    Why wont it display my data on the axis natively without formatting?  It seems to convert the data to a day range defaulted to 12 AM.   What am I doing wrong?

Data is like:

8/24/2017 1:00 

8/24/2017 2:00 

8/24/2017 3:00 

 

 

                 @(Html.Kendo().Chart<ElmahErrorsHourCountModel>()

                  .Name("HourlyErrorTotals")
                  .DataSource(ds => ds.Read(read => read.Action("HourlyErrorsCountByDate_Read", "Grid")))
                  .HtmlAttributes(new { style = "height:225px; width= 100%" })
                  .Series(series =>
                  {
                      series
                          .Line(model => model.ErrorCount);
                  })
                  .CategoryAxis(axis => axis
                      .Categories(model => model.HourlyDateTime)
                      .Labels(label => label.Rotation(-90))
                      .Crosshair(c => c.Visible(true))
                      
                  )
                  .ValueAxis(axis => axis.Numeric()
                      .Labels(labels => labels.Format("{0:N0}"))
                  )

                  .Tooltip(tooltip => tooltip
                      .Visible(true)
                      .Shared(true)
                      .Format("{0:N0}")
                  )
                  )  

4 Answers, 1 is accepted

Sort by
0
Mike
Top achievements
Rank 1
answered on 25 Aug 2017, 01:33 PM
It would also be great to know where the documentation is for the razor on formatting of the axis.  I searched and searched and found nothing.  It's been very frustrating. 
0
Mike
Top achievements
Rank 1
answered on 25 Aug 2017, 01:53 PM

Attached is the data that is transformed into JSON.  I've also attached what the chart is doing when ran with axis formatting like this

 

.CategoryAxis(axis => axis
                      .Date()
                      .Labels(labels => labels.DateFormats(formats => formats.Days("MM/dd/yyyy HH tt")))
                      .Categories(model => model.HourlyDateTime)
                      .Labels(label => label.Rotation(-90))
                      .Crosshair(c => c.Visible(true))
                  )

 

 

Below is from the gridcontroller.cs   The

        public ActionResult HourlyErrorsCountByDate_Read([DataSourceRequest]DataSourceRequest request)
        {
            try
            {
                ElmahService es = new ElmahService();
                var newErrors = es.GetErrorsHourCount().ToList();

                return Json(newErrors, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

 

 

 

0
Konstantin Dikov
Telerik team
answered on 28 Aug 2017, 01:17 PM
Hi Mike,

You could set the BaseUnits to "Hours" and change the displayed format as shown below:
.CategoryAxis(axis => axis
        .Date()
        .BaseUnit(ChartAxisBaseUnit.Hours)
        .Labels(labels => labels.Format("MM/dd/yyyy HH tt"))
    )

However, please note that the above will be usable only if all of the dates are from the same date.


Regards,
Konstantin Dikov
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Mike
Top achievements
Rank 1
answered on 30 Aug 2017, 06:54 PM

That didnt work.  I needed to display the data I presented to the chart.  Date + time.  I got it to work after several hours of painful trial and error.  For some reason sorting the data source allowed me to present the data as I sent to the chart.  It is very frustrating as I could find nothing that spoke if this in the docs.  

      @(Html.Kendo().Chart<ElmahErrorsHourCountModel>()
                  .Name("HourlyErrorTotals")
                  .Events(e => e.SeriesClick("click")
                  )
                  .DataSource(ds => ds.Read(read => read.Action("HourlyErrorsCountByDate_Read", "Grid"))
                    .Sort(sort=>sort.Add(model=>model.HourlyDateTime).Ascending())
                  )
                  .HtmlAttributes(new { style = "height:225px; width= 100%" })
                  .Series(series =>
                  {
                      series
                          .Line(model => model.ErrorCount);
                  })
                  .CategoryAxis(axis => axis
                      .Date()
                      .Labels(l => l.Format("MM/dd hh tt"))
                      .Categories(model => model.HourlyDateTime)
                      .Crosshair(c => c.Visible(true))
                  )
                  .ValueAxis(axis => axis.Numeric()
                      .Labels(labels => labels.Format("{0:N0}"))
                  )

                  .Tooltip(tooltip => tooltip
                      .Visible(true)
                      .Shared(false)
                      .Format("{0:N0}")
                      .Template("#= category #:  Errors: #= value #")

                  )
                  )  

 

Tags
Charts
Asked by
Mike
Top achievements
Rank 1
Answers by
Mike
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or