Skip to content

Commit

Permalink
Unify indicator graph styles
Browse files Browse the repository at this point in the history
  • Loading branch information
terotik committed Nov 29, 2024
1 parent b0b9fe6 commit 883a722
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 32 deletions.
19 changes: 15 additions & 4 deletions components/graphs/GraphAsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import dayjs from 'common/dayjs';
import { Table, Collapse, Button } from 'reactstrap';
import { beautifyValue } from 'common/data/format';
import Icon from 'components/common/Icon';
import { useTranslations } from 'next-intl';
import { useFormatter, useTranslations } from 'next-intl';

const CollapsibleTable = styled.div`
background-color: ${(props) => props.theme.themeColors.white};
padding: ${(props) => props.theme.spaces.s050};
`;

const Trigger = styled.div`
padding-bottom: ${(props) => props.theme.spaces.s050};
background-color: ${(props) => props.theme.cardBackground.primary};
text-align: center;
`;
Expand Down Expand Up @@ -56,6 +55,7 @@ const TriggerButton = styled(Button)`

function GraphAsTable(props) {
const t = useTranslations();
const format = useFormatter();
const [isOpen, setIsOpen] = useState(false);
const toggle = () => setIsOpen(!isOpen);

Expand Down Expand Up @@ -83,14 +83,25 @@ function GraphAsTable(props) {
tableRows.sort((a, b) => dayjs(a).diff(dayjs(b)));
}

const dateFormat = timeResolution === 'YEAR' ? 'YYYY' : 'l';
const dateFormat =
timeResolution === 'YEAR'
? {
year: 'numeric',
}
: {
year: 'numeric',
month: 'numeric',
day: 'numeric',
};

// Create table data row by row
const tableData = [];
tableRows.map((row, i) => {
const rowObj = {};
rowObj.label =
data[0].xType === 'time' ? dayjs(row).format(dateFormat) : row;
data[0].xType === 'time'
? format.dateTime(new Date(row), dateFormat)
: row;
rowObj.values = [];
data.map((trace, i) => {
const indexOfX = trace.x.indexOf(row);
Expand Down
42 changes: 19 additions & 23 deletions components/graphs/IndicatorGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const createLayout = (
const fontFamily =
'-apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, ' +
'helvetica neue, helvetica, Ubuntu, roboto, noto, arial, sans-serif';
const hasCategories = !hasTimeDimension;
const hasCategories = false;

// With Plotly you have choice between one significant digit precision for y axis ticks (.1r)
// then on smaller (first digit) ranges you get repeated numbers on ticks.
Expand Down Expand Up @@ -117,6 +117,7 @@ const createLayout = (
},
...yaxes,
...xaxes,
hovermode: 'x unified',
paper_bgcolor: theme.themeColors.white,
plot_bgcolor: graphCustomBackground || theme.themeColors.white,
autosize: true,
Expand Down Expand Up @@ -219,7 +220,7 @@ const createTraces: (params: CreateTracesParams) => TracesOutput = (params) => {
allXValues.push(...trace.x);

// we have multiple categories in one time point - draw bar groups
if (!hasTimeDimension) {
if (false) {
modTrace.x = modTrace.x.map((name) => splitLines(name));
modTrace.type = 'bar';
modTrace.marker = {
Expand All @@ -233,7 +234,7 @@ const createTraces: (params: CreateTracesParams) => TracesOutput = (params) => {
layoutConfig.xaxis!.tickvals = undefined;
}
// we have one or more categories as time series - draw lines and markers
if (hasTimeDimension) {
if (true) {
modTrace.type = 'scatter';
// we fill traces if there is only one trace and area graph is enabled
if (traceCount === 1 && useAreaGraph) {
Expand All @@ -258,7 +259,7 @@ const createTraces: (params: CreateTracesParams) => TracesOutput = (params) => {
modTrace.marker = {
size: 8,
symbol: plotColors.symbols[idx % numSymbols],
color: '#ffffff',
//color: '#ffffff',
line: {
width: 2,
color: plotColors.mainScale[idx % numColors],
Expand Down Expand Up @@ -390,17 +391,17 @@ function IndicatorGraph(props: IndicatorGraphProps) {
trace: theme.graphColors.red070,
trend: theme.graphColors.red030,
goalScale: [
theme.graphColors.green070,
theme.graphColors.green030,
theme.graphColors.green050,
theme.graphColors.green030,
theme.graphColors.green070,
theme.graphColors.green090,
theme.graphColors.green010,
],
mainScale: [
theme.graphColors.red070,
theme.graphColors.blue050,
theme.graphColors.blue070,
theme.graphColors.yellow030,
theme.graphColors.green030,
theme.graphColors.red070,
theme.graphColors.blue030,
theme.graphColors.yellow070,
theme.graphColors.green070,
Expand Down Expand Up @@ -463,11 +464,7 @@ function IndicatorGraph(props: IndicatorGraphProps) {
styleCount = 1;
}
}
if (!hasTimeDimension) {
// For bar graphs, the red color looks too heavy.
// Shift to blue.
plotColors.mainScale.shift();
}

const mainTraces = createTraces({
traces,
unit: yRange.unit,
Expand Down Expand Up @@ -543,20 +540,19 @@ function IndicatorGraph(props: IndicatorGraphProps) {
name: goalTrace.name,
type: 'scatter',
cliponaxis: false,
mode: goalTrace.scenario ? 'markers' : 'lines+markers',
...(!goalTrace.scenario && {
line: {
width: 3,
dash: 'dash',
color: plotColors.goalScale[idx % plotColors.goalScale.length],
},
}),
mode: 'lines+markers',

line: {
width: 3,
dash: 'dash',
color: plotColors.goalScale[idx % plotColors.goalScale.length],
},
marker: {
size: 12,
symbol: 'x',
symbol: 'circle',
color: plotColors.goalScale[idx % plotColors.goalScale.length],
},
opacity: 0.7,
opacity: 0.5,
hovertemplate: `(%{x}) ${goalTrace.name}: %{y} ${yRange.unit}`,
hoverlabel: {
namelength: 0,
Expand Down
5 changes: 2 additions & 3 deletions components/indicators/IndicatorVisualisation.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,12 @@ function getTraces(
if (dimensions.length === 0) {
return [
{
xType: cube.length === 1 ? 'category' : 'time',
xType: 'time',
name: name,
dataType: 'total',
x: cube.map((val) => {
const d = dayjs(val.date);
return cube.length < 2 ? d.year() : val.date;
return val.date;
}),
y: cube.map((val) => val.value),
},
Expand Down Expand Up @@ -609,7 +609,6 @@ function IndicatorVisualisation({ indicatorId, indicatorLink }) {
plan: { scenarios },
} = data;

console.log('indicator', data);
if (!indicator)
return <Alert color="danger">{t('indicator-not-found')}</Alert>;

Expand Down
5 changes: 4 additions & 1 deletion components/paths/contentblocks/CategoryPageHeaderBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ function CategoryPageHeaderBlock(props: CategoryPageHeaderProps) {
? getBreadcrumbsFromCategoryHierarchy([page.category.parent], false)
: [];

// TODO: A better way to find root category list page
/*
// Not needed, leaving this here for reference
const rootCategoryListPage =
page?.category && page.category.type.id === '76'
? { id: 0, name: 'Bereiche', url: '/klimaschutzplan/bereiche' }
Expand All @@ -99,6 +100,8 @@ function CategoryPageHeaderBlock(props: CategoryPageHeaderProps) {
: null;
if (rootCategoryListPage) breadcrumbs.unshift(rootCategoryListPage);
if (currentCategoryListPage) breadcrumbs.push(currentCategoryListPage);
*/

return (
<Background $hasHeaderImage={!!headerImage}>
<Container>
Expand Down
2 changes: 1 addition & 1 deletion components/paths/graphs/IndicatorSparkline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ const IndicatorSparkline = (props: IndicatorSparklineProps) => {
symbol: 'circle',
symbolSize: 6,
lineStyle: {
color: theme.graphColors.blue050,
color: theme.graphColors.blue070,
width: 2,
},
z: 2,
Expand Down

0 comments on commit 883a722

Please sign in to comment.