Skip to content

Commit f02e646

Browse files
✨ (slope) improve svg output for figma
1 parent b3eda9e commit f02e646

File tree

1 file changed

+69
-57
lines changed

1 file changed

+69
-57
lines changed

packages/@ourworldindata/grapher/src/slopeCharts/SlopeChart.tsx

+69-57
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ export class SlopeChart
11411141

11421142
private renderSlopes() {
11431143
return (
1144-
<>
1144+
<g id={makeIdForHumanConsumption("slopes")}>
11451145
{this.renderSeries.map((series) => (
11461146
<Slope
11471147
key={series.seriesName}
@@ -1151,7 +1151,28 @@ export class SlopeChart
11511151
outlineStroke={this.backgroundColor}
11521152
/>
11531153
))}
1154-
</>
1154+
</g>
1155+
)
1156+
}
1157+
1158+
private renderInteractiveSlopes(): React.ReactElement {
1159+
return (
1160+
<g
1161+
ref={this.slopeAreaRef}
1162+
onMouseMove={this.onMouseMove}
1163+
onTouchMove={this.onMouseMove}
1164+
onTouchStart={this.onMouseMove}
1165+
onMouseLeave={this.onMouseLeave}
1166+
>
1167+
<rect
1168+
x={this.startX}
1169+
y={this.bounds.y}
1170+
width={this.endX - this.startX}
1171+
height={this.bounds.height}
1172+
fillOpacity={0}
1173+
/>
1174+
{this.renderSlopes()}
1175+
</g>
11551176
)
11561177
}
11571178

@@ -1176,7 +1197,7 @@ export class SlopeChart
11761197
const { xDomain, startX, endX } = this
11771198

11781199
return (
1179-
<>
1200+
<g id={makeIdForHumanConsumption("horizontal-axis")}>
11801201
<MarkX
11811202
label={this.formatColumn.formatTime(xDomain[0])}
11821203
x={startX}
@@ -1193,32 +1214,6 @@ export class SlopeChart
11931214
labelPadding={this.xLabelPadding}
11941215
fontSize={this.yAxis.tickFontSize}
11951216
/>
1196-
</>
1197-
)
1198-
}
1199-
1200-
private renderChartArea() {
1201-
return (
1202-
<g>
1203-
{this.renderYAxis()}
1204-
{this.renderXAxis()}
1205-
<g
1206-
id={makeIdForHumanConsumption("slopes")}
1207-
ref={this.slopeAreaRef}
1208-
onMouseMove={this.onMouseMove}
1209-
onTouchMove={this.onMouseMove}
1210-
onTouchStart={this.onMouseMove}
1211-
onMouseLeave={this.onMouseLeave}
1212-
>
1213-
<rect
1214-
x={this.startX}
1215-
y={this.bounds.y}
1216-
width={this.endX - this.startX}
1217-
height={this.bounds.height}
1218-
fillOpacity={0}
1219-
/>
1220-
{this.renderSlopes()}
1221-
</g>
12221217
</g>
12231218
)
12241219
}
@@ -1284,6 +1279,30 @@ export class SlopeChart
12841279
)
12851280
}
12861281

1282+
private renderInteractive(): React.ReactElement {
1283+
return (
1284+
<>
1285+
{this.renderYAxis()}
1286+
{this.renderXAxis()}
1287+
{this.renderInteractiveSlopes()}
1288+
{this.renderLineLegends()}
1289+
{this.renderNoDataSection()}
1290+
{this.tooltip}
1291+
</>
1292+
)
1293+
}
1294+
1295+
private renderStatic(): React.ReactElement {
1296+
return (
1297+
<>
1298+
{this.renderYAxis()}
1299+
{this.renderXAxis()}
1300+
{this.renderSlopes()}
1301+
{this.renderLineLegends()}
1302+
</>
1303+
)
1304+
}
1305+
12871306
render() {
12881307
if (this.failMessage)
12891308
return (
@@ -1297,14 +1316,9 @@ export class SlopeChart
12971316
</>
12981317
)
12991318

1300-
return (
1301-
<g>
1302-
{this.renderChartArea()}
1303-
{this.renderLineLegends()}
1304-
{this.renderNoDataSection()}
1305-
{this.tooltip}
1306-
</g>
1307-
)
1319+
return this.manager.isStatic
1320+
? this.renderStatic()
1321+
: this.renderInteractive()
13081322
}
13091323
}
13101324

@@ -1336,10 +1350,7 @@ function Slope({
13361350
hover.background || focus.background ? 0.66 * strokeWidth : strokeWidth
13371351

13381352
return (
1339-
<g
1340-
id={makeIdForHumanConsumption("slope", seriesName)}
1341-
className="slope"
1342-
>
1353+
<g id={makeIdForHumanConsumption(seriesName)} className="slope">
13431354
{showOutline && (
13441355
<LineWithDots
13451356
startPoint={startPoint}
@@ -1411,22 +1422,16 @@ function GridLines({ bounds, yAxis }: GridLinesProps) {
14111422
{yAxis.tickLabels.map((tick) => {
14121423
const y = yAxis.place(tick.value)
14131424
return (
1414-
<g
1415-
id={makeIdForHumanConsumption(
1416-
"grid-line",
1417-
tick.formattedValue
1418-
)}
1425+
<line
1426+
id={makeIdForHumanConsumption(tick.formattedValue)}
14191427
key={tick.formattedValue}
1420-
>
1421-
<line
1422-
x1={bounds.left + yAxis.width}
1423-
y1={y}
1424-
x2={bounds.right}
1425-
y2={y}
1426-
stroke="#ddd"
1427-
strokeDasharray="3,2"
1428-
/>
1429-
</g>
1428+
x1={bounds.left + yAxis.width}
1429+
y1={y}
1430+
x2={bounds.right}
1431+
y2={y}
1432+
stroke="#ddd"
1433+
strokeDasharray="3,2"
1434+
/>
14301435
)
14311436
})}
14321437
</g>
@@ -1450,7 +1455,14 @@ function MarkX({
14501455
}) {
14511456
return (
14521457
<>
1453-
<line x1={x} y1={top} x2={x} y2={bottom} stroke="#999" />
1458+
<line
1459+
id={makeIdForHumanConsumption(label)}
1460+
x1={x}
1461+
y1={top}
1462+
x2={x}
1463+
y2={bottom}
1464+
stroke="#999"
1465+
/>
14541466
<text
14551467
x={x}
14561468
y={bottom + labelPadding}

0 commit comments

Comments
 (0)