Skip to content

Instantly share code, notes, and snippets.

View TCotton's full-sized avatar
🎯
Focusing

Andrew Walpole TCotton

🎯
Focusing
View GitHub Profile
@TCotton
TCotton / barchart.js
Last active November 4, 2018 10:19
this is a barchart
import React, { Component } from 'react';
import dummyData from './dummy-data.json';
import * as d3 from 'd3';
import './Barchart.css';
/**
"barData": [
15,
13,
10,
@TCotton
TCotton / index.js
Last active November 3, 2018 13:38
This is an index file
// select the svg container first
const svg = d3.select('.canvas')
.append('svg')
.attr('width', 600)
.attr('height', 600);
// create margins & dimensions
const margin = {top: 20, right: 20, bottom: 100, left: 100};
const graphWidth = 600 - margin.left - margin.right;
const graphHeight = 600 - margin.top - margin.bottom;
@TCotton
TCotton / barchart_with_react_d3.jsx
Created September 14, 2018 06:49
Barchart with React
import React from 'react';
import { scaleBand, scaleLinear } from 'd3-scale';
import { tsvParse } from 'd3-dsv';
import { max } from 'd3-array';
import { axisBottom, axisLeft } from 'd3-axis';
import { select } from 'd3-selection';
// Same as data.tsv
import dataTsv from './data';
@TCotton
TCotton / donut_chart_react.jsx
Created September 14, 2018 06:48
An example of creating a donut chart with React
import React from 'react';
import { scaleOrdinal } from 'd3-scale';
import { arc as d3Arc, pie as d3Pie } from 'd3-shape';
import { csvParse } from 'd3-dsv';
// Same as data.csv
import dataCsv from './data';
import './chart.css';
@TCotton
TCotton / reducer.js
Created September 2, 2018 14:46
redux asynchronous action generator
// import React from 'react';
import { combineReducers, createStore, compose } from 'redux';
import axios from 'axios';
const nameReducer = (state = 'Anonymous', action) => {
switch (action.type) {
case 'CHANGE_NAME':
return action.name;
default:
return state;
@TCotton
TCotton / webpack.config.js
Created May 9, 2018 16:28 — forked from timurcatakli/webpack.config.js
An Easy to Understand Webpack 4+ Configuration File with Comments
const publicPath = 'public';
// Node os module
// The os module provides a number of operating system-related utility methods.
// It can be accessed using:
const os = require('os');
// Using a single monolithic configuration file impacts comprehension and
// removes any potential for reusability.
// As the needs of your project grow, you have to figure out the means to manage
// webpack configuration more effectively.
@TCotton
TCotton / for_map.js
Created August 12, 2017 19:36
#2: I need to create a new array from a given one and transform all the elements
// Given the array of prices, return a new array with the prices n % lower.
const discount = (originalPrices, discountAmount) => {
const multiplier = 1 - discountAmount;
// we must clone the array
let result = new Array(originalPrices);
for (let i = 0; i < originalPrices.length; i++) {
result[i] = originalPrices[i] * multiplier;
}
return result;
}
@TCotton
TCotton / for_reduce.js
Created August 12, 2017 19:33
#1: I need to go over an array and get a single value as a result
// for loop
const sum = (array) => {
let result = 0;
for (let i = 0; i < array.length; i++) {
result += array[i];
}
return result;
}
const numbers = [5, 25, 8, 18];
@TCotton
TCotton / chart.js
Created August 12, 2017 19:05
A better way to structure D3 code (updated with ES6 and D3 v4)
class Chart {
constructor(opts) {
// load in arguments from config object
this.data = opts.data;
this.element = opts.element;
// create the chart
this.draw();
}
@TCotton
TCotton / filter_map.js
Created August 12, 2017 18:55
filter and map example
const newReleases = [{
"id": 70111470,
"title": "Die Hard",
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg",
"uri": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 4.0,
"bookmark": []
},
{
"id": 654356453,