-
Notifications
You must be signed in to change notification settings - Fork 16
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
Feature/add configurable graph domain #136
Changes from 10 commits
8657e08
e09b234
93eb6cf
831d62c
20c1996
57a4d2d
a8ac36b
6f36ac3
56f73b0
d3413df
1a63530
e38cfdf
42dacb2
6962e4d
e3fb06c
072f929
ae1df61
c478e8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,5 +44,4 @@ describe('Test IndicatorHelper static methods', () => { | |
|
||
defaultValues.chartConfiguration = original; | ||
}) | ||
|
||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import {SubindicatorFilter} from "../../src/js/profile/subindicator_filter"; | ||
|
||
const indicators = { | ||
'Age by race': { | ||
'groups': { | ||
'gender': { | ||
'Female': {'subindicator1': {'count': 10}, 'subindicator2': {'count': 20}}, | ||
'Male': {'subindicator1': {'count': 30}, 'subindicator2': {'count': 40}}, | ||
}, | ||
'race': { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
'Race1': {'subindicator1': {'count': 50}, 'subindicator2': {'count': 60}}, | ||
'Race2': {'subindicator1': {'count': 70}, 'subindicator2': {'count': 80}}, | ||
}, | ||
}, | ||
'subindicators': [ | ||
{label: 'subindicator1', "count": 90}, | ||
{label: 'subindicator2', "count": 100}, | ||
] | ||
}, | ||
'Another indicator': {} | ||
} | ||
|
||
function testChartData(chartData, expectedData) { | ||
expect(chartData[0].value).toBe(expectedData[0].value); | ||
expect(chartData[0].label).toBe(expectedData[0].label); | ||
expect(chartData[1].value).toBe(expectedData[1].value); | ||
expect(chartData[1].label).toBe(expectedData[1].label); | ||
} | ||
|
||
describe('Testing Subindicator Filter', () => { | ||
const si = new SubindicatorFilter(); | ||
const title = 'Age by race'; | ||
si.indicators = indicators; | ||
|
||
test.each([ | ||
[{group: 'race', subindicator: 'Race2'}, indicators[title].groups.race.Race2], | ||
[{group: 'gender', subindicator: 'Female'}, indicators[title].groups.gender.Female], | ||
])('Extract groups correctly', (value, expected) => { | ||
const chartData = si.getFilteredGroups(indicators[title].groups, value.group, value.subindicator) | ||
testChartData(chartData, [ | ||
{label: 'subindicator1', value: expected.subindicator1.count}, | ||
{label: 'subindicator2', value: expected.subindicator2.count}, | ||
]) | ||
|
||
expect(chartData[0].value).toBe(expected.subindicator1.count); | ||
expect(chartData[1].value).toBe(expected.subindicator2.count); | ||
}) | ||
|
||
test('Handles missing group correctly', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
const chartData = si.getFilteredGroups(indicators[title].groups, 'Missing group', 'XXXXXX') | ||
expect(chartData.length).toBe(0) | ||
}) | ||
|
||
test('Handles missing subindicator correctly', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
const chartData = si.getFilteredGroups(indicators[title].groups, 'Gender', 'Missing subindicator') | ||
expect(chartData.length).toBe(0) | ||
}) | ||
|
||
test('Extracts subindicators correctly', () => { | ||
const subindicators = si.getFilteredSubindicators(indicators[title].subindicators) | ||
testChartData(subindicators, [ | ||
{label: 'subindicator1', value: 90}, | ||
{label: 'subindicator2', value: 100} | ||
]) | ||
}) | ||
|
||
test('Filters correctly', () => { | ||
let chartData = si.getFilteredData('All values', '', title) | ||
testChartData(chartData, [ | ||
{label: 'subindicator1', value: 90}, | ||
{label: 'subindicator2', value: 100} | ||
]) | ||
|
||
chartData = si.getFilteredData('Female', 'gender', title) | ||
testChartData(chartData, [ | ||
{label: 'subindicator1', value: 10}, | ||
{label: 'subindicator2', value: 20} | ||
]) | ||
|
||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
let chartConfiguration = [{"label": "Value", "formatting": "~s"}]; | ||
|
||
const DEFAULT_CONFIG = 'default' | ||
let chartConfiguration = { | ||
types: { | ||
Value: {formatting: '~s', minX: DEFAULT_CONFIG, maxX: DEFAULT_CONFIG}, | ||
Percentage: {formatting: '.0%', minX: DEFAULT_CONFIG, maxX: DEFAULT_CONFIG} | ||
} | ||
}; | ||
export const defaultValues = { | ||
chartConfiguration | ||
chartConfiguration, | ||
DEFAULT_CONFIG | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,15 @@ export class Indicator extends Observable { | |
} | ||
} | ||
|
||
let c = new Chart(this.formattingConfig, this.subindicators, this.groups, detail, 'Percentage', indicator, title); | ||
|
||
const configuration = detail.indicators[title].chartConfiguration; | ||
const config = { | ||
adieyal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
chart: detail.indicators[title].chartConfiguration, | ||
formatting: this.formattingConfig | ||
} | ||
const indicators = detail.indicators; | ||
|
||
let c = new Chart(config, this.subindicators, this.groups, indicators, 'Percentage', indicator, title); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of creating the variable let c = new Chart(config, this.subindicators, this.groups, detail.indicators, 'Percentage', indicator, title); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. detail needs to be refactored out of this class entirely |
||
this.bubbleEvents(c, [ | ||
'profile.chart.saveAsPng', 'profile.chart.valueTypeChanged', | ||
'profile.chart.download_csv', 'profile.chart.download_excel', 'profile.chart.download_json', 'profile.chart.download_kml', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,6 +135,25 @@ export class SubindicatorFilter extends Observable { | |
this.populateDropdown(subindicatorDd, subindicators, callback); | ||
} | ||
|
||
getFilteredGroups(groups, selectedGroup, selectedFilter) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Identical blocks of code found in 2 locations. Consider refactoring. |
||
const group = groups[selectedGroup] | ||
if (group == undefined) | ||
return [] | ||
|
||
const groupValue = Object.entries(group).find(g => g[0] == selectedFilter) | ||
if (groupValue == undefined) | ||
return [] | ||
|
||
const subindicators = Object.entries(groupValue[1]) | ||
return subindicators.map(cd => new SubIndicator(cd)) | ||
} | ||
|
||
getFilteredSubindicators(subindicators) { | ||
return subindicators.map(el => { | ||
return new SubIndicator([el.label, el]) | ||
}) | ||
} | ||
|
||
getFilteredData = (selectedFilter, selectedGroup, title) => { | ||
this.triggerEvent('point_tray.subindicator_filter.filter', { | ||
indicator: title, | ||
|
@@ -143,27 +162,18 @@ export class SubindicatorFilter extends Observable { | |
}); | ||
|
||
let chartData = []; | ||
const indicatorEntries = Object.entries(this.indicators) | ||
const indicator = indicatorEntries.find(el => el[0] == title) | ||
|
||
if (selectedFilter !== allValues) { | ||
for (const [indicatorTitle, subindicator] of Object.entries(this.indicators)) { | ||
//filter indicatorTitle | ||
if (indicatorTitle === title) { | ||
for (const [key, value] of Object.entries(subindicator.groups[selectedGroup])) { | ||
if (key === selectedFilter) { | ||
Object.entries(value).forEach((cd) => { | ||
chartData.push(new SubIndicator(cd)) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
for (const [indicatorTitle, subindicator] of Object.entries(this.indicators)) { | ||
if (indicatorTitle === title) { | ||
chartData = subindicator.subindicators; | ||
} | ||
} | ||
} | ||
if (indicator == undefined) | ||
return chartData | ||
|
||
const subindicatorData = indicator[1] | ||
|
||
if (selectedFilter !== allValues) | ||
return this.getFilteredGroups(subindicatorData.groups, selectedGroup, selectedFilter) | ||
else | ||
return this.getFilteredSubindicators(subindicatorData.subindicators) | ||
|
||
return chartData; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.