将结果转换为d3中的百分比 [英] Convert result to percentage in d3

查看:391
本文介绍了将结果转换为d3中的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是开始学习d3.js +折线图与传奇&工具提示。我可以使它工作,但我想要的是使yAxis结果是百分比格式。尝试使用tickFormat但不知何故,它不会将我的原始数字转换为百分比。 T__T

I'm only starting to learn d3.js + Line chart with Legend & Tooltips. And I'm able to make it work but what I want is to have the yAxis result to be in percentage format. Tried using tickFormat but somehow it doesn't convert my raw number to percentage. T__T

以下是我的js代码

chart = d3LineWithLegend()
            .xAxis.label('Date')
            .width(700)
            .height(300)
            .yAxis.label('Frequency');


var svg = d3.select('#histogram svg').datum(data);

svg.transition().duration(1000)
  .attr('width', 700)
  .attr('height', 300)
  .call(chart);


chart.dispatch.on('showTooltip', function(e) {
    var offset = $('#histogram').offset(), // { left: 0, top: 0 }
    left = e.pos[0] + offset.left,
    top = e.pos[1] + offset.top,
    // formatter = d3.format(".04f");
    formatter = d3.format(".0%");
    var yAxis = d3.svg.axis().orient("left").tickFormat(formatter);//added this part but it didn't work
    console.log(yAxis);

    var dateObj = (new Date(e.point[0]));

    var year = dateObj.getFullYear();
    var month = dateObj.getMonth() + 1;
    var day = dateObj.getDate();
    var hours = dateObj.getHours();
    var date = month + '-' + day + '-' + year;
    if(filter === 'hour') {
      date = date + ', ' + hours + ':00';
    }

    var content = '<h3>' + date + '</h3>' +
              '<p>' +
              '<span class="value">' + (e.point[1]) + '</span>' +
              '</p>';

    nvtooltip.show([left, top], content);
});

chart.dispatch.on('hideTooltip', function(e) {
   nvtooltip.cleanup();
});

在上面的代码中似乎有什么问题?如果我不使它在客户端工作我想在服务器端调整。但是工作太多。

What seem to be the problem in my above code? If I don't make it work on the client side I'm thinking of adjusting this in server side. But too much work.

推荐答案

d3.format(。0%)不会缩放。它所做的只是乘以100,并在其末尾添加一个%。 (我相信.0%将不会增加精确度。如果你想精确到十分之一,请改用.1%,不知道是否需要)

d3.format(".0%") won't do scaling. All it does is simply multiply by 100 and add a % to the end of it. (And I believe that .0% will add no precision. If you want precision to the tenth's place, use .1% instead. Don't know if this was wanted)

您可能希望使用d3.scale.linear(),以便首先缩放数据,使其在0到1的范围内。然后,您可以创建一个使用从0到1的域的yscale,对应于最后y轴的0%到100%。

You might want to use d3.scale.linear() in order to scale your data first so that it is in the range of 0 to 1. And then you can create a yscale that uses domains from 0 to 1, which will correspond to 0% to 100% in the final y-axis.

//Run data through this pre-scaling.
prescale = d3.scale.linear()
                   .domain([dataMin, dataMax])
                   .range([0,1])

//Use this y-scale in place of another potential y-scaling function.

yScale = d3.scale.linear()
                 .domain([0, 1])
                 .range([canvasHeightLow, canvasHeightHigh]);


//Afterwards, you can use this yScale to build your yAxis

formatter = d3.format(".0%");

yAxis = d3.svg.axis().orient("left")
                     .scale(yScale)
                     .tickFormat(formatter)

希望这有助。

这篇关于将结果转换为d3中的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆