javascript - 删除 X 和 Y 轴上的标签并删除 X 轴上的最后一条网格线

标签 javascript css d3.js

HTML:

<div id="line-graph"></div>

CSS:

.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.dot {
  fill: lightblue;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.axis {
  fill: yellow;
  font-family: Times New Roman;
  font-size: 24px;
  font-color: red;
}

.axis line {
  stroke: gray;
}

.axis path{
  stroke: lime;
}

.axis text {
  fill: lightblue;
}

.grid line {
  stroke: lightgrey;
}

.grid path {
  stroke-width: 0;
}

JS:

'use strict';

const data = d3.range(40).map((i) => {
    if (i % 5) {
        return {
            x: (i / 39),
            y: ((Math.sin(i / 3) + 2) / 4)
        };
    }
    return null;
});

const margin = { 
    top: 40, 
    right: 40, 
    bottom: 40, 
    left: 40 
};
const width = (960 - margin.left - margin.right);
const height = (500 - margin.top - margin.bottom);

const x = d3.scaleLinear().range([0, width]);
const y = d3.scaleLinear().range([height, 0]);

const line = d3.line()
    .defined(d => d)
    .x(d => x(d.x))
    .y(d => y(d.y));

const svg = d3.select('#line-graph')
    .append('svg:svg')
    .datum(data)
    .attr('width', (width + margin.left + margin.right))
    .attr('height', (height + margin.top + margin.bottom))
    .append('g')
    .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');

// X-Axis line
svg.append('g') // g for group
    .attr('class', 'axis') // CSS class
    .attr('transform', 'translate(0,' + height + ')')
    .call(d3.axisBottom(x));

// X-Axis gridlines
const customXAxis = d3.axisBottom(x)
    .ticks(9) // How many gridlines
    .tickSize(-height)
    .tickFormat('');

// Add X-Axis gridlines
svg.append('g') 
    .attr('class', 'grid') // CSS class
    .attr('transform', 'translate(0,' + height + ')')
    .call(customXAxis);

// Y-Axis line
svg.append('g') // g for group
    .attr('class', 'axis') // CSS class
    .call(d3.axisLeft(y));

// Grapth line
svg.append('path')
    .attr('class', 'line') // CSS class
    .attr('d', line);

// Graph line dots
svg.selectAll('.dot')
    .data(data.filter(d => d))
    .enter().append('circle')
    .attr('class', 'dot') // CSS class
    .attr('cx', line.x())
    .attr('cy', line.y())
    .attr('r', 3.5);

目前:

enter image description here

想要:

enter image description here

如您所见,一些 X 轴和 Y 轴标签消失了,但直线的长度仍然相同。 X 轴上的最后一条网格线消失了。

我还想在 X 轴向下(使高度更高)和 Y 轴向左(使宽度更长)上扩展黄色区域,用油漆写这个很痛苦,所以我跳过了展示。

最佳答案

所以我终于修好了。花了一些时间看了一些奇怪的例子,但弄明白了。我删除了底部的标签,但如果您查看代码,就很容易调整。 https://jsfiddle.net/Lt83y1ph/

JS:

'use strict';

const data = d3.range(40).map((i) => {
    if (i % 5) {
        return {
            x: (i / 39),
            y: ((Math.sin(i / 3) + 2) / 4)
        };
    }
    return null;
});

const margin = { 
    top: 40, 
    right: 40, 
    bottom: 40, 
    left: 60 
};
const width = (960 - margin.left - margin.right);
const height = (500 - margin.top - margin.bottom);

const x = d3.scaleLinear().range([0, width]);
const y = d3.scaleLinear().range([height, 0]);

const line = d3.line()
    .defined(d => d)
    .x(d => x(d.x))
    .y(d => y(d.y));

const svg = d3.select('#line-graph')
    .append('svg:svg')
    .datum(data)
    .attr('width', (width + margin.left + margin.right))
    .attr('height', (height + margin.top + margin.bottom))
    .append('g')
    .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');

// X-Axis
const xAxis = d3.axisBottom(x)
    .ticks(9) // How many gridlines
    .tickSizeInner(-height) // Ticks in between the outer ticks
    .tickSizeOuter(0) // Ticks on both outer sides
    .tickFormat(''); // Removes tick labels
// Custom X-Axis function
function customXAxis(g) {
    g.call(xAxis);
}
// Add X-Axis
svg.append('g') 
    .attr('class', 'axis') // CSS class
    .attr('transform', 'translate(0,' + height + ')')
    .call(customXAxis);

// Y-Axis
const yAxis = d3.axisLeft(y)
    .ticks(9) // How many gridlines
    .tickSizeInner(10) // Ticks in between the outer ticks
    .tickSizeOuter(0) // Ticks on both outer sides
    .tickPadding(10); // Spacing between ticks and labels
// Custom Y-Axis function
function customYAxis(g) {
    g.call(yAxis);
    g.select('.domain').remove(); // Removes the initial axis line
    g.select('.tick:nth-of-type(1)').attr('display', 'none');
    g.select('.tick:nth-of-type(2)').attr('display', 'none');
    g.select('.tick:nth-last-of-type(1)').attr('display', 'none');
    g.select('.tick:nth-last-of-type(2)').attr('display', 'none');
}
// Add Y-Axis
svg.append('g') 
    .attr('class', 'axis') // CSS class
    .call(customYAxis);

// Grapth line
svg.append('path')
    .attr('class', 'line') // CSS class
    .attr('d', line);

// Graph line dots
svg.selectAll('.dot')
    .data(data.filter(d => d))
    .enter().append('circle')
    .attr('class', 'dot') // CSS class
    .attr('cx', line.x())
    .attr('cy', line.y())
    .attr('r', 3.5);

CSS:

.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.dot {
  fill: lightblue;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.axis {
  fill: black;
  font-family: Times New Roman;
  font-size: 24px;
}

.axis line {
  stroke: lightgrey;
}

.axis text {
  fill: lightblue;
}

关于javascript - 删除 X 和 Y 轴上的标签并删除 X 轴上的最后一条网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48118170/

相关文章:

javascript - 在没有 JavaScript 的情况下将灵活的容器设置为图像的宽度和高度

数字后的 HTML 列表文本缩进

javascript - JQuery填写表单但无法识别

javascript - 似乎无法让 setTimeout 工作?

javascript - ExtJS 3.0 中更通用的面板加载掩码

javascript - D3——嵌套饼图不显示所有楔形

animation - 如何为元素设置动画以跟随 d3 中的圆弧质心?

html - 如何仅使用 CSS 和 HTML 制作返回顶部按钮?

css - 覆盖CSS变量

javascript - 强制链接在节点下绘制