Published
Edited
Oct 1, 2019
Insert cell
Insert cell
dataset = d3.csv("https://gist.githubusercontent.com/emanueles/d8df8d875edda71aa2e2365fae2ce225/raw/1e949d3da02ed6caa21fe3a7a12a4e5a611a4bab/stocks.csv").then(function(data){
// formatando nossos dados
let parseDate = d3.timeParse("%Y/%m/%d")
data.forEach(function(d,i){
d.date = parseDate(d.date)
d.google = +d.google
d.facebook = +d.facebook
})
return data
})
Insert cell
facts = crossfilter(dataset)
Insert cell
dateDim = facts.dimension( d => d.date)
Insert cell
googleDim = facts.dimension( d => d.google)
Insert cell
topTenGoogle = googleDim.top(10)
Insert cell
bottomTenGoogle = googleDim.bottom(10)
Insert cell
googleByDayGroup = dateDim.group().reduceSum(d => d.google)
Insert cell
fbByDayGroup = dateDim.group().reduceSum(d => d.facebook)
Insert cell
firstFacebook = dateDim.bottom(1)[0].facebook
Insert cell
firstGoogle = dateDim.bottom(1)[0].google
Insert cell
googleByDayGroup2 = dateDim.group().reduceSum(function(d) {
return ((d.google - firstGoogle) / firstGoogle) * 100;
});
Insert cell
fbByDayGroup2 = dateDim.group().reduceSum(function(d) {
return ((d.facebook - firstFacebook) / firstFacebook) * 100;
});
Insert cell
function container(id, title) {
return `
<div class='container'>
<div class='content'>
<div class='container'>
<div class='row'>
<div class='span12' id='${id}'>
<h4>${title}</h4>
</div>
</div>
</div>
</div>
</div>`
}
Insert cell
buildvis = {
let view = md`${container('chart','Gráfico 1: Valores das ações do Google')}`
let lineChart = dc.lineChart(view.querySelector("#chart"))
let xScale = d3.scaleTime()
.domain([dateDim.bottom(1)[0].date, dateDim.top(1)[0].date])
lineChart.width(800)
.height(400)
.dimension(dateDim)
.margins({top: 30, right: 50, bottom: 25, left: 40})
.renderArea(false)
.x(xScale)
.xUnits(d3.timeDays)
.renderHorizontalGridLines(true)
.legend(dc.legend().x(680).y(10).itemHeight(13).gap(5))
.brushOn(true)
.group(googleByDayGroup, 'Google')
dc.renderAll()
return view
}
Insert cell
buildcomposite = {
let view = md`${container('chart2', 'Gráfico 2: Valores das ações do Google e do Facebook')}`
let compositeChart = dc.compositeChart(view.querySelector("#chart2"))
let xScale = d3.scaleTime()
.domain([dateDim.bottom(1)[0].date, dateDim.top(1)[0].date])
compositeChart.width(800)
.height(400)
.margins({top: 50, right: 50, bottom: 25, left: 40})
.dimension(dateDim)
.x(xScale)
.xUnits(d3.timeDays)
.renderHorizontalGridLines(true)
.legend(dc.legend().x(700).y(5).itemHeight(13).gap(5))
.brushOn(false)
.compose([
dc.lineChart(compositeChart)
.group(googleByDayGroup, 'Google')
.ordinalColors(['steelblue']),
dc.lineChart(compositeChart)
.group(fbByDayGroup, 'Facebook')
.ordinalColors(['darkorange'])])
dc.renderAll()
return view
}
Insert cell
acoes = {
let view = md`${container('chart3', 'Gráfico 3: Valores das ações do Google e do Facebook')}`
let compositeChart = dc.compositeChart(view.querySelector("#chart3"))
let xScale = d3.scaleTime()
.domain([dateDim.bottom(1)[0].date, dateDim.top(1)[0].date])
compositeChart.width(800)
.height(400)
.margins({top: 50, right: 50, bottom: 25, left: 40})
.dimension(dateDim)
.x(xScale)
.xUnits(d3.timeDays)
.renderHorizontalGridLines(true)
.legend(dc.legend().x(700).y(5).itemHeight(13).gap(5))
.brushOn(false)
.compose([
dc.lineChart(compositeChart)
.group(googleByDayGroup2, 'Google')
.ordinalColors(['steelblue']),
dc.lineChart(compositeChart)
.group(fbByDayGroup2, 'Facebook')
.ordinalColors(['darkorange'])])
dc.renderAll()
return view
}
Insert cell
Insert cell
by_year = {
let factsMovies = crossfilter(data_movies)
let dateDimMovies = factsMovies.dimension(d => d.Year);
let grossByYearGroup = dateDimMovies.group().reduceSum(d => d.Worldwide_Gross_M)

let view = md`${container('chart4','Total Arrecadado nas Bilheterias por Ano')}`
let barChart = dc.barChart(view.querySelector("#chart4"))

barChart
.width(800)
.height(400)
.gap(30)
.margins({top: 30, right: 50, bottom: 25, left: 40})
.dimension(dateDimMovies)
.group(grossByYearGroup)
.x(d3.scaleBand()).xUnits(dc.units.ordinal)
.renderHorizontalGridLines(true)
.controlsUseVisibility(true)
.brushOn(true)

dc.renderAll()

return view
}
Insert cell
by_genre = {
let factsMovies = crossfilter(data_movies)
let genreDimMovies = factsMovies.dimension(d => d.Genre)
let grossByGenreGroup = genreDimMovies.group().reduceSum(d => d.Worldwide_Gross_M)

let view = md`${container('chart5','Total Arrecadado nas Bilheterias por Gênero)')}`
let barChart = dc.barChart(view.querySelector("#chart5"))
let xScale = d3.scaleLinear()
.range([0, width])
.domain([genreDimMovies.bottom(1)[0].Genre, genreDimMovies.top(1)[0].Genre])

barChart
.width(800)
.height(400)
.gap(30)
.margins({top: 30, right: 50, bottom: 25, left: 40})
.dimension(genreDimMovies)
.group(grossByGenreGroup)
.x(d3.scaleBand()).xUnits(dc.units.ordinal)
.ordering(function(d) { return -d.value; })
.renderHorizontalGridLines(true)
.controlsUseVisibility(true)
.brushOn(true)

dc.renderAll()

return view
}
Insert cell
html `<div id="test">
<div>
<a class="reset" style="visibility: hidden" href="javascript:chart.filterAll();dc.redrawAll();">reset</a>
</div>
</div>
`
Insert cell
chart = dc.barChart("#test");
Insert cell
d3.csv("https://raw.githubusercontent.com/dc-js/dc.js/master/web/examples/morley.csv").then(function(experiments) {
experiments.forEach(function(x) {
x.Speed = +x.Speed;
});
var ndx = crossfilter(experiments),
runDimension = ndx.dimension(function(d) {return +d.Run;}),
speedSumGroup = runDimension.group().reduceSum(function(d) {return d.Speed * d.Run / 1000;});
chart
.width(768)
.height(480)
.x(d3.scaleBand())
.xUnits(dc.units.ordinal)
.brushOn(true)
.yAxisLabel("This is the Y Axis!")
.dimension(runDimension)
.group(speedSumGroup)
.controlsUseVisibility(true)
.addFilterHandler(function(filters, filter) {return [filter];}); // this
chart.render();
});
Insert cell
html`Esta célula inclui o css do dc.
<style>
.dc-chart path.dc-symbol, .dc-legend g.dc-legend-item.fadeout {
fill-opacity: 0.5;
stroke-opacity: 0.5; }

.dc-chart rect.bar {
stroke: none;
cursor: pointer; }
.dc-chart rect.bar:hover {
fill-opacity: .5; }

.dc-chart rect.deselected {
stroke: none;
fill: #ccc; }

.dc-chart .pie-slice {
fill: #fff;
font-size: 12px;
cursor: pointer; }
.dc-chart .pie-slice.external {
fill: #000; }
.dc-chart .pie-slice :hover, .dc-chart .pie-slice.highlight {
fill-opacity: .8; }

.dc-chart .pie-path {
fill: none;
stroke-width: 2px;
stroke: #000;
opacity: 0.4; }

.dc-chart .selected path, .dc-chart .selected circle {
stroke-width: 3;
stroke: #ccc;
fill-opacity: 1; }

.dc-chart .deselected path, .dc-chart .deselected circle {
stroke: none;
fill-opacity: .5;
fill: #ccc; }

.dc-chart .axis path, .dc-chart .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges; }

.dc-chart .axis text {
font: 10px sans-serif; }

.dc-chart .grid-line, .dc-chart .axis .grid-line, .dc-chart .grid-line line, .dc-chart .axis .grid-line line {
fill: none;
stroke: #ccc;
shape-rendering: crispEdges; }

.dc-chart .brush rect.selection {
fill: #4682b4;
fill-opacity: .125; }

.dc-chart .brush .custom-brush-handle {
fill: #eee;
stroke: #666;
cursor: ew-resize; }

.dc-chart path.line {
fill: none;
stroke-width: 1.5px; }

.dc-chart path.area {
fill-opacity: .3;
stroke: none; }

.dc-chart path.highlight {
stroke-width: 3;
fill-opacity: 1;
stroke-opacity: 1; }

.dc-chart g.state {
cursor: pointer; }
.dc-chart g.state :hover {
fill-opacity: .8; }
.dc-chart g.state path {
stroke: #fff; }

.dc-chart g.deselected path {
fill: #808080; }

.dc-chart g.deselected text {
display: none; }

.dc-chart g.row rect {
fill-opacity: 0.8;
cursor: pointer; }
.dc-chart g.row rect:hover {
fill-opacity: 0.6; }

.dc-chart g.row text {
fill: #fff;
font-size: 12px;
cursor: pointer; }

.dc-chart g.dc-tooltip path {
fill: none;
stroke: #808080;
stroke-opacity: .8; }

.dc-chart g.county path {
stroke: #fff;
fill: none; }

.dc-chart g.debug rect {
fill: #00f;
fill-opacity: .2; }

.dc-chart g.axis text {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
pointer-events: none; }

.dc-chart .node {
font-size: 0.7em;
cursor: pointer; }
.dc-chart .node :hover {
fill-opacity: .8; }

.dc-chart .bubble {
stroke: none;
fill-opacity: 0.6; }

.dc-chart .highlight {
fill-opacity: 1;
stroke-opacity: 1; }

.dc-chart .fadeout {
fill-opacity: 0.2;
stroke-opacity: 0.2; }

.dc-chart .box text {
font: 10px sans-serif;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
pointer-events: none; }

.dc-chart .box line {
fill: #fff; }

.dc-chart .box rect, .dc-chart .box line, .dc-chart .box circle {
stroke: #000;
stroke-width: 1.5px; }

.dc-chart .box .center {
stroke-dasharray: 3, 3; }

.dc-chart .box .data {
stroke: none;
stroke-width: 0px; }

.dc-chart .box .outlier {
fill: none;
stroke: #ccc; }

.dc-chart .box .outlierBold {
fill: red;
stroke: none; }

.dc-chart .box.deselected {
opacity: 0.5; }
.dc-chart .box.deselected .box {
fill: #ccc; }

.dc-chart .symbol {
stroke: none; }

.dc-chart .heatmap .box-group.deselected rect {
stroke: none;
fill-opacity: 0.5;
fill: #ccc; }

.dc-chart .heatmap g.axis text {
pointer-events: all;
cursor: pointer; }

.dc-chart .empty-chart .pie-slice {
cursor: default; }
.dc-chart .empty-chart .pie-slice path {
fill: #fee;
cursor: default; }

.dc-data-count {
float: right;
margin-top: 15px;
margin-right: 15px; }
.dc-data-count .filter-count, .dc-data-count .total-count {
color: #3182bd;
font-weight: bold; }

.dc-legend {
font-size: 11px; }
.dc-legend .dc-legend-item {
cursor: pointer; }

.dc-hard .number-display {
float: none; }

div.dc-html-legend {
overflow-y: auto;
overflow-x: hidden;
height: inherit;
float: right;
padding-right: 2px; }
div.dc-html-legend .dc-legend-item-horizontal {
display: inline-block;
margin-left: 5px;
margin-right: 5px;
cursor: pointer; }
div.dc-html-legend .dc-legend-item-horizontal.selected {
background-color: #3182bd;
color: white; }
div.dc-html-legend .dc-legend-item-vertical {
display: block;
margin-top: 5px;
padding-top: 1px;
padding-bottom: 1px;
cursor: pointer; }
div.dc-html-legend .dc-legend-item-vertical.selected {
background-color: #3182bd;
color: white; }
div.dc-html-legend .dc-legend-item-color {
display: table-cell;
width: 12px;
height: 12px; }
div.dc-html-legend .dc-legend-item-label {
line-height: 12px;
display: table-cell;
vertical-align: middle;
padding-left: 3px;
padding-right: 3px;
font-size: 0.75em; }

.dc-html-legend-container {
height: inherit; }
</style>`
Insert cell
dc = require("dc")
Insert cell
crossfilter = require("crossfilter2")
Insert cell
d3 = require("d3")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more