Skip to content

Commit

Permalink
Refactored with Legend component & added test
Browse files Browse the repository at this point in the history
  • Loading branch information
graphieros committed Dec 15, 2023
1 parent a7fc0a9 commit 09e7b4d
Show file tree
Hide file tree
Showing 21 changed files with 608 additions and 579 deletions.
108 changes: 108 additions & 0 deletions cypress/fixtures/rings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"dataset": [
{
"name": "serie 1",
"color": "#3366cc",
"values": [
120,120
]
},
{
"name": "serie 2",
"color": "#dc3912",
"values": [
60,60
]
},
{
"name": "serie 3",
"color": "#ff9900",
"values": [
15,15
]
},
{
"name": "serie 4",
"color": "#109618",
"values": [
30,30
]
}
],
"config": {
"useCssAnimation": true,
"useBlurOnHover": true,
"style": {
"fontFamily": "inherit",
"chart": {
"backgroundColor":"#FFFFFF",
"color":"#2D353C",
"layout": {
"rings": {
"strokeWidth": 2,
"stroke":"#FFFFFF",
"gradient": {
"show": true,
"intensity": 40,
"underlayerColor":"#FFFFFF"
},
"useShadow": true
}
},
"legend": {
"backgroundColor": "#FFFFFF",
"color": "#2D353C",
"show": true,
"fontSize": 16,
"bold": false,
"roundingValue": 0,
"roundingPercentage": 0
},
"title": {
"text": "Title",
"color": "#2D353C",
"fontSize": 20,
"bold": true,
"subtitle": {
"color": "#A1A1A1",
"text": "Subtitle",
"fontSize": 16,
"bold": false
}
},
"tooltip": {
"show": true,
"color": "#2D353C",
"backgroundColor": "#FFFFFF",
"fontSize": 14,
"showValue": true,
"showPercentage": true,
"roundingValue": 0,
"roundingPercentage": 0
}
}
},
"userOptions": {
"show": true,
"title": "options",
"labels": {
"showTable": "Show table"
}
},
"table": {
"show": false,
"th": {
"backgroundColor": "#FAFAFA",
"color": "#2D353C",
"outline": "1px solid #e1e5e8"
},
"td": {
"backgroundColor": "#FFFFFF",
"color": "#2D353C",
"outline": "1px solid #e1e5e8",
"roundingValue": 0,
"roundingPercentage": 0
}
}
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vue-data-ui",
"private": false,
"version": "1.9.21",
"version": "1.9.22",
"type": "module",
"description": "A user-empowering data visualization Vue components library",
"keywords": [
Expand Down
66 changes: 66 additions & 0 deletions src/atoms/Legend.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<script setup>
import Shape from './Shape.vue';
const props = defineProps({
legendSet: {
type: Array,
default() {
return []
}
},
config: {
type: Object,
default() {
return {}
}
}
})
const emit = defineEmits(['clickMarker'])
function handleClick(legend, i) {
emit('clickMarker', { legend, i})
}
</script>

<template>
<div
:data-cy="config.cy"
class="vue-data-ui-legend"
:style="`background:${config.backgroundColor};font-size:${config.fontSize}px;color:${config.color};padding-bottom:${config.paddingBottom}px;font-weight:${config.fontWeight}`"
>
<div v-for="(legend, i) in legendSet" class="vue-data-ui-legend-item">
<svg @click="handleClick(legend, i)" v-if="legend.shape" :height="config.fontSize" :width="config.fontSize" viewBox="0 0 20 20" :style="`overflow: visible;opacity:${legend.opacity}`">
<Shape
:shape="legend.shape"
:radius="9"
stroke="none"
:plot="{ x: 10, y: 10 }"
:fill="legend.color"
/>
</svg>
<slot name="item" :legend="legend" :index="i"/>
</div>

</div>
</template>

<style scoped>
.vue-data-ui-legend {
height: 100%;
width:100%;
display: flex;
align-items:center;
flex-wrap: wrap;
justify-content:center;
column-gap: 18px;
}
.vue-data-ui-legend-item {
display: flex;
align-items:center;
gap: 6px;
cursor: pointer;
height: 24px;
}
</style>
97 changes: 3 additions & 94 deletions src/components/vue-ui-donut.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,10 @@ describe('<VueUiDonut />', () => {
const grandTotal = fixture.dataset.map((d) => d.values.reduce((a, b) => a + b, 0)).reduce((a, b) => a + b, 0);

for (let i = 0; i < fixture.dataset.length; i += 1) {
cy.get(`[data-cy="donut-div-legend-marker-${i}"]`)
.should('exist')
.invoke('attr', 'fill')
.should('eq', sortedDataset[i].color);

cy.get(`[data-cy="donut-div-legend-name-${i}"]`)
.should('exist')
.contains(sortedDataset[i].name);

const thisValue = sortedDataset[i].values.reduce((a, b) => a + b, 0);

cy.get(`[data-cy="donut-div-legend-value-${i}"]`)
.should('exist')
.contains(Number(thisValue.toFixed(fixture.config.style.chart.legend.roundingValue)).toLocaleString());

const thisPercentage = `(${(thisValue / grandTotal * 100).toFixed(fixture.config.style.chart.legend.roundingPercentage)}%)`;

cy.get(`[data-cy="donut-div-legend-percentage-${i}"]`)
.should('exist')
.contains(thisPercentage);
cy.contains(sortedDataset[i].name);
cy.contains(Number(thisValue.toFixed(fixture.config.style.chart.legend.roundingValue)).toLocaleString());
cy.contains(`(${(thisValue / grandTotal * 100).toFixed(fixture.config.style.chart.legend.roundingPercentage)}%)`)
}

let modifiedConfig = {
Expand All @@ -88,40 +72,6 @@ describe('<VueUiDonut />', () => {
}
});


cy.get('[data-cy="donut-text-title"]')
.should('exist')
.contains(fixture.config.style.chart.title.text);

cy.get('[data-cy="donut-text-subtitle"]')
.should('exist')
.contains(fixture.config.style.chart.title.subtitle.text);

cy.get('[data-cy="donut-foreignObject-legend"]').should('exist');

for (let i = 0; i < fixture.dataset.length; i += 1) {
cy.get(`[data-cy="donut-foreignObject-legend-marker-${i}"]`)
.should('exist')
.invoke('attr', 'fill')
.should('eq', sortedDataset[i].color);

cy.get(`[data-cy="donut-foreignObject-legend-name-${i}"]`)
.should('exist')
.contains(sortedDataset[i].name);

const thisValue = sortedDataset[i].values.reduce((a, b) => a + b, 0);

cy.get(`[data-cy="donut-foreignObject-legend-value-${i}"]`)
.should('exist')
.contains(Number(thisValue.toFixed(fixture.config.style.chart.legend.roundingValue)).toLocaleString());

const thisPercentage = `(${(thisValue / grandTotal * 100).toFixed(fixture.config.style.chart.legend.roundingPercentage)}%)`;

cy.get(`[data-cy="donut-foreignObject-legend-percentage-${i}"]`)
.should('exist')
.contains(thisPercentage);
}

for (let i = 0; i < fixture.dataset.length; i += 1) {
cy.get(`[data-cy="donut-arc-${i}"]`)
.should('exist')
Expand Down Expand Up @@ -153,47 +103,6 @@ describe('<VueUiDonut />', () => {
})
}

cy.get(`[data-cy="donut-trap-0"]`)
.then(($element) => {

cy.wrap($element)
.trigger('mouseenter', { force: true })

cy.get(`[data-cy="tooltip"]`).should('exist');
cy.get(`[data-cy="donut-tooltip-name"]`)
.should('exist')
.contains(sortedDataset[0].name);

cy.get(`[data-cy="donut-tooltip-marker"]`)
.should('exist')
.invoke('attr', 'fill')
.should('eq', sortedDataset[0].color);

const expectedTooltipValue = sortedDataset[0].values.reduce((a, b) => a + b, 0).toFixed(fixture.config.style.chart.tooltip.roundingValue);

cy.get(`[data-cy="donut-tooltip-value"]`)
.should('exist')
.contains(expectedTooltipValue);

cy.wrap($element)
.invoke('attr', 'stroke')
.should('eq', 'rgba(0,0,0,0.1)');

cy.wrap($element)
.click({ force: true });

cy.get('.vue-ui-donut-arc-path').should(($arcs) => {
expect($arcs).to.have.length(3)
})

cy.get(`[data-cy="donut-foreignObject-legend-item-0"]`)
.click();

cy.get('.vue-ui-donut-arc-path').should(($arcs) => {
expect($arcs).to.have.length(4)
})
});

for (let i = 0; i < fixture.dataset.length; i += 1) {
const thisValue = sortedDataset[i].values.reduce((a, b) => a + b, 0);
const thisPercentage = `${(thisValue / grandTotal * 100).toFixed(fixture.config.style.chart.legend.roundingPercentage)}%`;
Expand Down
Loading

0 comments on commit 09e7b4d

Please sign in to comment.