Advertisement
dhshin

overview+detail_skel2

Jun 28th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.58 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8">
  5.   <meta name="viewport" content="width=device-width">
  6.   <title>JS Bin</title>
  7. </head>
  8. <body>
  9.   <svg width="960" height="500"></svg>
  10.   <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js"></script>
  11.   <script>
  12.    
  13.     function ready(fn) {
  14.       if (document.readyState != 'loading'){
  15.         fn();
  16.       } else {
  17.         document.addEventListener('DOMContentLoaded', fn);
  18.       }
  19.     }
  20.    
  21.     ready(init);
  22.    
  23.     function init() {
  24.      
  25.       let parseDate = d3.timeParse("%b-%y");
  26.      
  27.       function parsing(d) {
  28.         d.date = parseDate(d.date);
  29.         return d;
  30.       }
  31.      
  32.       data = data.map(parsing);
  33.      
  34.       let svg = d3.select("svg");
  35.       let margin = {top: 20, right: 20, bottom: 110, left: 40};
  36.       let margin2 = {top: 430, right: 20, bottom: 30, left: 40};
  37.       let width = +svg.attr("width") - margin.left - margin.right;
  38.       let height = +svg.attr("height") - margin.top - margin.bottom;
  39.       let height2 = +svg.attr("height") - margin2.top - margin2.bottom;
  40.      
  41.       let chart = svg
  42.         .append('g')
  43.           .attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')');
  44.      
  45.       let overview = svg
  46.         .append('g')
  47.           .attr('transform', 'translate(' + margin2.left + ', ' + margin2.top + ')');
  48.      
  49.       let x = d3
  50.       .scaleTime()
  51.       .domain(d3.extent(data, d => d.date))
  52.       .range([0, width]);
  53.      
  54.       let x2 = d3
  55.       .scaleTime()
  56.       .domain(x.domain())
  57.       .range(x.range());
  58.      
  59.       let y = d3
  60.       .scaleLinear()
  61.       .domain([0, d3.max(data, d => d.price)])
  62.       .range([height, 0]);
  63.      
  64.       let y2 = d3
  65.       .scaleLinear()
  66.       .domain(y.domain())
  67.       .range([height2, 0]);
  68.      
  69.       // μΆ• 그리기
  70.       let xAxis = d3.axisBottom(x);
  71.       let xAxis2 = d3.axisBottom(x2);
  72.       let yAxis = d3.axisLeft(y);
  73.      
  74.       chart
  75.         .append('g')
  76.           .attr('transform', 'translate(0, ' + height + ')')
  77.           .call(xAxis);
  78.      
  79.       chart
  80.         .append('g')
  81.           .call(yAxis);
  82.      
  83.       overview
  84.         .append('g')
  85.           .attr('transform', 'translate(0, ' + height2 + ')')
  86.           .call(xAxis2);
  87.      
  88.       let priceArea = d3.area()
  89.       .x(d => x(d.date))
  90.       .y1(d => y(d.price))
  91.       .y0(height)
  92.       .curve(d3.curveMonotoneX);
  93.      
  94.       chart
  95.       .append('path')
  96.         .datum(data)
  97.         .attr('fill', 'steelblue')
  98.         .attr('d', priceArea);
  99.     }
  100.        
  101.   </script>
  102. </body>
  103. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement