radarchart.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*https://github.com/IngoKl/D3-v4-Radar-Chart-Redesign/blob/master/index.html*/
  2. /* MIT License */
  3. Shiny.addCustomMessageHandler("radarchart_values", function(message) {
  4. var dataset = message[0].value;
  5. var margin = {top: 50, right: 90, bottom: 30, left: 90},
  6. width = (window.innerWidth * 0.3) - margin.left - margin.right,
  7. height = (window.innerHeight * 0.3) - margin.top - margin.bottom;
  8. //////////////////// Draw the Chart //////////////////////////
  9. var radarChartOptions = {
  10. w: width,
  11. h: height,
  12. margin: margin,
  13. maxValue: 0.5,
  14. labelFactor: 1.25, //How much farther than the radius of the outer circle should the labels be placed
  15. opacityArea: 0.20, //The opacity of the area of the blob
  16. strokeWidth: 2, //The width of the stroke around each blob
  17. };
  18. //Call function to draw the Radar chart
  19. RadarChart("#radarchart_area", dataset, radarChartOptions);
  20. function RadarChart(plot_area, data, cfg) {
  21. //If the supplied maxValue is smaller than the actual one, replace by the max in the data
  22. var maxValue = Math.max(cfg.maxValue, d3.max(data, function(d) {
  23. return d.value;
  24. }));
  25. var allAxis = (data.map(function(d) { //Names of each axis
  26. return d.axis;
  27. }));
  28. var total = allAxis.length, //The number of different axes
  29. radius = Math.min(cfg.w/2, cfg.h/2), //Radius of the outermost circle
  30. angleSlice = Math.PI * 2 / total; //The width in radians of each "slice"
  31. //Scale for the radius
  32. var rScale = d3.scaleLinear()
  33. .range([0, radius])
  34. .domain([0, maxValue]);
  35. //////////// Create the container SVG and g /////////////
  36. //Initiate the radar chart SVG
  37. var svg = d3.select(plot_area).append("svg")
  38. .attr("width", cfg.w + cfg.margin.left + cfg.margin.right)
  39. .attr("height", cfg.h + cfg.margin.top + cfg.margin.bottom)
  40. .attr("class", "radar");
  41. //Append a g element
  42. var g = svg.append("g")
  43. .attr("transform", "translate(" + (cfg.w/2 + cfg.margin.left) + "," + (cfg.h/2 + cfg.margin.top) + ")");
  44. /////////////// Draw the Circular grid //////////////////
  45. //Wrapper for the grid & axes
  46. var axisGrid = g.append("g").attr("class", "axisWrapper");
  47. //////////////////// Draw the axes //////////////////////
  48. //Create the straight lines radiating outward from the center
  49. var axis = axisGrid.selectAll(".axis")
  50. .data(allAxis)
  51. .enter()
  52. .append("g")
  53. .attr("class", "axis");
  54. //Append the lines
  55. axis.append("line")
  56. .attr("x1", 0)
  57. .attr("y1", 0)
  58. .attr("x2", function(d, i) {
  59. return rScale(maxValue*1) * Math.cos(angleSlice*i - Math.PI/2);
  60. })
  61. .attr("y2", function(d, i) {
  62. return rScale(maxValue*1) * Math.sin(angleSlice*i - Math.PI/2);
  63. })
  64. .attr("class", "line")
  65. .style("stroke", "#CDCDCD")
  66. .style("stroke-width", "2px")
  67. .style("stroke-dasharray", "5,5");
  68. //Append the labels at each axis
  69. axis.append("text")
  70. .attr("class", "legend")
  71. .style("font-size", "11px")
  72. .attr("text-anchor", "middle")
  73. .attr("dy", "0.35em")
  74. .attr("x", function(d, i) {
  75. return rScale(maxValue * cfg.labelFactor) * Math.cos(angleSlice*i - Math.PI/2);
  76. })
  77. .attr("y", function(d, i) {
  78. return rScale(maxValue * cfg.labelFactor) * Math.sin(angleSlice*i - Math.PI/2);
  79. })
  80. .text(function(d){return d;});
  81. ///////////// Draw the radar chart blobs ////////////////
  82. //The radial line function
  83. var radarLine = d3.radialLine()
  84. .curve(d3.curveLinearClosed)
  85. .radius(function(d) {
  86. return rScale(d.value);
  87. })
  88. .angle(function(d,i) {return i*angleSlice; });
  89. //Create a wrapper for the blobs
  90. var blobWrapper = g.selectAll(".radarWrapper")
  91. .data(data)
  92. .enter()
  93. .append("g")
  94. .attr("class", "radarWrapper");
  95. //Append the backgrounds
  96. blobWrapper
  97. .append("path")
  98. .attr("class", "radarArea")
  99. .attr("d", radarLine(data))
  100. .style("fill", "#EDC951")
  101. .style("fill-opacity", cfg.opacityArea);
  102. //Create the outlines
  103. blobWrapper.append("path")
  104. .attr("class", "radarStroke")
  105. .attr("d", radarLine(data))
  106. .style("stroke-width", cfg.strokeWidth + "px")
  107. .style("stroke", "#EDC951")
  108. .style("fill", "none");
  109. }//RadarChart
  110. });