Skip to content

Commit

Permalink
fix(bar): get working with scales updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wyze committed Jun 22, 2021
1 parent cdd88ef commit 48880b4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
8 changes: 4 additions & 4 deletions packages/bar/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import {
import { AxisProps, GridValues } from '@nivo/axes'
import { OrdinalColorScaleConfig, InheritedColorConfig } from '@nivo/colors'
import { LegendProps } from '@nivo/legends'
import { Scale, BandScale } from '@nivo/scales'
import { ScaleSpec, ScaleBandSpec } from '@nivo/scales'

declare module '@nivo/bar' {
export type Value = string | number

export interface Data {
data: object[]
data: Array<Record<string, unknown>>
}

export interface BarDatum {
Expand Down Expand Up @@ -99,8 +99,8 @@ declare module '@nivo/bar' {
maxValue: number | 'auto'
padding: number

valueScale: Scale
indexScale: BandScale
valueScale: ScaleSpec
indexScale: ScaleBandSpec

axisBottom: AxisProps | null
axisLeft: AxisProps | null
Expand Down
18 changes: 10 additions & 8 deletions packages/bar/src/compute/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { scaleBand } from 'd3-scale'
import { computeScale } from '@nivo/scales'

/**
* Generates indexed scale.
*
* @param {Array.<Object>} data
* @param {Function} getIndex
* @param {Array.<number>} range
* @param {number} padding
* @Param {scalePropType} indexScale
* @Param {number} size
* @Param {'x' | 'y'} axis
* @returns {Function}
*/
export const getIndexScale = (data, getIndex, range, padding, indexScale) => {
return scaleBand()
.domain(data.map(getIndex))
.range(range)
.round(Boolean(indexScale.round))
.padding(padding)
export const getIndexScale = (data, getIndex, padding, indexScale, size, axis) => {
return computeScale(
indexScale,
{ all: data.map(getIndex), min: 0, max: 0 },
size,
axis
).padding(padding)
}

export const normalizeData = (data, keys) =>
Expand Down
19 changes: 15 additions & 4 deletions packages/bar/src/compute/grouped.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,17 @@ export const generateGroupedBars = ({
}) => {
const keys = props.keys.filter(key => !hiddenIds.includes(key))
const data = normalizeData(props.data, keys)
const [axis, range] = layout === 'vertical' ? ['y', [0, width]] : ['x', [height, 0]]
const indexScale = getIndexScale(data, props.getIndex, range, padding, indexScaleConfig)
const [axis, otherAxis, size] = layout === 'vertical' ? ['y', 'x', width] : ['x', 'y', height]
const indexScale = getIndexScale(
data,
props.getIndex,
padding,
indexScaleConfig,
size,
otherAxis
)

const scaleSpec = {
axis,
max: maxValue,
min: minValue,
reverse,
Expand All @@ -172,7 +178,12 @@ export const generateGroupedBars = ({
const min = clampMin(Math.min(...values))
const max = zeroIfNotFinite(Math.max(...values))

const scale = computeScale(scaleSpec, { [axis]: { min, max } }, width, height)
const scale = computeScale(
scaleSpec,
{ all: values, min, max },
axis === 'x' ? width : height,
axis
)

const [xScale, yScale] = layout === 'vertical' ? [indexScale, scale] : [scale, indexScale]

Expand Down
19 changes: 15 additions & 4 deletions packages/bar/src/compute/stacked.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,17 @@ export const generateStackedBars = ({
const keys = props.keys.filter(key => !hiddenIds.includes(key))
const stackedData = stack().keys(keys).offset(stackOffsetDiverging)(normalizeData(data, keys))

const [axis, range] = layout === 'vertical' ? ['y', [0, width]] : ['x', [height, 0]]
const indexScale = getIndexScale(data, props.getIndex, range, padding, indexScaleConfig)
const [axis, otherAxis, size] = layout === 'vertical' ? ['y', 'x', width] : ['x', 'y', height]
const indexScale = getIndexScale(
data,
props.getIndex,
padding,
indexScaleConfig,
size,
otherAxis
)

const scaleSpec = {
axis,
max: maxValue,
min: minValue,
reverse,
Expand All @@ -169,7 +175,12 @@ export const generateStackedBars = ({
const min = Math.min(...values)
const max = Math.max(...values)

const scale = computeScale(scaleSpec, { [axis]: { min, max } }, width, height)
const scale = computeScale(
scaleSpec,
{ all: values, min, max },
axis === 'x' ? width : height,
axis
)

const [xScale, yScale] = layout === 'vertical' ? [indexScale, scale] : [scale, indexScale]

Expand Down

0 comments on commit 48880b4

Please sign in to comment.