Skip to content

Commit

Permalink
chart data updated
Browse files Browse the repository at this point in the history
  • Loading branch information
theDevSoham committed Apr 28, 2023
1 parent 1984113 commit e396835
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
20 changes: 16 additions & 4 deletions src/components/Body.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
// local imports
import React from 'react'
import Chart1 from './Chart1'
import ChartDisplay from './ChartDisplay'
import { getChart1Data } from '../static/helpers'
import { type ChartDataType } from '../interfaces/interface'

const Body: React.FC = () => {
const [chartNum, setChartNum] = React.useState<number>(1)
const [currentChartData, setCurrentChartData] = React.useState<ChartDataType>(
getChart1Data()
)

// local functions
const updateChart = (e: React.MouseEvent<HTMLButtonElement>): void => {
e.preventDefault()
setChartNum((chartNum) => {
if (chartNum === 1) return 2
else return 1
})
}

// useEffect hook
React.useEffect(() => {
if (chartNum === 1) setCurrentChartData(getChart1Data())
else if (chartNum === 2) setCurrentChartData(getChart1Data())
}, [chartNum])

// return statement
return (
<section className="w-full h-8/10 flex flex-col justify-center items-center">
<div className="w-full h-8/10 px-4">
{chartNum === 1 && <Chart1 />}
</div>
<div className="w-full h-8/10 px-4">{<ChartDisplay chartData={currentChartData} />}</div>
<div className="w-full h-2/10 flex justify-center items-center">
<button
className="bg-blue-700 text-white font-bold py-4 px-8 rounded"
Expand Down
11 changes: 9 additions & 2 deletions src/components/Chart1.tsx → src/components/ChartDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {

// importing default canvas renderer
import { CanvasRenderer } from 'echarts/renderers'
import { type ChartDataType } from '../interfaces/interface'

// Register the required components
echarts.use([
Expand All @@ -33,7 +34,13 @@ echarts.use([
ToolboxComponent
])

const Chart1: React.FC = () => {
// local prop type declaration

interface ChartPropTypes {
chartData: ChartDataType
}

const ChartDisplay: React.FC<ChartPropTypes> = (props) => {
return (
<ReactEChartsCore
echarts={echarts}
Expand All @@ -47,4 +54,4 @@ const Chart1: React.FC = () => {
)
}

export default Chart1
export default ChartDisplay
6 changes: 6 additions & 0 deletions src/interfaces/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface ChartDataType {
horizontal: number[]
vertical: number[]
}

export type { ChartDataType }
21 changes: 18 additions & 3 deletions src/static/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
// importing the json data
import data from '../assets/Wine-Data.json'
import type { ChartDataType } from '../interfaces/interface'

const getData = (): void => {
console.log(data)
const getChart1Data = (): ChartDataType => {
const flavanoids = data.map((item) => {
if (typeof item.Flavanoids === 'string') {
return parseFloat(item.Flavanoids)
}

return item.Flavanoids
})
const ash = data.map((item) => {
if (typeof item.Ash === 'string') {
return parseFloat(item.Ash)
}

return item.Ash
})
return { horizontal: flavanoids, vertical: ash }
}

export { getData }
export { getChart1Data }
9 changes: 5 additions & 4 deletions src/static/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const option = {
trigger: 'axis'
},
legend: {
data: ['a', 'b', 'c']
data: ['a', 'b', 'c'],
padding: 20
},
toolbox: {
feature: {
Expand Down Expand Up @@ -37,23 +38,23 @@ const option = {
],
series: [
{
name: 'q',
name: 'a',
type: 'line',
stack: 'x',
smooth: true,
areaStyle: { normal: {} },
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'w',
name: 'b',
type: 'line',
stack: 'x',
smooth: true,
areaStyle: { normal: {} },
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: 'r',
name: 'c',
type: 'line',
stack: 'x',
smooth: true,
Expand Down

0 comments on commit e396835

Please sign in to comment.