-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathSingleValue.tsx
60 lines (55 loc) · 1.83 KB
/
SingleValue.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Copyright (c) 2023 Google LLC
* SPDX-License-Identifier: MIT
*/
import React from 'react';
import type { SingleValueProps } from '@looker/visualizations-adapters';
import numeral from 'numeral';
import styled from 'styled-components';
import { VisWrapper } from '@looker/visualizations-adapters';
export const SingleValue = ({
data,
fields,
width,
height,
config,
}: SingleValueProps) => {
if (!data) {
return null;
}
const { series = {} } = config;
// only allow one measure for single_value
const { name } = fields.measures[0];
const firstSeries = Array.isArray(series) ? series[0] : series[name || ''];
const { color, label, value_format } = firstSeries;
const value: number = data[0][name || ''];
const formattedValue = numeral(value).format(value_format);
return (
<VisWrapper>
<SingleValueLayout width={width} height={height}>
<SingleValueContent color={color}>{formattedValue}</SingleValueContent>
{label && <SingleValueTitle color={color}>{label}</SingleValueTitle>}
</SingleValueLayout>
</VisWrapper>
);
};
type SingleValueLayoutProps = { width?: number; height?: number };
type SingleValueContentProps = { color?: string };
const SingleValueLayout = styled.div<SingleValueLayoutProps>`
align-items: center;
background: ${({ theme }) => theme.colors.background};
display: grid;
grid-template-columns: 1fr;
height: ${({ height }) => `${height}px` || `auto`};
justify-items: center;
width: ${({ width }) => `${width}px` || `auto`};
`;
const SingleValueContent = styled.div<SingleValueContentProps>`
color: ${({ color }) => `${color}`};
font-size: ${({ theme }) => theme.fontSizes.xxxlarge};
`;
const SingleValueTitle = styled.div<SingleValueContentProps>`
color: ${({ color }) => `${color}`};
font-size: ${({ theme }) => theme.fontSizes.large};
opacity: 50%;
`;