Skip to content
Snippets Groups Projects
Commit b15ef356 authored by esikkala's avatar esikkala
Browse files

Add optional config object for result mappers

parent ab16992a
No related branches found
No related tags found
No related merge requests found
......@@ -106,7 +106,7 @@ export const getAllResults = ({
} else {
({ endpoint, defaultConstraint, langTag, langTagSecondary } = config)
}
const { filterTarget, resultMapper, postprocess = null } = config
const { filterTarget, resultMapper, resultMapperConfig, postprocess = null } = config
let { q } = config
if (constraints == null && defaultConstraint == null) {
q = q.replace(/<FILTER>/g, '# no filters')
......@@ -153,6 +153,7 @@ export const getAllResults = ({
endpoint: endpoint.url,
useAuth: endpoint.useAuth,
resultMapper,
resultMapperConfig,
postprocess,
resultFormat
})
......
......@@ -81,20 +81,7 @@ export const mapNameSampoResults = sparqlBindings => {
return results
}
export const mapLineChart = sparqlBindings => {
const seriesData = []
const categoriesData = []
sparqlBindings.map(b => {
seriesData.push(b.count.value)
categoriesData.push(b.category.value)
})
return {
seriesData,
categoriesData
}
}
export const mapLineChartFillEmptyValues = sparqlBindings => {
export const mapLineChart = ({ sparqlBindings, config }) => {
const seriesData = []
const categoriesData = []
const sparqlBindingsLength = sparqlBindings.length
......@@ -102,15 +89,23 @@ export const mapLineChartFillEmptyValues = sparqlBindings => {
const currentCategory = parseInt(b.category.value)
const currentValue = parseInt(b.count.value)
seriesData.push(currentValue)
categoriesData.push(currentCategory)
if (index + 1 < sparqlBindingsLength) {
categoriesData.push(
config && config.xAxisConverter
? config.xAxisConverter(currentCategory)
: currentCategory
)
if (config && config.fillEmptyValues && index + 1 < sparqlBindingsLength) {
let categoryIter = currentCategory
const nextNonZeroCategory = parseInt(bindings[index + 1].category.value)
// add zeros until we reach the next category with a non zero value
while (categoryIter < nextNonZeroCategory - 1) {
categoryIter += 1
seriesData.push(0)
categoriesData.push(categoryIter)
categoriesData.push(
config && config.xAxisConverter
? config.xAxisConverter(categoryIter)
: categoryIter
)
}
}
})
......
......@@ -5,6 +5,7 @@ export const runSelectQuery = async ({
query,
endpoint,
resultMapper,
resultMapperConfig = null,
postprocess = null,
previousSelections = null, // not in use
resultFormat,
......@@ -29,7 +30,12 @@ export const runSelectQuery = async ({
data: q
})
if (resultFormat === 'json') {
const mappedResults = resultMapper(response.data.results.bindings, previousSelections)
if (resultMapper) {
}
const mappedResults = resultMapperConfig
? resultMapper({ sparqlBindings: response.data.results.bindings, config: resultMapperConfig })
: resultMapper(response.data.results.bindings)
if (postprocess) {
postprocess.func({ data: mappedResults, config: postprocess.config })
}
......
......@@ -175,7 +175,10 @@ export const backendSearchConfig = {
perspectiveID: 'perspective1',
q: productionsByDecadeQuery,
filterTarget: 'instance',
resultMapper: mapLineChart
resultMapper: mapLineChart,
resultMapperConfig: {
fillEmptyValues: false
}
},
eventLineChart: {
perspectiveID: 'perspective1',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment