在D3图表中将Xaxis值显示为非时间序列值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在D3图表中将Xaxis值显示为非时间序列值相关的知识,希望对你有一定的参考价值。
我是D3 JS的新手,我使用StackOverflow的一些资源创建了一个多行D3图表。我附上了这个代码。我的问题出现在我的图表中,我需要从当月到同月的X轴显示13个月。在那种情况下,我遇到了几个问题,最后提出了这个问题。现在不存在另一个问题,比如我需要支持月份名称的本地化。但是因为我使用x轴的时间序列,所以当我将月份名称更改为另一种语言时会出错。我希望如果我使用非时间系列的X轴这个问题不会出现?有没有办法转换为非时间序列?
小提琴:https://jsfiddle.net/yasirunilan/p68z0bqo/7/
var data = [{
name: "USA",
values: [{
date: "2010-October",
price: "100"
},
{
date: "2010-November",
price: "110"
},
{
date: "2010-December",
price: "145"
},
{
date: "2011-January",
price: "241"
},
{
date: "2011-February",
price: "101"
},
{
date: "2011-March",
price: "90"
},
{
date: "2011-April",
price: "120"
},
{
date: "2011-May",
price: "135"
},
{
date: "2011-June",
price: "99"
},
{
date: "2011-July",
price: "105"
},
{
date: "2011-August",
price: "101"
},
{
date: "2011-September",
price: "106"
}
]
},
{
name: "UK",
values: [{
date: "2010-October",
price: "130"
},
{
date: "2010-November",
price: "120"
},
{
date: "2010-December",
price: "115"
},
{
date: "2011-January",
price: "220"
},
{
date: "2011-February",
price: "100"
},
{
date: "2011-March",
price: "140"
},
{
date: "2011-April",
price: "90"
},
{
date: "2011-May",
price: "235"
},
{
date: "2011-June",
price: "160"
},
{
date: "2011-July",
price: "86"
},
{
date: "2011-August",
price: "201"
},
{
date: "2011-September",
price: "140"
}
]
}
];
const margin = 80;
const width = 1000 - 2 * margin;
const height = 550 - 2 * margin;
var duration = 250;
var lineOpacity = "0.25";
var lineOpacityHover = "0.85";
var otherLinesOpacityHover = "0.1";
var lineStroke = "1.5px";
var lineStrokeHover = "2.5px";
var circleOpacity = '0.85';
var circleOpacityOnLineHover = "0.25"
var circleRadius = 3;
var circleRadiusHover = 6;
/* Format Data */
var parseDate = d3.time.format("%Y-%B");
data.forEach(function(d) {
d.values.forEach(function(d) {
d.date = parseDate.parse(d.date);
d.price = +d.price;
});
});
/* Scale */
var xScale = d3.time.scale()
.domain(d3.extent(data[0].values, d => d.date))
.range([0, width - margin]);
var yScale = d3.scale.linear()
.domain([0, d3.max(data[0].values, d => d.price)])
.range([height - margin, 0]);
// var color = d3.scale.ordinal(d3.schemeCategory10);
var color = d3.scale.category10();
/* Add SVG */
var svg = d3.select("svg")
.attr("width", (width + margin) + "px")
.attr("height", (height + margin) + "px")
.append('g')
.attr("transform", `translate(${margin}, ${margin})`);
/* Add line into SVG */
var line = d3.svg.line()
.x(d => xScale(d.date))
.y(d => yScale(d.price));
let lines = svg.append('g')
.attr('class', 'lines');
lines.selectAll('.line-group')
.data(data).enter()
.append('g')
.attr('class', 'line-group')
.on("mouseover", function(d, i) {
svg.append("text")
.attr("class", "title-text")
.style("fill", color(i))
.text(d.name)
.attr("text-anchor", "middle")
.attr("x", (width - margin) / 2)
.attr("y", 5);
})
.on("mouseout", function(d) {
svg.select(".title-text").remove();
})
.append('path')
.attr('class', 'line')
.attr('d', d => line(d.values))
.style('stroke', (d, i) => color(i))
.style('opacity', lineOpacity)
.on("mouseover", function(d) {
d3.selectAll('.line')
.style('opacity', otherLinesOpacityHover);
d3.selectAll('.circle')
.style('opacity', circleOpacityOnLineHover);
d3.select(this)
.style('opacity', lineOpacityHover)
.style("stroke-width", lineStrokeHover)
.style("cursor", "pointer");
})
.on("mouseout", function(d) {
d3.selectAll(".line")
.style('opacity', lineOpacity);
d3.selectAll('.circle')
.style('opacity', circleOpacity);
d3.select(this)
.style("stroke-width", lineStroke)
.style("cursor", "none");
});
/* Add circles in the line */
lines.selectAll("circle-group")
.data(data).enter()
.append("g")
.style("fill", (d, i) => color(i))
.selectAll("circle")
.data(d => d.values).enter()
.append("g")
.attr("class", "circle")
.on("mouseover", function(d) {
d3.select(this)
.style("cursor", "pointer")
.append("text")
.attr("class", "text")
.text(`${d.price}`)
.attr("x", d => xScale(d.date) + 5)
.attr("y", d => yScale(d.price) - 10);
})
.on("mouseout", function(d) {
d3.select(this)
.style("cursor", "none")
.transition()
.duration(duration)
.selectAll(".text").remove();
})
.append("circle")
.attr("cx", d => xScale(d.date))
.attr("cy", d => yScale(d.price))
.attr("r", circleRadius)
.style('opacity', circleOpacity)
.on("mouseover", function(d) {
d3.select(this)
.transition()
.duration(duration)
.attr("r", circleRadiusHover);
})
.on("mouseout", function(d) {
d3.select(this)
.transition()
.duration(duration)
.attr("r", circleRadius);
});
var xAxis = d3.svg.axis().scale(xScale)
.orient("bottom").tickFormat(d3.time.format("%B")).tickSize(1);
var yAxis = d3.svg.axis().scale(yScale)
.orient("left").tickSize(1);
svg.append("g")
.attr("class", "x axis")
.attr("transform", `translate(0, ${height-margin})`)
.call(xAxis.ticks(d3.time.months));
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append('text')
.attr("y", 15)
.attr("transform", "rotate(-90)")
.attr("fill", "#000")
.attr('text-anchor', 'middle')
.text("No. of Employees");
答案
我想你只需要重新格式化你的小提琴中的数据。在数组中的前3个对象中缺少指示它们来自哪个年份:
var data = [{
name: "USA",
values: [{
date: "October",
price: "100"
},
{
date: "November",
price: "110"
},
{
date: "December",
price: "145"
},
{
date: "2011-January",
price: "241"
},
Here is your fiddle again,其中我更改了日期的这些行,并在x轴上添加了月份所属年份的指示,以及更改轴文本的字体大小,因为它们相互重叠。
以上是关于在D3图表中将Xaxis值显示为非时间序列值的主要内容,如果未能解决你的问题,请参考以下文章