-1

I have some charts that are created client-side using Javascript. I would like them to use data queried and calculated server-side in my .net codebehind.

Each chart only needs passed 2 numbers, and I suppose a third variable (its name).

This is proving harder than I expected as I haven't found a working answer by googling?

I know I could create some hidden values on the page, but since the chart drawing script is in the head of the document surely it would try to create the charts before reading these values

user1765369
  • 1,333
  • 3
  • 11
  • 19
  • The delay the creation of the chart until the dom is [ready](http://stackoverflow.com/questions/9899372) so that those fields are available or request the data using ajax and draw/update the chart when you received the data. – t.niese Jan 18 '17 at 16:30
  • @t.niese so is the hidden values approach still best? – user1765369 Jan 18 '17 at 16:38
  • Depends, the `data-` attribute would be something that would fit best if you already know the data at the time when the site is conposed server side. `
    `
    – t.niese Jan 18 '17 at 16:53
  • Thanks @t.niese I did it this way. If you leave it as an answer I can mark it completed – user1765369 Jan 18 '17 at 19:52

1 Answers1

0

If you are using ASP.NET MVC and Razor, you could use the following method
1. Calculate the needed values for the charts in code behind and assign it to model properties.
2. Put Razor code to render chart variables above the chart drawing script.

<!-- Render chart variables -->   
<script type="text/javascript">   
    var chartVariable1 = @Html.Raw(@Model.ChartVariable1);  
</script>
<!-- Chart drawing script --> 
Trung Duong
  • 3,475
  • 2
  • 8
  • 9