forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SIP-6] removed get_data for BigNumber (apache#5861)
* removed get_data for BigNumber * removed duplicated condition * fixed typo
- Loading branch information
1 parent
80faa8d
commit ee89a3e
Showing
5 changed files
with
94 additions
and
82 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import * as color from 'd3-color'; | ||
|
||
import BigNumberVis, { renderTooltipFactory } from './BigNumber'; | ||
import { d3FormatPreset } from '../../modules/utils'; | ||
|
||
const TIME_COLUMN = '__timestamp'; | ||
|
||
function transform(data, formData) { | ||
let bigNumber; | ||
let trendlineData; | ||
const metricName = formData.metric.label || formData.metric; | ||
const compareSuffix = formData.compare_suffix || ''; | ||
const compareLag = +formData.compare_lag || 0; | ||
const supportTrendline = formData.viz_type === 'big_number'; | ||
const showTrendline = supportTrendline && formData.show_trend_line; | ||
let percentChange = 0; | ||
const subheader = formData.subheader || ''; | ||
let formattedSubheader = subheader; | ||
if (supportTrendline) { | ||
const sortedData = [...data].sort((a, b) => a[TIME_COLUMN] - b[TIME_COLUMN]); | ||
bigNumber = sortedData[sortedData.length - 1][metricName]; | ||
if (compareLag > 0) { | ||
const compareIndex = sortedData.length - (compareLag + 1); | ||
if (compareIndex >= 0) { | ||
const compareValue = sortedData[compareIndex][metricName]; | ||
percentChange = compareValue === 0 | ||
? 0 : (bigNumber - compareValue) / Math.abs(compareValue); | ||
const formatPercentChange = d3.format('+.1%'); | ||
formattedSubheader = `${formatPercentChange(percentChange)} ${compareSuffix}`; | ||
} | ||
} | ||
trendlineData = showTrendline | ||
? sortedData.map(point => ({ x: point[TIME_COLUMN], y: point[metricName] })) | ||
: null; | ||
} else { | ||
bigNumber = data[0][metricName]; | ||
trendlineData = null; | ||
} | ||
|
||
let className = ''; | ||
if (percentChange > 0) { | ||
className = 'positive'; | ||
} else if (percentChange < 0) { | ||
className = 'negative'; | ||
} | ||
|
||
return { | ||
bigNumber, | ||
trendlineData, | ||
className, | ||
subheader: formattedSubheader, | ||
showTrendline, | ||
}; | ||
} | ||
|
||
function adaptor(slice, payload) { | ||
const { formData, containerId } = slice; | ||
|
||
const transformedData = transform(payload.data, formData); | ||
const startYAxisAtZero = formData.start_y_axis_at_zero; | ||
const formatValue = d3FormatPreset(formData.y_axis_format); | ||
let userColor; | ||
if (formData.color_picker) { | ||
const { r, g, b } = formData.color_picker; | ||
userColor = color.rgb(r, g, b).hex(); | ||
} | ||
|
||
ReactDOM.render( | ||
<BigNumberVis | ||
width={slice.width()} | ||
height={slice.height()} | ||
formatBigNumber={formatValue} | ||
startYAxisAtZero={startYAxisAtZero} | ||
mainColor={userColor} | ||
gradientId={`big_number_${containerId}`} | ||
renderTooltip={renderTooltipFactory(formatValue)} | ||
{...transformedData} | ||
/>, | ||
document.getElementById(containerId), | ||
); | ||
} | ||
|
||
export default adaptor; |
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,5 @@ | ||
import adaptor from './adaptor'; | ||
import BigNumber from './BigNumber'; | ||
|
||
export { BigNumber }; | ||
export default adaptor; |
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