File size: 3,165 Bytes
50e8e86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const charts = {}

function initBarChart() {
  if (charts.barChart) return
  charts.barChart = new Chart(document.getElementById('BarChart-slider'), {
    type: 'bar',
    data: {
      labels: ['q', 'w', 'e', 'r', 't', 'y'],
      datasets: [
        {
          label: 'Dataset 1',
          data: [520, 760, 240, 433, 156, 380],
          backgroundColor: '#64bbf3',
        },
        {
          label: 'Dataset 2',
          data: [450, 810, 370, 400, 215, 300],
          backgroundColor: '#ee6c71',
        },
      ],
    },
    options: {
      responsive: true,
      plugins: {
        legend: {
          position: 'top',
        },
        title: {
          display: true,
          text: 'Chart.js Bar Chart',
        },
      },
    },
  })
}

function initLineChart() {
  if (charts.lineChart) return
  charts.lineChart = new Chart(document.getElementById('LineChart-slider'), {
    type: 'line',
    data: {
      labels: ['qq', 'w', 'e', 'r', 't', 'y'],
      datasets: [
        {
          label: 'Dataset 1',
          data: [520, 760, 240, 433, 156, 380],
          backgroundColor: '#64bbf3',
        },
        {
          label: 'Dataset 2',
          data: [450, 810, 370, 400, 215, 300],
          backgroundColor: '#ee6c71',
        },
      ],
    },
    options: {
      responsive: true,
      plugins: {
        title: {
          display: true,
          text: 'Chart.js Line Chart',
        },
      },
    },
  })
}

function initPieChart() {
  if (charts.pieChart) return
  charts.pieChart = new Chart(document.getElementById('PieChart-slider'), {
    type: 'pie',
    data: {
      labels: ['1', '2', '3'],
      datasets: [
        {
          label: 'Dataset 1',
          data: [50, 15, 35],
          backgroundColor: ['#64bbf3', '#ee6c71', '#fecc4e'],
        },
      ],
    },
    options: {
      responsive: true,
      plugins: {
        legend: {
          labels: {
            boxWidth: 15,
            padding: 15,
          },
        },
        title: {
          display: true,
          text: 'Chart.js Pie Chart',
        },
      },
    },
  })
}

initBarChart()

var swiperOnButton = new Swiper('.my-swiper-on-button', {
  loop: false,
})

function onSlideChange() {
  const activeIndex = swiperOnButton.realIndex

  ;['barChart', 'lineChart', 'pieChart'].forEach((key) => {
    if (charts[key]) {
      charts[key].destroy()
      charts[key] = null
    }
  })

  if (activeIndex === 0) initBarChart()
  if (activeIndex === 1) initLineChart()
  if (activeIndex === 2) initPieChart()
}

swiperOnButton.on('slideChange', onSlideChange)

document.querySelectorAll('[data-slide-to]').forEach((button) => {
  button.addEventListener('click', () => {
    console.log(
      'click',
      charts,
      button.getAttribute('data-slide-to'),
      parseInt(button.getAttribute('data-slide-to'))
    )
    document.querySelectorAll('.swiper-button').forEach((btn) => {
      btn.classList.remove('active')
    })
    button.classList.add('active')
    const index = parseInt(button.getAttribute('data-slide-to'))
    swiperOnButton.slideTo(index)

    setTimeout(() => {
      onSlideChange()
    }, 10)
  })
})