Skip to content

Commit 63ee20d

Browse files
committed
feat: Improve negative value support
1 parent 143e3ce commit 63ee20d

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

example/src/App.tsx

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const chartColors: ChartProps['colors'] = {
2020
const generateDataPoints = ({
2121
startingValue = 400,
2222
minimum = 0,
23+
maximum = 3000,
2324
radomFactor = 20,
2425
} = {}) => {
2526
const points = []
@@ -31,9 +32,10 @@ const generateDataPoints = ({
3132
const randomVariation = (Math.random() - 0.5) * radomFactor
3233
value += randomVariation
3334

34-
// randomVariation was negative and value went below minimum
35-
if (value < minimum) {
36-
// invert direction to keep above minimum
35+
// either randomVariation was negative and value went below minimum
36+
// or randomVariation was positive and value went above maximum
37+
if (value < minimum || value > maximum) {
38+
// invert direction to keep within bounds
3739
value -= 2 * randomVariation
3840
}
3941

@@ -44,28 +46,36 @@ const generateDataPoints = ({
4446
}
4547

4648
enum Measurement {
47-
Red = 'Red',
49+
Temperature = 'Temperature',
4850
Blue = 'Blue',
4951
Green = 'Green',
5052
Pink = 'Pink',
5153
}
5254
const measurementKeys = Object.values(Measurement)
5355
const measurementsRecords: Record<Measurement, Dataset> = {
54-
[Measurement.Red]: {
56+
[Measurement.Temperature]: {
5557
unit: '°C',
56-
points: generateDataPoints(),
58+
points: generateDataPoints({
59+
maximum: 40,
60+
minimum: -10,
61+
radomFactor: 1,
62+
startingValue: -8,
63+
}),
5764
decimals: 0,
58-
areaColor: '#e78e96',
65+
areaColor: '#ff00ff', // '#83cba8',
5966
color: {
6067
type: 'thresholds',
61-
baseColor: '#CF1E2E',
62-
gradientBlur: 50,
68+
baseColor: '#3d91ff',
69+
gradientBlur: 2,
6370
thresholds: [
64-
{ value: 800, color: '#089851' },
65-
{ value: 400, color: '#F29400' },
71+
{ value: 32, color: '#bb2222' },
72+
{ value: 24, color: '#ffc400' },
73+
{ value: 16, color: '#089851' },
74+
{ value: 10, color: '#9ceeff' },
75+
{ value: 0, color: '#00d5ff' },
6676
],
6777
},
68-
measurementName: Measurement.Red,
78+
measurementName: Measurement.Temperature,
6979
},
7080
[Measurement.Blue]: {
7181
unit: 'l',
@@ -123,7 +133,7 @@ export default function App() {
123133
}, [timeDomainType])
124134

125135
const [enabledMeasurements, setEnabledMeasurements] = useState<Measurement[]>(
126-
[Measurement.Red]
136+
[Measurement.Temperature]
127137
)
128138

129139
const datasets = useMemo<Dataset[]>(

src/drawFunction.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,10 @@ function getTickCount(domain, { minDeltaY }) {
222222
return tickCount
223223
}
224224
225+
// TODO: Check why was avoiding needed. Condionally maybe?
226+
const avoidZeroAxisLabel = false
225227
function yFormat(value) {
226-
if (value === 0) return ''
228+
if (avoidZeroAxisLabel && value === 0) return ''
227229
const axisLabel = String(value)
228230
return axisLabel.length > yLabelMaxLength ? '' : axisLabel
229231
}
@@ -454,7 +456,7 @@ window.draw = (props) => {
454456
.area()
455457
.defined(isDefined)
456458
.x((d) => x(d.date))
457-
.y0(y(0))
459+
.y0(y(y.domain()[0]) + 1) // Use min value, +1 to avoid blinking space
458460
.y1((d) => y(d.value))
459461
)
460462
@@ -661,7 +663,7 @@ window.draw = (props) => {
661663
.area()
662664
.defined(isDefined)
663665
.x((d) => x(d.date))
664-
.y0(y(0))
666+
.y0(y(y.domain()[0]) + 1) // Use min value, +1 to avoid blinking space
665667
.y1((d) => y(d.value))
666668
)
667669
@@ -734,7 +736,7 @@ window.draw = (props) => {
734736
.area()
735737
.defined(isDefined)
736738
.x((d) => newX(d.date))
737-
.y0(y(0))
739+
.y0(y(y.domain()[0]) + 1) // Use min value, +1 to avoid blinking space
738740
.y1((d) => y(d.value))
739741
)
740742
})

0 commit comments

Comments
 (0)