Skip to content

Commit

Permalink
Add tooltips to charts
Browse files Browse the repository at this point in the history
  • Loading branch information
toddmedema committed Apr 27, 2024
1 parent f19234e commit a655454
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"pretty": "prettier --write \"src/**/*.{css,js,jsx,tsx,json}\""
"pretty": "prettier --write \"src/**/*.{css,js,jsx,tsx,json}\"",
"deploy": "./deploy.sh"
},
"eslintConfig": {
"extends": [
Expand Down
18 changes: 17 additions & 1 deletion src/components/base/ChartFinances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
VictoryLabel,
VictoryLine,
VictoryTheme,
VictoryVoronoiContainer,
VictoryTooltip,
} from "victory";

interface ChartData {
Expand All @@ -30,7 +32,7 @@ const ChartFinances = (props: Props): JSX.Element => {
const rangeMin = props.timeline[0].month;
const rangeMax = Math.max(
rangeMin + 11,
props.timeline[props.timeline.length - 1].month,
props.timeline[props.timeline.length - 1].month
);
const past = [] as ChartData[];
const projected = [] as ChartData[];
Expand All @@ -57,6 +59,20 @@ const ChartFinances = (props: Props): JSX.Element => {
domain={{ x: [rangeMin, rangeMax], y: [domainMin, domainMax] }}
domainPadding={{ y: [6, 6] }}
height={props.height || 300}
containerComponent={
<VictoryVoronoiContainer
voronoiDimension="x"
labels={({ datum }) => props.format(datum.value).toString()}
labelComponent={
<VictoryTooltip
cornerRadius={2}
constrainToVisibleArea
flyoutStyle={{ fill: "white" }}
style={{ textAnchor: "end" }}
/>
}
/>
}
>
<VictoryAxis
tickFormat={(t) => formatMonthChartAxis(t, multiyear)}
Expand Down
37 changes: 32 additions & 5 deletions src/components/base/ChartForecastFuelPrices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
VictoryLegend,
VictoryLine,
VictoryTheme,
VictoryVoronoiContainer,
VictoryTooltip,
} from "victory";
import {
formatMonthChartAxis,
getDateFromMinute,
} from "../../helpers/DateTime";
import { formatMoneyConcise } from "../../helpers/Format";
import { formatMoneyConcise, formatMoneyStable } from "../../helpers/Format";
import { TickPresentFutureType } from "../../Types";
import { chartTheme, fuelColors } from "../../Theme";

Expand Down Expand Up @@ -40,6 +42,27 @@ export default class ChartForecastFuelPrices extends React.PureComponent<
domain={domain}
domainPadding={{ y: [6, 6] }}
height={height || 300}
containerComponent={
<VictoryVoronoiContainer
voronoiDimension="x"
// Labels are rendered on EACH chart, so we only render on Coal, otherwise we get duplicate labels
voronoiBlacklist={["naturalGas", "oil", "uranium"]}
labels={({ datum }) =>
`Coal: ${formatMoneyStable(datum.Coal)}
Natural Gas: ${formatMoneyStable(datum["Natural Gas"])}
Oil: ${formatMoneyStable(datum.Oil)}
Uranium: ${formatMoneyStable(datum.Uranium)}`
}
labelComponent={
<VictoryTooltip
cornerRadius={2}
constrainToVisibleArea
flyoutStyle={{ fill: "white" }}
style={{ textAnchor: "end" }}
/>
}
/>
}
>
<VictoryAxis
tickCount={6}
Expand Down Expand Up @@ -72,30 +95,33 @@ export default class ChartForecastFuelPrices extends React.PureComponent<
}}
/>
<VictoryLine
name="coal"
data={timeline}
x="minute"
y="Natural Gas"
y="Coal"
interpolation="natural"
style={{
data: {
stroke: fuelColors["Natural Gas"],
stroke: fuelColors.Coal,
strokeWidth: 1,
},
}}
/>
<VictoryLine
name="naturalGas"
data={timeline}
x="minute"
y="Coal"
y="Natural Gas"
interpolation="natural"
style={{
data: {
stroke: fuelColors.Coal,
stroke: fuelColors["Natural Gas"],
strokeWidth: 1,
},
}}
/>
<VictoryLine
name="oil"
data={timeline}
x="minute"
y="Oil"
Expand All @@ -108,6 +134,7 @@ export default class ChartForecastFuelPrices extends React.PureComponent<
}}
/>
<VictoryLine
name="uranium"
data={timeline}
x="minute"
y="Uranium"
Expand Down
16 changes: 16 additions & 0 deletions src/components/base/ChartForecastStorage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
VictoryLabel,
VictoryLine,
VictoryTheme,
VictoryVoronoiContainer,
VictoryTooltip,
} from "victory";
import { TickPresentFutureType } from "../../Types";
import {
Expand Down Expand Up @@ -39,6 +41,20 @@ export default class chartForecastStorage extends React.PureComponent<
domain={domain}
domainPadding={{ y: [6, 6] }}
height={height || 300}
containerComponent={
<VictoryVoronoiContainer
voronoiDimension="x"
labels={({ datum }) => formatWattHours(datum.storedWh)}
labelComponent={
<VictoryTooltip
cornerRadius={2}
constrainToVisibleArea
flyoutStyle={{ fill: "white" }}
style={{ textAnchor: "end" }}
/>
}
/>
}
>
<VictoryAxis
tickCount={6}
Expand Down
23 changes: 23 additions & 0 deletions src/components/base/ChartForecastSupplyByFuel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
VictoryLegend,
VictoryLine,
VictoryTheme,
VictoryVoronoiContainer,
VictoryTooltip,
} from "victory";
import {
formatMonthChartAxis,
Expand Down Expand Up @@ -61,6 +63,26 @@ export default class ChartForecastSupplyByFuel extends React.PureComponent<
domain={domain}
domainPadding={{ y: [6, 6] }}
height={height || 300}
containerComponent={
<VictoryVoronoiContainer
voronoiDimension="x"
// Labels are rendered on EACH chart, so we only render on first one, otherwise we get duplicate labels
voronoiBlacklist={fuels.slice(1)}
labels={({ datum }) =>
fuels
.map((f: FuelNameType) => f + ": " + formatWatts(datum[f]))
.join("\n")
}
labelComponent={
<VictoryTooltip
cornerRadius={2}
constrainToVisibleArea
flyoutStyle={{ fill: "white" }}
style={{ textAnchor: "end" }}
/>
}
/>
}
>
<VictoryAxis
tickCount={6}
Expand Down Expand Up @@ -96,6 +118,7 @@ export default class ChartForecastSupplyByFuel extends React.PureComponent<
{fuels.map((f: FuelNameType, i: number) => (
<VictoryLine
key={i}
name={f}
data={data}
x="minute"
y={f}
Expand Down
23 changes: 23 additions & 0 deletions src/components/base/ChartForecastSupplyDemand.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
VictoryLabel,
VictoryLine,
VictoryTheme,
VictoryVoronoiContainer,
VictoryTooltip,
} from "victory";
import { TickPresentFutureType } from "../../Types";
import {
Expand Down Expand Up @@ -52,6 +54,24 @@ export default class chartForecastSupplyDemand extends React.PureComponent<
domain={domain}
domainPadding={{ y: [6, 6] }}
height={height || 300}
containerComponent={
<VictoryVoronoiContainer
voronoiDimension="x"
// Labels are rendered on EACH chart, so we only render on supply, otherwise we get duplicate labels
voronoiBlacklist={["demand", "blackouts"]}
labels={({ datum }) =>
`Supply: ${formatWatts(datum.supplyW)}\nDemand: ${formatWatts(datum.demandW)}`
}
labelComponent={
<VictoryTooltip
cornerRadius={2}
constrainToVisibleArea
flyoutStyle={{ fill: "white" }}
style={{ textAnchor: "end" }}
/>
}
/>
}
>
<VictoryAxis
tickCount={6}
Expand Down Expand Up @@ -85,6 +105,7 @@ export default class chartForecastSupplyDemand extends React.PureComponent<
}}
/>
<VictoryLine
name="supply"
data={timeline}
x="minute"
y="supplyW"
Expand All @@ -96,6 +117,7 @@ export default class chartForecastSupplyDemand extends React.PureComponent<
}}
/>
<VictoryLine
name="demand"
data={timeline}
x="minute"
y="demandW"
Expand All @@ -106,6 +128,7 @@ export default class chartForecastSupplyDemand extends React.PureComponent<
}}
/>
<VictoryArea
name="blackouts"
data={blackouts}
x="minute"
y="value"
Expand Down
24 changes: 23 additions & 1 deletion src/components/base/ChartForecastWeather.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
VictoryLegend,
VictoryLine,
VictoryTheme,
VictoryVoronoiContainer,
VictoryTooltip,
} from "victory";
import { TICK_MINUTES } from "../../Constants";
import { TickPresentFutureType } from "../../Types";
Expand Down Expand Up @@ -47,6 +49,24 @@ export default class ChartForecastWeather extends React.PureComponent<
domain={domain}
domainPadding={{ y: [6, 6] }}
height={height || 300}
containerComponent={
<VictoryVoronoiContainer
voronoiDimension="x"
// Labels are rendered on EACH chart, so we only render on temperature, otherwise we get duplicate labels
voronoiBlacklist={["wind"]}
labels={({ datum }) =>
`Temperature: ${Math.round(datum.temperatureC)}°C\nWind: ${Math.round(datum.windKph)} km/h`
}
labelComponent={
<VictoryTooltip
cornerRadius={2}
constrainToVisibleArea
flyoutStyle={{ fill: "white" }}
style={{ textAnchor: "end" }}
/>
}
/>
}
>
<VictoryAxis
tickCount={6}
Expand Down Expand Up @@ -78,6 +98,7 @@ export default class ChartForecastWeather extends React.PureComponent<
}}
/>
<VictoryLine
name="temperature"
data={data}
x="minute"
y="temperatureC"
Expand All @@ -90,6 +111,7 @@ export default class ChartForecastWeather extends React.PureComponent<
}}
/>
<VictoryLine
name="wind"
data={data}
x="minute"
y="windKph"
Expand All @@ -110,7 +132,7 @@ export default class ChartForecastWeather extends React.PureComponent<
symbolSpacer={5}
data={[
{ name: "Heat (°C)", symbol: { fill: temperatureColor } },
{ name: "Wind (Kph)", symbol: { fill: windColor } },
{ name: "Wind (km/h)", symbol: { fill: windColor } },
]}
/>
</VictoryChart>
Expand Down
Loading

0 comments on commit a655454

Please sign in to comment.