Skip to content

Commit

Permalink
Merge pull request #834 from CodeForAfrica/ft/hhurumap-heatmap
Browse files Browse the repository at this point in the history
HeatMapChart
  • Loading branch information
kelvinkipruto authored Aug 12, 2024
2 parents e8c48e8 + 00d48be commit d54a423
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import theme from "@/climatemappedafrica/theme";
const {
BarChartScope,
DonutChartScope,
HeatMapScope,
LineChartScope,
MultiLineChartScope,
TreemapChartScope,
Expand Down Expand Up @@ -61,6 +62,9 @@ export default function configureScope(
case "treemap":
vegaSpec = TreemapChartScope(scopeOptions);
break;
case "heatmap":
vegaSpec = HeatMapScope(scopeOptions);
break;
case "stacked":
if (isMobile) {
vegaSpec = VerticalStackedChartScope(scopeOptions);
Expand Down
4 changes: 4 additions & 0 deletions apps/pesayetu/src/components/HURUmap/Chart/configureScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import theme from "@/pesayetu/theme";
const {
BarChartScope,
DonutChartScope,
HeatMapScope,
LineChartScope,
MultiLineChartScope,
TreemapChartScope,
Expand Down Expand Up @@ -68,6 +69,9 @@ export default function configureScope(
vegaSpec = StackedChartScope(scopeOptions);
}
break;
case "heatmap":
vegaSpec = HeatMapScope(scopeOptions);
break;
default:
if (isMobile) {
vegaSpec = VerticalBarChartScope(scopeOptions);
Expand Down
2 changes: 1 addition & 1 deletion packages/hurumap-core/src/ChartTooltip/ChartTooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StyledEngineProvider } from "@mui/material/styles";
import React, { useEffect } from "react";
import ReactDOM from "react-dom";

import Tooltip from "./Tooltip"; // Import your ChartTooltip component
import Tooltip from "./Tooltip";

function ChartTooltip({
id,
Expand Down
140 changes: 140 additions & 0 deletions packages/hurumap-core/src/Scope/HeatMapScope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import merge from "deepmerge";

import Scope from "./Scope";

export default function HeatMapScope(props) {
const {
primaryData,
metadata,
config,
secondaryData,
primaryParentData,
secondaryParentData,
isCompare,
isMobile,
theme,
args,
} = props;

const { primary_group: primaryGroup } = metadata;

return merge(
Scope({
primaryData,
metadata,
config,
secondaryData,
primaryParentData,
secondaryParentData,
chartType: "heatmap",
theme,
args,
}),
{
height: isMobile && isCompare && secondaryData?.length > 1 ? 620 : 310,
signals: [
{
name: "height",
value: isMobile && isCompare && secondaryData?.length > 1 ? 620 : 310,
},
{
name: "isMobile",
value: isMobile,
},
{
name: "isCompare",
value: isCompare,
},
{ name: "stripeWidth", update: "width/length(data('primary'))" },
],
scales: [
{
name: "scaleX",
type: "linear",
domain: {
data: "primary",
field: primaryGroup,
},
range: [0, { signal: "width" }],
zero: false,
},
{
name: "color",
type: "linear",
domain: {
data: "primary",
field: {
signal: "datatype[Units]",
},
},
range: [theme.palette.secondary.main, theme.palette.primary.main],
reverse: true,
},
],
marks: [
{
name: "stripe",
type: "rect",
interactive: true,
from: {
data: "primary",
},
encode: {
enter: {
xc: {
scale: "scaleX",
field: primaryGroup,
},
fill: {
scale: "color",
field: {
signal: "datatype[Units]",
},
},
},
update: {
y: {
signal: 0,
},
height: {
signal: "height",
},
width: {
signal: "stripeWidth",
},
tooltip: {
signal: "{'group': datum[mainGroup], 'count': datum.count}",
},
},
hover: {
y: {
value: -4,
},
height: {
signal: "height + 8",
},
},
},
},
],
axes: [
{
scale: "scaleX",
orient: "bottom",
domain: false,
format: ".4",
labelColor: "black",
},
],
legends: [
{
fill: "color",
type: "gradient",
titleFontSize: 12,
titlePadding: 4,
gradientLength: { signal: "height" },
},
],
},
);
}
2 changes: 2 additions & 0 deletions packages/hurumap-core/src/Scope/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BarChartScope from "./BarChartScope";
import DonutChartScope from "./DonutChartScope";
import HeatMapScope from "./HeatMapScope";
import LineChartScope from "./LineChartScope";
import MultiLineChartScope from "./MultiLineChartScope";
import Scope from "./Scope";
Expand All @@ -12,6 +13,7 @@ export default {
Scope,
BarChartScope,
DonutChartScope,
HeatMapScope,
LineChartScope,
MultiLineChartScope,
VerticalBarChartScope,
Expand Down

0 comments on commit d54a423

Please sign in to comment.