Вот мой код: < /p>
struct TemperatureData: Identifiable {
let id = UUID()
let time: Date
let value: Double
}
struct TemperatureChartView: View {
@Binding var temperatures: [TemperatureData]
var body: some View
{
let now = Date()
let oneHourAgo = Calendar.current.date(byAdding: .hour, value: -1, to: now)!
let tickTimes = stride(from: oneHourAgo, through: now, by: 60 * 5).map { $0 }
let xAxisPoints = [34,35,36,37,38,39];
VStack(spacing: 12) {
// Header with Trend and Ave
HStack {
Text("Trend: \(trendString)")
Spacer()
Text("Ave : \(averageString)")
}
.font(.headline)
.padding(.horizontal)
// Chart with styled background
ScrollView(.horizontal) {
Chart {
ForEach(temperatures) { temp in
PointMark(
x: .value("Time", temp.time),
y: .value("Temperature", temp.value)
)
.foregroundStyle(Color.red)
.symbolSize(100)
}
}
.chartXScale(domain: oneHourAgo...now)
.chartXAxis {
AxisMarks(values: tickTimes) { val in
//AxisGridLine()
AxisValueLabel {
if let date = val.as(Date.self) {
Text(date, format: .dateTime.hour().minute())
}
}
}
}
.chartYScale(domain: 35.0...39.0)
.chartYAxis() {
AxisMarks(values: xAxisPoints) { val in
AxisGridLine()
AxisValueLabel {
if let v = val.as(Double.self) {
Text(String(format: "%.1fº", v))
}
}
}
}
.chartPlotStyle { plotArea in
plotArea
.background(
VStack(spacing: 0) {
Color.orange.opacity(0.1).frame(height: 40)
Color.green.opacity(0.05).frame(height: 40)
Color.orange.opacity(0.1).frame(height: 40)
}
)
}
.frame(minWidth: 600, maxHeight: 250)
}
}
.padding()
.background(Color(.systemGroupedBackground))
.clipShape(RoundedRectangle(cornerRadius: 16))
}
private var trendString: String {
guard temperatures.count > 1 else { return "-" }
let sorted = temperatures.sorted(by: { $0.time < $1.time })
let trend = sorted.last!.value - sorted.first!.value
return trend > 0 ? "↑" : trend < 0 ? "↓" : "→"
}
private var averageString: String {
guard !temperatures.isEmpty else { return "-" }
let avg = temperatures.map(\.value).reduce(0, +) / Double(temperatures.count)
return String(format: "%.1fº", avg)
}
}
struct TemperatureChartView_Previews: PreviewProvider {
static var previews: some View {
let now = Date()
let sampleData = stride(from: 0, through: 3600, by: 600).map { offset -> TemperatureData in
let time = now.addingTimeInterval(-Double(3600 - offset))
let value = Double.random(in: 36.0...38.5)
return TemperatureData(time: time, value: value)
}
return TemperatureChartView(temperatures: .constant(sampleData))
.frame(width: 350, height: 400)
}
}
< /code>
Я хочу, чтобы yaxis построил левую сторону экрана. Теперь он находится на правой стороне.
Подробнее здесь: https://stackoverflow.com/questions/796 ... the-screen
Диаграмма, ось Y не сдвигается в левую сторону экрана ⇐ IOS
Программируем под IOS
-
Anonymous
1749720303
Anonymous
Вот мой код: < /p>
struct TemperatureData: Identifiable {
let id = UUID()
let time: Date
let value: Double
}
struct TemperatureChartView: View {
@Binding var temperatures: [TemperatureData]
var body: some View
{
let now = Date()
let oneHourAgo = Calendar.current.date(byAdding: .hour, value: -1, to: now)!
let tickTimes = stride(from: oneHourAgo, through: now, by: 60 * 5).map { $0 }
let xAxisPoints = [34,35,36,37,38,39];
VStack(spacing: 12) {
// Header with Trend and Ave
HStack {
Text("Trend: \(trendString)")
Spacer()
Text("Ave : \(averageString)")
}
.font(.headline)
.padding(.horizontal)
// Chart with styled background
ScrollView(.horizontal) {
Chart {
ForEach(temperatures) { temp in
PointMark(
x: .value("Time", temp.time),
y: .value("Temperature", temp.value)
)
.foregroundStyle(Color.red)
.symbolSize(100)
}
}
.chartXScale(domain: oneHourAgo...now)
.chartXAxis {
AxisMarks(values: tickTimes) { val in
//AxisGridLine()
AxisValueLabel {
if let date = val.as(Date.self) {
Text(date, format: .dateTime.hour().minute())
}
}
}
}
.chartYScale(domain: 35.0...39.0)
.chartYAxis() {
AxisMarks(values: xAxisPoints) { val in
AxisGridLine()
AxisValueLabel {
if let v = val.as(Double.self) {
Text(String(format: "%.1fº", v))
}
}
}
}
.chartPlotStyle { plotArea in
plotArea
.background(
VStack(spacing: 0) {
Color.orange.opacity(0.1).frame(height: 40)
Color.green.opacity(0.05).frame(height: 40)
Color.orange.opacity(0.1).frame(height: 40)
}
)
}
.frame(minWidth: 600, maxHeight: 250)
}
}
.padding()
.background(Color(.systemGroupedBackground))
.clipShape(RoundedRectangle(cornerRadius: 16))
}
private var trendString: String {
guard temperatures.count > 1 else { return "-" }
let sorted = temperatures.sorted(by: { $0.time < $1.time })
let trend = sorted.last!.value - sorted.first!.value
return trend > 0 ? "↑" : trend < 0 ? "↓" : "→"
}
private var averageString: String {
guard !temperatures.isEmpty else { return "-" }
let avg = temperatures.map(\.value).reduce(0, +) / Double(temperatures.count)
return String(format: "%.1fº", avg)
}
}
struct TemperatureChartView_Previews: PreviewProvider {
static var previews: some View {
let now = Date()
let sampleData = stride(from: 0, through: 3600, by: 600).map { offset -> TemperatureData in
let time = now.addingTimeInterval(-Double(3600 - offset))
let value = Double.random(in: 36.0...38.5)
return TemperatureData(time: time, value: value)
}
return TemperatureChartView(temperatures: .constant(sampleData))
.frame(width: 350, height: 400)
}
}
< /code>
Я хочу, чтобы yaxis построил левую сторону экрана. Теперь он находится на правой стороне.
Подробнее здесь: [url]https://stackoverflow.com/questions/79660298/chart-the-y-axis-not-shift-to-left-side-of-the-screen[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия