Skip to content

Commit

Permalink
refactor: Update chart tooltips to use best unit (#575)
Browse files Browse the repository at this point in the history
* refactor: Update chart tooltips to use best unit

* chore: Add changeset

* Update .changeset/cool-dingos-visit.md

Co-authored-by: elessar.eth <paulo.colombo@pm.me>

---------

Co-authored-by: elessar.eth <paulo.colombo@pm.me>
  • Loading branch information
luis-herasme and PJColombo authored Sep 20, 2024
1 parent 9b6847f commit 53acaf1
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-dingos-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@blobscan/web": patch
---

Fixed an issue where the chart tooltip displayed zero for very low values. It now shows the most appropriate unit for each value.
11 changes: 6 additions & 5 deletions apps/web/src/components/Charts/Block/DailyAvgBlobFeeChart.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { FC } from "react";
import type { EChartOption } from "echarts";

import { findBestUnit, formatWei, prettyFormatWei } from "@blobscan/eth-units";

import { ChartCard } from "~/components/Cards/ChartCard";
import { useScaledWeiAmounts } from "~/hooks/useScaledWeiAmounts";
import type { DailyBlockStats } from "~/types";
import { formatNumber } from "~/utils";
import { buildTimeSeriesOptions } from "~/utils";

export type DailyAvgBlobFeeChartProps = {
Expand All @@ -14,14 +15,14 @@ export type DailyAvgBlobFeeChartProps = {

export const DailyAvgBlobFeeChart: FC<Partial<DailyAvgBlobFeeChartProps>> =
function ({ days, avgBlobFees }) {
const { scaledValues, unit } = useScaledWeiAmounts(avgBlobFees);
const { unit } = useScaledWeiAmounts(avgBlobFees);

const options: EChartOption<EChartOption.Series> = {
...buildTimeSeriesOptions({
dates: days,
axisFormatters: {
yAxisTooltip: (value) => `${formatNumber(value, "compact")} ${unit}`,
yAxisLabel: (value) => `${formatNumber(value, "compact")} ${unit}`,
yAxisTooltip: (value) => formatWei(value, findBestUnit(value)),
yAxisLabel: (value) => prettyFormatWei(value, unit),
},
yUnit: "ethereum",
}),
Expand All @@ -31,7 +32,7 @@ export const DailyAvgBlobFeeChart: FC<Partial<DailyAvgBlobFeeChartProps>> =
series: [
{
name: "Avg. Blob Fees",
data: scaledValues,
data: avgBlobFees,
type: "line",
},
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { FC } from "react";
import type { EChartOption } from "echarts";

import { findBestUnit, formatWei, prettyFormatWei } from "@blobscan/eth-units";

import { ChartCard } from "~/components/Cards/ChartCard";
import { useScaledWeiAmounts } from "~/hooks/useScaledWeiAmounts";
import type { DailyBlockStats } from "~/types";
import { buildTimeSeriesOptions, formatNumber } from "~/utils";
import { buildTimeSeriesOptions } from "~/utils";

export type DailyAvgBlobGasPriceChartProps = {
days: DailyBlockStats["days"];
Expand All @@ -14,14 +16,14 @@ export type DailyAvgBlobGasPriceChartProps = {
export const DailyAvgBlobGasPriceChart: FC<
Partial<DailyAvgBlobGasPriceChartProps>
> = function ({ days, avgBlobGasPrices }) {
const { scaledValues, unit } = useScaledWeiAmounts(avgBlobGasPrices);
const { unit } = useScaledWeiAmounts(avgBlobGasPrices);

const options: EChartOption<EChartOption.Series> = {
...buildTimeSeriesOptions({
dates: days,
axisFormatters: {
yAxisTooltip: (value) => `${formatNumber(value, "compact")} ${unit}`,
yAxisLabel: (value) => `${formatNumber(value, "compact")} ${unit}`,
yAxisTooltip: (value) => formatWei(value, findBestUnit(value)),
yAxisLabel: (value) => prettyFormatWei(value, unit),
},
yUnit: "ethereum",
}),
Expand All @@ -31,7 +33,7 @@ export const DailyAvgBlobGasPriceChart: FC<
series: [
{
name: "Avg. Blob Gas Prices",
data: scaledValues,
data: avgBlobGasPrices,
type: "line",
},
],
Expand Down
14 changes: 7 additions & 7 deletions apps/web/src/components/Charts/Block/DailyBlobFeeChart.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { FC } from "react";
import type { EChartOption } from "echarts";

import { findBestUnit, formatWei, prettyFormatWei } from "@blobscan/eth-units";

import { ChartCard } from "~/components/Cards/ChartCard";
import { useScaledWeiAmounts } from "~/hooks/useScaledWeiAmounts";
import type { DailyBlockStats } from "~/types";
import { formatNumber } from "~/utils";
import { buildTimeSeriesOptions } from "~/utils";

export type DailyBlobFeeChartProps = {
Expand All @@ -14,16 +15,15 @@ export type DailyBlobFeeChartProps = {

export const DailyBlobFeeChart: FC<Partial<DailyBlobFeeChartProps>> =
function ({ days, blobFees }) {
const { scaledValues, unit } = useScaledWeiAmounts(
blobFees?.map((x) => Number(x))
);
const data = blobFees?.map((x) => Number(x));
const { unit } = useScaledWeiAmounts(data);

const options: EChartOption<EChartOption.SeriesBar> = {
...buildTimeSeriesOptions({
dates: days,
axisFormatters: {
yAxisTooltip: (value) => `${formatNumber(value, "compact")} ${unit}`,
yAxisLabel: (value) => `${formatNumber(value, "compact")} ${unit}`,
yAxisTooltip: (value) => formatWei(value, findBestUnit(value)),
yAxisLabel: (value) => prettyFormatWei(value, unit),
},
yUnit: "ethereum",
}),
Expand All @@ -33,7 +33,7 @@ export const DailyBlobFeeChart: FC<Partial<DailyBlobFeeChartProps>> =
series: [
{
name: "Blob Fees",
data: scaledValues,
data: data,
type: "bar",
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import * as echarts from "echarts";
import type { EChartOption } from "echarts";
import { useTheme } from "next-themes";

import { findBestUnit, formatWei, prettyFormatWei } from "@blobscan/eth-units";

import { ChartCard } from "~/components/Cards/ChartCard";
import { useScaledWeiAmounts } from "~/hooks/useScaledWeiAmounts";
import type { DailyBlockStats } from "~/types";
import { buildTimeSeriesOptions, formatNumber } from "~/utils";
import { buildTimeSeriesOptions } from "~/utils";

export type DailyBlobGasComparisonChartProps = Partial<{
days: DailyBlockStats["days"];
Expand All @@ -18,16 +20,15 @@ export type DailyBlobGasComparisonChartProps = Partial<{
export const DailyBlobGasComparisonChart: FC<DailyBlobGasComparisonChartProps> =
function ({ blobAsCalldataGasUsed, blobGasUsed, days, opts = {} }) {
const { resolvedTheme } = useTheme();
const { scaledValues, unit } = useScaledWeiAmounts(
blobGasUsed ? blobGasUsed.map((x) => Number(x)) : []
);
const data = blobGasUsed?.map((x) => Number(x));
const { unit } = useScaledWeiAmounts(data);

const options: EChartOption<EChartOption.Series> = {
...buildTimeSeriesOptions({
dates: days,
axisFormatters: {
yAxisTooltip: (value) => `${formatNumber(value, "compact")} ${unit}`,
yAxisLabel: (value) => `${formatNumber(value, "compact")} ${unit}`,
yAxisTooltip: (value) => formatWei(value, findBestUnit(value)),
yAxisLabel: (value) => prettyFormatWei(value, unit),
},
}),
grid: {
Expand All @@ -36,7 +37,7 @@ export const DailyBlobGasComparisonChart: FC<DailyBlobGasComparisonChartProps> =
series: [
{
name: "Blob Gas Used",
data: scaledValues,
data: data,
stack: "gas",
type: "bar",

Expand Down
15 changes: 8 additions & 7 deletions apps/web/src/components/Charts/Block/DailyBlobGasUsedChart.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { FC } from "react";
import type { EChartOption } from "echarts";

import { findBestUnit, formatWei, prettyFormatWei } from "@blobscan/eth-units";

import { ChartCard } from "~/components/Cards/ChartCard";
import { useScaledWeiAmounts } from "~/hooks/useScaledWeiAmounts";
import type { DailyBlockStats } from "~/types";
import { buildTimeSeriesOptions, formatNumber } from "~/utils";
import { buildTimeSeriesOptions } from "~/utils";

export type DailyBlobGasUsedChartProps = Partial<{
days: DailyBlockStats["days"];
Expand All @@ -13,15 +15,14 @@ export type DailyBlobGasUsedChartProps = Partial<{

const BaseChart: FC<DailyBlobGasUsedChartProps & { title: string }> =
function ({ days, blobGasUsed, title }) {
const { scaledValues, unit } = useScaledWeiAmounts(
blobGasUsed ? blobGasUsed.map((x) => Number(x)) : []
);
const data = blobGasUsed?.map((x) => Number(x));
const { unit } = useScaledWeiAmounts(data);
const options: EChartOption<EChartOption.SeriesBar> = {
...buildTimeSeriesOptions({
dates: days,
axisFormatters: {
yAxisTooltip: (value) => `${formatNumber(value, "compact")} ${unit}`,
yAxisLabel: (value) => `${formatNumber(value, "compact")} ${unit}`,
yAxisTooltip: (value) => formatWei(value, findBestUnit(value)),
yAxisLabel: (value) => prettyFormatWei(value, unit),
},
}),
grid: {
Expand All @@ -30,7 +31,7 @@ const BaseChart: FC<DailyBlobGasUsedChartProps & { title: string }> =
series: [
{
name: "Blob Gas Used",
data: scaledValues,
data: data,
stack: "gas",
type: "bar",
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { FC } from "react";
import type { EChartOption } from "echarts";

import { findBestUnit, formatWei, prettyFormatWei } from "@blobscan/eth-units";

import { ChartCard } from "~/components/Cards/ChartCard";
import { useScaledWeiAmounts } from "~/hooks/useScaledWeiAmounts";
import type { DailyTransactionStats } from "~/types";
import { buildTimeSeriesOptions, formatNumber } from "~/utils";
import { buildTimeSeriesOptions } from "~/utils";

export type DailyAvgMaxBlobGasFeeChartProps = {
days: DailyTransactionStats["days"];
Expand All @@ -15,16 +17,16 @@ export type DailyAvgMaxBlobGasFeeChartProps = {
export const DailyAvgMaxBlobGasFeeChart: FC<
Partial<DailyAvgMaxBlobGasFeeChartProps>
> = function ({ days, avgMaxBlobGasFees, compact = false }) {
const { scaledValues, unit } = useScaledWeiAmounts(avgMaxBlobGasFees);
const { unit } = useScaledWeiAmounts(avgMaxBlobGasFees);

const options: EChartOption<
EChartOption.SeriesBar | EChartOption.SeriesLine
> = {
...buildTimeSeriesOptions({
dates: days,
axisFormatters: {
yAxisTooltip: (value) => `${formatNumber(value)} ${unit}`,
yAxisLabel: (value) => `${formatNumber(value)} ${unit}`,
yAxisTooltip: (value) => formatWei(value, findBestUnit(value)),
yAxisLabel: (value) => prettyFormatWei(value, unit),
},
}),
grid: {
Expand All @@ -33,7 +35,7 @@ export const DailyAvgMaxBlobGasFeeChart: FC<
series: [
{
name: "Avg. Max Blob Gas Fees",
data: scaledValues,
data: avgMaxBlobGasFees,
type: compact ? "line" : "bar",
smooth: true,
},
Expand Down
3 changes: 3 additions & 0 deletions packages/eth-format/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ export function countIntegerDigits(value: string | number | bigint): number {
export function shiftDecimal(value: EthAmount, decimals: number): string {
value = value.toString();

// Convert scientific notation to standard decimal notation
value = fullwideFormatter.format(value as Intl.StringNumericLiteral);

const negative = value.startsWith("-");

if (negative) {
Expand Down

0 comments on commit 53acaf1

Please sign in to comment.