Skip to content

Commit c0df810

Browse files
improve canvas graphs
1 parent 82a02c3 commit c0df810

2 files changed

Lines changed: 16 additions & 16 deletions

File tree

client/src/components/sidebar/game/resource-graph.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,9 @@ export const ResourceGraph: React.FC<Props> = (props: Props) => {
3737
const data = props.active && round ? getChartData(round, props.property) : []
3838

3939
return (
40-
<div className="mt-2 px-2 w-full">
41-
<h2 className="mx-auto text-center">{props.propertyDisplayName}</h2>
42-
{/*
43-
<D3LineChart
44-
data={data}
45-
width={300 + 40} // Add 40 so that tooltip is visible outside of SVG container
46-
height={200}
47-
margin={{ top: 20, right: 20 + 20, bottom: 30, left: 40 + 20 }}
48-
/>
49-
*/}
50-
<QuickLineChart data={data} width={300} height={200} margin={{ top: 0, right: 0, bottom: 0, left: 0 }} />
40+
<div className="mt-2 w-full">
41+
<h2 className="mx-auto text-center mb-2">{props.propertyDisplayName}</h2>
42+
<QuickLineChart data={data} width={350} height={200} margin={{ top: 2, right: 20, bottom: 17, left: 30 }} />
5143
</div>
5244
)
5345
}

client/src/util/graph-util.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ export function getAxes(
66
margin: { top: number; right: number; bottom: number; left: number },
77
size: { x: number; y: number }
88
) {
9-
const xScale = (value: number) => (value * (width - margin.left - margin.right)) / size.x + margin.left
10-
const yScale = (value: number) => height - margin.bottom - (value / size.y) * (height - margin.top - margin.bottom)
9+
// +/- 1 is to avoid overlapping axis lines
10+
const xScale = (value: number) => (value * (width - margin.left - margin.right)) / size.x + margin.left + 1
11+
const yScale = (value: number) =>
12+
height - margin.bottom - (value / size.y) * (height - margin.top - margin.bottom) - 1
1113
const innerWidth = width - margin.left - margin.right
1214
const innerHeight = height - margin.top - margin.bottom
1315
return { xScale, yScale, innerWidth, innerHeight }
@@ -56,7 +58,9 @@ export function drawXAxis(
5658
context.lineTo(xPos, height - margin.bottom + 6)
5759
context.stroke()
5860
context.fillStyle = options.textColor ?? 'black'
59-
context.fillText(Math.round(range.min + i * labelGap).toString(), xPos - 3, height)
61+
const label = Math.round(range.min + i * labelGap).toString()
62+
const textWidth = context.measureText(label).width
63+
context.fillText(label, xPos - textWidth / 2, height)
6064
}
6165
}
6266
}
@@ -82,7 +86,9 @@ export function drawYAxis(
8286
context.lineTo(margin.left - 6, yPos)
8387
context.stroke()
8488
context.fillStyle = options.textColor ?? 'black'
85-
context.fillText(point.toString(), 0, yPos + 5)
89+
const label = point.toString()
90+
const textWidth = context.measureText(label).width
91+
context.fillText(label, margin.left - textWidth - 10, yPos + 5)
8692
}
8793
} else if (options.count) {
8894
const gap = (range.max - range.min) / (options.count - (options.centered ? 0 : 1))
@@ -94,7 +100,9 @@ export function drawYAxis(
94100
context.lineTo(margin.left - 6, yPos)
95101
context.stroke()
96102
context.fillStyle = options.textColor ?? 'black'
97-
context.fillText(Math.round(range.min + i * labelGap).toString(), 0, yPos + 5)
103+
const label = Math.round(range.min + i * labelGap).toString()
104+
const textWidth = context.measureText(label).width
105+
context.fillText(label, margin.left - textWidth - 10, yPos + 5)
98106
}
99107
}
100108
}

0 commit comments

Comments
 (0)