MVC3 atvaizduoti grafika


/ Published in: C#
Save to your folder(s)

grafikas atvaizduojamas kreive, pyrago forma, stulpeliais. Pateikiamas kaip sugeneruotas png failas.


Copy this code and paste it in your HTML
  1. // kodas chtml faile:
  2. // <img src="@Url.Action("grafikas")" alt="Drawing chart with HTML Helper" />
  3.  
  4. // kodas kontroleryje:
  5. [ActionName("grafikas")] // ---------------- galima keist path pagal regionus!
  6. //[OutputCache(Duration = 3)] // ------------ jei nustatomas keshavimas, kai issaugot bandai gaunasi .bmp failas
  7. public ActionResult DrawChart()
  8. {
  9. /*
  10.   * reikia pasiimt i������¡ sistemos userio Guid, tai nereik����¯�¿�½s nieko perduot per parametrus!!!
  11.   */
  12.  
  13.  
  14.  
  15. string[] arrYvalues = new string[10];
  16. string[] arrXvalues = new string[10];
  17. Random rn = new Random();
  18.  
  19. for (int i = 1; i < 10; i++)
  20. {
  21. arrXvalues[i] = i.ToString();
  22. arrYvalues[i] = i.ToString() + rn.Next(10).ToString();
  23. }
  24.  
  25. string myTheme =
  26. @"<Chart BackColor=""255, 255, 255"" >
  27. <ChartAreas>
  28. <ChartArea Name=""Default"" BorderColor=""255, 255, 255"" BorderDashStyle=""Solid"" BackColor=""Transparent"" ShadowColor=""Transparent"">
  29. <AxisY LineColor=""64, 64, 64, 64"">
  30. <MajorGrid Interval=""Auto"" LineColor=""64, 64, 64, 64"" />
  31. <LabelStyle Font=""Trebuchet MS, 8.25pt"" />
  32. </AxisY>
  33. <AxisX LineColor=""64, 64, 64, 64"">
  34. <MajorGrid Interval=""Auto"" LineColor=""64, 64, 64, 64"" />
  35. <LabelStyle Interval=""Auto"" Font=""Trebuchet MS, 8.25pt"" />
  36. </AxisX>
  37. </ChartArea>
  38. </ChartAreas>
  39. </Chart>";
  40.  
  41. var chart = new Chart(width: 500, height: 250, theme: myTheme)
  42. .AddSeries(
  43. chartType: "line",
  44. xValue: arrXvalues,
  45. yValues: arrYvalues)
  46. .GetBytes("png");
  47.  
  48. return File(chart, "image/bytes");
  49. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.