【问题标题】:NVD3 - show tick lines only on axesNVD3 - 仅在轴上显示刻度线
【发布时间】:2022-08-04 09:56:16
【问题描述】:

TLDR:我有一个 NVD3 图表,它显示整个轴上的刻度线,但我想更改它,以便它只在可能的情况下显示在轴线上。

这是一个活生生的例子:

var app = angular.module(\'plunker\', [\'nvd3\']);

app.controller(\'MainCtrl\', function($scope) {
  $scope.options = {
            chart: {
                type: \'lineChart\',
                height: 450,
                margin : {
                    top: 20,
                    right: 20,
                    bottom: 80,
                    left: 55
                },
                x: function(d){ return d.x; },
                y: function(d){ return d.y; },
                useInteractiveGuideline: true,
                xAxis: {
                    axisLabel: \'Timeline\',
                    tickFormat: function(d) {
                        return d3.time.format(\'%B %d\')(new Date(d))
                    },
                    ticks: 6,
                    showMaxMin: false
                },
                yAxis: {
                    axisLabel: \'Molecular density (kg/m^3)\',
                    tickFormat: function(d){
                        return d3.format(\'.02f\')(d);
                    },
                    axisLabelDistance: -10,
                    showMaxMin: false
                }
            }
        };

        $scope.data = [{\"key\":\"K7 molecules\",\"values\":[{\"x\":1435708800000,\"y\":8},{\"x\":1435795200000,\"y\":9},{\"x\":1435881600000,\"y\":8},{\"x\":1435968000000,\"y\":8},{\"x\":1436054400000,\"y\":9},{\"x\":1436140800000,\"y\":9},{\"x\":1436227200000,\"y\":8},{\"x\":1436313600000,\"y\":8},{\"x\":1436400000000,\"y\":9},{\"x\":1436486400000,\"y\":9},{\"x\":1436572800000,\"y\":7},{\"x\":1436659200000,\"y\":8}],\"area\":true,\"color\":\"#0CB3EE\"},{\"key\":\"N41 type C molecules\",\"values\":[{\"x\":1435708800000,\"y\":8},{\"x\":1435795200000,\"y\":7},{\"x\":1435881600000,\"y\":8},{\"x\":1435968000000,\"y\":9},{\"x\":1436054400000,\"y\":7},{\"x\":1436140800000,\"y\":9},{\"x\":1436227200000,\"y\":8},{\"x\":1436313600000,\"y\":9},{\"x\":1436400000000,\"y\":9},{\"x\":1436486400000,\"y\":9},{\"x\":1436572800000,\"y\":9},{\"x\":1436659200000,\"y\":8}],\"area\":true,\"color\":\"#383838\"}];
});
<!DOCTYPE html>
<html ng-app=\"plunker\">

  <head>
    <meta charset=\"utf-8\" />
    <title>Angular-nvD3 Line Chart</title>
    <script>document.write(\'<base href=\"\' + document.location + \'\" />\');</script>
    <link rel=\"stylesheet\" href=\"style.css\" />
    <link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.min.css\"/>
    <script src=\"//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js\"></script>
    <script src=\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js\" charset=\"utf-8\"></script>
    <script src=\"https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.min.js\"></script>
    <script src=\"https://rawgit.com/krispo/angular-nvd3/v1.0.1/dist/angular-nvd3.js\"></script>
    <script src=\"app.js\"></script>
  </head>

  <body ng-controller=\"MainCtrl\">
    
    <nvd3 options=\"options\" data=\"data\" class=\"with-3d-shadow with-transitions\"></nvd3>
    
  </body>

</html>

有什么办法可以让刻度线只出现在轴线上?为了清楚起见,这就是它的样子:

我使用不同的库来生成以下图,并且我希望刻度线仅出现在此示例的轴线上:

  • 只需在此处注意“不,NVD3 无法完成”之类的内容也是可以接受的答案。我想知道如何做到这一点,前提是它可以做到。谢谢!

标签: d3.js nvd3.js


【解决方案1】:

NVD3 似乎没有真正的方法可以做到这一点,因为它没有提供在轴上显示刻度线的方法。但是,我们可以通过获取图表 SVG 然后对其进行修改来添加我们自己的刻度线。

我附上了一个向 X 轴添加刻度线的示例,它基本上是基于这里的 jsfiddle 稍微修改的:http://jsfiddle.net/3r88bgjw

var data;
data = [{
  values: [],
}, ];

var i, x;
var prevVal = 3000;
var tickCount = 2000;
for (i = 0; i < tickCount; i++) {
  x = 1425096000 + i * 10 * 60; // data points every ten minutes
  if (Math.random() < 0.8) { // add some gaps
    prevVal += (Math.random() - 0.5) * 500;
    if (prevVal <= 0) {
      prevVal = Math.random() * 100;
    }
    data[0].values.push({
      x: x * 1000,
      y: prevVal
    });
  }
}

var chart;

nv.addGraph(function() {
  chart = nv.models.historicalBarChart();
  chart.xScale(d3.time.scale()) // use a time scale instead of plain numbers in order to get nice round default values in the axis
    .color(['#68c'])
    .useInteractiveGuideline(true) // check out the css that turns the guideline into this nice thing
    .margin({
      "left": 80,
      "right": 50,
      "top": 20,
      "bottom": 30,
    })
    .noData("There is no data to display.")
    .duration(0);

  var tickMultiFormat = d3.time.format.multi([
    ["%-I:%M%p", function(d) {
      return d.getMinutes();
    }], // not the beginning of the hour
    ["%-I%p", function(d) {
      return d.getHours();
    }], // not midnight
    ["%b %-d", function(d) {
      return d.getDate() != 1;
    }], // not the first of the month
    ["%b %-d", function(d) {
      return d.getMonth();
    }], // not Jan 1st
    ["%Y", function() {
      return true;
    }]
  ]);
  chart.xAxis
    .showMaxMin(false)
    .tickPadding(10)
    .tickFormat(function(d) {
      return tickMultiFormat(new Date(d));
    });

  chart.yAxis
    .tickFormat(d3.format(",.0f"));

  var svgElem = d3.select('#chart svg');
  svgElem
    .datum(data)
    .transition()
    .call(chart);

  // make our own x-axis tick marks because NVD3 doesn't provide any
  var tickY2 = chart.yAxis.scale().range()[1];
  var lineElems = svgElem
    .select('.nv-x.nv-axis.nvd3-svg')
    .select('.nvd3.nv-wrap.nv-axis')
    .select('g')
    .selectAll('.tick')
    .data(chart.xScale().ticks())
    .append('line')
    .attr('class', 'x-axis-tick-mark')
    .attr('x2', 0)
    .attr('y1', tickY2 + 7)
    .attr('y2', tickY2)
    .attr('stroke-width', 3);

  // set up the tooltip to display full dates
  var tsFormat = d3.time.format('%b %-d, %Y %I:%M%p');
  var contentGenerator = chart.interactiveLayer.tooltip.contentGenerator();
  var tooltip = chart.interactiveLayer.tooltip;
  tooltip.contentGenerator(function(d) {
    d.value = d.series[0].data.x;
    return contentGenerator(d);
  });
  tooltip.headerFormatter(function(d) {
    return tsFormat(new Date(d));
  });

  return chart;
});
<div>Try resizing the panel to see the various types of time labels.</div>
<br>
<div id="chart">
  <svg></svg>
</div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.4/nv.d3.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.4/nv.d3.min.js"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-11
    • 2020-07-26
    • 2014-12-24
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多