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

[bugfix] fixing && linting the histogram viz #987

Merged
merged 1 commit into from
Aug 20, 2016
Merged
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
1 change: 1 addition & 0 deletions caravel/assets/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"prefer-arrow-callback": 0,
"func-names": 0,
"react/jsx-no-bind": 0,
"no-confusing-arrow": 0,
}
}
284 changes: 142 additions & 142 deletions caravel/assets/visualizations/histogram.js
Original file line number Diff line number Diff line change
@@ -1,151 +1,151 @@
// JS
const d3 = require('d3')
const px = window.px || require('../javascripts/modules/caravel.js')
import { category21 } from '../javascripts/modules/colors';
import d3 from 'd3';

// CSS
require('./histogram.css')
require('./histogram.css');

function histogram(slice) {

const div = d3.select(slice.selector)

const _draw = function(data, numBins) {

// Set Margins
const margin = {
top: 50,
right: 10,
bottom: 20,
left: 50,
};
const navBarHeight = 36;
const navBarTitleSize = 12;
const navBarBuffer = 10;
const width = slice.width() - margin.left - margin.right;
const height = slice.height() - margin.top - margin.bottom - navBarHeight - navBarBuffer;

// Set Histogram objects
const formatNumber = d3.format(',.0f');
const formatTicks = d3.format(',.00f');
const x = d3.scale.ordinal();
const y = d3.scale.linear();
const xAxis = d3.svg.axis().scale(x).orient('bottom').ticks(numBins).tickFormat(formatTicks);
const yAxis = d3.svg.axis().scale(y).orient('left').ticks(numBins*3);
// Calculate bins for the data
const bins = d3.layout.histogram().bins(numBins)(data);

// Set the x-values
x.domain(bins.map(function(d) { return d.x;}))
.rangeRoundBands([0, width], .1);
// Set the y-values
y.domain([0, d3.max(bins, function(d) { return d.y;})])
.range([height, 0]);

// Create the svg value with the bins
const svg = div.selectAll('svg').data([bins]).enter().append('svg');

// Make a rectangular background fill
svg.append('rect')
.attr('width', '100%')
.attr('height', '100%')
.attr('fill', '#f6f6f6');

// Transform the svg to make space for the margins
const gEnter = svg
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');

// Add the bars and the x axis
gEnter.append('g').attr('class', 'bars');
gEnter.append('g').attr('class', 'x axis');

// Add width and height to the svg
svg.attr('width', slice.width())
.attr('height', slice.height());

// Create the bars in the svg
const bar = svg.select('.bars').selectAll('.bar').data(bins);
bar.enter().append('rect');
bar.exit().remove();
// Set the Height and Width for each bar
bar .attr('width', x.rangeBand())
.attr('x', function(d) { return x(d.x); })
.attr('y', function(d) { return y(d.y); })
.attr('height', function(d) {
return y.range()[0] - y(d.y);
})
.attr('fill', function(d) { return px.color.category21(d.length); })
.order();

// Find maximum length to position the ticks on top of the bar correctly
const maxLength = d3.max(bins, function(d) { return d.length;});
function textAboveBar(d) {
return d.length/maxLength < 0.1;
}

// Add a bar text to each bar in the histogram
svg.selectAll('.bartext')
.data(bins)
.enter()
.append('text')
.attr('dy', '.75em')
.attr('y', function(d) {
let padding = 0.0
if (textAboveBar(d)) {
padding = 12.0
} else {
padding = -8.0
}
return y(d.y) - padding;
})
.attr('x', function(d) { return x(d.x) + (x.rangeBand()/2);})
.attr('text-anchor', 'middle')
.attr('font-weight', 'bold')
.attr('font-size', '15px')
.text(function(d) { return formatNumber(d.y); })
.attr('fill', function(d) {
if(textAboveBar(d)) { return 'black'; } else { return 'white'; }
})
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');

// Update the x-axis
svg.append('g')
.attr('class', 'axis')
.attr('transform', 'translate(' + margin.left + ',' + (height + margin.top) + ')')
.text('values')
.call(xAxis);

// Update the Y Axis and add minor lines
svg.append('g')
.attr('class', 'axis')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
.text('count')
.call(yAxis)
.selectAll('g')
.filter(function(d) { return d; })
.classed('minor', true);
};
const div = d3.select(slice.selector);

const render = function() {

d3.json(slice.jsonEndpoint(), function(error, json) {
if(error !== null) {
slice.error(error.responseText, error);
return '';
}

const numBins = Number(json.form_data.link_length) || 10;

div.selectAll('*').remove();
_draw(json.data, numBins);
slice.done(json);
});
const draw = function (data, numBins) {
// Set Margins
const margin = {
top: 50,
right: 10,
bottom: 20,
left: 50,
};
const navBarHeight = 36;
const navBarBuffer = 10;
const width = slice.width() - margin.left - margin.right;
const height = slice.height() - margin.top - margin.bottom - navBarHeight - navBarBuffer;

// Set Histogram objects
const formatNumber = d3.format(',.0f');
const formatTicks = d3.format(',.00f');
const x = d3.scale.ordinal();
const y = d3.scale.linear();
const xAxis = d3.svg.axis()
.scale(x)
.orient('bottom')
.ticks(numBins)
.tickFormat(formatTicks);
const yAxis = d3.svg.axis()
.scale(y)
.orient('left')
.ticks(numBins * 3);
// Calculate bins for the data
const bins = d3.layout.histogram().bins(numBins)(data);

// Set the x-values
x.domain(bins.map((d) => d.x))
.rangeRoundBands([0, width], 0.1);
// Set the y-values
y.domain([0, d3.max(bins, (d) => d.y)])
.range([height, 0]);

// Create the svg value with the bins
const svg = div.selectAll('svg')
.data([bins])
.enter()
.append('svg');

// Make a rectangular background fill
svg.append('rect')
.attr('width', '100%')
.attr('height', '100%')
.attr('fill', '#f6f6f6');

// Transform the svg to make space for the margins
const gEnter = svg
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');

// Add the bars and the x axis
gEnter.append('g').attr('class', 'bars');
gEnter.append('g').attr('class', 'x axis');

// Add width and height to the svg
svg.attr('width', slice.width())
.attr('height', slice.height());

// Create the bars in the svg
const bar = svg.select('.bars').selectAll('.bar').data(bins);
bar.enter().append('rect');
bar.exit().remove();
// Set the Height and Width for each bar
bar.attr('width', x.rangeBand())
.attr('x', (d) => x(d.x))
.attr('y', (d) => y(d.y))
.attr('height', (d) => y.range()[0] - y(d.y))
.style('fill', (d) => category21(d.length))
.order();

// Find maximum length to position the ticks on top of the bar correctly
const maxLength = d3.max(bins, (d) => d.length);
function textAboveBar(d) {
return d.length / maxLength < 0.1;
}

// Add a bar text to each bar in the histogram
svg.selectAll('.bartext')
.data(bins)
.enter()
.append('text')
.attr('dy', '.75em')
.attr('y', function (d) {
let padding = 0.0;
if (textAboveBar(d)) {
padding = 12.0;
} else {
padding = -8.0;
}
return y(d.y) - padding;
})
.attr('x', (d) => x(d.x) + (x.rangeBand() / 2))
.attr('text-anchor', 'middle')
.attr('font-weight', 'bold')
.attr('font-size', '15px')
.text((d) => formatNumber(d.y))
.attr('fill', (d) => textAboveBar(d) ? 'black' : 'white')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');

// Update the x-axis
svg.append('g')
.attr('class', 'axis')
.attr('transform', 'translate(' + margin.left + ',' + (height + margin.top) + ')')
.text('values')
.call(xAxis);

// Update the Y Axis and add minor lines
svg.append('g')
.attr('class', 'axis')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
.text('count')
.call(yAxis)
.selectAll('g')
.filter(function (d) { return d; })
.classed('minor', true);
};

const render = function () {
d3.json(slice.jsonEndpoint(), function (error, json) {
if (error !== null) {
slice.error(error.responseText, error);
return;
}

const numBins = Number(json.form_data.link_length) || 10;

div.selectAll('*').remove();
draw(json.data, numBins);
slice.done(json);
});
};

return {
render: render,
resize: render,
};
return {
render,
resize: render,
};
}

module.exports = histogram;
Expand Down