简体   繁体   中英

Log scale on D3.js

I'm trying to use a scale to draw an audio waveform. The linear scale is too "dynamic", and I'd like to apply a compression on the peaks. I thought about a log scale, although I really don't know how to use it in D3.

Linear scale:

  // Scale for time xScale = d3.scaleLinear() .domain([0,samples_length]).range([0,width]); // Scale for samples yScale = d3.scaleLinear() .domain([-1,0,1]).range([0,height]); let area = d3.area() .x(function(d, i){return xScale(i)}) .y0(function(d){return yScale(d); }) .y1(function(d){return height-yScale(d); }); let waveform = d3.select("#waveform") .append("svg") .attr("width", width) .attr("height", height); waveform.append("path") .attr("d", area(samples)) .attr("fill", "steelblue"); 

The linear scale gives me:

在此输入图像描述

And I'd like something more like this: 在此输入图像描述

I also tried the Symlog scale:

  // Scale for time xScale = d3.scaleSymlog() .domain([0,samples_length]).range([0,width]); // Scale for samples yScale = d3.scaleSymlog() .domain([-1,0,1]).range([0,height]); let area = d3.area() .x(function(d, i){return xScale(i)}) .y0(function(d){return yScale(d); }) .y1(function(d){return height-yScale(d); }); let waveform = d3.select("#waveform") .append("svg") .attr("width", width) .attr("height", height); waveform.append("path") .attr("d", area(samples)) .attr("fill", "steelblue"); } 

And the Symlog gives me:

在此输入图像描述

Any suggestion?

I managed to fix the dynamic plot by simply reducing the scale range:

  // Scale for samples yScale = d3.scaleSymlog() .domain([-1,0,1]) .range([0,height/4]) .constant(-1); 

The final result looks more pleasant:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM