Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement d3 #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
<head>
<meta charset="UTF-8">
<title>D3 Challenge</title>
<style> /* set the CSS */

.axis { font: 14px sans-serif; }

</style>
</head>
<body>
<div id="container">

<h1>AFC Wimbledon Results</h1>
<div id="results"></div>
<hr />
<h2>AFC Wimbledon Top Scorer</h2>
<div id="top-score"></div>
</div>
<script src="https://d3js.org/d3.v4.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="script.js"></script>
<script src="d3.layout.cloud.js"></script>
<script src="topscore.js"></script>
Expand Down
74 changes: 71 additions & 3 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,92 @@
// Our canvas
const width = 750,
height = 300,
margin = 20
marginLeft = 40
margin = 20,
marginLeft = 40,
multiplier = 20

// Drawing area
let svg = d3.select('#results')
.append('svg')
.attr('width', width)
.attr('height', height)
.style('background', '#cacaca')

// Data reloading
let reload = () => {
// Your data parsing here...
d3.tsv('afcw-results.tsv', (rows) => {
console.log('ini rows', rows);
redraw(rows)
})
}

// redraw function
let redraw = (data) => {
// Your data to graph here
const yScale = d3.scaleLinear()
.domain([0, 4])
// .domain([0, d3.max(data.map(dataItem => dataItem.GoalsScored))])
.range([0, height - marginLeft])

const xScale = d3.scaleLinear()
.domain([0, data.length])
.range([0, width - (2*marginLeft)])

const colorScale = d3.scaleLinear()
.domain([0, d3.max(data.map(dataItem => dataItem.GoalsScored))])
.range(['peru', 'teal'])

// add the x Axis
var scaleX = d3.scaleLinear()
.domain([0, data.length])
.range([0, width - (2*marginLeft)])

var x_axis = d3.axisBottom()
.scale(scaleX)

svg.append("g")
.attr("transform", `translate(${marginLeft}, ${height-margin})`)
.call(x_axis.ticks(data.length))

// add the y Axis
var scaleY = d3.scaleLinear()
.domain([0, 4])
// .domain([0, d3.max(data.map(dataItem => dataItem.GoalsAllowed))])
.range([height - marginLeft, 0])

var y_axis = d3.axisLeft()
.scale(scaleY)

svg.append("g")
.attr("transform", `translate(${marginLeft}, ${margin})`)
.call(y_axis)

svg.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr('class', 'bar')
.attr('x', (d, index) => {
return xScale(index) + marginLeft
})
.attr('y', (d, index) => {
return height - yScale(d.GoalsScored) - margin
})
.attr('width', (d) => {
return xScale(1) - 2
})
.attr('height', (d) => {
return yScale(d.GoalsScored)
})
.attr('fill', colorScale)
.on('mouseover', function (d, index) {
d3.select(this).style('fill', '#bada55')
})
.on('mouseout', function (d, index) {
d3.select(this).style('fill', 'teal')
})

}

reload()
reload()
42 changes: 37 additions & 5 deletions topscore.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
/* global d3 width height */
/* global d3 */

// Our canvas
// var width = 750, height = 500

let fill = d3.scaleOrdinal(d3.schemeCategory20)
let leaderScale = d3.scaleLinear()
.range([5, 40])

const draw = (words) => {
function draw(words) {
// Draw your data here...

d3.select("#top-score").append("svg")
.attr('width', 750)
.attr('height', 500)
.append("g")
.attr("transform", "translate(" + 750 / 2 + "," + 500 / 2 + ")")
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", function(d) { return d.size + "px"; })
.style("font-family", "Impact")
.style("fill", function(d, i) { return fill(i); })
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function(d) { return d.text; });
}


const load = () => {
// Load your data here...
d3.tsv('afcw-results.tsv', (rows) => {
console.log('ini rows', rows);
// draw(layout.words)
d3.layout.cloud()
.size([750, 500])
.words(rows.map(function(d) {
return {text: d.Opponent, size: 10 + Math.random() * 90, test: "haha"};
}))
.padding(5)
.rotate(function() { return ~~(Math.random() * 2) * 90; })
.font("Impact")
.fontSize(function(d) { return d.size; })
.on("end", draw)
.start();
})
}

load()
load()