Line chart ajax refreshing getting Uncaught TypeError: Cannot read properties of undefined (reading 'getTime')

1 Answer 539 Views
Chart
CHIHPEI
Top achievements
Rank 2
Iron
Iron
Iron
CHIHPEI asked on 03 Nov 2022, 08:45 AM

Scenario:

I would like to retrieve data with ajax,

once getting data from my database,

then pass Json data back to frontend and refresh line chart

Question:

While I got the data and pass it back to frontend,

it showed type error: Cannot read property 'getTime' of undefined.

What could cause this error?

To be mentioned, the json Data is in the same type with line chart, which is IEnumerable<EqEvDTDataViewModel>.

Code:

My Ajax Function

function instDataChartDropDownSelect(e) {

            var dataItem = this.dataItem(e.item);
            var instID = dataItem.InstID;

            $.ajax({
                type: "POST",
                url: '@Url.Action("MyAction","MyController")',
                data: { 'instID': instID },
                dataType: 'json',
                success: function (response) {
                    $("#dtchart").data("kendoChart").dataSource.data(response);
                },
                error: function (response) {
                }
            });
        }

My Line Chart
@(Html.Kendo().Chart(instDatas.EqEvDTDataViewModel)
                                        .Name("dtchart")
                                        .Title("DT Time History Chart")
                                        .Legend(legend => legend
                                            .Position(ChartLegendPosition.Bottom)
                                        )
                                        .ChartArea(chartArea => chartArea
                                            .Background("transparent")
                                        )
                                        .SeriesDefaults(seriesDefaults =>
                                            seriesDefaults.Line().Style(ChartSeriesStyle.Smooth)
                                        )
                                        .Series(series => {
                                            series.Line(model => model.ReadData).Name("Displacement");
                                        })
                                        .ValueAxis(axis => axis
                                            .Numeric()
                                        )
                                        .CategoryAxis(axis => axis
                                            .Categories( m => m.ReadTime)
                                            .Type(ChartCategoryAxisType.Date)
                                            .BaseUnit(ChartAxisBaseUnit.Fit)
                                            .Labels(labels => labels.Rotation(-90))
                                            .Crosshair(c => c.Visible(true))
                                            .AutoBaseUnitSteps(config => config.Milliseconds(1))
                                        )
                                        .Tooltip(tooltip => tooltip
                                            .Visible(true)
                                            .Shared(true)
                                            .Format("{0:N0}")
                                        )
                                 )

1 Answer, 1 is accepted

Sort by
0
Accepted
CHIHPEI
Top achievements
Rank 2
Iron
Iron
Iron
answered on 07 Nov 2022, 05:06 AM

Hi everyone,

I've found the reason why I got this error,

it's because the of the CMS that I'm using.

It will automatically change the typename of the model to Camel Case.

In this case,

By configuring the JsonSerializerSettings and pass it while returning Json would solve the problem.

JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = Newtonsoft.Json.TypeNameHandling.None };

Tags
Chart
Asked by
CHIHPEI
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
CHIHPEI
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or