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

[Solved] Wrong chart rendered because of premature Ajax binding

2 Answers 91 Views
Chart
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Achilles
Top achievements
Rank 1
Achilles asked on 27 Feb 2012, 03:00 PM
Hi,

I am using a MVC grid control and a chart control in the same page , both bound to their data source by Ajax binding in the Razor.  
The execution architecture is as :
1. The Grid control gets it data from the controller which in turn gets it from a web-service
2. When the the grid control completes loading, it reads some settings and calls a rebind on the chart with some calculated parameters
3. The chart renders with the right parameters

This works !!

My problem is that, because the chart is Ajax-bound, it calls on it's data source anyway. If the default call happens before the grid is loaded, I get 2 redraws. If the default call happens after the programatical rebinding, then I get a wrong chart drawn on type of the right chart.

Is there a way I can do a late binding of the chart's data-source by client API to avoid this problem ? I'm open to other alternate suggestions. I'm attaching a sample. Please have a close look when the chart loads the first time. (To reload the chart, reload the current page)

My actual code for the Ajax binding is

.DataBinding(dataBinding => dataBinding
    .Ajax().Select("FetchChartData",
    "Chart",
    new {
        displayMode = Model.Attributes.DisplayType,
        day = Model.Attributes.StartDate,
        duration = Model.Attributes.DurationType,
        datatype = Model.Attributes.DataType
    }))
.ClientEvents(events => events
    .OnLoad("onChartLoad")
    .OnDataBound("onDataBound"))


My second problem, is that if I refresh my chart, it calls a rebind . In my sample if you "Refresh" with the 2nd inverter selected,  and then check/uncheck the "Show marker" check-box, you can see my problem ....

Thanks

Achilles

2 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 02 Mar 2012, 10:02 AM
Hello Achilles,

To prevent the chart from binding immediately you should add a handler to OnDataBinding event and use the event argument's preventDefault method. You could either use a flag or unbind the event once the first request is prevented.
function onDataBinding(e) {
    e.preventDefault();
    $(this).unbind('dataBinding');
}
Regarding your other question, I can confirm that this is not the expected behaviour. A workaround for now is to use the Chart's redraw method instead e.g.
chart = getChart();
chart._chart.redraw();


Kind regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
Achilles
Top achievements
Rank 1
answered on 07 Mar 2012, 12:53 PM
Thanks . It solves the problem.

Achilles
Tags
Chart
Asked by
Achilles
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Achilles
Top achievements
Rank 1
Share this question
or