首页 > 解决方案 > 如何在 D3 图表库中从零开始 xAxis 标签

问题描述

我目前正在使用 D3 Chart 来绘制我的图表。

我画折线图的问题是图的横轴不是从零开始,而是图画的线从零开始,而且线和横轴不在同一个方向(见下文例子)

注意:xAxis 值为 stringyAxis 值为 number

我希望水平轴和图表绘制的线都从零开始。

let data = [
    {news_count: 10, timestamp: 1630284002},
    {news_count: 25, timestamp: 1630398624},
    {news_count: 14, timestamp: 1630485024},
    {news_count: 19, timestamp: 1630571424},
    {news_count: 34, timestamp: 1630657824},
    {news_count: 45, timestamp: 1630744224},
    {news_count: 63, timestamp: 1630830624},
    {news_count: 57, timestamp: 1630917024},
    {news_count: 74, timestamp: 1631003424},
    {news_count: 100, timestamp: 1631089824},
    {news_count: 110, timestamp: 1631675428}
];

data.forEach(datum => {
    datum.date = unix_timestamp_converter(datum.timestamp);
});

let maxNewsCount = d3.max(data, function(d){ return d.news_count });
let maxDate = d3.max(data, function(d){ return d.timestamp });
let minDate = d3.min(data, function(d){ return d.timestamp });


var margin = {top: 50, right: 50, bottom: 50, left: 50}
  ,width = window.innerWidth - margin.left - margin.right // Use the window's width 
  ,height = window.innerHeight - margin.top - margin.bottom; // Use the window's height

let xAxisDomain = [];

data.forEach(function(datum, index) {
    xAxisDomain[index] = unix_timestamp_converter(datum.timestamp);
});

let y = d3.scaleLinear()
          .domain([0, maxNewsCount])
          .range([height, 0]);

let x = d3.scaleBand()
          .domain(xAxisDomain)
          .range([0,width])
          .paddingInner(0.05)
          .align(0.1);

let yAxis = d3.axisLeft(y);
let xAxis = d3.axisBottom(x);



let svg = d3.select('body').append('svg')
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom);

let chartGroup = svg.append('g')
                    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");


let line = d3.line()
             .x(function(d){ return x(unix_timestamp_converter(d.timestamp)) })
             .y(function(d){ return y(d.news_count) })
             .curve(d3.curveMonotoneX);

chartGroup.append('path')
          .attr("class", "line")
          .attr('d', line(data));

chartGroup.append('g')
          .attr('class', 'x axis')
          .attr("transform", "translate(0," + height + ")")
          .call(xAxis);

chartGroup.append('g')
          .attr('class', 'y axis')
          .call(yAxis);

chartGroup.selectAll(".dot")
            .data(data)
            .enter().append("circle") // Uses the enter().append() method
            .attr("class", "dot") // Assign a class for styling
            .attr("cx", function(d, i) { return x(d.date) })
            .attr("cy", function(d) { return y(d.news_count) })
            .attr("r", 5);


// Other functions here ...

function unix_timestamp_converter(timestamp, format = 'short') {
    let timeStampInMilliSecond = timestamp * 1000;

    let date = new Date(timeStampInMilliSecond);

    date = date.toLocaleDateString('en-US', {
            day: '2-digit',
            month: '2-digit'
    });

     return `${date}`;
}
body { font: 12px Arial;}

.line {
    fill: none;
    stroke: #ffab00;
    stroke-width: 3;
}
  
.overlay {
  fill: none;
  pointer-events: all;
}

/* Style the dots by assigning a fill and stroke */
.dot {
    fill: #ffab00;
    stroke: #fff;
}
  
.focus circle {
    fill: none;
    stroke: steelblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<div id="chart"></div>

标签: javascriptd3.jschartslinechart

解决方案


推荐阅读