-
Notifications
You must be signed in to change notification settings - Fork 70
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
added stats charts in xy-chart #75
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4bf4d55
added stats charts in xy-chart
1f0321c
added test files and exposed computeStats
83f810a
fixed lint in demo
ff4097e
added renderTooltip support for boxplot
6fbc442
move box plot example to a separate file
beaa221
removed label props and used absolute import
3600eba
fixed typos
bdb21a6
added rendertooltip for violinplot and added disableMouseEvent prop
eaf230d
updated readme
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
270 changes: 270 additions & 0 deletions
270
packages/demo/examples/01-xy-chart/StatsSeriesExample.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
/* eslint react/prop-types: 0 */ | ||
import React from 'react'; | ||
|
||
import { | ||
XAxis, | ||
YAxis, | ||
BoxPlotSeries, | ||
ViolinPlotSeries, | ||
PatternLines, | ||
LinearGradient, | ||
theme, | ||
} from '@data-ui/xy-chart'; | ||
|
||
import ResponsiveXYChart from './ResponsiveXYChart'; | ||
|
||
import { statsData } from './data'; | ||
|
||
const { colors } = theme; | ||
|
||
function renderViolinPlotTooltip({ datum, color }) { | ||
const { x, y, binData } = datum; | ||
const label = x || y; | ||
return ( | ||
<div> | ||
<div> | ||
<strong style={{ color }}>{label}</strong> | ||
</div> | ||
<div> | ||
<strong style={{ color }}>Bin Number </strong> | ||
{binData.length} | ||
</div> | ||
</div> | ||
); | ||
} | ||
function renderBoxPlotTooltip({ datum, color }) { | ||
const { | ||
x, | ||
y, | ||
min, | ||
max, | ||
median, | ||
firstQuartile, | ||
thirdQuartile, | ||
outliers, | ||
} = datum; | ||
|
||
const label = x || y; | ||
return ( | ||
<div> | ||
<div> | ||
<strong style={{ color }}>{label}</strong> | ||
</div> | ||
<div> | ||
<strong style={{ color }}>Min </strong> | ||
{min && min.toFixed ? min.toFixed(2) : min} | ||
</div> | ||
<div> | ||
<strong style={{ color }}>Max </strong> | ||
{max && max.toFixed ? max.toFixed(2) : max} | ||
</div> | ||
<div> | ||
<strong style={{ color }}>Median </strong> | ||
{median && median.toFixed ? median.toFixed(2) : median} | ||
</div> | ||
<div> | ||
<strong style={{ color }}>First Quartile </strong> | ||
{firstQuartile && firstQuartile.toFixed ? firstQuartile.toFixed(2) : firstQuartile} | ||
</div> | ||
<div> | ||
<strong style={{ color }}>Third Quartile </strong> | ||
{thirdQuartile && thirdQuartile.toFixed ? thirdQuartile.toFixed(2) : thirdQuartile} | ||
</div> | ||
<div> | ||
<strong style={{ color }}>Outliers Number </strong> | ||
{outliers.length} | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export function SimpleBoxPlotSeriesExample() { | ||
const boxPlotData = statsData.map(s => s.boxPlot); | ||
const values = boxPlotData.reduce((r, e) => r.push(e.min, e.max, ...e.outliers) && r, []); | ||
const minYValue = Math.min(...values); | ||
const maxYValue = Math.max(...values); | ||
const yDomain = [minYValue - (0.1 * Math.abs(minYValue)), | ||
maxYValue + (0.1 * Math.abs(minYValue))]; | ||
return ( | ||
<ResponsiveXYChart | ||
theme={{}} | ||
ariaLabel="Required label" | ||
xScale={{ | ||
type: 'band', | ||
paddingInner: 0.15, | ||
paddingOuter: 0.3, | ||
}} | ||
yScale={{ type: 'linear', | ||
domain: yDomain, | ||
}} | ||
renderTooltip={renderBoxPlotTooltip} | ||
showYGrid | ||
> | ||
<LinearGradient | ||
id="aqua_lightaqua_gradient" | ||
from="#99e9f2" | ||
to="#c5f6fa" | ||
/> | ||
<YAxis numTicks={4} /> | ||
<BoxPlotSeries | ||
data={boxPlotData} | ||
fill="url(#aqua_lightaqua_gradient)" | ||
stroke="#22b8cf" | ||
strokeWidth={1.5} | ||
/> | ||
<XAxis /> | ||
</ResponsiveXYChart> | ||
); | ||
} | ||
|
||
export function SingleBoxPlotSeriesExample() { | ||
const singleStats = [statsData[0]]; | ||
const boxPlotData = singleStats.map((s) => { | ||
const { boxPlot } = s; | ||
const { x, ...rest } = boxPlot; | ||
return { | ||
y: x, | ||
...rest, | ||
}; | ||
}); | ||
const values = boxPlotData.reduce((r, e) => r.push(e.min, e.max, ...e.outliers) && r, []); | ||
const minXValue = Math.min(...values); | ||
const maxXValue = Math.max(...values); | ||
const xDomain = [minXValue - (0.1 * Math.abs(minXValue)), | ||
maxXValue + (0.1 * Math.abs(maxXValue))]; | ||
return ( | ||
<ResponsiveXYChart | ||
theme={{}} | ||
ariaLabel="Required label" | ||
yScale={{ | ||
type: 'band', | ||
paddingInner: 0.15, | ||
paddingOuter: 0.3, | ||
}} | ||
xScale={{ type: 'linear', | ||
domain: xDomain, | ||
}} | ||
renderTooltip={renderBoxPlotTooltip} | ||
showYGrid | ||
> | ||
<LinearGradient | ||
id="aqua_lightaqua_gradient" | ||
from="#99e9f2" | ||
to="#c5f6fa" | ||
/> | ||
<BoxPlotSeries | ||
data={boxPlotData} | ||
fill="url(#aqua_lightaqua_gradient)" | ||
stroke="#22b8cf" | ||
strokeWidth={1.5} | ||
horizontal | ||
/> | ||
<XAxis /> | ||
</ResponsiveXYChart> | ||
); | ||
} | ||
|
||
export function HorizontalBoxPlotViolinPlotSeriesExample() { | ||
const boxPlotData = statsData.map((s) => { | ||
const { boxPlot } = s; | ||
const { x, ...rest } = boxPlot; | ||
return { | ||
y: x, | ||
...rest, | ||
}; | ||
}); | ||
const violinData = statsData.map(s => ({ y: s.boxPlot.x, binData: s.binData })); | ||
const values = boxPlotData.reduce((r, e) => r.push(e.min, e.max, ...e.outliers) && r, []); | ||
const minXValue = Math.min(...values); | ||
const maxXValue = Math.max(...values); | ||
const xDomain = [minXValue - (0.1 * Math.abs(minXValue)), | ||
maxXValue + (0.1 * Math.abs(maxXValue))]; | ||
return ( | ||
<ResponsiveXYChart | ||
theme={{}} | ||
ariaLabel="Required label" | ||
yScale={{ | ||
type: 'band', | ||
paddingInner: 0.15, | ||
paddingOuter: 0.3, | ||
}} | ||
xScale={{ type: 'linear', | ||
domain: xDomain, | ||
}} | ||
renderTooltip={renderBoxPlotTooltip} | ||
showYGrid | ||
> | ||
<PatternLines | ||
id="vViolinLines" | ||
height={3} | ||
width={3} | ||
stroke="#ced4da" | ||
strokeWidth={1} | ||
fill="rgba(0,0,0,0.3)" | ||
/> | ||
<YAxis numTicks={4} /> | ||
<ViolinPlotSeries | ||
data={violinData} | ||
fill="url(#vViolinLines)" | ||
stroke="#22b8cf" | ||
strokeWidth={0.5} | ||
horizontal | ||
disableMouseEvents | ||
/> | ||
<BoxPlotSeries | ||
data={boxPlotData} | ||
fill={colors.categories[0]} | ||
stroke={colors.categories[0]} | ||
widthRatio={0.5} | ||
fillOpacity={0.2} | ||
strokeWidth={1} | ||
horizontal | ||
/> | ||
<XAxis /> | ||
</ResponsiveXYChart> | ||
); | ||
} | ||
|
||
export function ViolinPlotSeriesExample() { | ||
const boxPlotData = statsData.map(s => s.boxPlot); | ||
const violinData = statsData.map(s => ({ x: s.boxPlot.x, binData: s.binData })); | ||
const values = boxPlotData.reduce((r, e) => r.push(e.min, e.max, ...e.outliers) && r, []); | ||
const minYValue = Math.min(...values); | ||
const maxYValue = Math.max(...values); | ||
const yDomain = [minYValue - (0.1 * Math.abs(minYValue)), | ||
maxYValue + (0.1 * Math.abs(minYValue))]; | ||
return ( | ||
<ResponsiveXYChart | ||
theme={{}} | ||
ariaLabel="Required label" | ||
xScale={{ | ||
type: 'band', | ||
paddingInner: 0.15, | ||
paddingOuter: 0.3, | ||
}} | ||
yScale={{ type: 'linear', | ||
domain: yDomain, | ||
}} | ||
renderTooltip={renderViolinPlotTooltip} | ||
showYGrid | ||
> | ||
<PatternLines | ||
id="hViolinLines" | ||
height={3} | ||
width={3} | ||
stroke="#ced4da" | ||
strokeWidth={1} | ||
fill="rgba(0,0,0,0.3)" | ||
orientation={['horizontal']} | ||
/> | ||
<YAxis numTicks={4} /> | ||
<ViolinPlotSeries | ||
data={violinData} | ||
fill="url(#hViolinLines)" | ||
stroke="#22b8cf" | ||
strokeWidth={0.5} | ||
/> | ||
<XAxis /> | ||
</ResponsiveXYChart> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since this file is getting so big I think it'd be good to start moving the examples into their own files. I did this with
<StackedAreaSeries />
, could we do that here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good! I moved them out as a separate file