Advertisement
Guest User

Untitled

a guest
Jul 13th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.48 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <style>
  4.  
  5. body {
  6. font: 10px sans-serif;
  7. }
  8.  
  9. .chart {
  10. background: #fff;
  11. }
  12.  
  13. p {
  14. font: 12px helvetica;
  15. }
  16.  
  17.  
  18. .axis path, .axis line {
  19. fill: none;
  20. stroke: #000;
  21. stroke-width: 2px;
  22. shape-rendering: crispEdges;
  23. }
  24.  
  25. button {
  26. position: absolute;
  27. right: 50px;
  28. top: 10px;
  29. }
  30.  
  31. </style>
  32. <body>
  33. <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
  34. <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
  35.  
  36.  
  37.  
  38. <div class="chart"></div>
  39. </body>
  40.  
  41. <script>
  42. var dataset;
  43. function getData() {
  44. $.ajax({
  45. type: "POST",
  46. contentType: "application/json; charset=utf-8",
  47. url: "VoxPoliticoService.asmx/ComparePoliticians",
  48. data: {},
  49. dataType: "json",
  50. success: function (res) {
  51.  
  52. dataset = res.d;
  53.  
  54. renderChart();
  55. }
  56. });
  57. }
  58.  
  59. jQuery(document).ready(function ($) {
  60. getData();
  61.  
  62. });
  63.  
  64.  
  65. function renderChart()
  66. {
  67. color = "blue";
  68.  
  69. var datearray = [];
  70. var colorrange = [];
  71. var data = dataset;
  72.  
  73.  
  74.  
  75.  
  76. if (color == "blue") {
  77. colorrange = ["#045A8D", "#2B8CBE", "#74A9CF", "#A6BDDB", "#D0D1E6", "#F1EEF6"];
  78. }
  79. else if (color == "pink") {
  80. colorrange = ["#980043", "#DD1C77", "#DF65B0", "#C994C7", "#D4B9DA", "#F1EEF6"];
  81. }
  82. else if (color == "orange") {
  83. colorrange = ["#B30000", "#E34A33", "#FC8D59", "#FDBB84", "#FDD49E", "#FEF0D9"];
  84. }
  85. strokecolor = colorrange[0];
  86.  
  87. var format = d3.time.format("%m/%d/%y");
  88.  
  89. var margin = {top: 20, right: 40, bottom: 30, left: 30};
  90. var width = document.body.clientWidth - margin.left - margin.right;
  91. var height = 400 - margin.top - margin.bottom;
  92.  
  93. var tooltip = d3.select("body")
  94. .append("div")
  95. .attr("class", "remove")
  96. .style("position", "absolute")
  97. .style("z-index", "20")
  98. .style("visibility", "hidden")
  99. .style("top", "30px")
  100. .style("left", "55px");
  101.  
  102. var x = d3.time.scale()
  103. .range([0, width]);
  104.  
  105. var y = d3.scale.linear()
  106. .range([height-10, 0]);
  107.  
  108. var z = d3.scale.ordinal()
  109. .range(colorrange);
  110.  
  111. var xAxis = d3.svg.axis()
  112. .scale(x)
  113. .orient("bottom")
  114. .ticks(d3.time.weeks);
  115.  
  116. var yAxis = d3.svg.axis()
  117. .scale(y);
  118.  
  119. var yAxisr = d3.svg.axis()
  120. .scale(y);
  121.  
  122. var stack = d3.layout.stack()
  123. .offset("silhouette")
  124. .values(function(d) { return d.values; })
  125. .x(function(d) { return d.date; })
  126. .y(function(d) { return d.value; });
  127.  
  128. var nest = d3.nest()
  129. .key(function(d) { return d.key; });
  130.  
  131. var area = d3.svg.area()
  132. .interpolate("cardinal")
  133. .x(function(d) { return x(d.date); })
  134. .y0(function(d) { return y(d.y0); })
  135. .y1(function(d) { return y(d.y0 + d.y); });
  136.  
  137. var svg = d3.select(".chart").append("svg")
  138. .attr("width", width + margin.left + margin.right)
  139. .attr("height", height + margin.top + margin.bottom)
  140. .append("g")
  141. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  142.  
  143. data.forEach(function(d) {
  144. d.date = format.parse(d.date);
  145. d.value = +d.value;
  146. });
  147.  
  148. var layers = stack(nest.entries(data));
  149.  
  150. x.domain(d3.extent(data, function(d) { return d.date; }));
  151. y.domain([0, d3.max(data, function(d) { return d.y0 + d.y; })]);
  152.  
  153. svg.selectAll(".layer")
  154. .data(layers)
  155. .enter().append("path")
  156. .attr("class", "layer")
  157. .attr("d", function(d) { return area(d.values); })
  158. .style("fill", function(d, i) { return z(i); });
  159.  
  160.  
  161. svg.append("g")
  162. .attr("class", "x axis")
  163. .attr("transform", "translate(0," + height + ")")
  164. .call(xAxis);
  165.  
  166. svg.append("g")
  167. .attr("class", "y axis")
  168. .attr("transform", "translate(" + width + ", 0)")
  169. .call(yAxis.orient("right"));
  170.  
  171. svg.append("g")
  172. .attr("class", "y axis")
  173. .call(yAxis.orient("left"));
  174.  
  175. svg.selectAll(".layer")
  176. .attr("opacity", 1)
  177. .on("mouseover", function(d, i) {
  178. svg.selectAll(".layer").transition()
  179. .duration(250)
  180. .attr("opacity", function(d, j) {
  181. return j != i ? 0.6 : 1;
  182. })})
  183.  
  184. .on("mousemove", function(d, i) {
  185. mousex = d3.mouse(this);
  186. mousex = mousex[0];
  187. var invertedx = x.invert(mousex);
  188. invertedx = invertedx.getMonth() + invertedx.getDate();
  189. var selected = (d.values);
  190. for (var k = 0; k < selected.length; k++) {
  191. datearray[k] = selected[k].date
  192. datearray[k] = datearray[k].getMonth() + datearray[k].getDate();
  193. }
  194.  
  195. mousedate = datearray.indexOf(invertedx);
  196. pro = d.values[mousedate].value;
  197.  
  198. d3.select(this)
  199. .classed("hover", true)
  200. .attr("stroke", strokecolor)
  201. .attr("stroke-width", "0.5px"),
  202. tooltip.html( "<p>" + d.key + "<br>" + pro + "</p>" ).style("visibility", "visible");
  203.  
  204. })
  205. .on("mouseout", function(d, i) {
  206. svg.selectAll(".layer")
  207. .transition()
  208. .duration(250)
  209. .attr("opacity", "1");
  210. d3.select(this)
  211. .classed("hover", false)
  212. .attr("stroke-width", "0px"), tooltip.html( "<p>" + d.key + "<br>" + pro + "</p>" ).style("visibility", "hidden");
  213. })
  214.  
  215. var vertical = d3.select(".chart")
  216. .append("div")
  217. .attr("class", "remove")
  218. .style("position", "absolute")
  219. .style("z-index", "19")
  220. .style("width", "1px")
  221. .style("height", "380px")
  222. .style("top", "10px")
  223. .style("bottom", "30px")
  224. .style("left", "0px")
  225. .style("background", "#fff");
  226.  
  227. d3.select(".chart")
  228. .on("mousemove", function(){
  229. mousex = d3.mouse(this);
  230. mousex = mousex[0] + 5;
  231. vertical.style("left", mousex + "px" )})
  232. .on("mouseover", function(){
  233. mousex = d3.mouse(this);
  234. mousex = mousex[0] + 5;
  235. vertical.style("left", mousex + "px")});
  236.  
  237. }
  238.  
  239. </script>
  240.  
  241. [WebMethod]
  242. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  243. public PoliticianWords[] ComparePoliticians()
  244. {
  245. List<PoliticianWords> ret = new List<PoliticianWords>();
  246. Random r = new Random();
  247. string[] politicians = { "A B", "C D", "E F", "G H" };
  248.  
  249. Random gen = new Random();
  250.  
  251. List<DateTime> dates = new List<DateTime>();
  252.  
  253.  
  254. for (int i = 0; i < 20; i++)
  255. {
  256. DateTime start = new DateTime(1991, 9, 11);
  257.  
  258. int range = (DateTime.Today - start).Days;
  259. dates.Add(start.AddDays(gen.Next(range)));
  260. }
  261.  
  262. dates.Sort();
  263.  
  264.  
  265. foreach (string politician in politicians)
  266. {
  267. foreach(DateTime d in dates)
  268. {
  269. PoliticianWords pw = new PoliticianWords();
  270. pw.key = politician;
  271. pw.value = gen.Next(0, 100);
  272. pw.date = d.Day.ToString() + "/" + d.Month.ToString() + "/" + d.Year.ToString();
  273. ret.Add(pw);
  274. }
  275. }
  276.  
  277. return ret.ToArray();
  278.  
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement