【问题标题】:NVD3 Format Date Differently From X-AxisNVD3 格式日期不同于 X 轴
【发布时间】:2014-10-11 15:39:28
【问题描述】:

我有一个 NVD3 图表,希望将工具提示窗口中的日期值格式化为精确到秒的完整日期,同时仅按天更改 x 轴上的标签。

这是我的图表:

    d3.json("mydata.json", function (error,data) {
        var chart;
        nv.addGraph(function() {
            var chart = nv.models.lineChart()
                    .margin({left: 100})  //Adjust chart margins to give the x-axis some breathing room.
                    .useInteractiveGuideline(true)  //We want nice looking tooltips and a guideline!
                    .transitionDuration(350)  //how fast do you want the lines to transition?
                    .showLegend(true)       //Show the legend, allowing users to turn on/off line series.
                    .showYAxis(true)        //Show the y-axis
                    .showXAxis(true)        //Show the x-axis
                ;
            chart.xAxis     //Chart x-axis settings
                .axisLabel('Date')
                .tickFormat(function(d){return d3.time.format('%d-%m-%y')(new Date(d));});
            chart.yAxis     //Chart y-axis settings
                .axisLabel('Temperature')
                .tickFormat(d3.format('.02f'));
            d3.select('#chart svg')    //Select the <svg> element you want to render the chart in.
                .datum(data)         //Populate the <svg> element with chart data...
                .call(chart);          //Finally, render the chart!
            nv.utils.windowResize(function() { chart.update() });
            return chart;
        });
    });

还有一些示例数据:

[{"key":"Ambient Temperature","color":"#0000ff","values":[{"x":1407978270000,"y":22.47},{"x":1407978353000,"y":22.53},{"x":1407978353000,"y":22.53},{"x":1407978415000,"y":22.63},{"x":1407978523000,"y":22.51},{"x":1407978524000,"y":22.51},{"x":1407978546000,"y":22.5},{"x":1407978562000,"y":22.41},{"x":1407978571000,"y":22.35}]},{"key":"Windchill Temperature","color":"#00ff00","values":[{"x":1407978270000,"y":22.47},{"x":1407978353000,"y":22.53},{"x":1407978353000,"y":22.53},{"x":1407978415000,"y":22.63},{"x":1407978523000,"y":22.51},{"x":1407978524000,"y":22.51},{"x":1407978546000,"y":22.5},{"x":1407978562000,"y":22.41},{"x":1407978571000,"y":22.35}]},{"key":"Dewpoint Temperature","color":"#ff0000","values":[{"x":1407978270000,"y":10.86},{"x":1407978353000,"y":10.05},{"x":1407978353000,"y":10.05},{"x":1407978415000,"y":9.6},{"x":1407978523000,"y":9.97},{"x":1407978524000,"y":9.97},{"x":1407978546000,"y":10.83},{"x":1407978562000,"y":11.19},{"x":1407978571000,"y":11.15}]}]

关于如何实现这一点的任何建议?谢谢!

【问题讨论】:

标签: nvd3.js


【解决方案1】:

http://cmaurer.github.io/angularjs-nvd3-directives/multi.bar.chart.html

你可以这样看: 使用 Angular,这不是我的工作!:

$scope.toolTipContentFunction = function(){
    return function(key, x, y, e, graph) {
        return  'Super New Tooltip' +
            '<h1>' + key + '</h1>' +
            '<p>' +  y + ' at ' + x + '</p>'
    }
}



<div ng-controller="ExampleCtrl">
    <nvd3-multi-bar-chart
        data="exampleData"
        id="toolTipContentExample"
        width="550"
        height="350"
        xAxisTickFormat="xAxisTickFormatFunction()"
        showXAxis="true"
        showYAxis="true"
        interactive="true"
        tooltips="true"
        tooltipcontent="toolTipContentFunction()">
            <svg></svg>
    </nvd3-multi-bar-chart>
</div>

【讨论】:

  • 感谢您的解决方案!不幸的是,我现在无法验证它,因为此时我为了某些方便而搬到了人力车。除非有人对此提出异议,否则我将标记为答案。再次感谢! :)
猜你喜欢
  • 2013-10-27
  • 2016-02-19
  • 1970-01-01
  • 2020-05-03
  • 1970-01-01
  • 1970-01-01
  • 2020-12-07
  • 2014-08-12
  • 1970-01-01
相关资源
最近更新 更多