セグメント期間

```
import UIKit
import Charts

class ViewController: UIViewController, ChartViewDelegate {

    @IBOutlet weak var lineChartView: LineChartView!
    @IBOutlet weak var segmentControl: UISegmentedControl!

    let rawDataGraph: [Double] = [20, 24, 21, 25, 26, 30, 31, 20]
    let unixTimeData: [Double] = [1630400000, 1630403600, 1630407200, 1630410800, 1630414400, 1630418000, 1630421600, 1630425200]

    override func viewDidLoad() {
        super.viewDidLoad()

        configureLineChartView()
        setupData()
        setupXAxis()
        setupYAxis()
        lineChartView.delegate = self

        segmentControl.addTarget(self, action: #selector(segmentControlValueChanged), for: .valueChanged)
    }

    func configureLineChartView() {
        lineChartView.chartDescription.text = "JST日時をわかるように表示したグラフ"
        lineChartView.dragEnabled = true
        lineChartView.setScaleEnabled(true)
        lineChartView.pinchZoomEnabled = true
        lineChartView.rightAxis.enabled = false
    }

    func setupData() {
        let entries = rawDataGraph.enumerated().map { (index, value) -> ChartDataEntry in
            return ChartDataEntry(x: unixTimeData[index], y: value)
        }

        let dataSet = LineChartDataSet(entries: entries, label: "Data")
        dataSet.lineWidth = 2.0
        dataSet.circleRadius = 4.0
        dataSet.circleHoleRadius = 2.0
        dataSet.setColor(.blue)
        dataSet.setCircleColor(.blue)
        dataSet.valueTextColor = .black

        let data = LineChartData(dataSet: dataSet)
        lineChartView.data = data
    }

    var xAxis: XAxis!

    func setupXAxis() {
        xAxis = lineChartView.xAxis
        xAxis.enabled = true
        xAxis.labelPosition = .bottom
        xAxis.labelTextColor = .black
        xAxis.drawAxisLineEnabled = true
        xAxis.drawGridLinesEnabled = false

        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
        dateFormatter.dateFormat = "MM/dd HH:mm"
        xAxis.valueFormatter = IndexAxisValueFormatter(values: unixTimeData.map { unixTime in
            let date = Date(timeIntervalSince1970: unixTime)
            return dateFormatter.string(from: date)
        })
    }

    func setupYAxis() {
        let leftAxis = lineChartView.leftAxis
        leftAxis.axisMinimum = 0.0
        leftAxis.axisMaximum = 40.0
        leftAxis.drawZeroLineEnabled = true
        leftAxis.zeroLineColor = .systemGray

        leftAxis.labelTextColor = .black
        leftAxis.gridColor = .systemGray
        leftAxis.drawAxisLineEnabled = false
    }

    @objc func segmentControlValueChanged(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0: // デフォルト表示
            let dateFormatter = DateFormatter()
            dateFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
            dateFormatter.dateFormat = "MM/dd HH:mm"
            xAxis.valueFormatter = IndexAxisValueFormatter(values: unixTimeData.map { unixTime in
                let date = Date(timeIntervalSince1970: unixTime)
                return dateFormatter.string(from: date)
            })
            xAxis.setLabelCount(unixTimeData.count, force: true) // データ数に合わせてラベル数を設定
            lineChartView.setVisibleXRangeMaximum(6) // 表示するデータ範囲を制限
        case 1: // 2時間ごとの表示
            let interval: TimeInterval = 60 * 60 * 2
            let xValues = unixTimeData.filter { unixTime in
                unixTime.truncatingRemainder(dividingBy: interval) == 0
            }
            xAxis.valueFormatter = IndexAxisValueFormatter(values: xValues)
            xAxis.setLabelCount(xValues.count, force: true) // ラベル数を設定
            lineChartView.setVisibleXRangeMaximum(Double(xValues.count)) // 表示するデータ範囲を制限
        default:
            break
        }
    }

    func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
        let unixTime = entry.x
        let yValue = entry.y

        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
        dateFormatter.dateFormat = "MM/dd HH:mm"
        let jstDateTime = dateFormatter.string(from: Date(timeIntervalSince1970: unixTime))

        let alertController = UIAlertController(title: "特定のプロットが選択されました",
                                                message: "JST日時: \(jstDateTime)\nY値: \(yValue)",
                                                preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertController.addAction(okAction)
        present(alertController, animated: true, completion: nil)
    }
}
```