Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.47 KB | None | 0 0
  1. var Radar_Chart = function(opt) {
  2. this.cfData = opt.cfData;
  3. this.dim = opt.dim;
  4. this.data = opt.cfData;
  5. this.element = opt.element;
  6. this.id = opt.id;
  7. this.orientation = opt.orientation;
  8. this.width = opt.width;
  9. this.height = opt.height;
  10. this.r = opt.circleRadius;
  11. this.padding = opt.padding;
  12. this.speed = opt.speed;
  13. this.optionalColorPalette = opt.colorPalette;
  14. this.xTicksNum = opt.xTicksNum;
  15. this.yTicksNum = opt.yTicksNum;
  16. this.barPaddingInner = opt.barPaddingInner;
  17. this.fontSize = opt.fontSize;
  18. this.maxWidth = opt.maxWidth;
  19. this.viewBoxOverwrite = opt.viewBoxOverwrite;
  20. this.startingRadian = Math.PI/2;
  21. this.shapeFill = opt.shapeFill;
  22. this.shapeOpacity = opt.shapeOpacity;
  23.  
  24. this.draw();
  25.  
  26. };
  27.  
  28.  
  29. Radar_Chart.prototype.generatePolarScale = function(){
  30.  
  31. this.dataMaxValue = d3.max(this.data, function(d) {
  32. return d.value ;
  33. });
  34.  
  35. this.dimensionCount = this.data.length;
  36. this.dimensionRadianSpacing = (2*Math.PI)/this.dimensionCount;
  37.  
  38. //normalize the data to the max of the values in the set
  39. this.yScale = d3.scaleLinear()
  40. .domain([0,this.dataMaxValue])
  41. .range([0,this.r]);
  42.  
  43. //map the dimensions to [0,dimensionCount] to create spacing 2*PI/dimensionCount in the chart
  44. this.xScale = d3.scaleOrdinal()
  45. .domain(this.data.map(function(d){return d.key}))
  46. .range(d3.range(0,this.dimensionCount,1));
  47.  
  48. //using [-r,r] as the domain
  49. this.polarYScale = d3.scaleLinear()
  50. .domain([-this.r,this.r])
  51. .range([this.height - 2 * this.padding, 0]);
  52.  
  53. this.polarXScale = d3.scaleLinear()
  54. .domain([-this.r,this.r])
  55. .range([0, this.width - 2 * this.padding]);
  56.  
  57.  
  58. this.yAxis = d3.axisLeft().ticks(3).scale(this.polarYScale);
  59. this.xAxis = d3.axisBottom().ticks(3).scale(this.polarXScale);
  60. };
  61.  
  62. Radar_Chart.prototype.draw = function() {
  63.  
  64. var svg = d3.select(this.element).append('svg')
  65. .attr("id",this.id)
  66. .attr("padding",this.padding)
  67. .attr('viewBox',"0 0 "+ this.width+" "+this.height)
  68. .attr("preserveAspectRatio","xMinYMin")
  69. .style("max-width",this.maxWidth)
  70.  
  71. this.plot = svg.append('g')
  72. .attr('class', 'Rader_Chart_holder')
  73. .attr('transform', "translate(" + this.padding + "," + this.padding + ")");
  74.  
  75.  
  76. this.generatePolarScale();
  77. //this.addAxis();
  78. this.drawLines();
  79. this.drawShape();
  80. this.addCenterCircle();
  81. this.addButtons();
  82.  
  83. };
  84.  
  85.  
  86. Radar_Chart.prototype.addAxis = function() {
  87. this.plot.append("g")
  88. .attr("id", "x-axisGroup")
  89. .attr("class", "x-axis")
  90. .attr("transform", "translate(" + "0" + "," + (this.height - 2 * this.padding) + ")");
  91.  
  92. this.plot.select(".x-axis")
  93. .transition()
  94. .duration(1000)
  95. .call(this.xAxis);
  96.  
  97. this.plot.append("g")
  98. .attr("id", "y-axisGroup")
  99. .attr("class", "y-axis")
  100. .attr("transform", "translate(0,0)");
  101.  
  102. this.plot.select(".y-axis")
  103. .transition()
  104. .duration(1000)
  105. .call(this.yAxis);
  106.  
  107. };
  108.  
  109. Radar_Chart.prototype.addCenterCircle = function(){
  110.  
  111. var that = this;
  112.  
  113. //create chart reference point at center
  114.  
  115. this.plot.append("circle")
  116. .attr("class",".chartCenter")
  117. .attr("cx",function(d){
  118. var r = 0
  119. return that.polarXScale(r);
  120. })
  121. .attr("cy",function(d){
  122. var r = 0
  123. return that.polarYScale(r);
  124. })
  125. .attr("r",1)
  126. .attr("fill","#a7a7a7");
  127.  
  128. };
  129.  
  130.  
  131. Radar_Chart.prototype.drawShape = function(){
  132.  
  133. var that = this;
  134.  
  135. //find the vertices for the shape of the data
  136.  
  137. var vertices = this.plot.selectAll(".chartVertex")
  138. .data(this.data,function(d){return d.key});
  139.  
  140. var spokes = this.plot.selectAll(".chartSpokes")
  141. .data(this.data,function(d){return d.key});
  142.  
  143. //remove any elements that don't have data
  144. vertices.exit().remove();
  145. spokes.exit().remove();
  146.  
  147.  
  148. //update any elements that have new data
  149.  
  150. vertices
  151. .transition()
  152. .duration(this.speed)
  153. .attr("transform",function(d){
  154. var r = that.yScale(that.dataMaxValue);
  155. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  156. return "translate("+that.polarXScale(r*Math.cos(theta))+","+that.polarYScale(r*Math.sin(theta))+")";
  157. });
  158.  
  159. vertices
  160. .selectAll("text")
  161. .transition().duration(1000)
  162. .attr("text-anchor",function(d){
  163. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  164.  
  165. if(Math.cos(theta)<0){
  166. var text_anchor = "end"
  167. }else{
  168. var text_anchor = "start"
  169. };
  170. return text_anchor;
  171. })
  172.  
  173. spokes
  174. .transition()
  175. .duration(this.speed)
  176. .attr("x2",function(d){
  177. var r = that.yScale(that.dataMaxValue);
  178. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  179. return that.polarXScale(r*Math.cos(theta));
  180. })
  181. .attr("y2",function(d){
  182. var r = that.yScale(that.dataMaxValue);
  183. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  184. return that.polarYScale(r*Math.sin(theta));
  185. });
  186.  
  187.  
  188. //add new elements
  189.  
  190. verticesGroups = vertices.enter()
  191. .append("g")
  192. .attr("class","chartVertex")
  193. .attr("transform",function(d){
  194. var r = that.yScale(that.dataMaxValue);
  195. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  196. return "translate("+that.polarXScale(r*Math.cos(theta))+","+that.polarYScale(r*Math.sin(theta))+")";
  197. });
  198.  
  199. verticesGroups.append("circle")
  200. .attr("class","chartVertex")
  201. .attr("r",1)
  202. .attr("fill","#ababab");
  203.  
  204. verticesGroups.append("text")
  205. .text(function(d){return d.key})
  206. .attr("text-anchor",function(d){
  207. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  208.  
  209. if(Math.cos(theta)<0){
  210. var text_anchor = "end"
  211. }else{
  212. var text_anchor = "start"
  213. };
  214. return text_anchor;
  215. })
  216. .attr("font-size",5)
  217.  
  218. spokes.enter().append("line")
  219. .attr("class","chartSpokes")
  220. .attr("x1",this.polarXScale(0))
  221. .attr("y1",this.polarYScale(0))
  222. .attr("x2",function(d){
  223. var r = that.yScale(that.dataMaxValue);
  224. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  225. return that.polarXScale(r*Math.cos(theta));
  226. })
  227. .attr("y2",function(d){
  228. var r = that.yScale(that.dataMaxValue);
  229. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  230. return that.polarYScale(r*Math.sin(theta));
  231. })
  232. .style("stroke-width",0.2)
  233. .style("stroke","#D0D0D0")
  234. .style("stroke-dasharray", ("1, 1"));
  235.  
  236. };
  237.  
  238. Radar_Chart.prototype.drawLines = function(){
  239.  
  240. var that = this;
  241.  
  242. var outlineHalfFunction = d3.line()
  243. .x(function(d){
  244. var r = that.yScale(that.dataMaxValue/2);
  245. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  246. return that.polarXScale(r*Math.cos(theta));
  247. })
  248. .y(function(d){
  249. var r = that.yScale(that.dataMaxValue/2);
  250. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  251. return that.polarYScale(r*Math.sin(theta));
  252. })
  253. .curve(d3.curveLinearClosed)
  254.  
  255. var outlineFullFunction = d3.line()
  256. .x(function(d){
  257. var r = that.yScale(that.dataMaxValue);
  258. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  259. return that.polarXScale(r*Math.cos(theta));
  260. })
  261. .y(function(d){
  262. var r = that.yScale(that.dataMaxValue);
  263. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  264. return that.polarYScale(r*Math.sin(theta));
  265. })
  266. .curve(d3.curveLinearClosed)
  267.  
  268. var lineFunction = d3.line()
  269. .x(function(d){
  270. var r = that.yScale(d.value);
  271. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  272. return that.polarXScale(r*Math.cos(theta));
  273. })
  274. .y(function(d){
  275. var r = that.yScale(d.value);
  276. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  277. return that.polarYScale(r*Math.sin(theta));
  278. })
  279. .curve(d3.curveLinearClosed)
  280.  
  281. this.plot.append("path")
  282. .attr("class","radarPath")
  283. .attr("d",lineFunction(this.data))
  284. .attr("fill",this.shapeFill)
  285. .attr("stroke","#a7a7a7")
  286. .attr("stroke-width",1);
  287.  
  288. this.plot.append("path")
  289. .attr("class","outlineHalfPath")
  290. .attr("d",outlineHalfFunction(this.data))
  291. .attr("fill","none")
  292. .attr("stroke","#DCDCDC")
  293. .attr("stroke-width",0.5)
  294. .style("stroke-dasharray", ("3, 3"));
  295.  
  296. this.plot.append("path")
  297. .attr("class","outlineFullPath")
  298. .attr("d",outlineFullFunction(this.data))
  299. .attr("fill","none")
  300. .attr("stroke","#DCDCDC")
  301. .attr("stroke-width",0.5)
  302. .style("stroke-dasharray", ("3, 3"));
  303.  
  304. };
  305.  
  306. Radar_Chart.prototype.updateLines = function(){
  307.  
  308. var that = this;
  309.  
  310. var outlineHalfFunction = d3.line()
  311. .x(function(d){
  312. var r = that.yScale(that.dataMaxValue/2);
  313. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  314. return that.polarXScale(r*Math.cos(theta));
  315. })
  316. .y(function(d){
  317. var r = that.yScale(that.dataMaxValue/2);
  318. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  319. return that.polarYScale(r*Math.sin(theta));
  320. })
  321. .curve(d3.curveLinearClosed);
  322.  
  323. var outlineFullFunction = d3.line()
  324. .x(function(d){
  325. var r = that.yScale(that.dataMaxValue);
  326. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  327. return that.polarXScale(r*Math.cos(theta));
  328. })
  329. .y(function(d){
  330. var r = that.yScale(that.dataMaxValue);
  331. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  332. return that.polarYScale(r*Math.sin(theta));
  333. })
  334. .curve(d3.curveLinearClosed);
  335.  
  336. var lineFunction = d3.line()
  337. .x(function(d){
  338. var r = that.yScale(d.value);
  339. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  340. return that.polarXScale(r*Math.cos(theta));
  341. })
  342. .y(function(d){
  343. var r = that.yScale(d.value);
  344. var theta = (that.dimensionRadianSpacing*that.xScale(d.key) + that.startingRadian) % (2*Math.PI);
  345. return that.polarYScale(r*Math.sin(theta));
  346. })
  347. .curve(d3.curveLinearClosed);
  348.  
  349. this.plot.select(".radarPath")
  350. .transition().duration(1000)
  351. .attr("d",lineFunction(this.data));
  352.  
  353. this.plot.select(".outlineHalfPath")
  354. .transition().duration(1000)
  355. .attr("d",outlineHalfFunction(this.data));
  356.  
  357. this.plot.select(".outlineFullPath")
  358. .transition().duration(1000)
  359. .attr("d",outlineFullFunction(this.data));
  360.  
  361. };
  362.  
  363. Radar_Chart.prototype.addButtons = function(){
  364.  
  365. var that = this;
  366.  
  367. d3.select(".button-container").append("button")
  368. .text("Add New Data")
  369. .on("click",function(){
  370. that.addData()
  371. });
  372.  
  373. d3.select(".button-container").append("button")
  374. .text("Random Data")
  375. .on("click",function(){
  376. that.updateData()
  377. });
  378.  
  379.  
  380. };
  381.  
  382. Radar_Chart.prototype.addData = function(){
  383.  
  384. var newData =
  385. {
  386. key:"Dimension" + (this.dimensionCount + 1),
  387. value:Math.floor(Math.random() * 10) + 1
  388. }
  389.  
  390. this.data.push(newData);
  391.  
  392. this.redraw();
  393.  
  394. };
  395.  
  396. Radar_Chart.prototype.updateData = function(){
  397.  
  398. this.data.map(function(d){
  399. d.value = Math.floor(Math.random() * 10) + 1;
  400. });
  401.  
  402. this.redraw();
  403.  
  404. };
  405.  
  406. Radar_Chart.prototype.redraw = function(){
  407.  
  408. this.generatePolarScale();
  409. this.updateLines();
  410. this.drawShape();
  411.  
  412.  
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement