【问题标题】:Labeling the axis with alphanumeric characters用字母数字字符标记轴
【发布时间】:2014-04-26 00:07:55
【问题描述】:

考虑代码 index.html

<!DOCTYPE html>
<html>
  <head>
    <title>D3 test</title>
          <style>

       .grid .tick {
stroke: lightgrey;
        opacity: 0.7;
   }
      .grid path {
stroke-width: 0;
 }
  .chart {
     }
    .main text {
   font: 10px sans-serif;
 }
    .axis line, .axis path {
shape-rendering: crispEdges;
stroke: black;
fill: none;
    }
    circle {
fill: steelblue;
      }



     </style>
    <script type="text/javascript" src="http://mbostock.github.com/d3/d3.v2.js">   

                    </script>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script src="http://d3js.org/topojson.v1.min.js"></script>
  </head>
  <body>
    <div class='content'>
      <!-- /the chart goes here -->
    </div>

   <script type="text/javascript" src="scatterchart.js"></script>

  </body>
</html>

还有scatterchart.js,脚本如下

var color = d3.scale.ordinal().range(["#b41f2d", "#ff7f0e"]);

     var data = [

[2,2],
[3,3],

[4,4],
[5, 4],
[5.5, 5],
[6, 6],
[6, 7],

[6.5,8],
[6.5,16],

[17, 16]
 ];

var margin = {
top: 20,
right: 15,
bottom: 60,
left: 25
   }, width = 950 - margin.left - margin.right,
height = 480 - margin.top - margin.bottom;

    var x = d3.scale.linear()
.domain([0, d3.max(data, function (d) {
return d[0];
   })])
 .range([0, width]);

  var y = d3.scale.linear()
.domain([0, d3.max(data, function (d) {
return d[1];
 })])
//.range([height, 0]) //flip y
.range([0, height]);

      var chart = d3.select('body')
.append('svg:svg')
.attr('width', width + margin.right + margin.left)
.attr('height', height + margin.top + margin.bottom)
.attr('class', 'chart');

    var main = chart.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
.attr('width', width)
.attr('height', height)
.attr('class', 'main');

   // draw the x axis
 var xAxis = d3.svg.axis()
.scale(x)
  //.orient('bottom')
.orient('top'); // adjust ticks to new x axis position

    main.append('g')
//.attr('transform', 'translate(0,' + height + ')')
.attr('transform', 'translate(0,0)') // move x axis up
.attr('class', 'main axis date')
.call(xAxis);

     // draw the y axis
  var yAxis = d3.svg.axis()
.scale(y)
.orient('left');

 main.append('g')
.attr('transform', 'translate(0,0)')
.attr('class', 'main axis date')
.call(yAxis);

  var g = main.append("svg:g");

  g.selectAll("scatter-dots")
  .data(data)
.enter().append("svg:circle")
.attr("cx", function (d, i) {
return x(d[0]);
 }) 
 .attr("cy", function (d) {
return y(d[1]);
 }) 
.attr("r", 5)
.style("fill", function (d) { return color(d[0]);}) ;

     // begin of drawing lines
 var line = d3.svg.line()
.x(function(d){return x(d[0]);})
.y(function(d){return y(d[1]);})
.interpolate("linear");  

 g.append("path")
.attr("d", function(d) { return line(data)})
.attr("transform", "translate(0,0)")
.style("stroke-width", 2)
        .style("stroke", "steelblue")
        .style("fill", "none");
  // end of drawing lines

   main.append("g")
.attr("class", "grid")
.attr("transform", "translate(0," + height + ")")
.call(make_x_axis()
.tickSize(-height, 0, 0)
.tickFormat(""))

      main.append("g")
.attr("class", "grid")
.call(make_y_axis()
.tickSize(-width, 0, 0)
.tickFormat(""))

  function make_x_axis() {
return d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .ticks(30)
  }

   function make_y_axis() {
return d3.svg.axis()
    .scale(y)
    .orient("left")
    .ticks(17)
  }

在此示例中,如何将 x 轴标记为 x1,x2,x3..... 依此类推,而不是 0,1,2,...,将 y 轴标记为 y1,y2,y3... . 等等而不是 0,1,2,?

【问题讨论】:

标签: javascript html css svg d3.js


【解决方案1】:

您可以使用axis.tickFormat 定义一个简单的自定义格式化程序。这是 x 轴的示例:

axis.tickFormat(function(d) {
    return "x" + d;
});

【讨论】:

  • var x = d3.scale.linear() .domain([0, axis.tickFormat(function(d) { return "x" + d; })]) 这是正确的添加方式吗.tickformat ?
  • 不,这与设置秤没有任何关系。这应该和以前一样。然后,当您创建轴(在您的示例中为 make_x_axis)时,您应该在轴上调用 tickFormat。渲染轴时,它将为要渲染的每个刻度标签调用格式化程序。
  • function make_x_axis() { return d3.svg.axis() .scale(x) .orient("bottom") .axis.tickFormat(function(d) { return "x" + d; }) } 我应该把 .tickformat 放在这里吗?
  • 是的,看起来不错。当然,您还需要为 y 轴执行此操作。有用吗?
  • 您的刻度标签在不起作用时会是什么样子?如果在代码中设置断点,d 的值是否符合您的预期?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 2013-06-24
相关资源
最近更新 更多