个性化d3 x轴标签

时间:2017-06-26 17:59:25

标签: javascript d3.js svg

我正在制作折线图,经过一些谷歌搜索,我想出了这个:

function maxValue(input) {
          var current = 0;

          for (var i = 0; i < input.length; i++) {
              if (input[i] > current) {
                current = input[i];
              }
          }

          return current;
        }

        var m = [80, 80, 80, 80]; // margins
        var w = 900 - m[1] - m[3]; // width
        var h = 600 - m[0] - m[2]; // height

        var data = [1, 6, 5, 12, 30, 45, 50, 48, 60, 69, 62, 80];

        var months = ["January", "February", "March", "April", "May",
                     "June", "July", "August", "September", "October", 
                     "November", "December"];

        var x = d3.scale.linear().domain([1, 12]).range([0, w]);
        var y = d3.scale.linear().domain([0, maxValue(data)]).range([h, 0]);

        var line = d3.svg.line()
            .x(function(d,i) { 
                return x(i + 1); 
            })
            .y(function(d) {
                return y(d); 
            })

            var graph = d3.select("#graph").append("svg:svg")
                  .attr("width", w + m[1] + m[3])
                  .attr("height", h + m[0] + m[2])
                .append("svg:g")
                  .attr("transform", "translate(" + m[3] + "," + m[0] + ")");

            var xAxis = d3.svg.axis().scale(x).tickSize(-h).tickSubdivide(true);
            graph.append("svg:g")
                  .attr("class", "x axis")
                  .attr("transform", "translate(0," + h + ")")
                  .call(xAxis);


            var yAxisLeft = d3.svg.axis().scale(y).ticks(3).orient("left");
            graph.append("svg:g")
                  .attr("class", "y axis")
                  .attr("transform", "translate(-25,0)")
                  .call(yAxisLeft);

            graph.append("svg:path").attr("d", line(data));

            graph.append("text")
              .attr("x", w/2)
              .attr("y", -20)
              .style("text-anchor", "middle")
              .style("font-size", "18")
              .text("Total Hits in the Last Year");

正如您所看到的,我为一年中的几个月制作了一个数组,并希望x轴显示月份而不是数值。但我无法告诉它在哪里制作标签。谁知道如何帮忙?谢谢!

P.S。做什么&#34; svg:svg&#34;做的不仅仅是&#34; svg?&#34;

编辑:

&#13;
&#13;
function maxValue(input) {
          var current = 0;
          
          for (var i = 0; i < input.length; i++) {
              if (input[i] > current) {
                current = input[i];
              }
          }
          
          return current;
        }
      
		var m = [80, 80, 80, 80]; // margins
		var w = 900 - m[1] - m[3]; // width
		var h = 600 - m[0] - m[2]; // height
      
		var data = [1, 6, 5, 12, 30, 45, 50, 48, 60, 69, 62, 80];

        var months = ["January", "February", "March", "April", "May",
                     "June", "July", "August", "September", "October", 
                     "November", "December"];
      
		var x = d3.scale.linear().domain([1, 12]).range([0, w]);
		var y = d3.scale.linear().domain([0, maxValue(data)]).range([h, 0]);

		var line = d3.svg.line()
			.x(function(d,i) { 
				return x(i + 1); 
			})
			.y(function(d) {
				return y(d); 
			})

			var graph = d3.select("#graph").append("svg:svg")
			      .attr("width", w + m[1] + m[3])
			      .attr("height", h + m[0] + m[2])
			    .append("svg:g")
			      .attr("transform", "translate(" + m[3] + "," + m[0] + ")");

			var xAxis = d3.svg.axis().scale(x).tickSize(-h).tickSubdivide(true);
			graph.append("svg:g")
			      .attr("class", "x axis")
			      .attr("transform", "translate(0," + h + ")")
			      .call(xAxis);


			var yAxisLeft = d3.svg.axis().scale(y).ticks(3).orient("left");
			graph.append("svg:g")
			      .attr("class", "y axis")
			      .attr("transform", "translate(-25,0)")
			      .call(yAxisLeft);
			
  			graph.append("svg:path").attr("d", line(data));
			
            graph.append("text")
              .attr("x", w/2)
              .attr("y", -20)
              .style("text-anchor", "middle")
              .style("font-size", "18")
              .text("Total Hits in the Last Year");
&#13;
path {
				stroke: steelblue;
				stroke-width: 1;
				fill: none;
			}
			
			.axis {
			  shape-rendering: crispEdges;
			}

			.x.axis line {
			  stroke: lightgrey;
			}

			.x.axis .minor {
			  stroke-opacity: .5;
			}

			.x.axis path {
			  display: none;
			}

			.y.axis line, .y.axis path {
			  fill: none;
			  stroke: #000;
			}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<html>
	<head>
		<title>Simple Line Graph</title>
	</head>
	<body>

	<div id="graph" class="aGraph" style="position:absolute;top:0px;left:0; float:left;"></div>

	</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您可以使用tickFormat函数为xAxis从数组中获取月份名称:

var xAxis = d3.svg.axis().scale(x).tickSize(-h).tickSubdivide(true).tickFormat(function(d) {
        return months[d-1]; 
    });