这是一个已经问过的问题,但没有一个解决方案适合我,所以再次提出。
1.我正在尝试根据某个阈值更改我的D3条形图中条形的颜色(如果条形超过y轴上的特定值,在这种情况下为30)如何更改酒吧的颜色到别的地方说蓝色。
我还想在y = 30处绘制一条水平线。
此数据是data.tsv文件
letter frequency attachdate a 13 12-May-2016 b 14 11-May-2016
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: orange;
}
.bar:hover {
fill: orangered ;
}
.x.axis path {
display: none;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
</style>
var margin = {top: 40, right: 20, bottom: 160, left: 50},
width = 960 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
var formatPercent = d3.format(".0%");
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Number of days:</strong> <span style='color:red'>" + d.frequency + "</span>"+"</b><br/>Attach date: " + d.attachdate;
})
var svg = d3.select("body").append("svg")
.attr("align","center")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.call(tip);
d3.tsv("data.tsv", type, function(error, data) {
x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, 60]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0,400)")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", "rotate(-65)");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Number of days");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height - y(d.frequency); })
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
});
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "18px" )
.style("text-decoration", "underline")
.style("fill", "red")
.text("Some heading");
function type(d) {
d.frequency = +d.frequency;
return d;
}
</script>
</body>
答案 0 :(得分:0)
任何不明白的理由:
public partial class UpdateFeedPage : ContentPage
{
public static List<FileObject> CurrentSelectedFile = new List<FileObject>();
public static string PostText = "";
public UpdateFeedPage()
{
InitializeComponent();
Title = "New Update";
UpdateFeedEditor.Text = PostText;
}
protected override void OnAppearing()
{
AttachedFilesListView.ItemsSource = CurrentSelectedFile;
UpdateFeedEditor.Focus();
base.OnAppearing();
}
async void onPostClicked(object sender, EventArgs e)
{
await Navigation.PopAsync();
}
async void onAttachFileClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new FilePickerPage());
}
}
等
答案 1 :(得分:0)
正如@Stephen所说,你基本上可以利用class属性来管理你的酒吧的颜色。另一种方法是使用fill
属性并使用函数返回所需的值。
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height - y(d.frequency); })
.attr("fill", function(d) {
if (d.frequency >= 30) {
return 'red';
}
return 'green';
});
至于你想画的线你可能会做这样的事情:
svg.append("line")
.attr("x1", 0)
.attr("y1", y(30))
.attr("x2", width)
.attr("y2", y(30))
.style("stroke", "#828282")
.attr('stroke-dasharray', '2,2')
.attr('stroke-width', '1')
以下是一个代码与您的代码类似的plunker:https://plnkr.co/edit/3danSMezWDsbnJZXEUTH?p=preview