I have an ASP.NET project that uses some analytics controls for viewing various data. The controls are populated via JavaScript functions such as this:
Morris.Bar({
element: 'hero-bar',
data: [
{ device: '1', sells: 136 },
{ device: '3G', sells: 1037 },
{ device: '3GS', sells: 275 },
{ device: '4', sells: 380 },
{ device: '4S', sells: 655 },
{ device: '5', sells: 1571 }
],
xkey: 'device',
ykeys: ['sells'],
labels: ['Sells'],
barRatio: 0.4,
xLabelMargin: 10,
hideHover: 'auto',
barColors: ["#3d88ba"]
});
What I need to do is to populate the 'data: []' array by passing some values from the code behind (where I get the actual values from a database).
How can I go about passing the data across, and in what format does it need to come across? How do I add the variable into the JavaScript?
Can I pass it over as a string array by running a foreach loop in my code behind on the collection, so:
foreach(var item in items)
{
//build array here called "myDataFromCodeBehind"
sb.Append(" { device: xVariable, sells: yVariable },");
}
And then call the string array from the JavaScript
var myData = "<%=myDataFromCodeBehind %>";
Morris.Bar({
element: 'hero-bar',
data: [myData],
// etc
Or would that not work? Something tells me I'm going about it all wrong. I'm new to JavaScript and I'm not sure if this will work.