Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Charts: Vectorized and function plots

Swift Charts: Vectorized and function plots

yamakentoc

June 28, 2024
Tweet

More Decks by yamakentoc

Other Decks in Programming

Transcript

  1. 4XJGU$IBSUTͷ͓͞Β͍ w ๮άϥϑΛඳը͍ͨ͠৔߹͸#BS.BSLΛ࢖༻ w ୹͍ίʔυ͚ͩͰ؆୯ʹάϥϑΛඳըͰ͖Δ import SwiftUI import Charts struct

    BarChartView: View { let dataList = [ (month: "Jan", value: 20), (month: "Feb", value: 60), (month: "Mar", value: 45) ] var body: some View { Chart(dataList, id: \.month) { BarMark( x: .value("Month", $0.month), y: .value("Value", $0.value) ) } } }
  2. w ྫ͑͹ʮ ʯΛඳը͍ͨ͠৔߹ y = x2 -JOF1MPU Chart { LinePlot(x:

    "x", y: "y") { x in x * x // y=x^2 } } .chartXScale(domain: -5...5) // X࣠ͷൣғ .chartYScale(domain: -5...5) // Y࣠ͷൣғ ˞Πϝʔδ Ҿ༻ݩɿIUUQTXXXHFPHFCSBPSHHSBQIJOH MBOHKB
  3. "SFB1MPU w ͱ ͷྖҬΛຒΊΔ৔߹ y = x + 1 y

    = x2 Chart { AreaPlot(x: "x", yStart: "x + 1", yEnd: "x^2") { x in (yStart: x + 1, yEnd: x * x) } } .chartXScale(domain: -5...5) // X࣠ͷൣғ .chartYScale(domain: -5...5) // Y࣠ͷൣғ Ҿ༻ݩɿIUUQTXXXHFPHFCSBPSHHSBQIJOH MBOHKB ˞Πϝʔδ
  4. ൒ܘͷԁΛඳը͢Δ৔߹ w ൒ܘͷԁΛදؔ͢਺ x(t) = 2 cos(t) y(t) = 2

    sin(t) Chart { LinePlot(x: "x", y: "y", t: "t", domain: -.pi ... .pi) { t in let x = 2 * cos(t) let y = 2 * sin(t) return (x, y) } } .chartXScale(domain: -5...5) .chartYScale(domain: -5...5) ˞Πϝʔδ Ҿ༻ݩɿIUUQTXXXHFPHFCSBPSHHSBQIJOH MBOHKB
  5. ϋʔτΛඳը͢Δ৔߹ x(t) = 2 sin3(t) y(t) = cos(t)(2 − cos(t)

    − cos2(t)) Ҿ༻ݩɿIUUQTEFWFMPQFSBQQMFDPNWJEFPTQMBZXXED
  6. ۠෼ؔ਺΋ѻ͏͜ͱ͕Ͱ͖Δ w ۠෼ؔ਺ʢ1JFDFXJTFGVODUJPOʣͱ͸ʜ w ۠෼͝ͱʹҟͳΔؔ਺Λద༻͢Δؔ਺ f(x) = { x2 JG

    x > 0 −x2 JG x ≤ 0 Chart { LinePlot(x: "x", y: "y") { x in if x > 0 { x * x // y=x^2 } else { -x * x // y=-x^2 } } } .chartXScale(domain: -5...5) .chartYScale(domain: -5...5) Ҿ༻ݩɿIUUQTXXXHFPHFCSBPSHHSBQIJOH MBOHKB ˞Πϝʔδ
  7. ۠෼ؔ਺Ͱग़ྗ͕ͳ͍৔߹ w ۠෼ʹΑͬͯ͸6OEF fi OFEͷ͜ͱ΋͋Δ  w OBOΛࢦఆ͢Δ͜ͱͰग़ྗ͕ͳ͍͜ͱΛ௨஌ f(x) =

    { x2 JG x > 0 6OEF fi OFE JG x ≤ 0 Chart { LinePlot(x: "x", y: "y") { x in if x > 0 { x * x // y=x^2 } else { .nan // ग़ྗ͕ͳ͍͜ͱΛ௨஌ } } } .chartXScale(domain: -5...5) .chartYScale(domain: -5...5) ˞Πϝʔδ Ҿ༻ݩɿIUUQTXXXHFPHFCSBPSHHSBQIJOH MBOHKB
  8. w ྫ͑͹ҎԼͷؔ਺ͷ৔߹ɺYʹͳΔͱʮ ʯͱͳΓɺΤϥʔʹͳΔͷͰආ͚ Δඞཁ͕͋Δɻ͜ͷ৔߹΋OBOΛࢦఆ͢Δ͜ͱͰճආ͢Δ͜ͱ͕Ͱ͖Δ 1 0 ۠෼ؔ਺Ͱग़ྗ͕ͳ͍৔߹ y = {

    6OEF fi OFE JG x = 0 1 x JG x ≠ 0 Chart { LinePlot(x: "x", y: "y") { x in if x != 0 { 1 / x } else { .nan // ग़ྗ͕ͳ͍͜ͱΛ௨஌ } } } .chartXScale(domain: -5...5) .chartYScale(domain: -5...5) ˞Πϝʔδ Ҿ༻ݩɿIUUQTXXXHFPHFCSBPSHHSBQIJOH MBOHKB
  9. #BS.BSLͷఆٛํ๏ let dataList = [ (month: "Jan", value: 20), (month:

    "Feb", value: 60), (month: "Mar", value: 45) ] … Chart(dataList, id: \.month) { BarMark( x: .value("Month", $0.month), y: .value("Value", $0.value) ) } w %BUBΛ$IBSUʹ౉͠ɺWBMVFͷΑ͏ʹ஋Λࢦఆ͍ͯͨ͠
  10. #BS1MPUͷఆٛํ๏ w %BUBΛ௚઀#BS1MPUͷҾ਺ʹ౉͠ɺ,FZ1BUIͰ஋ΛࢦఆՄೳʹͳͬͨ let dataList = [ (month: "Jan", value:

    20), (month: "Feb", value: 60), (month: "Mar", value: 45) ] … Chart(dataList, id: \.month) { BarMark( x: .value("Month", $0.month), y: .value("Value", $0.value) ) } let dataList = [ (month: "Jan", value: 20), (month: "Feb", value: 60), (month: "Mar", value: 45) ] … Chart { BarPlot( dataList, x: .value("Month", \.month), y: .value("Value", \.value) ) }
  11. 1MPUͷύϑΥʔϚϯεΛ্͛Δํ๏ w ࢖༻͢ΔελΠϧ͝ͱʹσʔλͷίϨΫγϣϯΛάϧʔϓԽ͢Δ͜ͱͰɺ 4XJGU$IBSUT͕ελΠϧͷมߋճ਺ΛݮΒ͢͜ͱ͕Ͱ͖Δ w 1MPU಺ͰDPNQVUFEϓϩύςΟͷར༻Λආ͚ɺ4UPSFEϓϩύςΟʹ͢Δ͜ͱ ͰϨϯμϦϯάॲཧΛܰݮͰ͖Δ w άϥϑͷදࣔ͢Δൣғ EPNBJO

    Λ༧Ίࢦఆ͢Δ͜ͱͰΑΓޮ཰తʹඳը͞Ε Δ w Ұ෦ͷελΠϧͷΧελϚΠζ͸ɺେྔͷσʔλϙΠϯτͰ͸໨ཱͨͳ͘ͳΔ ͷ͕ҰൠతͳͷͰɺελΠϧͷΧελϚΠζΛ͠ͳ͍ํ͕ɺάϥϑͷύϑΥʔ ϚϯεΛ޲্ͤ͞Δ͜ͱ͕Ͱ͖Δ
  12. ·ͱΊ w ؔ਺Λ࢖ͬͯάϥϑΛඳըͰ͖ΔΑ͏ʹͳͬͨ w -JOF1MPUɿͭͷؔ਺ΛඳըͰ͖Δ w "SFB1MPUɿͭͷؔ਺ͷؒΛຒΊΔ͜ͱ͕Ͱ͖Δ w ύϥϝτϦοΫؔ਺΍۠෼ؔ਺΋࢖༻Ͱ͖Δ w

    େن໛ͳσʔλΛΑΓޮ཰తʹॲཧͰ͖ΔΑ͏ʹͳͬͨ w 1MPUΛ࢖͏͜ͱͰฒྻʹσʔλΛॲཧͰ͖Δ w 1MPUͰ͸,FZ1BUIͰ஋ΛࢦఆͰ͖Δ w 1MPUͷύϑΥʔϚϯεΛ্͛Δํ๏͕͋Δ