Fixed up chart

This commit is contained in:
Max Robinson
2021-02-10 18:09:18 -07:00
parent 382794bf3f
commit 7b19798be9
14 changed files with 318 additions and 563 deletions

View File

@@ -0,0 +1,55 @@
const FossilRecord = require("../FossilRecord");
class ChartController {
constructor(title) {
this.data = [];
this.chart = new CanvasJS.Chart("chartContainer", {
zoomEnabled: true,
title:{
text: title
},
data: this.data
});
this.chart.render();
this.data
}
setData() {
alert("Must override updateData!");
}
render() {
this.chart.render();
}
updateData() {
var r_len = FossilRecord.tick_record.length;
var newest_t = -1;
var oldest_t = 0;
if (this.data[0].dataPoints.length>0) {
newest_t = this.data[0].dataPoints[this.data[0].dataPoints.length-1].x;
newest_t = this.data[0].dataPoints[0].x;
}
if (newest_t < FossilRecord.tick_record[r_len-1]) {
this.addNewest();
}
if (oldest_t < FossilRecord.tick_record[0]) {
this.removeOldest();
}
}
addNewest() {
alert("Must override addNewest!");
}
removeOldest() {
alert("Must override addNewest!");
}
clear() {
this.data.length = 0;
this.chart.render();
}
}
module.exports = ChartController;

View File

@@ -0,0 +1,41 @@
const FossilRecord = require("../FossilRecord");
const ChartController = require("./ChartController");
class MutationChart extends ChartController {
constructor() {
super("Mutation Rate");
}
setData() {
this.clear();
this.data.push({
type: "line",
markerType: "none",
color: 'black',
showInLegend: true,
name: "pop1",
legendText: "Average Mutation Rate",
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)
}
addNewest() {
var i = FossilRecord.tick_record.length-1;
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

@@ -0,0 +1,41 @@
const FossilRecord = require("../FossilRecord");
const ChartController = require("./ChartController");
class PopulationChart extends ChartController {
constructor() {
super("Population");
}
setData() {
this.clear();
this.data.push({
type: "line",
markerType: "none",
color: 'black',
showInLegend: true,
name: "pop1",
legendText: "Total Population",
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)
}
addNewest() {
var i = FossilRecord.tick_record.length-1;
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

@@ -0,0 +1,40 @@
const FossilRecord = require("../FossilRecord");
const ChartController = require("./ChartController");
class SpeciesChart extends ChartController {
constructor() {
super("Species");
}
setData() {
this.clear();
this.data.push({
type: "line",
markerType: "none",
color: 'black',
showInLegend: true,
name: "spec",
legendText: "Number of Species",
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});
}
}
addNewest() {
var i = FossilRecord.tick_record.length-1;
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

@@ -6,7 +6,10 @@ const FossilRecord = {
this.extinct_species = [];
// if an organism has fewer than this cumulative pop, discard them on extinction
this.min_discard = 5;
this.min_discard = 10;
this.record_size_limit = 500; // store this many data points
this.setData();
},
setEnv: function(env) {
@@ -59,10 +62,34 @@ const FossilRecord = {
}
},
setData() {
// 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];
},
updateData() {
var tick = this.env.total_ticks;
this.tick_record.push(tick);
this.pop_counts.push(this.env.organisms.length);
this.species_counts.push(this.extant_species.length);
this.av_mut_rates.push(this.env.averageMutability());
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();
}
},
clear_record: function() {
this.extant_species = [];
this.extinct_species = [];
// console.log("Cleared", this.extant_species, this.extinct_species)
this.setData();
},
}

View File

@@ -6,8 +6,13 @@ class Species {
this.cumulative_pop = 1;
this.start_tick = start_tick;
this.end_tick = -1;
this.color = '#asdfasdf';
this.name = '_' + Math.random().toString(36).substr(2, 9);;
this.color = Math.floor(Math.random()*16777215).toString(16);
if (ancestor != null) {
// needs to be reworked, maybe removed
var mutator = Math.floor(Math.random()*16777215)-8000000;
this.color = (mutator + parseInt(ancestor.color, 16)).toString(16);
}
this.name = '_' + Math.random().toString(36).substr(2, 9);
this.extinct = false;
}

65
src/Stats/StatsPanel.js Normal file
View File

@@ -0,0 +1,65 @@
const PopulationChart = require("./Charts/PopulationChart");
const SpeciesChart = require("./Charts/SpeciesChart");
const MutationChart = require("./Charts/MutationChart");
const ChartSelections = [PopulationChart, SpeciesChart, MutationChart];
class StatsPanel {
constructor(env) {
this.defineControls();
this.chart_selection = 0;
this.setChart();
this.env = env;
this.last_reset_count=env.reset_count;
}
setChart(selection=this.chart_selection) {
this.chart_controller = new ChartSelections[selection]();
this.chart_controller.setData();
this.chart_controller.render();
}
startAutoRender() {
this.setChart();
this.render_loop = setInterval(function(){this.updateChart();}.bind(this), 1000);
}
stopAutoRender() {
clearInterval(this.render_loop);
}
defineControls() {
$('#chart-option').change ( function() {
this.chart_selection = $("#chart-option")[0].selectedIndex;
this.setChart();
}.bind(this));
}
updateChart() {
if (this.last_reset_count < this.env.reset_count){
this.reset()
}
this.last_reset_count = this.env.reset_count;
this.chart_controller.updateData();
this.chart_controller.render();
}
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);
$('#avg-mut').text("Average Mutation Rate: " + Math.round(this.env.averageMutability() * 100) / 100);
$('#largest-org').text("Largest Organism: " + this.env.largest_cell_count + " cells");
}
reset() {
this.setChart();
}
}
module.exports = StatsPanel;