Skip to content
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

fix: handle single value vis-type as highcharts chart instance #3289

19 changes: 4 additions & 15 deletions cypress/elements/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
} from '@dhis2/analytics'

const visualizationContainerEl = 'visualization-container'
const visualizationTitleEl = 'visualization-title'
const visualizationSubtitleEl = 'visualization-subtitle'
const chartContainerEl = '.highcharts-container'
const highchartsLegendEl = '.highcharts-legend'
const highchartsTitleEl = '.highcharts-title'
Expand All @@ -24,11 +22,7 @@ const AOTitleDirtyEl = 'titlebar-dirty'
const timeout = {
timeout: 40000,
}
const nonHighchartsTypes = [
VIS_TYPE_OUTLIER_TABLE,
VIS_TYPE_PIVOT_TABLE,
VIS_TYPE_SINGLE_VALUE,
]
const nonHighchartsTypes = [VIS_TYPE_OUTLIER_TABLE, VIS_TYPE_PIVOT_TABLE]

export const expectVisualizationToBeVisible = (visType = VIS_TYPE_COLUMN) =>
nonHighchartsTypes.includes(visType)
Expand Down Expand Up @@ -64,13 +58,11 @@ export const expectChartToContainDimensionItem = (visType, itemName) => {
case VIS_TYPE_GAUGE:
case VIS_TYPE_YEAR_OVER_YEAR_COLUMN:
case VIS_TYPE_YEAR_OVER_YEAR_LINE:
case VIS_TYPE_SINGLE_VALUE:
cy.get(highchartsTitleEl)
.should('be.visible')
.and('contain', itemName)
break
case VIS_TYPE_SINGLE_VALUE:
cy.getBySel(visualizationTitleEl).should('contain', itemName)
break
case VIS_TYPE_PIVOT_TABLE:
cy.getBySel('visualization-column-header')
.contains(itemName)
Expand Down Expand Up @@ -119,10 +111,7 @@ export const expectChartItemsToHaveLength = (length) =>
cy.get(highchartsChartItemEl).children().should('have.length', length)

export const expectSVTitleToHaveColor = (color) =>
cy.getBySel(visualizationTitleEl).invoke('attr', 'fill').should('eq', color)
cy.get('text.highcharts-title').should('have.css', 'color', color)

export const expectSVSubtitleToHaveColor = (color) =>
cy
.getBySel(visualizationSubtitleEl)
.invoke('attr', 'fill')
.should('eq', color)
cy.get('text.highcharts-subtitle').should('have.css', 'color', color)
1 change: 0 additions & 1 deletion cypress/elements/optionsModal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export {
expectLegendDisplayStyleToBeText,
expectLegendDisplayStyleToBeFill,
expectSingleValueToHaveTextColor,
expectSingleValueToNotHaveBackgroundColor,
expectSingleValueToHaveBackgroundColor,
toggleLegendKeyOption,
expectLegendKeyOptionToBeEnabled,
Expand Down
11 changes: 2 additions & 9 deletions cypress/elements/optionsModal/legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const legendKeyContainerEl = 'legend-key-container'
const legendKeyItemEl = 'legend-key-item'
const singleValueTextEl = 'visualization-primary-value'
const singleValueIconEl = 'visualization-icon'
const singleValueOutputEl = 'visualization-container'
const legendDisplayStrategyByDataItemEl = 'legend-display-strategy-BY_DATA_ITEM'
const legendDisplayStrategyFixedEl = 'legend-display-strategy-FIXED'
const legendDisplayStyleOptionTextEl = 'legend-display-style-option-TEXT'
Expand Down Expand Up @@ -76,16 +75,10 @@ export const expectFixedLegendSetToBe = (legendSetName) =>
cy.getBySel(fixedLegendSetSelectEl).should('contain', legendSetName)

export const expectSingleValueToHaveTextColor = (color) =>
cy.getBySel(singleValueTextEl).invoke('attr', 'fill').should('eq', color)

export const expectSingleValueToNotHaveBackgroundColor = () =>
cy.getBySel(singleValueOutputEl).should('not.have.attr', 'style')
cy.getBySel(singleValueTextEl).should('have.css', 'color', color)

export const expectSingleValueToHaveBackgroundColor = (color) =>
cy
.getBySel(singleValueOutputEl)
.invoke('attr', 'style')
.should('contain', `background-color: ${color}`)
cy.get('rect.highcharts-background').should('have.attr', 'fill', color)

export const expectSingleValueToHaveIconColor = (color) =>
cy
Expand Down
49 changes: 25 additions & 24 deletions cypress/integration/options/legend.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import {
setItemToType,
clickOptionsModalHideButton,
expectSingleValueToHaveBackgroundColor,
expectSingleValueToNotHaveBackgroundColor,
changeDisplayStyleToFill,
changeColor,
OPTIONS_TAB_STYLE,
Expand Down Expand Up @@ -156,17 +155,19 @@ describe('Options - Legend', () => {

it('applies different styles of legend to a Single Value chart', () => {
const TEST_ITEM = TEST_ITEMS[0]
const EXPECTED_STANDARD_TEXT_COLOR = '#212934'
const EXPECTED_CONTRAST_TEXT_COLOR = '#ffffff'
const EXPECTED_STANDARD_TEXT_COLOR = 'rgb(33, 41, 52)'
const EXPECTED_CONTRAST_TEXT_COLOR = 'rgb(255, 255, 255)'
const EXPECTED_BACKGROUND_COLOR_1 = '#FFFFB2'
const EXPECTED_TEXT_COLOR_1 = '#FFFFB2'
const EXPECTED_TEXT_COLOR_1 = 'rgb(255, 255, 178)'
const EXPECTED_BACKGROUND_COLOR_2 = '#B3402B'
const EXPECTED_TEXT_COLOR_2 = '#B3402B'
const EXPECTED_TEXT_COLOR_2 = 'rgb(179, 64, 43)'
const EXPECTED_CUSTOM_TITLE_COLOR = '#ff7700'
const EXPECTED_CUSTOM_TITLE_COLOR_RGB = 'rgb(255, 119, 0)'
const EXPECTED_CUSTOM_SUBTITLE_COLOR = '#ffaa00'
const EXPECTED_CUSTOM_SUBTITLE_COLOR_RGB = 'rgb(255, 170, 0)'
const TEST_LEGEND_SET_WITH_CONTRAST = 'Age 15y interval'
const EXPECTED_STANDARD_TITLE_COLOR = '#212934'
const EXPECTED_STANDARD_SUBTITLE_COLOR = '#4a5768'
const EXPECTED_STANDARD_TITLE_COLOR = 'rgb(33, 41, 52)'
const EXPECTED_STANDARD_SUBTITLE_COLOR = 'rgb(74, 87, 104)'

cy.log('navigates to the start page and adds data items')
goToStartPage()
Expand All @@ -176,7 +177,7 @@ describe('Options - Legend', () => {
clickDimensionModalUpdateButton()
expectVisualizationToBeVisible(VIS_TYPE_SINGLE_VALUE)
expectSingleValueToHaveTextColor(EXPECTED_STANDARD_TEXT_COLOR)
expectSingleValueToNotHaveBackgroundColor()
expectSingleValueToHaveBackgroundColor('transparent')

cy.log('enables legend')
openOptionsModal(OPTIONS_TAB_LEGEND)
Expand Down Expand Up @@ -206,7 +207,7 @@ describe('Options - Legend', () => {
// Legend on text, no contrast, no custom title colors
cy.log('verifies text color legend is applied')
expectSingleValueToHaveTextColor(EXPECTED_TEXT_COLOR_1)
expectSingleValueToNotHaveBackgroundColor()
expectSingleValueToHaveBackgroundColor('transparent')
expectSVTitleToHaveColor(EXPECTED_STANDARD_TITLE_COLOR)
expectSVSubtitleToHaveColor(EXPECTED_STANDARD_SUBTITLE_COLOR)

Expand All @@ -225,11 +226,11 @@ describe('Options - Legend', () => {
// Legend on text, with contrast (N/, no custom title colors
cy.log('verifies text color legend is applied')
expectSingleValueToHaveTextColor(EXPECTED_TEXT_COLOR_2)
expectSingleValueToNotHaveBackgroundColor()
expectSingleValueToHaveBackgroundColor('transparent')
expectSVTitleToHaveColor(EXPECTED_STANDARD_TITLE_COLOR)
expectSVSubtitleToHaveColor(EXPECTED_STANDARD_SUBTITLE_COLOR)

cy.log('changees legend display style to background color')
cy.log('changes legend display style to background color')
openOptionsModal(OPTIONS_TAB_LEGEND)
expectLegendDisplayStrategyToBeFixed()
expectLegendDisplayStyleToBeText()
Expand Down Expand Up @@ -259,8 +260,8 @@ describe('Options - Legend', () => {
)
expectSingleValueToHaveTextColor(EXPECTED_CONTRAST_TEXT_COLOR)
expectSingleValueToHaveBackgroundColor(EXPECTED_BACKGROUND_COLOR_2)
expectSVTitleToHaveColor(EXPECTED_CUSTOM_TITLE_COLOR)
expectSVSubtitleToHaveColor(EXPECTED_CUSTOM_SUBTITLE_COLOR)
expectSVTitleToHaveColor(EXPECTED_CUSTOM_TITLE_COLOR_RGB)
expectSVSubtitleToHaveColor(EXPECTED_CUSTOM_SUBTITLE_COLOR_RGB)

cy.log('changes legend display style to text color')
openOptionsModal(OPTIONS_TAB_LEGEND)
Expand All @@ -271,12 +272,12 @@ describe('Options - Legend', () => {
clickOptionsModalUpdateButton()
expectVisualizationToBeVisible(VIS_TYPE_SINGLE_VALUE)

// Legend on text, with contrast, with custom title colo
// Legend on text, with contrast, with custom title colors
cy.log('verifies text color legend and custom title colors are applied')
expectSingleValueToHaveTextColor(EXPECTED_TEXT_COLOR_2)
expectSingleValueToNotHaveBackgroundColor()
expectSVTitleToHaveColor(EXPECTED_CUSTOM_TITLE_COLOR)
expectSVSubtitleToHaveColor(EXPECTED_CUSTOM_SUBTITLE_COLOR)
expectSingleValueToHaveBackgroundColor('transparent')
expectSVTitleToHaveColor(EXPECTED_CUSTOM_TITLE_COLOR_RGB)
expectSVSubtitleToHaveColor(EXPECTED_CUSTOM_SUBTITLE_COLOR_RGB)

cy.log('changes legend display strategy to by data item')
openOptionsModal(OPTIONS_TAB_LEGEND)
Expand All @@ -287,12 +288,12 @@ describe('Options - Legend', () => {
clickOptionsModalUpdateButton()
expectVisualizationToBeVisible(VIS_TYPE_SINGLE_VALUE)

// Legend on text, no contrast, with custom title colo
// Legend on text, no contrast, with custom title colors
cy.log('verifies text color legend and custom title colors are applied')
expectSingleValueToHaveTextColor(EXPECTED_TEXT_COLOR_1)
expectSingleValueToNotHaveBackgroundColor()
expectSVTitleToHaveColor(EXPECTED_CUSTOM_TITLE_COLOR)
expectSVSubtitleToHaveColor(EXPECTED_CUSTOM_SUBTITLE_COLOR)
expectSingleValueToHaveBackgroundColor('transparent')
expectSVTitleToHaveColor(EXPECTED_CUSTOM_TITLE_COLOR_RGB)
expectSVSubtitleToHaveColor(EXPECTED_CUSTOM_SUBTITLE_COLOR_RGB)

cy.log('changes legend display style to background color')
openOptionsModal(OPTIONS_TAB_LEGEND)
Expand All @@ -309,8 +310,8 @@ describe('Options - Legend', () => {
)
expectSingleValueToHaveTextColor(EXPECTED_STANDARD_TEXT_COLOR)
expectSingleValueToHaveBackgroundColor(EXPECTED_BACKGROUND_COLOR_1)
expectSVTitleToHaveColor(EXPECTED_CUSTOM_TITLE_COLOR)
expectSVSubtitleToHaveColor(EXPECTED_CUSTOM_SUBTITLE_COLOR)
expectSVTitleToHaveColor(EXPECTED_CUSTOM_TITLE_COLOR_RGB)
expectSVSubtitleToHaveColor(EXPECTED_CUSTOM_SUBTITLE_COLOR_RGB)

cy.log('verifies legend key is hidden')
expectLegendKeyToBeHidden()
Expand Down Expand Up @@ -605,7 +606,7 @@ describe('Options - Legend', () => {
const TEST_ITEM = TEST_ITEMS[0]
const EXPECTED_FIXED_COLOR = '#c7e9c0'
const valueCellEl = 'visualization-value-cell'
const EXPECTED_SV_STANDARD_TEXT_COLOR = '#212934'
const EXPECTED_SV_STANDARD_TEXT_COLOR = 'rgb(33, 41, 52)'
const EXPECTED_PT_STANDARD_TEXT_COLOR = 'color: rgb(33, 41, 52)'

cy.log('navigates to the start page and adds data items')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"typescript": "^4.8.4"
},
"dependencies": {
"@dhis2/analytics": "^26.8.7",
"@dhis2/analytics": "^26.9.0",
"@dhis2/app-runtime": "^3.10.4",
"@dhis2/app-runtime-adapter-d2": "^1.1.0",
"@dhis2/app-service-datastore": "^1.0.0-beta.3",
Expand Down
20 changes: 8 additions & 12 deletions src/components/VisualizationPlugin/ChartPlugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isSingleValue, createVisualization } from '@dhis2/analytics'
import { createVisualization } from '@dhis2/analytics'
import PropTypes from 'prop-types'
import React, { useRef, useCallback, useEffect } from 'react'

Expand Down Expand Up @@ -31,19 +31,15 @@ const ChartPlugin = ({
},
undefined,
undefined,
isSingleValue(visualization.type) ? 'dhis' : 'highcharts' // output format
'highcharts' // output format
)

if (isSingleValue(visualization.type)) {
onChartGenerated(visualizationConfig.visualization)
} else {
onChartGenerated(
visualizationConfig.visualization.getSVGForExport({
sourceHeight: 768,
sourceWidth: 1024,
})
)
}
onChartGenerated(
visualizationConfig.visualization.getSVGForExport({
sourceHeight: 768,
sourceWidth: 1024,
})
)
},
[
canvasRef,
Expand Down
67 changes: 0 additions & 67 deletions src/components/VisualizationPlugin/__tests__/ChartPlugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,13 @@ import ChartPlugin from '../ChartPlugin.js'

jest.mock('@dhis2/analytics')

const dxMock = {
dimension: 'dx',
items: [
{
id: 'Uvn6LCg7dVU',
},
],
}

const peMock = {
dimension: 'pe',
items: [
{
id: 'LAST_12_MONTHS',
},
],
}

const ouMock = {
dimension: 'ou',
items: [
{
id: 'ImspTQPwCqd',
},
],
}

const mockExtraOptions = {
dashboard: false,
noData: {
text: 'No data',
},
}

const singleValueCurrentMock = {
type: analytics.VIS_TYPE_SINGLE_VALUE,
columns: [dxMock],
rows: [],
filters: [ouMock, peMock],
}

const metaDataMock = {
items: {
a: { name: 'a dim' },
Expand Down Expand Up @@ -79,10 +45,6 @@ const createVisualizationMock = {
},
}

const isSingleValueMockResponse = (visType) => {
return visType === analytics.VIS_TYPE_SINGLE_VALUE
}

describe('ChartPlugin', () => {
// eslint-disable-next-line no-import-assign, import/namespace
options.getOptionsForRequest = () => [
Expand Down Expand Up @@ -153,34 +115,5 @@ describe('ChartPlugin', () => {
done()
})
})

describe('Single value visualization', () => {
beforeEach(() => {
props.visualization = {
...singleValueCurrentMock,
}

// eslint-disable-next-line no-import-assign, import/namespace
analytics.isSingleValue = jest
.fn()
.mockReturnValue(
isSingleValueMockResponse(props.visualization.type)
)
})

it('provides dhis as output format to createChart', (done) => {
canvas()

setTimeout(() => {
expect(analytics.createVisualization).toHaveBeenCalled()

expect(
analytics.createVisualization.mock.calls[0][6]
).toEqual('dhis')

done()
})
})
})
})
})
Loading
Loading