<head>
<meta charset="UTF-8">
<meta content="utf-8" http-equiv="encoding">
<title>D3</title>
<link rel="stylesheet" type="text/css" href="mystyle1.css" />
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
</head>
<script src="http://d3js.org/d3.v3.min.js"></script>
<body>
<div id="chart"></div>
<script>
//for datepicker
function processData(f1, f2){
var v1= document.getElementById(f1).value.toString();
var v2= document.getElementById(f2).value.toString();
alert (v1+"\n"+v2);
return v1, v2;
valPicker1=v1; //passed values that were attained above in valpickr1 and pickr2
valPicker2=v2;
}
var myData = [100, 125, 320, 440];
var height = 300;
var width = 500;
var tooltip = d3.select('body').append('div')
.style('position', 'absolute')
.style('background', '#f4f4f4')
.style('padding', '5 15px')
.style('border', '1px #333 solid')
.style('border-radius', '5px')
.style('opacity', 'o')
var yScale = d3.scale.linear()
.domain([0, d3.max(myData)])
.range([0, height]);
var xScale = d3.scale.ordinal()
.domain(d3.range(0, myData.length))
.rangeBands([0, width])
var colors = d3.scale.linear()
.domain([0, myData.length])
.range(['#90ee90', '#30c230'])
d3.select('#chart').append('svg')
.attr('width', width)
.attr('height', height)
.style('background', '#f4f4f4')
.selectAll('rect')
.data(myData)
.enter().append('rect')
.style('fill', function (d, i) {
return colors(i);
})
.attr('width', xScale.rangeBand())
.attr('height', function (d) {
return yScale(d);
})
.attr('x', function (d, i) {
return xScale(i);
})
.attr('y', function (d) {
return height - yScale(d);
})
.on('mouseover', function (d) {
tooltip.transition()
.style('opacity', 1)
tooltip.html(d)
.style('left', (d3.event.pageX) + 'px')
.style('top', (d3.event.pagey) + 'px')
d3.select(this).style('opacity', 0.5)
})
.on('mouseout', function (d) {
tooltip.transition()
.style('opacity', 0)
d3.select(this).style('opacity', 1)
})
var vScale = d3.scale.linear()
.domain([0, d3.max(myData)])
.range([height, 0]);
var hScale = d3.scale.ordinal()
.domain(d3.range(0, myData.length))
.rangeBands([0, width])
//vaxis
var vAxis = d3.svg.axis()
.scale(vScale)
.orient('left')
//v guide
var vGuide = d3.select('svg')
.append('g')
vAxis(vGuide)
vGuide.attr('transform', 'translate(35,10)')
vGuide.selectAll('path')
.style('fill', 'none')
.style('stroke', '#000')
vGuide.selectAll('line')
.style('stroke', '#000')
//Haxis
var hAxis = d3.svg.axis()
.scale(hScale)
.orient('bottom')
//H guide
var hGuide = d3.select('svg')
.append('g')
hAxis(hGuide)
hGuide.attr('transform', 'translate(35,' + (height - 25) + ')')
hGuide.selectAll('path')
.style('fill', 'none')
.style('stroke', '#000')
hGuide.selectAll('line')
.style('stroke', '#000')
</script>
<div align ="center">
From : <input type="date" name="field1" id="field1" />
To : <input type="date" name="field2" id="field2" /><br /><br />
<input type="button" onclick="processData('field1','field2')" value="Submit" />
</div>
</body>
<html>
我想从我的2个日期选择器中获取值,这些值位于我的网页上,因此用户可以在运行时从那些2datepicekrs中选择值,即:和:来自:x axis
上只应显示两个日期值但是我不知道如何将这些值从日期选择器绑定到我的x-axis
。请帮忙。
答案 0 :(得分:0)
您必须将域设置为要为x轴显示的日期数组。并且您可以在选择日期时重新渲染图表。
检查以下代码是否有帮助 -
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta content="utf-8" http-equiv="encoding">
<title>D3</title>
<!-- <link rel="stylesheet" type="text/css" href="mystyle1.css" /> -->
<style>
body {
color: #000;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
</style>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
</head>
<script src="http://d3js.org/d3.v3.min.js"></script>
<body>
<div id="chart"></div>
<div align ="center">
From : <input type="date" name="field1" id="field1" />
To : <input type="date" name="field2" id="field2" /><br /><br />
<input type="button" onclick="render(true)" value="Submit" />
</div>
<script>
var myData = [
{ value: 100, date: '2016-01-01'},
{ value: 125, date: '2016-02-02'},
{ value: 320, date: '2016-03-03'},
{ value: 440, date: '2016-04-04'}
];
var parseDate = d3.time.format("%Y-%m-%d").parse;
var margin = { top: 20, right: 0, bottom: 80, left: 40 };
var width = 500 - margin.left - margin.right;
var height = 300 - margin.top - margin.bottom;
var xScale = d3.scale.ordinal().rangeRoundBands([0, width], .1);
var yScale = d3.scale.linear().range([height, 0]);
var hAxis = d3.svg.axis().scale(xScale).orient('bottom').tickFormat(d3.time.format("%Y-%m-%d"));
var vAxis = d3.svg.axis().scale(yScale).orient('left');
var tooltip = d3.select('body').append('div')
.style('position', 'absolute')
.style('background', '#f4f4f4')
.style('padding', '5 15px')
.style('border', '1px #333 solid')
.style('border-radius', '5px')
.style('opacity', 'o');
myData.forEach(function(d) {
d.date = parseDate(d.date);
d.value = +d.value;
});
function getDates() {
return [document.getElementById('field1').value, document.getElementById('field2').value];
}
function render(filterByDates) {
d3.select('svg').remove();
if(filterByDates) {
var date1 = parseDate(document.getElementById('field1').value);
var date2 = parseDate(document.getElementById('field2').value);
myData = myData.filter(function(d) {
return d.date >= date1 && d.date <= date2;
});
}
xScale.domain(myData.map(function(d) {
return d.date;
}));
yScale.domain([0, d3.max(myData, function (d) { return d.value; })]);
var svg = d3.select('#chart').append('svg')
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg
.append('g')
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(hAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.55em")
.attr("transform", "rotate(-90)" );
svg
.append('g')
.attr("class", "y axis")
// .attr('transform', 'translate(35,' + (height - 25) + ')')
.call(vAxis)
svg
.selectAll(".bar")
.data(myData)
.enter().append("rect")
.attr("class", "bar")
.style("fill", "steelblue")
.attr("x", function(d) {
return xScale(d.date);
})
.attr("width", xScale.rangeBand())
.attr("y", function(d) {
return yScale(d.value);
})
.attr("height", function(d) {
return height - yScale(d.value);
})
.on('mouseover', function (d) {
tooltip.transition()
.style('opacity', 1)
tooltip.html(d.value)
.style('left', (d3.event.pageX) + 'px')
.style('top', (d3.event.pagey) + 'px')
d3.select(this).style('opacity', 0.5)
})
.on('mouseout', function (d) {
tooltip.transition()
.style('opacity', 0)
d3.select(this).style('opacity', 1)
});
}
render(false);
</script>
</body>
<html>