cleaned chart code/added cells chart

This commit is contained in:
Max Robinson
2021-02-11 13:24:42 -07:00
parent 7b19798be9
commit 9626cd54d6
12 changed files with 137 additions and 51 deletions

View File

@@ -0,0 +1,51 @@
const CellStates = require("../../Organism/Cell/CellStates");
const FossilRecord = require("../FossilRecord");
const ChartController = require("./ChartController");
class CellsChart extends ChartController {
constructor() {
super("Organism Size / Composition");
}
setData() {
this.clear();
//this.mouth, this.producer, this.mover, this.killer, this.armor, this.eye
this.data.push({
type: "line",
markerType: "none",
color: 'black',
showInLegend: true,
name: "pop1",
legendText: "Avg. organism size",
dataPoints: []
}
);
for (var c of CellStates.living) {
this.data.push({
type: "line",
markerType: "none",
color: c.color,
showInLegend: true,
name: c.name,
legendText: "Avg. " + c.name + " cells",
dataPoints: []
}
);
}
this.addAllDataPoints();
}
addDataPoint(i) {
var t = FossilRecord.tick_record[i];
var p = FossilRecord.av_cells[i];
this.data[0].dataPoints.push({x:t, y:p});
var j=1;
for (var name in FossilRecord.av_cell_counts[i]) {
var count = FossilRecord.av_cell_counts[i][name];
this.data[j].dataPoints.push({x:t,y:count})
j++;
}
}
}
module.exports = CellsChart;

View File

@@ -18,6 +18,12 @@ class ChartController {
alert("Must override updateData!");
}
addAllDataPoints(){
for (var i in FossilRecord.tick_record) {
this.addDataPoint(i)
}
}
render() {
this.chart.render();
}
@@ -39,11 +45,18 @@ class ChartController {
}
addNewest() {
alert("Must override addNewest!");
var i = FossilRecord.tick_record.length-1;
this.addDataPoint(i);
}
addDataPoint(i) {
alert("Must override addDataPoint")
}
removeOldest() {
alert("Must override addNewest!");
for (var dps of this.data) {
dps.dataPoints.shift();
}
}
clear() {

View File

@@ -18,24 +18,14 @@ class MutationChart extends ChartController {
dataPoints: []
}
);
for (var i in FossilRecord.tick_record) {
var t = FossilRecord.tick_record[i];
var p = FossilRecord.av_mut_rates[i];
this.data[0].dataPoints.push({x:t, y:p});
}
// console.log(this.data)
this.addAllDataPoints();
}
addNewest() {
var i = FossilRecord.tick_record.length-1;
addDataPoint(i) {
var t = FossilRecord.tick_record[i];
var p = FossilRecord.av_mut_rates[i];
this.data[0].dataPoints.push({x:t, y:p});
}
removeOldest() {
this.data[0].dataPoints.shift();
}
}
module.exports = MutationChart;

View File

@@ -18,24 +18,14 @@ class PopulationChart extends ChartController {
dataPoints: []
}
);
for (var i in FossilRecord.tick_record) {
var t = FossilRecord.tick_record[i];
var p = FossilRecord.pop_counts[i];
this.data[0].dataPoints.push({x:t, y:p});
}
// console.log(this.data)
this.addAllDataPoints();
}
addNewest() {
var i = FossilRecord.tick_record.length-1;
addDataPoint(i) {
var t = FossilRecord.tick_record[i];
var p = FossilRecord.pop_counts[i];
this.data[0].dataPoints.push({x:t, y:p});
}
removeOldest() {
this.data[0].dataPoints.shift();
}
}
module.exports = PopulationChart;

View File

@@ -18,23 +18,14 @@ class SpeciesChart extends ChartController {
dataPoints: []
}
);
for (var i in FossilRecord.tick_record) {
var t = FossilRecord.tick_record[i];
var p = FossilRecord.species_counts[i];
this.data[0].dataPoints.push({x:t, y:p});
}
this.addAllDataPoints();
}
addNewest() {
var i = FossilRecord.tick_record.length-1;
addDataPoint(i) {
var t = FossilRecord.tick_record[i];
var p = FossilRecord.species_counts[i];
this.data[0].dataPoints.push({x:t, y:p});
}
removeOldest() {
this.data[0].dataPoints.shift();
}
}
module.exports = SpeciesChart;

View File

@@ -1,3 +1,4 @@
const CellStates = require("../Organism/Cell/CellStates");
const Species = require("./Species");
const FossilRecord = {
@@ -9,11 +10,11 @@ const FossilRecord = {
this.min_discard = 10;
this.record_size_limit = 500; // store this many data points
this.setData();
},
setEnv: function(env) {
this.env = env;
this.setData();
},
addSpecies: function(org, ancestor) {
@@ -66,9 +67,10 @@ const FossilRecord = {
// all parallel arrays
this.tick_record = [0];
this.pop_counts = [1];
this.av_pop_counts = [1]
this.species_counts = [1];
this.av_mut_rates = [5];
this.av_cells = [3];
this.av_cell_counts = [this.calcCellCountAverages()];
},
updateData() {
@@ -77,15 +79,41 @@ const FossilRecord = {
this.pop_counts.push(this.env.organisms.length);
this.species_counts.push(this.extant_species.length);
this.av_mut_rates.push(this.env.averageMutability());
let av_cell = 0;
if (this.env.organisms.length > 0) {
av_cell = this.env.total_cells / this.env.organisms.length;
}
this.av_cells.push(av_cell);
this.av_cell_counts.push(this.calcCellCountAverages())
if (this.tick_record.length > this.record_size_limit) {
this.tick_record.shift();
this.pop_counts.shift();
this.av_pop_counts.shift();
this.species_counts.shift();
this.av_mut_rates.shift();
this.av_cells.shift();
}
},
calcCellCountAverages() {
var total_org = this.env.organisms.length;
var cell_counts = {};
for (let c of CellStates.living) {
cell_counts[c.name] = 0;
}
for (let s of this.extant_species) {
for (let name in s.cell_counts) {
cell_counts[name] += s.cell_counts[name] * s.population;
}
}
if (total_org == 0)
return cell_counts;
for (let c in cell_counts) {
cell_counts[c] /= total_org;
}
return cell_counts;
},
clear_record: function() {
this.extant_species = [];
this.extinct_species = [];

View File

@@ -1,3 +1,5 @@
const CellStates = require("../Organism/Cell/CellStates");
class Species {
constructor(anatomy, ancestor, start_tick) {
this.anatomy = anatomy;
@@ -14,6 +16,18 @@ class Species {
}
this.name = '_' + Math.random().toString(36).substr(2, 9);
this.extinct = false;
this.calcAnatomyDetails();
}
calcAnatomyDetails() {
var cell_counts = {};
for (let c of CellStates.living) {
cell_counts[c.name] = 0;
}
for (let cell of this.anatomy.cells) {
cell_counts[cell.state.name]+=1;
}
this.cell_counts=cell_counts;
}
addPop() {

View File

@@ -1,9 +1,11 @@
const PopulationChart = require("./Charts/PopulationChart");
const SpeciesChart = require("./Charts/SpeciesChart");
const MutationChart = require("./Charts/MutationChart");
const CellsChart = require("./Charts/CellsChart");
const FossilRecord = require("./FossilRecord");
const ChartSelections = [PopulationChart, SpeciesChart, MutationChart];
const ChartSelections = [PopulationChart, SpeciesChart, CellsChart, MutationChart];
class StatsPanel {
constructor(env) {
@@ -47,12 +49,11 @@ class StatsPanel {
updateDetails() {
var org_count = this.env.organisms.length;
$('#org-count').text("Organism count: " + org_count);
if (org_count > this.organism_record)
this.organism_record = org_count;
$('#org-record').text("Highest count: " + this.env.organism_record);
$('#org-count').text("Total Population: " + org_count);
$('#species-count').text("Number of Species: " + FossilRecord.extant_species.length);
$('#largest-org').text("Largest Organism Ever: " + this.env.largest_cell_count + " cells");
$('#avg-mut').text("Average Mutation Rate: " + Math.round(this.env.averageMutability() * 100) / 100);
$('#largest-org').text("Largest Organism: " + this.env.largest_cell_count + " cells");
}