How to set specific y-axis label points in dygraphs?

*爱你&永不变心* 提交于 2019-11-30 22:50:42

You can do this by writing your own y-axis ticker function: http://dygraphs.com/options.html#ticker

This is pretty advanced customization, so be wary and read the documentation in dygraph-tickers.js. Here's some rough code:

<script type="text/javascript">
  g = new Dygraph(div, data, {
    axes: {
      y: {
        ticker: function(min, max, pixels, opts, dygraph, vals) {
          return [{v:0, label:"0"}, {v:5, label:"5"}, {v:10, label:"10"}];
        }
      }
    }
  });
</script>

One downside of this approach is that these are the only y-axis tick marks you'll ever get, even if the user pans/zooms on the y-axis. Depending on your application, that may or may not be OK.

If you just want to add a label to those that are auto-generated

g = new Dygraph(div, data, {
    axes: {
      y: {
        ticker: function(min, max, pixels, opts, dygraph, vals) {
          var your_value = 7.5;

          //Get auto-generated tickers (numericTicks is the default ticker generator)
          var tickers = Dygraph.numericTicks(min, max, pixels, opts, dygraph, vals);
          tickers.push({v: your_value, label: 'Custom Label'}); //Insert your label

          return tickers;
        }
      }
    }
  });
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!