Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有工具提示的d3条形图不工作_Javascript_Json_Django_D3.js_Svg - Fatal编程技术网

Javascript 带有工具提示的d3条形图不工作

Javascript 带有工具提示的d3条形图不工作,javascript,json,django,d3.js,svg,Javascript,Json,Django,D3.js,Svg,我一直在使用内置工具提示的D3 svg图表(使用D3提示库)。原始代码可以在这里看到: 我使用Django作为后端,并尝试从datetime开始每年填充日志计数。我成功地填充了图形的轴和标签(除了条形图) 这是我的html模板,名为graph.html: <!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px sans-serif; } .axis path, .axis lin

我一直在使用内置工具提示的D3 svg图表(使用D3提示库)。原始代码可以在这里看到:

我使用Django作为后端,并尝试从datetime开始每年填充日志计数。我成功地填充了图形的轴和标签(除了条形图)

这是我的html模板,名为
graph.html

<!DOCTYPE html>
<meta charset="utf-8">
<style>

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>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script>

var margin = {top: 40, right: 20, bottom: 30, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var parseDate = d3.time.format("%Y-%m-%dT00:00:00Z").parse;  // for dates like "2014-01-01T00:00:00Z"

var x = d3.time.scale()
    .range([0, width]);

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>Log Count:</strong> <span style='color:red'>" + d.count_items + "</span>";
  })


var svg = d3.select("body").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 + ")");

d3.json("{% url "log_count_by_year" %}", function(error, data) {
  data.forEach(function(d) {
    d.year = parseDate(d.year);
    d.count_items = +d.count_items;
  });

  x.domain(d3.extent(data, function(d) { return d.year; }));
  y.domain([0, d3.max(data, function(d) { return d.count_items; })]);

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

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", -38)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Log count");

  svg.selectAll(".bar")
        .data(data)
      .enter().append("rect")
        .attr("class", "bar")
        .attr("x", function(d) { return x(d.year); })
        .attr("width", x.rangeBand())
        .attr("y", function(d) { return y(d.count_items); })
        .attr("height", function(d) { return height - y(d.count_items); })
        .on('mouseover', tip.show)
        .on('mouseout', tip.hide)
});

</script>
</body>
</html>
当我调用api时,我成功地获得了JSON对象,下面是我得到的JSON对象:

[{"count_items": 22, "year": "2017-01-01T00:00:00Z"}, {"count_items": 16, "year": "2016-01-01T00:00:00Z"}, {"count_items": 16, "year": "2015-01-01T00:00:00Z"}, {"count_items": 6, "year": "2014-01-01T00:00:00Z"}, {"count_items": 1, "year": "2013-01-01T00:00:00Z"}, {"count_items": 1, "year": "2012-01-01T00:00:00Z"}, {"count_items": 2, "year": "2011-01-01T00:00:00Z"}, {"count_items": 1, "year": "2010-01-01T00:00:00Z"}, {"count_items": 2, "year": "2009-01-01T00:00:00Z"}, {"count_items": 1, "year": "2008-01-01T00:00:00Z"}, {"count_items": 1, "year": "2007-01-01T00:00:00Z"}, {"count_items": 2, "year": "2006-01-01T00:00:00Z"}, {"count_items": 1, "year": "2005-01-01T00:00:00Z"}, {"count_items": 1, "year": "2004-01-01T00:00:00Z"}]
但在前端,我只能看到轴和标签,没有条形图:
除了棒和刀尖,一切正常。有人能帮我找出代码的错误吗?

你这里的问题很简单:时间刻度没有
范围()

由于年份是分类变量,而不是定量变量(毕竟,这是条形图,不是折线图),我建议您只需将您的量表更改为序数:

var x = d3.scale.ordinal()
    .rangeBands([0, width], 0.2);
之后,删除解析器并相应地更改域:

x.domain(data.map(function(d) {
    return d.year;
}));
最后,不要忘记调用工具提示:

svg.call(tip);
以下是您的代码和这些更改:


身体{
字体:10px无衬线;
}
.轴线路径,
.轴线{
填充:无;
行程:#000;
形状渲染:边缘清晰;
}
.酒吧{
填充物:橙色;
}
.bar:悬停{
填充:橙色;
}
.x轴路径{
显示:无;
}
.d3提示{
线高:1;
字体大小:粗体;
填充:12px;
背景:rgba(0,0,0,0.8);
颜色:#fff;
边界半径:2px;
}
/*为工具提示创建一个小三角形延长线*/
.d3提示:之后{
框大小:边框框;
显示:内联;
字体大小:10px;
宽度:100%;
线高:1;
颜色:rgba(0,0,0,0.8);
内容:“\25BC”;
位置:绝对位置;
文本对齐:居中;
}
/*以不同方式设置向北工具提示的样式*/
第三点提示n:之后{
保证金:-1px0;
最高:100%;
左:0;
}
var保证金={
前40名,
右:20,,
底数:30,
左:40
},
宽度=960-margin.left-margin.right,
高度=500-margin.top-margin.bottom;
var x=d3.scale.ordinal()
.范围带([0,宽度],0.2);
变量y=d3.scale.linear()
.范围([高度,0]);
var xAxis=d3.svg.axis()
.比例(x)
.东方(“底部”);
var yAxis=d3.svg.axis()
.比例(y)
.东方(“左”);
var tip=d3.tip()
.attr('class','d3 tip')
.偏移量([-10,0])
.html(函数(d){
返回“日志计数:”+d.Count\u items+”;
})
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
.附加(“g”)
.attr(“转换”、“平移”(+margin.left+)、“+margin.top+”);
svg.call(tip);
风险值数据=[{
“计数项目”:22,
“年份”:“2017-01-01T00:00:00Z”
}, {
“计数项目”:16,
“年份”:“2016-01-01T00:00:00Z”
}, {
“计数项目”:16,
“年份”:“2015-01-01T00:00:00Z”
}, {
“计数项目”:6,
“年份”:“2014-01-01T00:00:00Z”
}, {
“计数项目”:1,
“年份”:“2013-01-01T00:00:00Z”
}, {
“计数项目”:1,
“年份”:“2012-01-01T00:00:00Z”
}, {
“计数项目”:2,
“年份”:“2011-01-01T00:00:00Z”
}, {
“计数项目”:1,
“年份”:“2010-01-01T00:00:00Z”
}, {
“计数项目”:2,
“年份”:“2009-01-01T00:00:00Z”
}, {
“计数项目”:1,
“年份”:“2008-01-01T00:00:00Z”
}, {
“计数项目”:1,
“年份”:“2007-01-01T00:00:00Z”
}, {
“计数项目”:2,
“年份”:“2006-01-01T00:00:00Z”
}, {
“计数项目”:1,
“年份”:“2005-01-01T00:00:00Z”
}, {
“计数项目”:1,
“年份”:“2004-01-01T00:00:00Z”
}];
data.forEach(函数(d){
d、 年份=d.年份拆分(“-”[0];
d、 计数项目=+d。计数项目;
});
x、 域(data.map)(函数(d){
返回d.year;
}));
y、 域([0,d3.max(数据,函数(d)){
返回d.count_项目;
})]);
svg.append(“g”)
.attr(“类”、“x轴”)
.attr(“变换”、“平移(0)”、“高度+”)
.呼叫(xAxis);
svg.append(“g”)
.attr(“类”、“y轴”)
.呼叫(yAxis)
.append(“文本”)
.attr(“变换”、“旋转(-90)”)
.attr(“y”,-38)
.attr(“dy”,“.71em”)
.style(“文本锚定”、“结束”)
.文本(“日志计数”);
svg.selectAll(“.bar”)
.数据(数据)
.enter().append(“rect”)
.attr(“类”、“条”)
.attr(“x”,函数(d){
回报x(d.年);
})
.attr(“宽度”,x.rangeBand())
.attr(“y”,函数(d){
返回y(d.计数项目);
})
.attr(“高度”,功能(d){
返回高度-y(d.计数项目);
})
.on('mouseover',tip.show)
.on('mouseout',tip.hide)
svg.call(tip);