PricingChart.jsx 3.66 KB
Newer Older
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
// PricingChart.jsx
import React, { useState } from 'react';
import '../styles/PricingChart.css';

const ChartLegend = ({ onLegendClick, highlightedBarTypes }) => (
  <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 }) => {
  const getBarHeight = () => {
    return (price / maxPrice) * 300;
  };

  return (
    <div
      className={`bar ${type}-bar`}
      style={{
        height: `${getBarHeight()}px`,
        opacity: highlighted ? 1 : 0.3,
      }}
    >
      <span className="price-label">{price}</span>
    </div>
  );
};

const ProviderColumn = ({ provider, maxPrice, highlightedBarTypes }) => (
  <div className="chart-column">
    <div className="bars-container">
      <ChartBar
        price={provider.inputPrice}
        type="input"
        maxPrice={maxPrice}
        highlighted={highlightedBarTypes.input}
      />
      <ChartBar
        price={provider.outputPrice}
        type="output"
        maxPrice={maxPrice}
        highlighted={highlightedBarTypes.output}
      />
    </div>
    <div className="provider-info">
      <img
62
        src={`${provider.logo}`}
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
        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 }) => {
  const [highlightedBarTypes, setHighlightedBarTypes] = useState({
    input: true,
    output: true,
  });

  const handleLegendClick = (barType) => {
    setHighlightedBarTypes((prevState) => ({
      ...prevState,
      [barType]: !prevState[barType],
    }));
  };

  const getMaxPrice = () => {
    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} />

      <div className="chart-area">
        <YAxis maxPrice={maxPrice} />
        <div className="chart-container">
          <GridLines />
          {data.providers.map((provider) => (
            <ProviderColumn
              key={provider.name}
              provider={provider}
              maxPrice={maxPrice}
              highlightedBarTypes={highlightedBarTypes}
            />
          ))}
        </div>
      </div>
    </div>
  );
};

export default PricingChart;