cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div id="result">
</div>
<div>
  <input type="button" value="Add" onclick="pushData()" />
  <select name="interpolate">
    <option value="linear">Linear</option>
    <option value="cardinal">CcurveCardinal</option>
    <option value="curveBasisClosed">CurveBasicClosed</option>
    <option value="curveAlpha0.5">CurveCatmullRom Alpha 0.5</option>    
    <option value="step-before">step-before</option>
    <option value="step-after">step-after</option>
  </select>
</div>
            
          
!
            
              /* 軸のスタイル */
.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: auto; /*アンチエイリアス処理*/
}
/* Pathのスタイル */
.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 1.5px;
}
            
          
!
            
              // マージンの設定(領域)
var margin = {top: 30, right: 20, bottom: 30, left: 50},
    width = 500 - margin.left - margin.right,
    height = 300 - margin.top - margin.bottom;

// 描画データ
var array = [ 2 , 0 , 3  , 4 , -1 , -2 , 5 , -3 , 1 , -5
            , 0 , 0 , 3  , 0 , -1 , -2 , -1 , -3 , 1 , -5 ] ;

// スケールの設定 x要素としてインデックス
var xScale = d3.scaleLinear() // v4
    .domain(d3.extent(array, function(d,i) { return i; }))
    .range([0, width]);

// スケールの設定 y要素として配列の値
var yScale = d3.scaleLinear() // v4
    .domain(d3.extent(array, function(d,i) { return d; }))
    .range([height, 0]);

// X軸
//var xAxis = d3.svg.axis()
//    .scale(xScale)
//   .orient("bottom")
//    .tickFormat(""); // 軸ラベルはなしにした
var xAxis = d3.axisBottom(xScale);

// Y軸
//var yAxis = d3.svg.axis()
//   .scale(yScale)
//    .orient("left");
var yAxis = d3.axisLeft(yScale);

// 線グラフのラインオブジェクト
var line = d3.line()
    .x(function(d,i) { return xScale(i); })
    .y(function(d,i) { return yScale(d); })
    .curve(d3.curveLinear);

// SVGの追加
var svg = d3.select("#result").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

// グループ要素にX軸を追加
svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height / 2 + ")") // 真ん中にした
    .call(xAxis);

// グループ要素にY軸を追加
svg.append("g")
    .attr("class", "y axis")
    .call(yAxis)
  .append("text")
    .attr("x", 10)
    .attr("y", -10)
    .style("text-anchor", "end")
    .text("Value");

// 実際のラインを追加
var paths = svg.append("path")
    .datum(array) // 配列をマッピング
    .attr("class", "line")
    .attr("d", line);

// 線種保存
var linestyle;

// データ追加し、再描画がするメソッド
function pushData(){
  // 先頭を削除
  array.shift();

  // +5から-5までの乱数発生
  var rand = Math.floor( Math.random() * 11 ) - 5 ;
  // 配列に追加
  array.push(rand);

  // カーブ属性を変更
  line.curve(getCurve());
  
  paths.attr("d", line);
}

// コンボからカーブ属性を取ってくるだけ
function getCurve(){
  var val = $('[name=interpolate]').val();
  
  if(val == "cardinal")
    return d3.curveCardinal; 
  else if(val == "curveBasisClosed")
    return d3.curveBasisClosed;
  else if(val == "curveAlpha0.5")
    return d3.curveCatmullRom.alpha(0.5);
  else if(val == "step-before")
    return d3.curveStepBefore;
  else if(val == "step-after")
    return d3.curveStepAfter;
  else
    return d3.curveLinear;
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console