cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-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.

            
              .meter {
  stroke: linear-gradient(white, gray);
}
.stop-left {
  stop-color: GreenYellow; 
}
.stop-middle {
  stop-color: Orange; 
}
.stop-right {
  stop-color: Red;
}
.filled {
  stroke: url(#mainGradient);
  fill: url(#mainGradient);
}
            
          
!
            
              // SVGの幅
var width = 200;
var height =200;

var p = Math.PI;
var startAngleRate = -1.4
var endAngleRate = 1.4
    
var svg = d3.select("#test").append("svg")
 .attr("width", width)
 .attr("height", height);

// メーターのグループ
var translated = svg.append("g")
  .attr("transform","translate(" + width/2 + "," + height/2 + ")");

var arc = d3.arc()
  .innerRadius(55)
  .outerRadius(60)
  .startAngle(startAngleRate * p/2) // ここはラジアン
  .endAngle(endAngleRate * p/2 )

// メーターの範囲を設定
var arcScale = d3.scaleLinear()
    .domain([0 , 100])
    .range([startAngleRate*90, endAngleRate*90]); // ここは度

var svgDefs = svg.append('defs');
var mainGradient = svgDefs.append('linearGradient')
    .attr('id', 'mainGradient');

// グラデーションの設定(3色)
mainGradient.append('stop')
    .attr('class', 'stop-left')
    .attr('offset', '0');
mainGradient.append('stop')
    .attr('class', 'stop-middle')
    .attr('offset', '0.6');
mainGradient.append('stop')
    .attr('class', 'stop-right')
    .attr('offset', '1');

// メーターバー部分の追加
translated.append("path")
    .attr("d", arc)
    .attr("class", "filled")

// メーターの針の追加 
var linePath = translated.append("line")
    .attr("x1",0)
    .attr("x2",0)
    .attr("y1",0)
    .attr("y2",-45)    
    .attr("stroke", "black")
    .attr("stroke-width",3)
    .attr("transform", "rotate(" +  arcScale(0) + ")" ); // 初期位置は0

// 目盛の追加
// 0 を100個の5間隔で配列を作成
var ticks = d3.range(0, 105, 5);
// グループ要素を目盛数分追加する
var ticks_g = svg.selectAll(".tick").data(ticks).enter()
  .append("g")
  .attr("class","tick")
  .attr("transform",function(d,i){ return "translate(" + width/2 + "," + height/2 + ")"; });
/* 
移動と回転を同時にする場合はこっち
.attr("transform",function(d,i){ 
    return "translate(" + width/2 + "," + height/2 + ")rotate(" + arcScale(d) + ")"; });
*/

// 目盛りの線
var ticksLine = d3.line()
  .x(0) 
  .y(function(d) { return d; });

// 目盛りの追加
ticks_g.append("path")
  .attr("d", function(d){ 
    if(d % 20 == 0 ){ return ticksLine([ -50 , -45]); } // 主目盛
    else{ return ticksLine([-50 , -48]); }} // 補助目盛
  )
  .attr("stroke", "gray")
  .attr("stroke-width",2)
  .attr("stroke-linecap","round")
  .attr("transform",function(d,i){ return "rotate(" + arcScale(d) + ")"; });
  
// ラベル 位置は角度から(x,y)を求めて、貼り付け
ticks_g.append("text")
  .attr("x",  function(d){ return 32 * Math.sin(arcScale(d) * (Math.PI / 180)); }) 
  .attr("y" , function(d){ return -32 * Math.cos(arcScale(d) * (Math.PI / 180)) + 2; })
  .attr("text-anchor", "middle")
  .attr("fill", "black")
  .attr("font-size", "14px")
  .attr("font-family", "sans-serif")
  .text(function(d){ if(d % 20 == 0 ){ return d; }});

// メータの保持する値
var value = 0; 
var tmp = 0;

// メータの更新
function update(){
  // 乱数取得
  var rand = d3.randomUniform(100)();
  value = tmp;
  
  // 針の動作アニメーション 以前の値⇒更新後の値へ針を動かす
  var t = d3.transition().duration(1000)
  linePath.transition(t)
  .attrTween("transform", function() { 
  return d3.interpolateString("rotate(" + arcScale(value) + ")", "rotate(" + arcScale(rand)+ ")"); });
  // 値を保持  
  tmp = rand;
}

// 1秒ごとに更新を呼び出し
d3.interval(update, 1000 );


            
          
!
999px
Loading ..................

Console