d3实现一个gdp柱状图动画图表效果代码

代码语言:html

所属分类:图表

代码描述:d3实现一个gdp柱状图动画图表效果代码

代码标签: d3 gdp 柱状图 动画 图表

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">

<style>
    @import url(https://fonts.googleapis.com/css?family=Asap);
body {
  background-color: #232836;
}

svg {
  font: 14px "Asap", Arial, sans-serif;
  margin: 20px auto;
  display: block;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(0, 0, 0, 0.2);
  background-color: #282e3d;
  fill: #7e828b;
  border-radius: 10px;
}

.chart__title {
  fill: #d3d4d2;
  font-size: 3em;
  text-anchor: middle;
}

.chart__description {
  fill: #d3d4d2;
  font-size: 0.8em;
  text-anchor: middle;
}

.bar {
  fill: #e82a78;
}

.bar--hover {
  fill: #f286b2;
}

path.line {
  fill: none;
  stroke: #000;
  stroke-width: 1px;
}

.domain {
  fill: none;
  stroke: black;
  stroke-width: 1;
}

.tooltip {
  position: absolute;
  padding: 5px;
  background: #d3d4d2;
  opacity: 0;
  box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.2);
  z-index: 1;
}
.tooltip__price {
  display: block;
  font-size: 1em;
  font-family: "Asap", Arial, sans-serif;
  font-weight: bold;
}

.tooltip__date {
  font-family: "Asap", Arial, sans-serif;
  font-size: 0.8em;
}
</style>
</head>

<body>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery.2.11.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/d3.3.5.5.js"></script>
    <script>
        var jsonUrl = '//repo.bfw.wiki/bfwrepo/json/GDP-data.json';
var months = {
  1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June',
  7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'}

var margin = {top: 100, right: 20, bottom: 70, left: 80};
var width = 900 - margin.left - margin.right;
var height = 500 - margin.top - margin.bottom;

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')
  .ticks(14);

var yAxis = d3.svg.axis()
  .scale(y)
  .orient('left')
  .ticks(8);

var tooltip = d3.select('body').append('div')
  .attr('class', 'tooltip')

var svg = d3.select('body')
  .append('div')
  .attr('class', 'svg-container')
  .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(jsonUrl, function(err, data) {
  if (err) throw err;
  
  x.domain([new Date(data.data[0][0]), new Date(data.data[data.data.length-1][0])]);
  y.domain([0, d3.max(data.data, function(d) { return d[1]})]);
  
  svg.append('text')
    .attr('x', width/2)
    .attr('y', 0 - (margin.top / 2))
    .attr('class', 'chart__title')
    .text(data.na.........完整代码请登录后点击上方下载按钮下载查看

网友评论0