Skip to content

Commit 32b59d5

Browse files
author
Marco
committed
Various chart axis fixes for CD
Accounts for the point when a Creative Different chart has only one data point. In this case it expands the domain 1 month before and after the single data point so it renders the bar and label in the center. It also expands the stroke width of the data so it' displays as a bar. Also fixed a bug with the ovelapping data points due to a feature we're not using yet. The feature allows CD to mark data points as `prioritized` but it was causing other problems and is not in use so I removed it for now.
1 parent e35fa74 commit 32b59d5

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ yarn-error.log*
3030
.yarn-integrity
3131

3232
build/
33+
dist/
3334

3435
# redis dump
3536
dump.rdb

app/javascript/ui/global/charts/AreaChart.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import {
1313
themeLabelStyles,
1414
} from '~/ui/global/charts/ChartUtils'
1515

16-
const chartStyle = (style, order) => {
16+
const chartStyle = (style, order, singleDataPoint) => {
1717
if (style.fill) {
1818
const darkFill = darkenColor(style.fill, order)
1919
const opacity = 0.8
20+
const strokeWidth = singleDataPoint ? 10 : null
2021
return {
21-
data: { fill: darkFill, opacity },
22+
data: { fill: darkFill, opacity, strokeWidth },
2223
labels: {
2324
fontSize: 18,
2425
},
@@ -87,10 +88,10 @@ const AreaChart = ({
8788
measure,
8889
})
8990
}
90-
91+
const singleDataPoint = values.length === 2 && values[1].isDuplicate
9192
return (
9293
<VictoryArea
93-
style={chartStyle(dataset.style || {}, colorOrder)}
94+
style={chartStyle(dataset.style || {}, colorOrder, singleDataPoint)}
9495
labels={d => d.value}
9596
labelComponent={
9697
<TickLabelWithTooltip

app/javascript/ui/global/charts/ChartGroup.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
victoryTheme,
2424
emojiSeriesForQuestionType,
2525
chartDomainForDatasetValues,
26+
domainXForSingleValue,
2627
formatValuesForVictory,
2728
} from '~/ui/global/charts/ChartUtils'
2829

@@ -95,10 +96,14 @@ class ChartGroup extends React.Component {
9596
)
9697
)
9798
})
98-
return chartDomainForDatasetValues({
99+
const domain = chartDomainForDatasetValues({
99100
values: allValues,
100101
maxYDomain: this.primaryDataset.max_domain,
101102
})
103+
if (allValues.length === 1) {
104+
domain.x = domainXForSingleValue(allValues[0].date)
105+
}
106+
return domain
102107
}
103108

104109
get isSmallChartStyle() {
@@ -178,6 +183,7 @@ class ChartGroup extends React.Component {
178183

179184
calculateLabelWidth(label) {
180185
const modifier = this.isSmallChartStyle ? 12 : 8
186+
if (!label.text) return 0
181187
return label.text.length * modifier
182188
}
183189

@@ -198,6 +204,7 @@ class ChartGroup extends React.Component {
198204
sortedLabels = sortedLabels.slice(1)
199205
}
200206
const overlappingLabels = []
207+
if (sortedLabels.length === 1) return []
201208
sortedLabels.forEach((label, i) => {
202209
let overlapping = false
203210
for (let j = i + 1; j < sortedLabels.length; j++) {
@@ -215,6 +222,9 @@ class ChartGroup extends React.Component {
215222
overlappingLabels.push(label)
216223
label.overlapping = true
217224
continue
225+
} else {
226+
overlappingLabels.push(subLabel)
227+
subLabel.overlapping = true
218228
}
219229
}
220230
}
@@ -239,18 +249,10 @@ class ChartGroup extends React.Component {
239249

240250
const nonPrioritizedLabels = []
241251
overlappingLabels.forEach(l => {
242-
const datum = this.primaryDatasetValues.find(dv =>
243-
_.isEqual(dv.date, l.datum)
244-
)
245-
if (datum) {
246-
if (datum.prioritized) {
247-
datum.overlappingLabel = true
248-
} else {
249-
nonPrioritizedLabels.push(l)
250-
}
251-
}
252+
nonPrioritizedLabels.push(l)
252253
})
253254

255+
console.log('here', nonPrioritizedLabels.map(l => l.x))
254256
return _.uniq(_.xorWith(renderedLabels, nonPrioritizedLabels, _.isEqual))
255257
}
256258

app/javascript/ui/global/charts/ChartUtils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ export const primaryFillColorFromDataset = dataset => {
6060
return dataset.style && dataset.style.fill ? dataset.style.fill : '#000000'
6161
}
6262

63+
export const domainXForSingleValue = date => {
64+
return [
65+
moment(date)
66+
.subtract('months', 1)
67+
.toDate(),
68+
moment(date)
69+
.add('months', 1)
70+
.toDate(),
71+
]
72+
}
73+
6374
export const chartDomainForDatasetValues = ({ values, maxYDomain }) => {
6475
if (values.length === 0) {
6576
return {

0 commit comments

Comments
 (0)