Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Good morning, I would like to use the library MORRIS.JS with ASP. Net, I know I must be a JSON object, but not how to do it, someone help me? As an example if I do a query on a database and want to send the data in JSON format so the library becomes MORRIS.JS the graph, can someone give me a simple example? Thanks to this community and sorry my English is not very good, I would say that is bad enough, but I will understand!
 
Linkt to library http://www.oesmith.co.uk/morris.js/index.html[^]
Posted 4-Feb-13 7:17am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

for example you can convert a generic list to json like below
public class Person
{
public string Name{get;set;}
public int Age{get;set;}
}
List<person> person=new List<person>();
person.Add(new Person{Name="Jemi","23"});
person.Add(new Person{Name="Jemi2","23"});
 
//Converting person list to jSon by using JavaScriptSerializer
JavaScriptSerializer jss = new JavaScriptSerializer(person);
 
string output = jss.Serialize(person);
 
Hope this helps
  Permalink  
v2
Comments
Garcia AlfredoS at 4-Feb-13 13:57pm
   
agree, however I should spend the format is as follows: /* * Play with this code and it'll update in the panel opposite. * * Why not try some of the options above? */ Morris.Donut({ element: 'donut-example', data: [ {label: "Download Sales", value: 12}, {label: "In-Store Sales", value: 30}, {label: "Mail-Order Sales", value: 20} ] }); I do not quite understand is how to format the data obtained either from an object in that format, can you give me a clearer example? aspx file including ... cs ..... and js. Because that's so generic it's hard to understand, can you put the aspx ... cs and js for these three archvios form a whole that can see more clearly? Thank you very much for your time!
Garcia AlfredoS at 4-Feb-13 14:10pm
   
I have to agree all formatted in json output, but with the example I gave above and must form the json of morris.js, ..... I can you clarify this?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I also encountered the same problem with you,just did a project on morris example,I hope to help you.
<body>
<div>
Morris
<div id="myfirstchart" style="height: 250px; " >
</div>
</div>
</body>
</html>
<script type="text/javascript">
new Morris.Line({
element: 'myfirstchart',
data: @ViewBag.jss ,
xkey: 'year',
ykeys: ['value','a','b'],
labels: ['Value','This A','this B']
});
 
</script>
 
public ActionResult Morris()
{
LineDemo lineDemo1 = new LineDemo();
lineDemo1.year="2008";
lineDemo1.value = 20;
lineDemo1.a=30;
lineDemo1.b=15;
 
LineDemo lineDemo2 = new LineDemo();
lineDemo2.year = "2009";
lineDemo2.value = 30;
lineDemo2.a = 40;
lineDemo2.b = 20;
 
LineDemo lineDemo3 = new LineDemo();
lineDemo3.year = "2010";
lineDemo3.value = 40;
lineDemo3.a = 50;
lineDemo3.b = 30;
 
LineDemo lineDemo4 = new LineDemo();
lineDemo4.year = "2011";
lineDemo4.value = 25;
lineDemo4.a = 35;
lineDemo4.b = 55;
 
List<LineDemo> listLine = new List<LineDemo>();
listLine.Add(lineDemo1);
listLine.Add(lineDemo2);
listLine.Add(lineDemo3);
listLine.Add(lineDemo4);
 
JavaScriptSerializer jss = new JavaScriptSerializer();
 
string output = jss.Serialize(listLine);
 
string demo = output.Replace("\"", "");
 
ViewBag.jss = demo;
 
return View();
}
 
}
 
public class LineDemo
{
public string year{get;set;}
public int value{get;set;}
public int a{get;set;}
public int b{get;set;}
}
 
The data transport by Json, It should be noted that Json' Key have quotation ,you must delete it,you can use Regular Expression . this is asp.net MVC. My English is not good.Hope you can understand.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 446
1 Sergey Alexandrovich Kryukov 360
2 Shai Vashdi 318
3 OriginalGriff 265
4 Peter Leow 171
0 Sergey Alexandrovich Kryukov 9,109
1 OriginalGriff 5,290
2 Peter Leow 4,010
3 Maciej Los 3,535
4 Abhinav S 3,263


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 15 Apr 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid