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

Some linting and plotting changes (in progress) #3

Merged
merged 8 commits into from
Oct 12, 2015
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
98 changes: 60 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,57 +21,76 @@ var dk = require('datakitjs');
//READ A CSV FILE

//file.csv
// COL1,COL2
// val11,val12
// val21,val22
// COL1, COL2
// val11, val12
// val21, val22

dk.csv('file.csv',function(data){
dk.csv('file.csv', function(data) {
console.log(data);
});

//Output:
//[{COL1:val11,COL2:val12},{COL1:val21,COL2:val22}]
//[{ COL1: val11, COL2: val12 }, { COL1: val21, COL2: val22 }]


//GET A COLUMN FROM AN ARRAY OF ROW OBJECTS
dk.csv('file.csv',function(data){
var c2 = dk.col(data,'COL2');
dk.csv('file.csv', function(data) {
var c2 = dk.col(data, 'COL2');
console.log(c2);
});

//Output:
//[val12,val22]
//[val12, val22]

// By default, dk.csv will convert all values to strings. You can convert select
// columns to numbers by passing an array of column names to 'dk.numeric'.
// columns to numbers by passing an array of column names to 'dk.numeric'.

//file2.csv
// COL1, COL2
// val11, 1
// val21, 2

dk.csv('file2.csv', function(data){
dk.csv('file2.csv', function(data) {
var d = dk.numeric(data, ['COL2'], 0) // The third parameter value will be filled
// in to blank cells. Its default value is 0.
var c2 = dk.col(d, 'COL2');
console.log(c2);
});

//Output:
//[1,2]
//[1, 2]


//PLOT ARRAY(S) OF DATA

var x1 = dk.norm(25);
var plot = dk.plot(x1); // x1 will be plotted on the y-axis, index on the x-axis
//dk.plot returns a string of the html used to create the plot.

var chart = new dk.Chart({
//optional config
height: 500,
width: 500,
xLab: 'x-Axis Label',
yLab: 'y-Axis Label'
});

var labels = dk.seq(1,25);
chart.addDataSet({
x: [1, 2, 3],
y: [4, 5, 6],
z: [2, 3, 5],
colors: ['blue', 'green', 'red']
}).addDataSet({
x: [1, 10],
y: [2, -1],
type: 'line'
}).addDataSet({
x: [10, 5, 1],
y: [4, 5, 2],
labels: ["first", "second", "third"]
}).plot();


var labels = dk.seq(1, 25);
var x2 = dk.norm(25);
var x3 = dk.norm(25);
dk.plot(labels,x1,x2,x3); //labels on the x-axis, the next three arrays will each be plotted on the y-axis
dk.plot(labels, x1, x2, x3); //labels on the x-axis, the next three arrays will each be plotted on the y-axis
```

###Statistical Methods
Expand All @@ -80,29 +99,32 @@ dk.plot(labels,x1,x2,x3); //labels on the x-axis, the next three arrays will eac
var dk = require('datakitjs');

//MEAN OF AN ARRAY
dk.mean([1,2,3]); //returns 2
dk.mean([1, 2, 3]); //returns 2

//STANDARD DEVIATION AND VARIANCE OF AN ARRAY
dk.sd([1,2,3]); //returns 1
dk.vari([1,2,3]); //returns 1
dk.sd([1, 2, 3]); //returns 1
dk.vari([1, 2, 3]); //returns 1

//COVARIANCE OF TWO ARRAYS
dk.cov([1,2,3],[3,2,1]); //returns -1
dk.cov([1, 2, 3], [3, 2, 1]); //returns -1

//SIMPLE LINEAR REGRESSION

var x = [1,2,3];
var y = [2,1,3];
var x = [1, 2, 3];
var y = [2, 1, 3];

var model = dk.reg(x,y);
var model = dk.reg(x, y);

// model.f is a function that returns the estimated y for an input x (estimated via standard OLS regression)
// model.f = function(x){
// model.f = function(x) {
// return (a + b * x);
// }
// };

// model.pts is an array of the estimated y for each element of x
// model.pts = [1.5,2,2.5];
// model.pts = [1.5, 2, 2.5];

// model.endPoints is an object with the coordinates of the boundary points
// model.endPoints = { x1: 1, x2: 3, y1: 1.5, y2: 2.5 };

```

Expand All @@ -112,31 +134,31 @@ var dk = require('datakitjs');

//GENERATE AN ARRAY WITH A SEQUENCE OF NUMBERS

dk.seq(1,5); //returns [1,2,3,4,5]
dk.seq(1, 5); //returns [1, 2, 3, 4, 5]

dk.seq(0,1,0.25); //returns [0,0.25,0.5,0.75,1]
dk.seq(0, 1, 0.25); //returns [0, 0.25, 0.5, 0.75, 1]

//GENERATE AN ARRAY WITH REPEATED VALUE

dk.rep(1,5); //returns [1,1,1,1,1]
dk.rep(1, 5); //returns [1, 1, 1, 1, 1]

//CHECK IF NUMBERS ARE CLOSE
dk.isclose(0,Math.pow(10,-15)); //returns true
dk.isclose(0, Math.pow(10, -15)); //returns true

dk.isclose(0,Math.pow(10,-5)); //returns false
dk.isclose(0, Math.pow(10, -5)); //returns false

//SUM AN ARRAY OF NUMBERS
//uses Kahan summation

dk.sum([1,2,3]); //returns 6
dk.sum([1, 2, 3]); //returns 6

//PRODUCT OF AN ARRAY OF NUMBERS
//implementation from 'Accurate Floating Point Product' - Stef Graillat

dk.prod([1,2,3]); //returns 6
dk.prod([1, 2, 3]); //returns 6

//MAX AND MIN OF AN ARRAY
var x = [1,2,3];
var x = [1, 2, 3];
dk.min(x); //returns 1
dk.max(x); //returns 3

Expand All @@ -148,16 +170,16 @@ var dk = require('datakitjs');

//GET AN ARRAY OF EXPONENTIALLY DISTRIBUTED VALUES

dk.exp(3,1); //returns [ 0.3584189321510761, 1.0466439500242446, 0.08887770301056963 ]
dk.exp(3, 1); //returns [0.3584189321510761, 1.0466439500242446, 0.08887770301056963]


//GET AN ARRAY OF NORMALLY DISTRIBUTED VALUES

dk.norm(3,0,1); //returns [ -1.709768103193772, 0.23530041388459744, 0.4431320382580479 ]
dk.norm(3, 0, 1); //returns [-1.709768103193772, 0.23530041388459744, 0.4431320382580479]

//GET AN ARRAY OF UNIFORMLY DISTRIBUTED VALUES

dk.uni(3); //returns [ 0.30658303829841316, 0.1601463456172496, 0.8538850131444633 ]
dk.uni(3); //returns [0.30658303829841316, 0.1601463456172496, 0.8538850131444633]

```

Expand Down
181 changes: 181 additions & 0 deletions lib/chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
var Chart = function(config) {
var self = this;

// initial configuration of size and datasets
var datasets = [];

self.config = config || {};
self.config.height = self.config.height || 500;
self.config.width = self.config.width || 500;
self.config.bufferHeight = self.config.height * 0.8;
self.config.bufferWidth = self.config.width * 0.6;

// public method to plot by calling all private methods
this.render = function() {
buildCanvas();
buildMapping();
buildAxes();
plotDataSets();
return self;
};

// public methods to get and set datasets
this.datasets = function() {
return datasets.slice();
};

this.addDataSet = function(dataset) {
datasets.push(dataset);
return self;
};

// private methods
var buildCanvas = function() {
self.canvas = d3.select(self.config.selector || 'body')
.append('svg')
.attr('height', self.config.height)
.attr('width', self.config.width);

self.buffer = self.canvas.append('g');

var xTranslate = (self.config.width - self.config.bufferWidth) / 2;
var yTranslate = (self.config.height - self.config.bufferHeight) / 2;

self.buffer.attr(
'transform',
'translate(' + xTranslate + ', ' + yTranslate + ')'
);
};

var buildMapping = function() {
var xMin = mini(datasets[0].x),
yMin = mini(datasets[0].y),
xMax = maxi(datasets[0].x),
yMax = maxi(datasets[0].y);

datasets.forEach(function(dataset, index) {
if (index > 0) {
xMin = Math.min(mini(dataset.x), xMin);
yMin = Math.min(mini(dataset.y), yMin);
xMax = Math.max(maxi(dataset.x), xMax);
yMax = Math.max(maxi(dataset.y), yMax);
}
});

self.xMap = d3.scale.linear()
.domain([xMin, xMax])
.range([0, self.config.bufferWidth]);

self.yMap = d3.scale.linear()
.domain([yMax, yMin])
.range([0, self.config.bufferHeight]);

};

var buildAxes = function() {
var xAxis = d3.svg.axis()
.scale(self.xMap);

var yAxis = d3.svg.axis()
.scale(self.yMap)
.orient('left');

self.buffer.append('g')
.attr('transform','translate(0,' + self.config.bufferHeight + ')')
.call(xAxis);

self.buffer.append('g')
.call(yAxis);

var xLabel = self.buffer.append('text')
.attr('x', self.config.bufferWidth * 0.5)
.attr('y', self.config.bufferHeight + 50)
.text(self.config.xLab)
.attr('text-anchor','middle');

var yLabel = self.buffer.append('text')
.attr('x', -self.config.bufferHeight * 0.5)
.attr('y', -50)
.attr('transform','rotate(-90)')
.text(self.config.yLab)
.attr('text-anchor','middle');

};

var plotDataSets = function() {
datasets.forEach(function(dataset) {
if (dataset.type == 'line') {
for (var i = 1; i < dataset.x.length; i++) {
self.buffer.append('line')
.attr('stroke-width', 1)
.attr('stroke', 'black')
.attr('x1', self.xMap(dataset.x[i-1]))
.attr('x2', self.xMap(dataset.x[i]))
.attr('y1', self.yMap(dataset.y[i-1]))
.attr('y2', self.yMap(dataset.y[i]));
};
}
else if (typeof dataset.labels !== 'undefined') {
for (var i = 0; i < dataset.x.length; i++) {
self.buffer.append('text')
.attr('x', self.xMap(dataset.x[i]))
.attr('y', self.yMap(dataset.y[i]))
.text(dataset.labels[i])
.attr('text-anchor','middle')
.attr('stroke', dataset.color || 'black');
};
}
else {
// make a scatter plot if not a line
for (var i = 0; i < dataset.x.length; i++) {
var zMin = typeof dataset.z === 'undefined' ? null : mini(dataset.z);
var zMax = typeof dataset.z === 'undefined' ? null : maxi(dataset.z);

self.buffer.append('circle')
.attr('r', function() {
if (typeof dataset.z === 'undefined') {
return self.config.height * self.config.width * (0.00002);
}
else {
var minSize = self.config.height * self.config.width * 0.000025;
var sizeMultiplier = self.config.height * self.config.width * (0.0001);
var proportionOfMaxValue = (dataset.z[i] - zMin) / (zMax - zMin);

return (minSize + sizeMultiplier * proportionOfMaxValue);
}
})
.attr('cx', self.xMap(dataset.x[i]))
.attr('cy', self.yMap(dataset.y[i]))
.attr('opacity',function() {
if (typeof z === 'undefined') {
return 1;
}
else{
return 0.3;
}
})
.attr('fill',function() {
if (typeof dataset.colors === 'undefined') {
return 'none';
}
else{
return dataset.colors[i];
}
})
.attr('stroke', 'black');
};
};
});
};

function mini(arr) {
return Math.min.apply(null, arr);
};

function maxi(arr) {
return Math.max.apply(null, arr);
};
};

module.exports = Chart;

Loading