Skip to content

Commit

Permalink
74 improve chart draw performance (#83)
Browse files Browse the repository at this point in the history
* Use cache to load pre-calculated data

* remove dead code

* use cache
  • Loading branch information
thearyadev authored Dec 21, 2023
1 parent b15522b commit 703af39
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
46 changes: 30 additions & 16 deletions static/js/season.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const charts = [ // define all chart ids here
'OTMP_DAMAGE_ALL', 'OTMP_TANK_ALL', 'O_ALL_AMERICAS', 'O_ALL_EUROPE',
'O_ALL_ASIA', 'O_ALL_ALL',
]

const chartCache = {}


const hero_color_data = $("colors").data().herocolors
function map_multi_array(arrayOfObjects){
const heroes = [];
Expand All @@ -30,37 +34,47 @@ function map_multi_array(arrayOfObjects){

}



function drawChart(chartName) {
if (!chartCache.hasOwnProperty(chartName)){ // chart not in cache
const chartElement = $(`#${chartName}`)

const chartDataRaw = chartElement.data().graphdata
const [heroes, counts] = map_multi_array(chartDataRaw.graph)
const stats = chartDataRaw.statistic
const entries = counts.reduce((acc, cur) => acc + cur, 0)
const chartTypeBreakdown = chartName.split("_")
let yAxisLength = 300; // standard size for all single role single region charts
if (chartName === "O_ALL_ALL"){ // o type, all roles, all regions
yAxisLength = 1250
}else if (chartTypeBreakdown[chartTypeBreakdown.length - 1] === "ALL" || chartTypeBreakdown[0] === "O") { // all roles, single region or O-type chart
yAxisLength = 500
}

chartCache[chartName] = {
heroes, counts, stats, entries, yAxisLength, chartElement
}

const chartElement = $(`#${chartName}`)
const chartDataRaw = chartElement.data().graphdata
const [heroes, counts] = map_multi_array(chartDataRaw.graph)
const stats = chartDataRaw.statistic
const entries = counts.reduce((acc, cur) => acc + cur, 0)
const chartTypeBreakdown = chartName.split("_")
let yAxisLength = 300; // standard size for all single role single region charts
if (chartName === "O_ALL_ALL"){ // o type, all roles, all regions
yAxisLength = 1250
}else if (chartTypeBreakdown[chartTypeBreakdown.length - 1] === "ALL" || chartTypeBreakdown[0] === "O") { // all roles, single region or O-type chart
yAxisLength = 500
}


const chartDataCached = chartCache[chartName]
Highcharts.chart(chartName, {
chart: {
type: 'column',
margin: [75, 50, 75, 50]
},
xAxis: {
categories: heroes,
categories: chartDataCached.heroes,
crosshair: true,
accessibility: {
description: 'Heroes'
}
},
yAxis: {
min: 0,
max: yAxisLength,
max: chartDataCached.yAxisLength,
title: {
text: 'Occurrences'
}
Expand All @@ -74,18 +88,18 @@ function drawChart(chartName) {
series: [
{
name: `Occurrences`,
data: counts.map((item, index) => {
data: chartDataCached.counts.map((item, index) => {
return {
y: item,
color: lookup_hero_color(heroes[index])
color: lookup_hero_color(chartDataCached.heroes[index])
}
})
},
]
});


chartElement.parent().append(`<p class='chart-stats small text-muted'><small>Mean: ${stats?.mean} | Variance: ${stats?.variance} | Standard Deviation: ${stats?.standard_deviation} | # Entires: ${entries}</small></p>`)
chartDataCached.chartElement.parent().append(`<p class='chart-stats small text-muted'><small>Mean: ${chartDataCached?.mean} | Variance: ${chartDataCached?.variance} | Standard Deviation: ${chartDataCached?.standard_deviation} | # Entires: ${chartDataCached.entries}</small></p>`)

}

Expand Down
24 changes: 17 additions & 7 deletions static/js/trends.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@

const hero_color_data = $("colors").data().herocolors
const cache = {

}

function drawHeroTrendChartAllRegions() {
const chartElement = $('#T_ALL_ALL')
const data = $('data').data().trends
const seasons = JSON.parse($('seasons').data().seasons.replace(/'/g, '"')).map(item => item.split("_")[0])

function drawHeroTrendChartAllRegions() {
const chartName = "HeroTrendChartAllRegions"
if (!cache.hasOwnProperty(chartName)){
const chartElement = $('#T_ALL_ALL')
const data = $('data').data().trends
const seasons = JSON.parse($('seasons').data().seasons.replace(/'/g, '"')).map(item => item.split("_")[0])
cache[chartName] = {
data,
seasons
}
}


const cachedData = cache[chartName]
Highcharts.chart('T_ALL_ALL', {
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle'
},
xAxis: {
categories: seasons,
categories: cachedData.seasons,
min: 0, // Replace with your actual minimum value
max: seasons.length -1, // Replace with your actual maximum value
max: cachedData.seasons.length -1, // Replace with your actual maximum value
startOnTick: false,
endOnTick: false,
},
Expand All @@ -39,7 +49,7 @@ Highcharts.chart('T_ALL_ALL', {



series: data.map(item => {
series: cachedData.data.map(item => {
console.log(item)
return {
color: lookup_hero_color(item.name),
Expand Down

0 comments on commit 703af39

Please sign in to comment.