1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// PricingChart.jsx
import React, { useState, useEffect, useRef } from 'react';
import { useScrollToTop } from '../hooks/useScrollToTop';
import '../styles/PricingChart.css';
const ChartLegend = ({ onLegendClick, highlightedBarTypes, showPricing = true }) => {
if (!showPricing) return null;
return (
<div className="legend">
<div
className="legend-item"
onClick={() => onLegendClick('input')}
style={{ cursor: 'pointer', opacity: highlightedBarTypes.input ? 1 : 0.5 }}
>
<div className="legend-color input-color"></div>
<span>Input Price</span>
</div>
<div
className="legend-item"
onClick={() => onLegendClick('output')}
style={{ cursor: 'pointer', opacity: highlightedBarTypes.output ? 1 : 0.5 }}
>
<div className="legend-color output-color"></div>
<span>Output Price</span>
</div>
</div>
);
};
const ChartBar = ({ price, type, maxPrice, highlighted, score }) => {
const getBarHeight = () => {
if (score !== undefined) {
return (score / maxPrice) * 200;
}
return (price / maxPrice) * 200;
};
return (
<div
className={`bar ${score !== undefined ? 'score-bar' : `${type}-bar`}`}
style={{
height: `${getBarHeight()}px`,
opacity: highlighted ? 1 : 0.3,
}}
>
<span className="price-label">{score !== undefined ? score : price}</span>
</div>
);
};
const ProviderColumn = ({ provider, maxPrice, highlightedBarTypes, showPricing = true }) => (
<div className="chart-column">
<div className="bars-container">
{showPricing ? (
<>
<ChartBar
price={provider.inputPrice}
type="input"
maxPrice={maxPrice}
highlighted={highlightedBarTypes.input}
/>
<ChartBar
price={provider.outputPrice}
type="output"
maxPrice={maxPrice}
highlighted={highlightedBarTypes.output}
/>
</>
) : (
<ChartBar
score={provider.score}
maxPrice={maxPrice}
highlighted={true}
/>
)}
</div>
<div className="provider-info">
<img
src={`${provider.logo}`}
alt={`${provider.name} logo`}
className="provider-logo"
/>
<span className="provider-name">{provider.name}</span>
</div>
</div>
);
const YAxis = ({ maxPrice }) => {
const numberOfTicks = 5;
const tickValues = [];
for (let i = 0; i <= numberOfTicks; i++) {
const value = ((maxPrice / numberOfTicks) * i).toFixed(2);
tickValues.push(value);
}
return (
<div className="y-axis">
{tickValues.reverse().map((value, index) => (
<div key={index} className="y-axis-label">
{value}
</div>
))}
</div>
);
};
const GridLines = () => (
<div className="grid-lines">
{[...Array(5)].map((_, index) => (
<div key={index} className="grid-line" style={{ bottom: `${(index / 4) * 100}%` }}></div>
))}
</div>
);
const PricingChart = ({ data, showPricing = true }) => {
useScrollToTop();
const [highlightedBarTypes, setHighlightedBarTypes] = useState({
input: true,
output: true,
});
const chartAreaRef = useRef(null);
const [hasScroll, setHasScroll] = useState(false);
const [showScrollHint, setShowScrollHint] = useState(false);
useEffect(() => {
const checkScroll = () => {
if (chartAreaRef.current) {
const { scrollWidth, clientWidth, scrollLeft } = chartAreaRef.current;
setHasScroll(scrollWidth > clientWidth);
// 只在滚动到最左侧时显示提示
setShowScrollHint(scrollWidth > clientWidth && scrollLeft === 0);
}
};
const handleScroll = () => {
if (chartAreaRef.current) {
const { scrollLeft } = chartAreaRef.current;
// 当用户开始滚动时隐藏提示
if (scrollLeft > 0) {
setShowScrollHint(false);
}
}
};
checkScroll();
window.addEventListener('resize', checkScroll);
if (chartAreaRef.current) {
chartAreaRef.current.addEventListener('scroll', handleScroll);
}
return () => {
window.removeEventListener('resize', checkScroll);
if (chartAreaRef.current) {
chartAreaRef.current.removeEventListener('scroll', handleScroll);
}
};
}, [data]);
const handleScrollHintClick = () => {
if (chartAreaRef.current) {
const { scrollWidth, clientWidth } = chartAreaRef.current;
chartAreaRef.current.scrollTo({
left: scrollWidth - clientWidth,
behavior: 'smooth'
});
setShowScrollHint(false);
}
};
const handleLegendClick = (barType) => {
setHighlightedBarTypes((prevState) => ({
...prevState,
[barType]: !prevState[barType],
}));
};
const getMaxPrice = () => {
if (!showPricing) {
return Math.max(...data.providers.map(provider => provider.score));
}
const prices = data.providers.flatMap((provider) => [
provider.inputPrice,
provider.outputPrice,
]);
return Math.max(...prices);
};
const maxPrice = getMaxPrice();
return (
<div className="pricing-chart">
<h1 className="chart-title">{data.title}</h1>
<h2 className="chart-subtitle">{data.subtitle}</h2>
<ChartLegend
onLegendClick={handleLegendClick}
highlightedBarTypes={highlightedBarTypes}
showPricing={showPricing}
/>
<div className={`chart-area ${hasScroll ? 'has-scroll' : ''}`} ref={chartAreaRef}>
<YAxis maxPrice={maxPrice} />
<div className="chart-container">
<GridLines />
{data.providers.map((provider) => (
<ProviderColumn
key={provider.name}
provider={provider}
maxPrice={maxPrice}
highlightedBarTypes={highlightedBarTypes}
showPricing={showPricing}
/>
))}
</div>
{showScrollHint && (
<div
className="scroll-hint-container"
onClick={handleScrollHintClick}
style={{ cursor: 'pointer' }}
>
<div className="scroll-hint">
<svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
<path d="M9.29 15.88L13.17 12 9.29 8.12c-.39-.39-.39-1.02 0-1.41.39-.39 1.02-.39 1.41 0l4.59 4.59c.39.39.39 1.02 0 1.41l-4.59 4.59c-.39.39-1.02.39-1.41 0-.38-.39-.39-1.03 0-1.42z"/>
</svg>
</div>
</div>
)}
</div>
</div>
);
};
export default PricingChart;